π One-stop destination for all your technical interview Preparation π
tie(w,x,y,z) = make_tuple(10,20,30,40)
:- w=10,x=20,y=30,z=40unordered_set
,unordered_map
are faster than set
and maps
.set
uses self balancing binery search trees therefore O(nlogn).unordered_set
uses hashing therefore O(1) same for the map
and unordered_map
.auto
keyword can be used for declaring variables, it automatically set the type of that variable.lower_bound
: find >= elem , upper_bound
: find > elementis_sorted
: Returns true
if the container is sorted else false
.bool
upto 10^8
or int
array upto 10^7
.bool
upto 10^7
or int
array upto 10^6
.a>>1
== divide a by 2.a<<1
== multiply a with 2.(a&1)==0
== number is even.(a&1)==1
== number is odd, make sure you use the brackets.a=a^b;b=a^b;a=a^b;
emplace_back
to push pair
in vector
insteed of pb(mp())
or pb({})
.__builtin_popcount(n)
== returns no. of set bits in n.(n & (1<<i))==0
then ith bit is 0 else 1.( n | (1<<i))
then ith bit set to 1.(n & ~(1<<i))
then ith bit set to 0&(1<<i)
for find.|(1<<i)
for set-1.& ~(1<<i)
for set-0.(n&(n-1))
used in many ways :
ex.1101->1100->1000->0000
then we can count no of set bits by count.(n&(n-1))==0
then it is power of two else not.temp.find(B) != string::npos
, find B is in string or not.s.insert(pos, string)
, insert string at pos position.v.erase(iterator)
to delete elem while traversing in vector. Remember to do itβ.auto it = lower_bound(v.begin(), v.end(),elem);
int index = it - v.begin();
OR index = it - v;
auto it = s.lower_bound(elem);
int elem = *it;
s.count()
returns 1 if element is present in set else 0.mp.count()
returns 1 if element is present in map else 0.We can use dec,hex,oct to print numbers in different bases.
// bitset<bitcnt>(num)
cout << bitset<12>(n) << "\n"; // Binary
cout << oct << n << "\n"; // Octal
cout << hex << n << "\n"; // Hexadecimal
cout << dec << n << "\n"; // Decimal-normal
std::minmax_element
== returns pair{minelement, maxelement}
vector<int> x = {1, 2, 3, 4};
auto it = minmax_element(all(x));
cout << *it.first << ' ' << *it.second << endl;
output:
1 4
is_palindrome
function using std::equal
.
bool is_palindrome(string s) {
return equal(all(s) - s.size() / 2, s.rbegin());
}
Β©οΈ prepared by chetan6780