Posts

Showing posts from December, 2024

13. Arrays (medium)

Image
buy & sell stocks✅ Next permutation🔥 Longest consecutive seq🔥 Rotate matrix by 90 CW✅ Count subarrays with sum k🔥 Rearrange the array in alternating + & - items ✅ Set matrix rows & cols to 0s for zero elmt✅ Spiral matrix✅ Leaders in an array✅ ---------------------------------------------------------------------------------------------------------- 1. Two sum problem- return index of 2 elements in array which adds up to the target sum Brute -   time: O(N^2),   space:O(1) linear search with two loops ------------------------------------------------------- Better:     time: O(NlogN),     space: O(N) mapping with two pass (  first build the map & next do a linear search and find remaining sum in map ) ------------------------------------------------------- Optimal -  time: O(n),  space: O(n) mapping with single pass (first check if answer exists & if not then insert the pair in map) Also, we prefer unordered...

Extra Masala

Image
1. Pass By Pointers :- fn argument mein use (*)                                            fn call mein use (&) ------------------------------------------------------------ 2. lower and upper bound works only with sorted stuff... ----------------------------------------------------------- 3. in multimap , mpp[k] not used, as keys ain't unique anymore... Hence, we work with a multimap as follows: mpp.insert( {1:"apple"} ) auto it = mpp.find(1) cout << (it → second) ----------------------------------------------------------- 4. Typecast :- char ch = 'a'      int y = (int)ch     or    int y = 'a' ----------------------------------------------------------- 5. Observations :- we can use 2 pointer for yes/ no type check wale questions  Subarray- contigous       VS      subsequ...