-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSomePointsduringCPP.txt
More file actions
148 lines (110 loc) · 3.79 KB
/
SomePointsduringCPP.txt
File metadata and controls
148 lines (110 loc) · 3.79 KB
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
===========================
CORE NOTES (DSA + C++)
===========================
1) -1 in C++ indicates that an element does not exist.
Since array indexing always starts from 0, an index can never be -1.
2) When a question mentions "NO EXTRA SPACE",
always try to use the TWO POINTER APPROACH.
3) Arrays created additionally to store values of another array
are called AUXILIARY ARRAYS.
4) In a 2D matrix, it is mandatory to specify the number of columns
while passing it to a user-defined function OR use double pointer.
Examples:
void spiralMatrix(int **mat, int n, int m)
void spiralMatrix(int mat[][100], int n, int m)
5) Difference between char array and string:
- Char array is a simple array of characters.
- String is an object of the string class.
- Strings are dynamic in size.
- Char arrays are fixed in size.
6) In OOP, if no access modifier is specified inside a class,
the default access modifier is PRIVATE.
7) To decide whether recursion is required:
Recursion is usually associated with exponential time complexity,
so use it only when the problem structure demands it.
8) Sudoku always has a FIXED size of 9x9.
9) STL containers are passed by VALUE by default.
To modify the original container, pass it by reference using &.
10) template <class T>
Template class allows using any data type for data structures
or STL-like functionality.
11) STL containers usually do not change original data
unless passed by reference (&).
12) Problems involving maximum/minimum (other than simple arrays/strings),
such as maximum subarray, substring, subsequence, max-sum, min-sum,
often indicate the use of GREEDY ALGORITHMS.
13) Take or not take decision problems:
- 0/1 Knapsack -> TV, Fridge, Washing Machine.
- Fractional Knapsack -> Sugar, Salt, Oil (quantity matters).
14) In Binary Trees:
- Traversal is possible only from Parent to Child.
- Backtracking is possible ONLY using recursion.
15) Return conventions:
- If return type is int and nothing exists -> return -1.
- If return type is Node* and nothing exists -> return NULL.
===========================
TWINKLING LOGIC PARTS
===========================
1) Array Right Rotation:
Reverse loop (n-1 to 1)
arr[i] = arr[i-1]
2) Array Left Rotation:
Straight loop (0 to n-2)
arr[i] = arr[i+1]
3) Matrix 180 Degree Rotation:
Swap rows n/2 times, then reverse each row.
4) Matrix 90 Degree Clockwise:
Transpose matrix, then reverse each row
OR after transpose, swap columns n/2 times.
5) Matrix 90 Degree Anti-Clockwise:
Transpose matrix, then swap rows n/2 times.
===========================
ON HOLD TOPICS
===========================
1) Common letter sequences in string.
2) Two strings are rotation of each other.
===========================
STL SYNTAX TO REMEMBER
===========================
1) Binary Search:
if (binary_search(arr, arr + n, findElement))
return 1;
else
return 0;
2) Vector:
vector<int> v;
vector<int> v2(5);
vector<int> v3(5, 1);
3) Set:
set<int> st;
st.insert(51);
st.size();
st.erase(51);
4) Multiset:
multiset<int> mst;
mst.insert(12);
In vectors Sorting is also same as -
// Ascending sort
sort(arr.begin(), arr.end());
// Descending sort
sort(arr.begin(), arr.end(), greater<int>());
// Reverse vector
reverse(arr.begin(), arr.end());
===========================
TERNARY OPERATOR SYNTAX
===========================
int age = 16;
bool isAdult = (age > 18)
? (cout << "Yes Mature" << endl, true)
: (cout << "Immature" << endl, false);
cout << isAdult;
===========================
END OF NOTES
===========================
*/
int main()
{
// This file is intentionally kept logic-free.
// Used purely as a structured DSA + C++ notes reference.
return 0;
}