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);


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 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...

6. concatenating two 2d vectors -
v1.insert( v1.end(),v2.begin(),v2.end() )

------------------------------------------------------------------------------------------------------------

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;

--------------------------------------------------------------------------------------------------------------------


signed means sign waale number matlab + aur - waale dono
ab negative waalo k liye 32 mein se 1 bit toh sign ki hai toh range hogyi
-2^31 se (2^31)-1
---------------------------------------------------------------------------------------------------------------------------
to append a new temp vector at end of vector v: 

v.insert( v.end(), temp.begin(), temp.end() );
------------------------------------------------------------------------------------------------------------------------
To find if a map elmt with key x exist or not:

if(mpp.count(x)==1) so it exists
----------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

18. greedy algorithm

19. strings (LB)

17. BINARY SEARCH on 2d arrrays