Hashmap List of Problems
Hashmap List of Problems
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.
10. Find maximum length sub-array having equal number of 0’s and
1's
36. Construct a binary tree from inorder and level order sequence
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
44. Iteratively print leaf to root path for every leaf node in a binary
tree
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
67. Find all common elements present in every row of given matrix
71.Find all words from given list that follows same order of characters
as given pattern