-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotes
164 lines (115 loc) · 5.27 KB
/
notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
if(s[i] == 'someChar') is correct
if(s[i] == "someChar") is wrong
anotherStr = strName.substr(start_index, upto_how_many_chars)
isdigit(char) returns whether char is digit or not
eg: isdigit(s[i])
- used for checking digits in a string
- similar functions
- islower(s[i])
- isupper(s[i])
while multiplying a constant number will some var, if it is large, add LL
eg: long long int ans = 2LL * (maxEl - minEl);
finding minimum and maximum el
int minEl = a[0];
int maxEl = a[0];
for (int i = 0; i < n; i++)
{
minEl = min(minEl, a[i]);
maxEl = max(maxEl, a[i]);
}
ceil function only works for float
eg ceil(n/2)
say n = 7 and n is int
cout << ceil(n/2) gives value 3
cout << ceil((float)n/2) gives value 4
subarray and substring are continous
subsequence is not continous
odd = odd + even
eg : odd = 3 + even
Frequency array for lowercase str
vector<char> freqArr(26);
for(auto ch : s)
freqArr[ch - 'a']++;
if(x & 1) can also be used for checking odd number condition
or if(x%2!=0)
if in a problem, it's given take the result without rounding,
dont consider floor or ceil, take the decimal point answer
If x>y then it will remain x>y no matter how many times we perform the operation.
Multiple different cases could be just one simple case
in any problem, where you replace a[i] with some a[i]+1/2 or something which involves a[i], u sort the array
for inserting more than two elements in max func:
max({max1, max2, max3..})
Property of XOR:
if we XOR a number with itself by even no of times
then we get result as 0
else if we XOR a number with itself by odd no of times
then we get result = number
eg : 10 XOR 10 = 1010 XOR 1010 = 0000 = 0
10 XOR 10 = 0
(10 XOR 10 XOR 10) = 10
Real life example of race condition::
#include <iostream>
#include <string>
using namespace std;
int main()
{
double num;
string mystr;
cout << "Please enter a number: " << "\n";
cin >> num;
cout << "Your number is: " << num << "\n";
cout << "Please enter your name: \n";
cout << "So your name is " << mystr << "?\n";
cout << "Have a nice day. \n";
}
In the above case, when running the program it never stopped to ask for the string. It just skipped over it.
remember the clock-wala problem ?you faced similar problem
So you run into a race condition because you assume the stream is clear each time you ask for a input.
The solution to above problem is always use cin.ignore(numeric_limits<streamsize>::max(), '\n'); before using getline()
or use cin.ignore() after taking a number input and before taking an int input
If you are precomputing something in a function, alwyas call that function in main, else there would no point in precomputing if you call it in solve()
Any no of 2^n+1 will have binary format as 1.0.1 i.e a no starting from 1 and ending with 1 and rest will be zeros
if X = gcd(A, B) then A % X = 0
inversion as a concept:
j > i & a[i] > a[j] i.e if you plot indices on X-axis and elements on Y-axis, you will get a negative slope
eg: arr[] = {5, 4, 3, 2, 1}
a number is divisible by 3 if the sum of the digits is divisible by 3 and a number is divisible by 99 if the sum of the digits is divisible by 9.
Any no of (2^n)-1 will have binary format as 111.. i.e n no of ones
eg 7 = 111, n=3
^ - XOR
a^b = c
then a^c = b
perfect squares have odd factors
Any no of 2^n+1 will have binary format as 1.0.1 i.e a no starting from 1 and ending with 1 and rest will be zeros
if X = gcd(A, B) then A % X = 0
inversion as a concept:
j > i & a[i] > a[j] i.e if you plot indices on X-axis and elements on Y-axis, you will get a negative slope
eg: arr[] = {5, 4, 3, 2, 1}
a number is divisible by 3 if the sum of the digits is divisible by 3 and a number is divisible by 99 if the sum of the digits is divisible by 9.
Any no of (2^n)-1 will have binary format as 111.. i.e n no of ones
eg 7 = 111, n=3
^ - XOR
a^b = c
then a^c = b
perfect squares have odd factors
the parity of an integer is the (non-negative) remainder obtained when dividing it by 2. For example, the parity of 246 is 0 and the parity of 11 is 1. In other words, an even number has parity 0 and an odd number has parity 1
n & (n - 1) drops the lowest set bit: https://leetcode.com/problems/number-of-1-bits/discuss/55255/C%2B%2B-Solution%3A-n-and-(n-1).
You can reverse a rows of any matrix by doing:
for(int i = 0; i < n; i++)
reverse(begin(matrix[i]), end(matrix[i]));
for reversing cols of a matrix:
reverse(begin(matrix), end(matrix));
Subarray question?
Do arr contain only positive els: Go for sliding window
else prefix sum
For unsetting the bits of a specific no, do an XOR with it
For setting bits of a specific no, do an OR with it
eg: https://leetcode.com/problems/longest-nice-subarray/submissions/
For reversing a vector of pair
sort(engineers.rbegin(), engineers.rend());
Getting binary representation of dec in string format: one-liner
string binaryRep = bitset<8>(decimalNo).to_string();
When a graph problem involves finding a shortest path, BFS should be used over DFS. This is because with BFS, all nodes at distance x from start will be visited before any node at distance x + 1 will be visited. Once we find the target (end), we know that we found it in the shortest number of steps possible
For finding if a key is present in map mp
Please use mp.count(key) instead of
mp.find(key) == mp.end()