Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Last active March 15, 2024 08:24
Show Gist options
  • Save janvichhabra/22f9a5c480a2a65d498ee106daab174f to your computer and use it in GitHub Desktop.
Save janvichhabra/22f9a5c480a2a65d498ee106daab174f to your computer and use it in GitHub Desktop.
Longest Subarray With Equal Number Of 0s 1s And 2s
1. What is the meaning of Longest Subarray With Equal Number Of 0s 1s And 2s?
ans -> Longest subarray for the given array which will contain equal number of 0s, 1s, and 2s , we have return its length only in this question.
2. How can we use HashMap in this question?
ans -> We make a hashmap to solve this problem where the key is a string which is represented by "x1-x0 # x2-x1" where the gap between count of 0s and 1s is separated from the gap between count of 1s and 2s by a "#".
3. Why the time complexity is O(n)?
Ans ->The time complexity is linear due to the traversal through the given array.
4. Can we use Hashset instead of HashMap?
ans -> No, for this perticular question we want the hashmap of type String-Integer where in front of every string we will store the index of the array at which we have derived that string, string contains the difference of "(count1- count0 + "#" + count2 - count1)"
5. 5. What is the length Of longest Subarray With Equal Number Of 0s 1s And 2s for arr = [0, 1, 0, 2, 0, 1, 0]?
Ans -> length Of longest Subarray With Equal Number Of 0s 1s And 2s is equals to 3.
1st arr = [1, 0, 2]
2nd arr = [2, 0, 1].
1. Can you think any approach by using HashMap?
2. Make a String V/S Integer HashMap and investigate what types of strings as a key you can store.
3. intialize 3 variables
1. count0 = count of number of zeros
2. count1 = count of number of ones
3. count2 = count of number of two
for the key string -> take delta value of (count1 - count0) and (count2 - count1) and make a key as "(count1- count0 + "#" + count2 - count1)"
and the value -> frequency of key
4. if key is present into HashMap then add the frequency of key into final count.
A. What is the Subarray?
1. A subarray is a Subsequence of array.
2. A subarray is a contiguous part of array.
3. Both of the above
4. None of the above
Ans -> 2. A subarray is a contiguous part of array.
B. What are the Subarrays of arr = [1,2,3]?
1. [1],[1,2],[1,2,3]
2. [1],[1,2],[1,2,3],[2],[2,3],[3]
3. [1],[1,2],[1,3],[1,2,3],[2],[2,3],[3]
4. None of the above
Ans -> 2. [1],[1,2],[1,2,3],[2],[2,3],[3]
C. What is formula to calculate how many numbers of subarray can be made for n length array?
1. n * (n + 1) / 2
2. n * (n - 1) / 2
3. n + (n / 2)
4. None of the above
Ans -> 1. n * (n + 1) / 2
Explain : for n = 3 number of subarays can be form 3 * (3 + 1) / 2 = 6
D. What is the Time Complexity to solve this question?
1. O(n)
2. O(n * logn)
3. O(n * n)
4. O(1)
Ans : 1. O(n)
Explaination : The time complexity is O(n) where n is the number of elements in the array as we are traversing the array to generate the strings and storing them in the hashmap.
E. What is the Space Complexity to solve this question?
1. O(n)
2. O(n * n)
3. O(logn)
4. O(1)
ANS : 1. O(n)
Explaination : The space complexity is also O(n) as we are storing the strings into the hashmap.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment