Posts

Showing posts from December, 2024

13. arrays medium

Image
Priority-wise Two sum problem✅ sort arr of 0s, 1s, 2s✅    DNF majority elmt✅              Moore voting algo max subarray sum✅      Kadane algo 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✅ ---------------------------------------------------------------------------------------------------------- 2 sum problem- return index of 2 elmts in array which adds up to equal the target sum Brute- O(N^2) Better-(NlogN) ---------------------------------------------------------------------------------------------------------------------------- Sort an array of 0s, 1s, 2s- Brute - (NlogN)  sort Better -(2N, with no extra space) cnt no of 0,1,2 and overwrite using loops Optimal -(N)  DNF Algo -( dutch national flag )   3 ...

Extra Masala

Image
Pass By Pointers :- mein upar * and niche & st.erase(it)------------------->  O(1) in unordered set, lower and upper bound not works, as they work only with sorted stuff... in multimap, mpp[k] not used, as keys ain't unique anymore... mpp.find(key)--> finds iterator pointing to key-value pair with given key for small ranges 1e5, use hashing for large ranges, 1e9 prefer mapping before coding any line of code which may lead to out of bounds in future, think of it and put a condition for its execution to handle edge case for later... L inear search < hashing < mapping TYPECAST- char x = 'a'      int y = (int)x     or    int y = 'a' We have a built-in reverse fn for arrays & vectors reverse(arr,arr+n); we can use 2 pointer for yes/no type questions index = it - v.begin() int summ = accumulate (v.begin(),v.end(),0); if you are making a function to solve a ques such that it takes 2 things- a & b and u want to solve like ki ...