Array Chap 1
Array Chap 1
2 sum problem
a. Sort array and maintain two pointer low and high
b. If a[low] + a[high] < target
Low++
Else
High--
3. Overlapping Intervals :
a. Hotel booking / cab booking
i. https://www.geeksforgeeks.org/find-k-bookings-possible-given-arrival-departure-times/
1. Create event points for every interval start, and end.
2. Sort it according to the day.
3. Now, iterate over them one by one. If you encounter a start, then the number of
active guests increase by one. If you encounter an end, the number of active
guests decrease by one.
4. If at any point, the number of active guests exceed K, we know that scheduling
is not possible.
b. https://www.interviewbit.com/problems/merge-intervals/
d. Non overlapping Intervals ( minimum number of intervals you need to remove to make the rest of the
intervals non-overlapping)
https://leetcode.com/problems/non-overlapping-intervals/
https://www.youtube.com/watch?v=_W0NzvQXnHg
i. Almost same as above problem
ii. Sort Intervals by end point
iii. shoot point = endpoint of first interval
iv. Now all the next intervals whose starting point < shoot point will be intersected by shoot point so
will have to remove it hence count++
v. Else new shoot point = current interval’s endpoint
vi. --------------------------------
vii. Sort the intervals by their right end ascending.
viii.Initialized the select intervals as an empty set
ix. Consider the sorted intervals one by one, add it if it is possible (only need to check the last
select interval and the current one).
e. Merge Intervals
i. https://leetcode.com/problems/merge-intervals/
ii. https://www.interviewbit.com/problems/merge-overlapping-intervals/ **
f. Approach 1
i. Sort intervals by decreasing order of end point
ii. X = start point y = end point
iii. Now all the points whose end point >= x will intersect so new x = min(x, new point start
point)
iv. Else they do not intersect
g. Approach 2 **
i. Sort intervals by increasing order of start point
ii. X = start point y = end point
iii. Now all points whose start point <= y will internsect so new y = max(y, new point end
point)
iv. Else they do not intersect
h. ============= === ==
== === ==
==== === ==========
4. Next Permutation
a. https://leetcode.com/problems/next-permutation/solution/
b. Go from right side find the first i for which a[i] < a[i+1]
c. Now from right side of a[i] find the number immediate larger than a[i] swap both
d. Sort right side of a[i]
5. Kth permutation
a. https://leetcode.com/problems/permutation-sequence/
b. N = 4 and k = 9
Take k = 8 (index 0)
1234
1 {2 3 4}
2 {1 3 4}
3 {1 2 4}
K = 9 index = k/(n-1)! = 9/3! = 1
new k = k %(n-1)! = 9%3! = 3
ans = 2 + recursively
6. Max sub circular sub array
https://leetcode.com/problems/maximum-sum-circular-subarray/submissions/
Ans = max(the max subarray sum, the total sum - the min subarray sum)
https://leetcode.com/problems/majority-element/
https://leetcode.com/problems/majority-element-ii/
https://gregable.com/2013/10/majority-vote-algorithm-find-majority.html
10. https://www.spoj.com/problems/MMASS/
https://github.com/jiteshsunhala/spoj-solutions/blob/master/MMASS.cpp
(CH)2((OH2)3H2O)5
USING stack
anticlockwise rotate
* first reverse left to right, then swap the symmetry
* 1 2 3 3 2 1 3 6 9
* 4 5 6 => 6 5 4 => 2 5 8
* 7 8 9 9 8 7 1 4 7
17. https://www.interviewbit.com/problems/first-missing-integer/
19. ATOI
a. https://www.interviewbit.com/problems/atoi/
b. Handle overflow
+7 → 7 +96afaddsf → 96 __-98dfd → -98
-7 → -7 -78dfe → -78
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
b. Integer to Roman
string M[] = {"", "M", "MM", "MMM"};
string C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
21. Validate sudoku
a. https://www.interviewbit.com/problems/valid-sudoku/
b. Create hash for row, column and 3*3 board and check whether no present among any one
22. Group anagram strings together
a. https://www.interviewbit.com/problems/anagrams/
b. Create map<vector<string>,i> check if string anagram already exist
23. Max points on a line
a. https://leetcode.com/problems/max-points-on-a-line/
b. Take each point and find slop with all other points
24. String concatenation
a. https://leetcode.com/problems/substring-with-concatenation-of-all-words/
b. Create a two map mp1 and mp2. Map1 stores freq of dict1 and mp2 while traversing given stg s
For each index i in range(0,n-1)
J=i
while(j < n)
If mp1 contains stg and mp2[stg] + 1 <= mp1[stg] than add stg to map2
if(size1 == size2) ans.add(i) break
Else break