-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.cpp
More file actions
144 lines (126 loc) · 2.84 KB
/
Copy pathArray.cpp
File metadata and controls
144 lines (126 loc) · 2.84 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
Static:
int arr[5];
Dynamic:
vector<int> arr;
vector<int> a;
vector<int> b(10);
vector<int> c(3,5);
vector<int> d={1,2,5,8};
a[3];
a.front();
a.back();
a.size();
a.empty();
a.push_back(7);
a.pop_back();
a.clear;
for(int i=0; i<arr.size(); i++){}
for(int x:arr){}
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
max_element(arr.begin(), arr.end());
min_element(arr.begin(), arr.end());
accumulate(arr.begin(), arr.end(), x);
count(arr.begin(), arr.end(), x);
find(arr.begin(), arr.end(), x);
binary_search(arr.begin(), arr.end(), x);
#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
#include <iostream>
// LINEAR TRAVERSAL
for (int x:arr){}
// MAX/ MIN
for (int x:arr){
val = (x > val) ? x: val;
\\ OR
val = max(val, x);
}
// CUMULATIVE
for (int i=0; i<arr.size(); i++){
cum[i]=cum[i-1]+arr[i];
}
// CUMULATIVE MAX/ MIN
for (int i=0; i<arr.size(); i++){
cum[i]= max(cum[i-1],arr[i]);
}
// FREQUENCY
for(int x:arr){
freq[x]++;
}
// KADANE'S ALGORITHM (LARGEST CONTINUOUS SUB-ARRAY)
for(int i=0; i<arr.size(); i++){
current_streak = max(current_streak, current_streak + arr[i]);
best = max(best, current_streak);
}
// DIFFERENCE ARRAY
for (int i=1; i<arr.size(); i++){
diff[i-1]= arr[i] - arr[i-1];
}
vector<vector<int>> arr(rows, vector<int>(cols)); \\ 2D ARRAY
vector<vector<vector<int>>> arr(rows, vector<vector<int>>(cols, vector<int>(depth))); \\ 3D ARRAY
// COMMON ALGORITHM PRE-DEFINED FUNCTION
#include<algorithm>
sort()
reverse()
find()
count()
binary_search()
lower_bound()
upper_bound()
min_element()
max_element()
unique()
fill()
rotate()
next_permutation()
prev_permutation()
nth_element()
partition()
stable_sort()
is_sorted()
// 1D PRE-DEFINED FUNCTIONS
v.push_back(x);
v.insert(pos, x);
v.pop_back();
v.erase(start, end); v.erase(start);
v.clear();
v.front();
v.back();
v.size();
v.empty();
v.assign(5, 10); // {10,10,10,10,10}
v.begin(); // first element
v.end(); // last element + 1 (no-element and pointer only)
v.rbegin(); // last element
v.rend(); // first element - 1 (no-element and pointer only)
find(v.begin(), v.end(), x);
count(v.begin(), v.end(), x);
sort(v.begin(), v.end());
sort(v.rbegin(), v.rend());
reverse(v.begin(), v.end());
*min_element(v.begin(), v.end());
*max_element(v.begin(), v.end());
// Multi-Dim PRE-DEFINED FUNCTIONS
mat[i][j]; // access
mat.push_back({1,2,3}); // insert row/ column
mat[0].push_back(5); // insert element
mat.size(); // rows
mat[0].size(); // columns
mat.pop_back(); // row operation
mat.erase(start, end);
mat[i].pop_back(); // column operation
mat[i].erase(start, end);
mat.clear();
// Traversal
for(int i=0;i<mat.size();i++)
for(int j=0;j<mat[i].size();j++)
cout<<mat[i][j];
for(auto &row : mat)
for(auto &x : row)
cout<<x;
// sort each row
for(auto &row : mat)
sort(row.begin(), row.end());
// sort rows
sort(mat.begin(), mat.end());