Extra Masala
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...
Linear 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);
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 humesha pehle wala yaani a mera chota ho then u can do- if[a.size()>b.size()] return fn(b,a);
(a+b)/2 == a+[(b-a)/2] == (a+b)>>1 == a+[(b-a)>>1]
-----------------------------------------------------------------------------------------------------------------------------
Optimisation techniques to reduce time complexity of a solution-
- 2 -pointer, 3- pointer, 4- pointer
- hashing like hash set or hash map
- prefix sum
-----------------------------------------------------------------------------------------------------------------------------
auto it = st.find(x);
auto it = mpp.find(k);
cout << *(it).second;
st.erase(it);----->single occurence erased out of multiple
st.erase(x)------>all occurences erased
gcd(a,b); sqrt(x);
-----------------------------------------------------------------------------------------------------------------------
Subarray- contigous VS subsequence- non contigous
1. no of subarrays- n(n+1)/2
2. no.of permutations- n!
3. XOR
a^a=0, 0^a=a
xor(a,b,c,d) <= max(a,b,c,d)
4. fn(const dtype &var) ----> avoids TLE / Segmentation fault, as used for read only operations
5. (2d)array ko 1d me bichhane ki ninja technique-😕
matrix is nothing but vector of vectors...
------------------------------------------------------------------------------------------------------------
imp thing--- notice if array contains 0s or not from the constraints as then problem gets modified a bit
also similarly check for negatives too
---------------------------------------------------------------------------------------------------------------
INT_MAX is ( 2*(1e9) )
so if in a ques nums[i] <= 2*(10^9)
so we can still store nums[i] in int, but we need to use long long for smtg like nums[i]+nums[j],
--------------------------------------------------------------------------------------------------------------------------
how to return ans with modulo- (for e.g.,)
const int mod = 1e9+7
.....
return ans%mod;
--------------------------------------------------------------------------------------------------------------------


Comments
Post a Comment