0% found this document useful (0 votes)
10 views

Hashmap List of Problems

Uploaded by

samajsevafree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Hashmap List of Problems

Uploaded by

samajsevafree
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

HashMaps and HashSets are powerful data structures that can be used to solve various

subarray-related problems efficiently. Here is a list of some subarray problems that can
be solved using HashMaps or HashSets:

1. Subarray Sum Equals K: Given an array of integers, find the total number of continuous
subarrays whose sum equals a target value K. You can use a HashMap to keep track of
prefix sums and their frequencies.
2. Longest Subarray with Sum K: Find the length of the longest subarray with a sum
equal to K. This problem can also be solved using a HashMap to store prefix sums.
3. Maximum Subarray Sum with No More Than K: Given an array of integers, find the
maximum sum of any subarray of size at most k. You can use a sliding window approach
with a TreeSet (a sorted set implemented as a balanced tree) to solve this efficiently.
4. Count Distinct Subarrays: Given an array, count the number of distinct subarrays. You
can use a HashSet to keep track of the distinct subarrays.
1. Find All Subarrays with Zero Sum: Given an array of integers, find all subarrays with a sum of
zero. You can use a HashMap to store the cumulative sum and its corresponding index. Initialize
a variable count to 0.
2. Initialize an empty dictionary prefix_sum to store the prefix sums and their frequencies.
3. Initialize a variable sum to 0.
4. Iterate through each element num in the array A. a. Add num to sum. b. If sum is 0,
increment count by 1. c. If sum is already present in prefix_sum, increment its frequency by 1.
Otherwise, add sum to prefix_sum with a frequency of 1.
5. Iterate through each key-value pair in prefix_sum. a. If the frequency of a prefix sum is freq,
increment count by freq * (freq - 1) / 2.
6. Return count modulo 10^9 + 7.
5.
6. Find the Subarray with the Smallest Sum: Given an array of integers, find the subarray
with the smallest sum. You can use a sliding window approach with a priority queue
(min-heap) or simply a variable to keep track of the minimum sum.
7. Longest Subarray with Equal Number of 0s and 1s: Given a binary array, find the
maximum length of a contiguous subarray with an equal number of 0s and 1s. You can
use a HashMap to store the cumulative count of 0s and 1s.
8. Longest Subarray with At Most Two Distinct Elements: Given an array of integers,
find the length of the longest subarray with at most two distinct elements. You can use a
HashMap to store the frequency of elements in the subarray.
9. Longest Subarray with Sum Divisible by K: Given an array of integers, find the length
of the longest subarray with a sum that is divisible by K. You can use a HashMap to store
the cumulative sum modulo K and its corresponding index.
10. Maximum Size Subarray Sum Equals Target: Given an array of integers, find the
maximum size of a subarray whose sum equals a target value. You can use a HashMap
to store prefix sums and their indices.
These are just a few examples of subarray problems that can be efficiently solved using
HashMaps or HashSets, but there are many more variations and complex problems that
can also benefit from these data structures. The choice of which data structure to use
depends on the specific problem and its requirements.

1. Find pair with given sum in an array

2. Check if subarray with 0 sum is exists or not

3. Print all sub-arrays with 0 sum

4. Find longest subsequence formed by consecutive integers

5. Find duplicates within given range k in an array

6. Count distinct absolute values in a sorted array

7. Find subarray having given sum in given array of integers

8. Find largest sub-array formed by consecutive integers

9. Find duplicate element in a limited range array

10. Find maximum length sub-array having equal number of 0’s and
1's

11.Find maximum length sub-array having given sum

12. Find majority element in an array (Boyer–Moore majority vote


algorithm)

13. Custom Sort | Sort array based on another array


14. Custom Sort | Sort elements by their frequency and Index

15. Efficiently Sort an Array with many Duplicated Values

16. Calculate frequency of all elements present in an array of


specified range in linear time and using constant space

17.Replace each element of an array by its corresponding rank

18. Group elements of an array based on their first occurrence

19. Find all Symmetric Pairs in an Array of Pairs

20. Construct the longest palindrome by shuffling or deleting


characters from a string

21. Find count of distinct elements in every sub-array of size k

22. Print all sub-arrays of an array having distinct elements

23. Find Minimum Index of Repeating Element in an Array

24. Find Index of Maximum Occurring Element with Equal


Probability

25. Check if an Array is Formed by Consecutive Integers

26. Find two non-overlapping pairs having same sum in an array

27. Find subarrays with given sum in an array

28. 3-Partition Problem


29. Find two odd occurring elements in an array without using any
extra space

30. Find pairs with given difference k in an array

31. Find odd occurring element in an array in single traversal

32. Determine if two integers are equal without using comparison


and arithmetic operators

33. 4 sum problem | Quadruplets with given sum

34. Find Triplet with given sum in an array

35. Find the surpasser count for each element of an array

36. Construct a binary tree from inorder and level order sequence

37. Construct a binary tree from inorder and postorder traversals

38. Construct a binary tree from inorder and preorder traversal

39. Clone a binary tree with random pointers

40. Efficiently print all nodes between two given levels in a binary
tree

41. Find preorder traversal of a binary tree from its inorder and
postorder sequence

42. Print Right View of a Binary Tree


43. Find postorder traversal of a binary tree from its inorder and
preorder sequence

44. Iteratively print leaf to root path for every leaf node in a binary
tree

45. Find a pair with given sum in a BST

46. Find maximum width of given binary tree

47. Build Binary Tree from given Parent array

48. Find the Diagonal Sum of given Binary Tree

49. Print Diagonal Traversal of Binary Tree

50. Perform vertical traversal of a binary tree

51. Find Vertical Sum in a given Binary Tree

52. Find ancestors of given node in a Binary Tree

53. Print Top View of Binary Tree

54. Print Bottom View of Binary Tree

55. Print Left View of Binary Tree

56. Print all nodes of a perfect binary tree in specific order

57. Reverse Level Order Traversal of Binary Tree

58. Spiral Order Traversal of Binary Tree


59. Level Order Traversal of Binary Tree

60. Construct a Binary Tree from Ancestor Matrix

61. Find Probability that a Person is Alive after Taking N steps on an


Island

62. Link nodes present in each level of a binary tree in the form of a
linked list

63. Convert a binary tree into a doubly linked list in spiral order

64. Remove duplicates from a linked list in single traversal

65. Detect Cycle in a linked list (Floyd’s Cycle Detection Algorithm)

66. Clone a Linked List with Random Pointers

67. Find all common elements present in every row of given matrix

68. Generate list of possible words from a character matrix

69. Find common elements present in all rows of a matrix

70. Find Duplicate Rows in a Binary Matrix

71.Find all words from given list that follows same order of characters
as given pattern

72. Find all possible combinations by replacing given digits with


characters of the corresponding list

73. Isomorphic Strings


74. Determine if two strings are anagram or not

75. Check if repeated subsequence is present in the string or not

You might also like