├── .vscode └── settings.json ├── Binary search ├── Problems │ ├── Hemdan and Company.2.cpp │ ├── Hemdan and Company.cpp │ ├── Physics Practical.2.cpp │ ├── Physics Practical.cpp │ ├── Sagheer and Nubian Market.2.cpp │ ├── magic-powder 1 - BF.cpp │ ├── magic-powder 1 - BS.cpp │ └── magic-powder 2.cpp ├── README.md └── binary search.md ├── Brute force ├── Problems │ ├── 2Char.2.cpp │ ├── 2Char.cpp │ ├── Average Length.cpp │ ├── Heavy Coins.cpp │ └── You're Given a String.cpp ├── README.md └── brute force.md ├── Elementary Math ├── Elementary math.cpp └── README.md ├── Factorization ├── Factorization.md └── README.md ├── Graph Theory ├── 2022 │ └── DFS and BFS.cpp └── 2023 │ ├── BFS.cpp │ ├── DFS.cpp │ └── adjacency matrix and list.cpp ├── Material ├── Momentum Library.pdf ├── Sayed.pdf ├── coach_library.pdf └── math_cheat_sheet.pdf ├── Primes ├── README.md └── primes.md ├── Recursion ├── problems │ ├── 23 out of 5.cpp │ └── The 3n + 1 problem.cpp ├── recursion.cpp ├── recursion_part1.md └── recursion_part2.md ├── STL ├── README.md ├── STL.cpp ├── STL_intro.md └── assets │ ├── map.png │ ├── queue.png │ ├── stack.png │ └── vector.png └── Time and Space Complexity ├── Space complexity.cpp └── Time complexity.cpp /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "conventionalCommits.scopes": ["STL"] 3 | } 4 | -------------------------------------------------------------------------------- /Binary search/Problems/Hemdan and Company.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Dandosh on --/2/2020. 3 | // 4 | // #pragma GCC optimize("O3") 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 11 | 12 | const int MAXN = 2e5 + 5; 13 | 14 | /* 15 | Link: https://codeforces.com/problemset/problem/580/B 16 | */ 17 | 18 | int main() 19 | { 20 | IO; 21 | #ifndef ONLINE_JUDGE 22 | //freopen("input.txt", "r", stdin); 23 | freopen("output.txt", "w", stdout); 24 | #endif 25 | int n, d; 26 | cin >> n >> d; 27 | vector> persons(n); 28 | for (int i = 0; i < n; i++) 29 | cin >> persons[i].first >> persons[i].second; 30 | 31 | sort(persons.begin(), persons.end()); 32 | for (int i = 1; i < n; i++) // cum sum over friendship factor 33 | persons[i].second += persons[i - 1].second; 34 | 35 | ll ans = 0; 36 | for (int i = 0; i < n; i++) 37 | { 38 | // get the farthest one I can take with me 39 | auto it = lower_bound(persons.begin(), persons.end(), make_pair(persons[i].first + d, 0LL)); 40 | ll limit = it - persons.begin() - 1; 41 | 42 | ans = max(ans, persons[limit].second - (i ? persons[i - 1].second : 0)); 43 | } 44 | 45 | cout << ans << endl; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Binary search/Problems/Hemdan and Company.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | // For Hassanosama, ICPC next year ISA. 3 | 4 | // Problem: https://codeforces.com/group/zvsr84He8w/contest/322905/problem/G 5 | 6 | #include 7 | 8 | #define endl '\n' 9 | #define sz size() 10 | #define vll vector 11 | #define vi vector 12 | #define pll pair 13 | #define pii pair 14 | #define all(v) ((v).begin()), ((v).end()) 15 | #define rall(v) ((v).rbegin()), ((v).rend()) 16 | #define IO ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 17 | 18 | using namespace std; 19 | using ll = long long int; 20 | using ld = long double; 21 | 22 | int MOD = 1e9 + 7; 23 | const ll OO = 1e18; 24 | const int oo = INT_MAX; 25 | const double EPS = 1e-9; 26 | 27 | const int dx[] = {1, -1, 0, 0}; 28 | const int dy[] = {0, 0, 1, -1}; 29 | 30 | ll GCD(ll a, ll b) { return ((!b) ? a : GCD(b, a % b)); } 31 | 32 | ll LCM(ll a, ll b) { return a / (GCD(a, b)) * b; } 33 | 34 | double distance(double x1, double y1, double x2, double y2) { return hypot(fabs(x1 - x2), fabs(y1 - y2)); } 35 | 36 | int comp_double(double a, double b) { 37 | if (fabs(a - b) <= EPS) return 0; // equal 38 | return (a < b) ? -1 : 1; 39 | } 40 | 41 | const int N = 1e5 + 15; 42 | int sm[N]; 43 | 44 | int main() { 45 | IO; 46 | #ifndef ONLINE_JUDGE 47 | freopen("In.txt", "r", stdin); 48 | freopen("Out.txt", "w", stdout); 49 | #else 50 | // freopen("input.in","r",stdin); 51 | // freopen("output.out","w",stdout); 52 | #endif 53 | ll n, d; 54 | cin >> n >> d; 55 | vector> a(n); 56 | vector b(n); 57 | 58 | for (auto &i :a) cin >> i.first >> i.second; 59 | 60 | sort(all(a)); 61 | for (int i = 1; i < n; ++i) { 62 | a[i].second += a[i - 1].second; 63 | } 64 | 65 | ll mx = LLONG_MIN; 66 | for (int i = 0; i < n; ++i) { 67 | int lst = lower_bound(all(a), make_pair(a[i].first + d, 0LL)) - a.begin() - 1; 68 | 69 | if (i == 0) { 70 | mx = max(mx, a[lst].second); 71 | } else { 72 | mx = max(mx, a[lst].second - a[i - 1].second); 73 | } 74 | } 75 | 76 | cout << mx << endl; 77 | } 78 | /* 79 | 80 | money friend 81 | 4 5 82 | 0 100 -> 0 83 | 75 1 -> 1 -> i 84 | 75 5 -> 2 85 | 150 20 -> 3 -> j 86 | 87 | i j 88 | | | 89 | idx: 0 1 2 3 90 | arr: 1 2 3 4 91 | acc: 1 3 6 10 92 | 93 | */ 94 | -------------------------------------------------------------------------------- /Binary search/Problems/Physics Practical.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Dandosh on --/2/2020. 3 | // 4 | // #pragma GCC optimize("O3") 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 11 | 12 | const int MAXN = 2e5 + 5; 13 | 14 | // Link: https://codeforces.com/problemset/problem/253/B 15 | 16 | int main() 17 | { 18 | IO; 19 | 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | 23 | int n; 24 | cin >> n; 25 | vector arr(n); 26 | for (int i = 0; i < n; i++) 27 | cin >> arr[i]; 28 | 29 | sort(arr.begin(), arr.end()); 30 | 31 | int ans = 0; 32 | for (int i = 0; i < n; i++) 33 | { 34 | int limit = upper_bound(arr.begin(), arr.end(), 2 * arr[i]) - arr.begin(); 35 | 36 | ans = max(ans, limit - i); 37 | } 38 | 39 | cout << n - ans << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Binary search/Problems/Physics Practical.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | // For Hassanosama, ICPC next year ISA. 3 | #include 4 | 5 | #define endl '\n' 6 | #define sz size() 7 | #define vll vector 8 | #define vi vector 9 | #define pll pair 10 | #define pii pair 11 | #define all(v) ((v).begin()), ((v).end()) 12 | #define rall(v) ((v).rbegin()), ((v).rend()) 13 | #define IO ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 14 | 15 | using namespace std; 16 | using ll = long long int; 17 | using ld = long double; 18 | 19 | int MOD = 1e9 + 7; 20 | const ll OO = 1e18; 21 | const int oo = INT_MAX; 22 | const double EPS = 1e-9; 23 | 24 | const int dx[] = {1, -1, 0, 0}; 25 | const int dy[] = {0, 0, 1, -1}; 26 | 27 | ll GCD(ll a, ll b) { return ((!b) ? a : GCD(b, a % b)); } 28 | 29 | ll LCM(ll a, ll b) { return a / (GCD(a, b)) * b; } 30 | 31 | double distance(double x1, double y1, double x2, double y2) { return hypot(fabs(x1 - x2), fabs(y1 - y2)); } 32 | 33 | int comp_double(double a, double b) { 34 | if (fabs(a - b) <= EPS) return 0; // equal 35 | return (a < b) ? -1 : 1; 36 | } 37 | 38 | int main() { 39 | IO; 40 | #ifndef ONLINE_JUDGE 41 | freopen("In.txt", "r", stdin); 42 | freopen("Out.txt", "w", stdout); 43 | #else 44 | freopen("input.txt", "r", stdin); 45 | freopen("output.txt", "w", stdout); 46 | #endif 47 | ll n; 48 | cin >> n; 49 | vector a(n); 50 | for (auto &i : a) cin >> i; 51 | 52 | sort(all(a)); 53 | 54 | int mx = 0; 55 | for (int i = 0; i < n; ++i) { 56 | int idx = upper_bound(all(a), 2 * a[i]) - a.begin(); 57 | 58 | mx = max(mx, idx - i); 59 | } 60 | 61 | cout << n - mx << endl; 62 | } 63 | -------------------------------------------------------------------------------- /Binary search/Problems/Sagheer and Nubian Market.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Dandosh on --/2/2020. 3 | // 4 | // #pragma GCC optimize("O3") 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 11 | 12 | const int MAXN = 2e5 + 5; 13 | 14 | int n, s, arr[MAXN]; 15 | 16 | pair ok(int noOfSouvs) 17 | { 18 | vector newValues(n); 19 | for (int i = 0; i < n; i++) 20 | newValues[i] = arr[i] + noOfSouvs * (i + 1LL); 21 | 22 | ll sum = 0; 23 | sort(newValues.begin(), newValues.end()); 24 | for (int i = 0; i < noOfSouvs; i++) 25 | sum += newValues[i]; 26 | 27 | return {sum <= s, sum}; 28 | } 29 | 30 | // Link: https://codeforces.com/problemset/problem/812/C 31 | 32 | int main() 33 | { 34 | IO; 35 | #ifndef ONLINE_JUDGE 36 | //freopen("input.txt", "r", stdin); 37 | freopen("output.txt", "w", stdout); 38 | #endif 39 | cin >> n >> s; 40 | for (int i = 0; i < n; i++) 41 | { 42 | cin >> arr[i]; 43 | } 44 | 45 | int l = 0, r = n, mid, noOfSouvs = 0; 46 | while (l <= r) 47 | { 48 | mid = l + (r - l) / 2; 49 | 50 | if (ok(mid).first) 51 | { 52 | l = mid + 1; 53 | noOfSouvs = mid; 54 | } 55 | else 56 | { 57 | r = mid - 1; 58 | } 59 | } 60 | 61 | cout << noOfSouvs << ' ' << ok(noOfSouvs).second << endl; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Binary search/Problems/magic-powder 1 - BF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ll long long 6 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 7 | 8 | const int N = 1e5 + 5; 9 | int need[N], have[N]; 10 | int n, k; 11 | 12 | bool ok(int noOfCakes) { 13 | ll magicPowder = k; 14 | 15 | for (int i = 0; i < n; i++) { 16 | ll extraNeed = (ll) need[i] * noOfCakes - have[i]; 17 | 18 | if (extraNeed < 0) // we have enough 19 | continue; 20 | 21 | magicPowder -= extraNeed; // remove the extra need from the magic powder 22 | if (magicPowder < 0) // magic powder isn't enough 23 | return false; 24 | } 25 | return true; 26 | } 27 | 28 | int main() { 29 | IO; 30 | 31 | cin >> n >> k; 32 | for (int i = 0; i < n; i++) 33 | cin >> need[i]; 34 | for (int i = 0; i < n; i++) 35 | cin >> have[i]; 36 | 37 | int res = 0; 38 | for (int i = 0; i <= 2000; i++) { 39 | if (!ok(i)) { 40 | break; 41 | } 42 | 43 | res = i; 44 | } 45 | 46 | cout << res << endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Binary search/Problems/magic-powder 1 - BS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ll long long 6 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 7 | 8 | const int N = 1e5 + 5; 9 | int need[N], have[N]; 10 | int n, k; 11 | 12 | bool ok(int noOfCakes) 13 | { 14 | ll magicPowder = k; 15 | 16 | for (int i = 0; i < n; i++) 17 | { 18 | ll extraNeed = (ll)need[i] * noOfCakes - have[i]; 19 | 20 | if (extraNeed < 0) // we have enough 21 | continue; 22 | 23 | magicPowder -= extraNeed; // remove the extra need from the magic powder 24 | if (magicPowder < 0) // magic powder isn't enough 25 | return false; 26 | } 27 | return true; 28 | } 29 | 30 | int main() 31 | { 32 | IO; 33 | 34 | cin >> n >> k; 35 | for (int i = 0; i < n; i++) 36 | cin >> need[i]; 37 | for (int i = 0; i < n; i++) 38 | cin >> have[i]; 39 | 40 | ll left = 0, right = 2e9 + 1, res = 0; 41 | 42 | while (left <= right) 43 | { 44 | ll mid = (left + right) / 2; 45 | if (ok(mid)) 46 | { 47 | left = mid + 1; 48 | res = mid; 49 | } 50 | else 51 | right = mid - 1; 52 | } 53 | 54 | cout << res << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Binary search/Problems/magic-powder 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define ll long long 6 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 7 | 8 | const int N = 1e5 + 5; 9 | int need[N], have[N]; 10 | int n, k; 11 | 12 | bool ok(int noOfCakes) 13 | { 14 | ll magicPowder = k; 15 | 16 | for (int i = 0; i < n; i++) 17 | { 18 | ll extraNeed = (ll)need[i] * noOfCakes - have[i]; 19 | 20 | if (extraNeed < 0) // we have enough 21 | continue; 22 | 23 | magicPowder -= extraNeed; // remove the extra need from the magic powder 24 | if (magicPowder < 0) // magic powder isn't enough 25 | return false; 26 | } 27 | return true; 28 | } 29 | 30 | int main() 31 | { 32 | IO; 33 | 34 | cin >> n >> k; 35 | for (int i = 0; i < n; i++) 36 | cin >> need[i]; 37 | for (int i = 0; i < n; i++) 38 | cin >> have[i]; 39 | 40 | ll left = 0, right = 2e9, res = 0; 41 | 42 | while (left <= right) 43 | { 44 | // ll mid = (left + right) / 2; 45 | ll mid = left + (right - left) / 2; 46 | if (ok(mid)) 47 | { 48 | left = mid + 1; 49 | res = mid; 50 | } 51 | else 52 | right = mid - 1; 53 | } 54 | 55 | cout << res << endl; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Binary search/README.md: -------------------------------------------------------------------------------- 1 | ![Component 26](https://user-images.githubusercontent.com/40190772/113439873-a7fe7180-93eb-11eb-976b-60946a117b15.png) 2 | -------------------------------------------------------------------------------- /Binary search/binary search.md: -------------------------------------------------------------------------------- 1 | # 🔎 Binary search 2 | 3 | ## Introduction 4 | 5 | - Find a specific page in a book 6 | - Guessing game 7 | 8 | ``` txt 9 | 0 1 2 3 4 5 6 => indexes 10 | 2 3 5 7 11 13 17 => array 11 | ``` 12 | 13 | - Complexity **O(?)** 14 | 15 | - **Monotonic behavior** 16 | 17 | ![image](https://user-images.githubusercontent.com/40351413/113416819-5beb0700-93c2-11eb-91af-1181472a1deb.png) 18 | ![image](https://user-images.githubusercontent.com/40351413/113416752-3f4ecf00-93c2-11eb-81b7-f2e244a28698.png) 19 | ![image](https://user-images.githubusercontent.com/40351413/113416770-4675dd00-93c2-11eb-99bd-2112e9e0701c.png) 20 | 21 | ## Two types of problems 22 | 23 | ### 1. Searching for a specific item 24 | 25 | #### Example 26 | 27 | - Guessing game 28 | 29 | #### Implementation 30 | 31 | ```cpp 32 | int n = 7, target = 11; 33 | vector arr = {2, 3, 5, 7, 11, 13, 17}; 34 | 35 | int index = -1; 36 | int left = 0, right = n - 1; 37 | 38 | while (left <= right) { 39 | // int mid = (left + right) / 2; 40 | int mid = left + (right - left) / 2; // Prevent overflow 41 | 42 | if (arr[mid] < target) 43 | left = mid + 1; 44 | else if (arr[mid] > target) 45 | right = mid - 1; 46 | else if (arr[mid] == target) { 47 | index = mid; 48 | break; 49 | } 50 | } 51 | 52 | cout << "Index: " << index << " - Element: " << arr[index] << endl; 53 | 54 | ``` 55 | 56 | ```cpp 57 | /** 58 | Output: 59 | Index: 4 - Element: 11 60 | */ 61 | 62 | ``` 63 | 64 | ### 2. Minimization and maximization problems 65 | 66 | #### Example 67 | 68 | - Get the minimum number that is greater than 8 69 | 70 | ``` txt 71 | 2 3 5 7 11 13 17 => array 72 | 0 0 0 0 1 1 1 => valid 73 | ``` 74 | 75 | #### Implementation 76 | 77 | ```cpp 78 | int n = 7, target = 8; 79 | vector arr = {2, 3, 5, 7, 11, 13, 17}; 80 | 81 | int index = -1; 82 | int left = 0, right = n - 1; 83 | 84 | while (left <= right) { 85 | int mid = (left + right) / 2; 86 | 87 | if (arr[mid] <= target) 88 | left = mid + 1; 89 | else if (arr[mid] > target) // upper-bound 90 | right = mid - 1, index = mid; 91 | // else if (arr[mid] >= target) // lower-bound 92 | // right = mid - 1, index = mid; 93 | } 94 | 95 | cout << "Index: " << index << " - Element: " << arr[index] << endl; 96 | 97 | ``` 98 | 99 | ```cpp 100 | /** 101 | Output: 102 | Index: 4 - Element: 11 103 | */ 104 | 105 | ``` 106 | 107 | ## Built-in functions (STL) 108 | 109 | > CPP provides a built in function in STL (Standard template library) 110 | 111 | ### binary_search 112 | 113 | - Return whether a vector has element x or not 114 | - Your array should be **sorted** 115 | 116 | ```cpp 117 | int x; 118 | cin >> x; 119 | 120 | if (binary_search(b.begin(), b.end(), x)) { 121 | cout << "Found" << endl; 122 | } else { 123 | cout << "Not found" << endl; 124 | } 125 | 126 | ``` 127 | 128 | ### lower_bound 129 | 130 | - returns an iterator pointing to the first element greater than or equal **‘x’** 131 | - Your array should be **sorted** 132 | 133 | ```cpp 134 | int x; 135 | cin >> x; 136 | auto it = lower_bound(b.begin(), b.end(), x); 137 | auto index = it - b.begin(); 138 | 139 | if (it != b.end()) { 140 | cout << *it << endl; 141 | } else { 142 | cout << "Not found\n"; 143 | } 144 | 145 | ``` 146 | 147 | ### upper_bound 148 | 149 | - Returns an iterator pointing to the first element greater than **‘x’** 150 | - Your array should be **sorted** 151 | 152 | ```cpp 153 | int x; 154 | cin >> x; 155 | 156 | auto it = upper_bound(b.begin(), b.end(), x); 157 | if (it != b.end()) { 158 | cout << *it << endl; 159 | } else { 160 | cout << "Not found\n"; 161 | } 162 | 163 | ``` 164 | 165 | ## Problems 166 | 167 | - Magic powder - 1 (brute force) - [Solution](https://github.com/ACM-FE-CU-Community/Sessions-Codes/blob/main/Binary%20search/Problems/magic-powder%201%20-%20BF.cpp) 168 | - [Magic powder - 1](https://codeforces.com/contest/670/problem/D1) (binary search) - [Solution](https://github.com/ACM-FE-CU-Community/Sessions-Codes/blob/main/Binary%20search/Problems/magic-powder%201%20-%20BS.cpp) 169 | - [Magic powder - 2](https://codeforces.com/contest/670/problem/D2) (binary search) - [Solution](https://github.com/ACM-FE-CU-Community/Sessions-Codes/blob/main/Binary%20search/Problems/magic-powder%202.cpp) 170 | -------------------------------------------------------------------------------- /Brute force/Problems/2Char.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Dandosh on --/2/2020. 3 | // 4 | // #pragma GCC optimize("O3") 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 11 | 12 | const int MAXN = 2e5 + 5; 13 | 14 | int main() 15 | { 16 | IO; 17 | #ifndef ONLINE_JUDGE 18 | //freopen("input.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | 22 | int n, ans = 0; 23 | string s; 24 | cin >> n; 25 | 26 | map, int> freq; 27 | for (int i = 0; i < n; i++) 28 | { 29 | cin >> s; 30 | set st; 31 | for (auto j : s) 32 | st.insert(j); 33 | 34 | if (st.size() <= 2) 35 | { 36 | char c1 = *st.begin(); 37 | char c2 = *(--st.end()); 38 | 39 | freq[{c1, c2}] += s.length(); 40 | } 41 | } 42 | 43 | for (char i = 'a'; i <= 'z'; i++) 44 | for (char j = 'a'; j <= 'z'; j++) 45 | if (i != j) 46 | ans = max(ans, freq[{i, j}] + freq[{i, i}] + freq[{j, j}]); 47 | else 48 | ans = max(ans, freq[{i, j}]); 49 | 50 | cout << ans << endl; 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Brute force/Problems/2Char.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | // For Hassanosama, ICPC next year ISA. 3 | #include 4 | 5 | #define endl '\n' 6 | #define sz size() 7 | #define vll vector 8 | #define vi vector 9 | #define pll pair 10 | #define pii pair 11 | #define all(v) ((v).begin()), ((v).end()) 12 | #define rall(v) ((v).rbegin()), ((v).rend()) 13 | #define IO ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 14 | 15 | using namespace std; 16 | using ll = long long int; 17 | using ld = long double; 18 | 19 | int MOD = 1e9 + 7; 20 | const ll OO = 1e18; 21 | const int oo = INT_MAX; 22 | const double EPS = 1e-9; 23 | 24 | const int dx[] = {1, -1, 0, 0}; 25 | const int dy[] = {0, 0, 1, -1}; 26 | 27 | ll GCD(ll a, ll b) { return ((!b) ? a : GCD(b, a % b)); } 28 | 29 | ll LCM(ll a, ll b) { return a / (GCD(a, b)) * b; } 30 | 31 | double distance(double x1, double y1, double x2, double y2) { return hypot(fabs(x1 - x2), fabs(y1 - y2)); } 32 | 33 | int comp_double(double a, double b) { 34 | if (fabs(a - b) <= EPS) return 0; // equal 35 | return (a < b) ? -1 : 1; 36 | } 37 | 38 | int main() { 39 | IO; 40 | #ifndef ONLINE_JUDGE 41 | freopen("In.txt", "r", stdin); 42 | freopen("Out.txt", "w", stdout); 43 | #else 44 | // freopen("input.in","r",stdin); 45 | // freopen("output.out","w",stdout); 46 | #endif 47 | int n; 48 | cin >> n; 49 | vector a(n); 50 | for (auto &i : a) cin >> i; 51 | 52 | int ret = INT_MIN; 53 | for (char i = 'a'; i <= 'z'; ++i) { 54 | for (char j = 'a'; j < 'z'; ++j) { 55 | 56 | int tot = 0; 57 | for (int k = 0; k < n; ++k) { 58 | bool isOk = true; 59 | for (auto c : a[k]) { 60 | if (c != i && c != j) { 61 | isOk = false; 62 | } 63 | } 64 | 65 | if (isOk) { 66 | tot += a[k].size(); 67 | } 68 | } 69 | 70 | ret = max(ret, tot); 71 | } 72 | } 73 | 74 | cout << ret << endl; 75 | } 76 | -------------------------------------------------------------------------------- /Brute force/Problems/Average Length.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | // For Hassanosama, ICPC next year ISA. 3 | // Problem link: https://atcoder.jp/contests/abc145/tasks/abc145_c 4 | #include 5 | 6 | #define endl '\n' 7 | #define sz size() 8 | #define vll vector 9 | #define vi vector 10 | #define pll pair 11 | #define pii pair 12 | #define all(v) ((v).begin()), ((v).end()) 13 | #define rall(v) ((v).rbegin()), ((v).rend()) 14 | #define IO \ 15 | ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) 16 | 17 | using namespace std; 18 | 19 | double distance(double x1, double y1, double x2, double y2) { 20 | return hypot(fabs(x1 - x2), fabs(y1 - y2)); 21 | } 22 | 23 | int main() { 24 | IO; 25 | int n; 26 | cin >> n; 27 | vector> a(n); 28 | for (auto &i : a) cin >> i.first >> i.second; 29 | 30 | int noOfCombinations = 1; 31 | for (int i = 1; i <= n; i++) { 32 | noOfCombinations *= i; 33 | } 34 | 35 | double sum = 0.0; 36 | sort(all(a)); 37 | 38 | do { 39 | for (int i = 0; i < n - 1; i++) { 40 | sum += distance(a[i].first, a[i].second, a[i + 1].first, a[i + 1].second); 41 | } 42 | 43 | } while (next_permutation(all(a))); 44 | 45 | /* 46 | * Average = Sum / noOfCombinations 47 | */ 48 | cout << fixed << setprecision(8) << sum / noOfCombinations << endl; 49 | } 50 | -------------------------------------------------------------------------------- /Brute force/Problems/Heavy Coins.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | // For Hassanosama, ICPC next year ISA. 3 | #ifdef _MSC_VER 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #endif 6 | #include 7 | #define vll vector 8 | #define vi vector 9 | #define sz size() 10 | #define pll pair 11 | #define pii pair 12 | #define IO \ 13 | ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) 14 | #define all(v) ((v).begin()), ((v).end()) 15 | #define rall(v) ((v).rbegin()), ((v).rend()) 16 | 17 | using namespace std; 18 | using ll = long long int; 19 | using ld = long double; 20 | 21 | int MOD = 1e9 + 7; 22 | const double EPS = 1e-9; 23 | const int oo = INT_MAX; 24 | const ll OO = 1e18; 25 | 26 | const int dx[] = {1, -1, 0, 0}; 27 | const int dy[] = {0, 0, 1, -1}; 28 | 29 | ll GCD(ll a, ll b) { return ((!b) ? a : GCD(b, a % b)); } 30 | ll LCM(ll a, ll b) { return a / (GCD(a, b)) * b; } 31 | 32 | int main() { 33 | IO; 34 | 35 | ll t; 36 | cin >> t; 37 | while (t--) { 38 | ll n, k; 39 | cin >> n >> k; 40 | vector a(n); 41 | 42 | for (auto& i : a) cin >> i; 43 | 44 | ll mx = LLONG_MIN; 45 | for (ll mask = 1; mask < (1 << n); mask++) { 46 | ll coinSum = 0; 47 | ll noOfCoins = 0; 48 | ll minCoin = LLONG_MAX; 49 | 50 | for (ll i = 0; i < n; i++) { 51 | if (mask & (1 << i)) { 52 | noOfCoins++; 53 | coinSum += a[i]; 54 | minCoin = min({minCoin, a[i]}); 55 | } 56 | } 57 | /* 58 | * Checks 59 | * 1. condition 1 60 | * 2. condition 2 61 | */ 62 | if (coinSum >= k && coinSum - k < minCoin) { 63 | mx = max({mx, noOfCoins}); 64 | } 65 | } 66 | 67 | cout << mx << endl; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Brute force/Problems/You're Given a String.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Dandosh on --/2/2020. 3 | // 4 | // #pragma GCC optimize("O3") 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define ll long long 10 | #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) 11 | 12 | const int MAXN = 2e5 + 5; 13 | 14 | int main() { 15 | IO; 16 | string s; 17 | cin >> s; 18 | int n = s.size(), ans = 0; 19 | 20 | for (int i = 0; i < n; i++) { 21 | string t; 22 | for (int j = i; j < n; j++) { 23 | t += s[j]; 24 | 25 | int firstOcc = -1, lastOcc = -1; 26 | for (int k = 0; k + t.size() <= n; k++) 27 | if (s.substr(k, t.size()) == t) { 28 | if (firstOcc == -1) firstOcc = k; 29 | lastOcc = k; 30 | } 31 | 32 | if (firstOcc != lastOcc) ans = max(ans, (int)t.size()); 33 | } 34 | } 35 | 36 | cout << ans << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Brute force/README.md: -------------------------------------------------------------------------------- 1 | ![Component 26](https://user-images.githubusercontent.com/40190772/113439873-a7fe7180-93eb-11eb-976b-60946a117b15.png) 2 | -------------------------------------------------------------------------------- /Brute force/brute force.md: -------------------------------------------------------------------------------- 1 | # 🔨 Brute force 2 | > What we have seen is a technique called brute force, in which we try all the combinations 3 | 4 | ## General Checks: 5 | - Check the constraints of the problem that trying all the combination will not get you TLE 6 | - Check corner cases of the problem (n = 0, n = 1, n = MAXN) 7 | 8 | # PowerSet (All subsets of S) 9 | > PowerSet of set `S` is the set of all subsets of `S`. 10 | 11 | > If set `S` has n element, So P(S) = 2 ^ n 12 | 13 | ```cpp 14 | for example: 15 | S = {a, b, c} 16 | 17 | P(S) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}. 18 | ``` 19 | 20 | ## Bad implementation 21 | 22 | ```cpp 23 | /** 24 | * ! 1. Not extendable 25 | * * 2. More handling 26 | */ 27 | 28 | set st; 29 | vector s = {'a', 'b', 'c'}; 30 | // vector s = {'a', 'b', 'c', 'd'}; 31 | 32 | 33 | for (int i = 0; i <= 1 ; i++) { // a 34 | for (int j =0; j <= 1 ; j++) { // b 35 | for (int k =0; k <= 1 ; k++) { // c 36 | string t; 37 | if (i) t += s[0]; 38 | if (j) t += s[1]; 39 | if (k) t += s[2]; 40 | 41 | st.insert(t); 42 | } 43 | } 44 | } 45 | ``` 46 | 47 | ## Better implementation 48 | 49 | 50 | ``` 51 | 0 => 000 52 | 1 => 001 53 | 2 => 010 54 | 3 => 011 55 | 4 => 100 56 | 5 => 101 57 | 6 => 110 58 | 7 => 111 59 | 60 | ``` 61 | ### Check if bit is setted or not 62 | 63 | ``` 64 | 1 << i = pow(2, i) 65 | 66 | if number of elements = 3 67 | 68 | (i = 0) => So => 001 69 | (i = 1) => So => 010 70 | (i = 2) => So => 100 71 | ``` 72 | 73 | ```cpp 74 | ll n = 3; 75 | string s = "abc"; 76 | 77 | for (ll mask = 0; mask < (1 << n); mask++) { 78 | string t; 79 | for (ll i = 0; i < n; i++) { 80 | if ((1 << i) & mask) { 81 | t += s[i]; 82 | } 83 | } 84 | 85 | st.insert(t); 86 | } 87 | ``` 88 | 89 | ### Let's see a real problem on power-set 90 | > Check heavy coins https://codeforces.com/gym/100712/ 91 | 92 | 93 | 94 | 95 | ### All arrangements 96 | 97 | S = {a, b, c} 98 | 99 | S1 = {a, b, c} 100 | S2 = {a, c, b} 101 | S3 = {b, c, a} 102 | S4 = {b, a, c} 103 | S5 = {c, a, b} 104 | S6 = {c, b, a} 105 | 106 | 107 | ```cpp 108 | for(int i = 0; i < n ; i++) { 109 | for(int j = 0; j < n ; j++) { 110 | for(int k = 0; k < n ; k++) { 111 | if (i != j && i != k) { 112 | 113 | } 114 | } 115 | } 116 | } 117 | ``` 118 | 119 | 120 | ```cpp 121 | /** 122 | * * A permutation is each one of the N! possible arrangements O(N!) 123 | * 124 | * * 1! = 1 125 | * * 2! = 2 126 | * * 3! = 6 127 | * * 4! = 24 128 | * * 5! = 120 129 | * * 6! = 720 130 | * * 7! = 5040 131 | * * 8! = 40320 132 | * * 9! = 362880 133 | * * 10! = 3628800 134 | * ! 11! = 39916800 135 | */ 136 | ``` 137 | ```cpp 138 | /** 139 | * ~ CPP provides a built in function in STL (Standard template library) 140 | * ^ next_permutation(array_boundaries) 141 | * 142 | * * next_permutation(arr, arr + size) => array 143 | * * next_permutation(vec.begin(), vec.end()) => vector 144 | */ 145 | 146 | 147 | vector s = {'a', 'b', 'c'}; 148 | 149 | do { 150 | cout << s[0] << " " << s[1] << " " << s[2] << endl; 151 | } while (next_permutation(s.begin(), s.end())); 152 | 153 | 154 | /** 155 | * ! Output 156 | * 157 | * a b c 158 | * a c b 159 | * b a c 160 | * b c a 161 | * c a b 162 | * c b a 163 | */ 164 | 165 | ``` 166 | 167 | 168 | -------------------------------------------------------------------------------- /Elementary Math/Elementary math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // #include 3 | 4 | /** 5 | * for better comment highlighting 6 | * https://marketplace.visualstudio.com/items?itemName=ParthR2031.colorful-comments 7 | */ 8 | 9 | using namespace std; 10 | using ll = long long int; 11 | using ld = long double; 12 | 13 | void builtin_functions() { 14 | /** 15 | * C++ offers for us some ready-made functions like: 16 | * 17 | * floor, ceil, round, fabs, abs, sqrt, log, log2, 18 | * log10, pow, cos, cosh, acosh 19 | * 20 | * to use them you need to include lib 21 | * 22 | * ? https://www.cplusplus.com/reference/cmath/ 23 | */ 24 | 25 | /** 26 | * 27 | * * Rounding functions 28 | * 29 | * & double ceil(double x) 30 | * returns the smallest integer as double not less than the argument provided 31 | * 32 | * & double floor(double x) 33 | * returns the integer value lesser or equal to argument passed in the function. 34 | * 35 | * & double round(double x) 36 | * returns the integral value that is nearest to x 37 | * 38 | * & double trunc(double x) 39 | * return x rounded towards zero(remove fraction) 40 | * 41 | * 42 | * 43 | * 44 | * * Geometric functions 45 | * 46 | * & double sin(double x) 47 | * takes angle (in radians) and return its sine 48 | * 49 | * & double cos(double x) 50 | * takes angle (in radians) and return its cosine 51 | * 52 | * & double tan(double x) 53 | * takes angle (in radians) and return its tangent 54 | * 55 | * & double asin(double x) 56 | * returns the arc sine of argument. The argument to asin() must be in the range -1 to 1 ; otherwise, a domain error occurs. 57 | * 58 | * & double acos(double x) 59 | * returns the arc cosine, The argument to acos() must be in the range -1 to 1; 60 | * 61 | * & double atan(double x) 62 | * returns the arc tangent of arg 63 | * 64 | * 65 | * * Misc functions 66 | * 67 | * & double fabs(double x) 68 | * returns the absolute value of any number. 69 | * 70 | * & int abs(int x) 71 | * return its absolute value 72 | * 73 | * & double pow(double x, double y) 74 | * return x (base) power y (exp) 75 | * 76 | * & double sqrt(double) 77 | * return its square root value. Number can not be negative value. 78 | * 79 | * & double cbrt (double) 80 | * return its cubic root value. Number can be negative value. 81 | * 82 | * & double hypot (double x, double y) 83 | * return hypotenuse of a right-angled triangle whose legs are x and y 84 | * sqrt(x * x + y * y) 85 | * 86 | * & double log(double) 87 | * returns the natural log of that number (base e) 88 | * 89 | * & double log10(double) 90 | * returns the 10 log of that number (base 10) 91 | * 92 | * & double log2(double) 93 | * returns the 2 log of that number (base 2) 94 | */ 95 | 96 | } 97 | 98 | void ceil_replacement() { 99 | /** 100 | * * replacement for ceil (double) to interger domain 101 | * 102 | * ? ceil(x, y) = (x+y-1) / y 103 | * ceil(x / y) 104 | * 105 | */ 106 | 107 | int x = 5, y = 2; 108 | int z = (x + y - 1) / y; // (6 / 2) => 3 109 | 110 | } 111 | 112 | /** 113 | * ^ EPS => will be mentioned in the problem 114 | * 115 | * * 0 => Equal 116 | * * 1 => First larger 117 | * * -1 => Second larger 118 | */ 119 | 120 | const double EPS = 1e-6; 121 | int comp_double(double a, double b) { 122 | if (fabs(a - b) <= EPS) return 0; 123 | return a < b ? -1 : 1; 124 | } 125 | 126 | void double_comparision() { 127 | /** 128 | * * Double Comparsion 129 | * 130 | * (1) 131 | * ! do not use float in competitive programming cuz it have some precision errors 132 | * ! Sometimes if you make float comparsion it'll fails (Only 7 decimal places) 133 | */ 134 | 135 | /** 136 | * * DON'T USE FLOAT 137 | * ! Output: Fails 138 | */ 139 | 140 | float x = 4.7; 141 | if (x == float(4.7)) cout << "Equal" << endl; 142 | else cout << "Fails" << endl; 143 | 144 | /** 145 | * ! 15 decimal digits of precision. 146 | * ^ NOT exactly 4.70000000000000000000000000 147 | * * IT's better to use comp_double 148 | */ 149 | double a = 4.7; 150 | double b = 4.7; 151 | 152 | if(comp_double(a, b)) { 153 | cout << "Equal" << endl; 154 | }else { 155 | cout << "Not Equal" << endl; 156 | } 157 | 158 | 159 | } 160 | 161 | 162 | ll fastpow(ll b, ll e) { 163 | if (!e) { 164 | return 1; 165 | } 166 | 167 | if (e % 2) { 168 | return b * fastpow(b, e - 1); 169 | } 170 | 171 | ll x = fastpow(b, e / 2); 172 | return x * x; 173 | } 174 | 175 | ll PowMod(ll b, ll e, ll M) { 176 | if (!e) { 177 | return 1; 178 | } 179 | 180 | ll p = PowMod((b * b) % M, e / 2, M); 181 | 182 | return (e % 2) ? (p * b) % M : p; 183 | } 184 | 185 | void modular_arithematic() { 186 | /** 187 | * * Modular Arithmetic 188 | * 189 | * Suppose we are now (9) in (12 time system) 190 | * ? what'll be the time after 4 hours 191 | * 192 | * 193 | * ~ 9 + 4 = 13 = 1 + 12 = 1 194 | * 195 | * & n = r (rem) + m * c(cycles) 196 | * 197 | * 5 + 0 * 12 198 | * 10 + 0 * 12 199 | * 1 + 1 * 12 200 | * 201 | * * So % used to remove the cycles from our number 202 | * 203 | * 13 % 12 = 1 204 | * 7 % 12 = 7 205 | * 206 | * 207 | * 208 | * 209 | * 210 | * 211 | * ? what if I ask you before 12 hours ? 212 | * * 9 => 9 - 12 = -3 % 12 => -3 hmm 🤔 213 | * 214 | * -3 % 12 = -3 + 12 = 9 215 | * 216 | * So we have to fix it (Fixing -ve) Rule 217 | * 218 | * ~ (a % n + n) % n 219 | * ~ (-3 % 12 + 12) % 12 => 9 220 | * (12 % 12 + 12) % 12 => 0 221 | * 222 | */ 223 | 224 | 225 | /** 226 | * * List of facts about modulus 227 | * 228 | * ^ (1) 229 | * (a%n = 0) => a divisible by n 230 | * 231 | * 232 | * ^ (2) 233 | * If a%n == b%n => (a - b) % n = 0 234 | 235 | * a = 13 236 | * b = 34 237 | * n = 21 238 | * 239 | * a - b = 21 240 | * 241 | * 242 | * ^ (3) 243 | * largest n such that a%n = b%n is n = b - a 244 | * 245 | * 246 | * ^ (4) 247 | * (a % n) % n = a % n 248 | * 249 | * ^ (5) 250 | * (n ^ x) % n = 0 => for any x >= 0 251 | * (n * n * n * ... * n) % n = ((n % n) * (n % n) * ...(n % n)) 252 | * 253 | * ^ (6) 254 | * -a%n != a%n = > (3 % 12 = 3 vs -3 % 12 = 9) 255 | * 256 | * ^ (7) 257 | * ((-a%n) + (a%n)) % n = 0 258 | * -a + a 259 | * 260 | * (x + (n - x) = n % n = 0) 261 | * 262 | * ^ (8) 263 | * (a + b) % n = (a%n + b%n) % n 264 | * 265 | * 266 | * (a + b + c + d) % n ? 267 | * n = 10 268 | * 269 | * 270 | * ^ (9) 271 | * ((((a%n + (b%n) ) % n + (c%n)) % n + (d%n)) % n 272 | * 273 | * 274 | * ^ (10) 275 | * x % (a + b) != x % a + x%b 276 | * a / (x + y) != a / x + a / y 277 | * 278 | * ^ (11) 279 | * x % 10 [get the last digit] 280 | * 281 | */ 282 | } 283 | 284 | 285 | 286 | 287 | void Basics() { 288 | builtin_functions(); 289 | ceil_replacement(); 290 | double_comparision(); 291 | 292 | /** 293 | * * Simple Trick: to get no of digits 294 | * 295 | * ^ #of digits = floor(log10(n)) 296 | */ 297 | int x = 123456; 298 | int noOfDigits = floor(log10(x)) + 1; // 6 299 | 300 | /** 301 | * * Built in pow function is slower O(n) 302 | * * 10 ^ 5 => 10*10*10*10*10 303 | * 304 | * 305 | * * Fast Power 306 | * ^ O(log(n)) 307 | */ 308 | 309 | ll a = 5, b = 12; 310 | ll pw = fastpow(a, b); 311 | cout << pw << endl; 312 | 313 | 314 | 315 | /** 316 | * * power mod (a ^ b) % MOD 317 | * * Use fast power 318 | */ 319 | ll e = 5, c = 10000, mod = 1e9 + 7; 320 | ll ans = PowMod(e, c, mod); 321 | 322 | 323 | 324 | modular_arithematic(); 325 | 326 | // Problems 327 | // 1. 328 | // https://codeforces.com/contest/742/problem/A 329 | // given a number n, find the last digit in pow(1378,n) 330 | Problem_1(); 331 | 332 | // 2. 333 | // https://codeforces.com/contest/732/problem/A 334 | // each shavel cost k egy-pound and we have unlimited number of coins of cost 335 | // 10 egy-pound and only one coin of cost r-egy-pound find the minimum number 336 | // of coins we need to pay to buy shavels without any coin-back 337 | Problem_2(); 338 | 339 | // 3. 340 | // TODO: https://codeforces.com/contest/602/problem/A 341 | // Given 2 numbers in different bases, compare between them = | > | < 342 | Problem_3(); 343 | 344 | // 4. 345 | // TODO: https://codeforces.com/contest/579/problem/A 346 | // you can put any number of bacteria into the box. And each night, every 347 | // bacterium in the box will split into two bacteria. You hope to see exactly 348 | // x bacteria in the box at some moment. What is the minimum number of 349 | // bacteria you need to put into the box across those days ? 350 | Problem_4(); 351 | } 352 | 353 | 354 | 355 | void Problem_1() { 356 | ll n; 357 | cin >> n; 358 | ll ans = PowMod(8, n, 10); 359 | cout << ans << endl; 360 | 4 % 4 361 | 1 362 | /** 363 | * Anyone has a recommendation 364 | */ 365 | 366 | // 8, 64, 512, 4096, 32768, 262144, .. 367 | // 8, 4, 2, 6, 8, 4, 2, 6, .... 368 | 369 | int k, r; cin >> k >> r; 370 | } 371 | 372 | 373 | void Problem_2() { 374 | ll k, r; 375 | cin >> k >> r; 376 | ll noOfCoins = 1; 377 | 378 | while (true) { 379 | if ((k * noOfCoins) % 10 == 0 || (k * noOfCoins - r) % 10 == 0) break; 380 | noOfCoins++; 381 | } 382 | 383 | cout << noOfCoins << endl; 384 | } 385 | 386 | void Problem_3() { 387 | ll A = 0, B = 0; 388 | ll n, ba; 389 | cin >> n >> ba; 390 | vll a(n); 391 | for (ll& i : a) cin >> i; 392 | reverse(all(a)); 393 | for (int i = 0; i < n; i++) A += a[i] * pow(ba, i); 394 | 395 | ll m, bb; 396 | cin >> m >> bb; 397 | vll b(m); 398 | for (ll& i : b) cin >> i; 399 | reverse(all(b)); 400 | for (int i = 0; i < m; i++) B += b[i] * pow(bb, i); 401 | 402 | if (A < B) 403 | puts("<"); 404 | else if (A > B) 405 | puts(">"); 406 | else 407 | puts("="); 408 | } 409 | 410 | void Problem_4() { 411 | ll n; 412 | cin >> n; 413 | ll cnt = 0; 414 | while (n) { 415 | cnt += (n & 1); 416 | n >>= 1; 417 | } 418 | cout << cnt << endl; 419 | 420 | // Cool !! 421 | // cout << __builtin_popcountll(n)) << endl; 422 | } 423 | 424 | int main() { 425 | IO; 426 | #ifndef ONLINE_JUDGE 427 | freopen("In.txt", "r", stdin); 428 | #else 429 | // freopen("input.in","r",stdin); 430 | // freopen("output.out","w",stdout); 431 | #endif 432 | Basics(); 433 | } 434 | -------------------------------------------------------------------------------- /Elementary Math/README.md: -------------------------------------------------------------------------------- 1 | ![Elementary Math](https://user-images.githubusercontent.com/40351413/114317325-cf4df080-9b07-11eb-947d-23a8a32bd50f.png) 2 | -------------------------------------------------------------------------------- /Factorization/Factorization.md: -------------------------------------------------------------------------------- 1 | # Factorization section 2 | 3 | ## Content 4 | 5 | - [Factorization section](#factorization-section) 6 | - [Content](#content) 7 | - [Brute force approach](#brute-force-approach) 8 | - [Optimized version](#optimized-version) 9 | - [Count divisors](#count-divisors) 10 | - [Count divisors for one number](#count-divisors-for-one-number) 11 | - [Count divisors for all numbers from 1 to n](#count-divisors-for-all-numbers-from-1-to-n) 12 | - [Backward thinking](#backward-thinking) 13 | - [Problems](#problems) 14 | - [Multiplication Table](#multiplication-table) 15 | - [Brute force solution](#brute-force-solution) 16 | - [Optimized solution](#optimized-solution) 17 | - [Bear and Poker](#bear-and-poker) 18 | 19 | ## Brute force approach 20 | 21 | ```cpp 22 | vector generateDivisorsBruteForce(int n) // O(n) 23 | { 24 | vector divisors; 25 | for (int i = 1; i <= n; i++) 26 | if (n % i == 0) 27 | divisors.push_back(i); 28 | return divisors; 29 | } 30 | ``` 31 | 32 | ## Optimized version 33 | 34 | - **Do we need to loop until n** 35 | 36 | ```cpp 37 | 16 = 1 * 16 38 | 16 = 2 * 8 39 | 16 = 4 * 4 => mirror 40 | 16 = 8 * 2 41 | 16 = 16 * 1 42 | ``` 43 | 44 | ```txt 45 | IF n = x * y && x <= y 46 | 47 | THEN x <= sqrt(n) and y >= sqrt(n) 48 | ``` 49 | 50 | ```cpp 51 | vector generateDivisorsSqrt(int n) // O(sqrt(n)) 52 | { 53 | vector divisors; 54 | 55 | for (int i = 1; i * i <= n; i++) 56 | { 57 | if (n % i == 0) { 58 | divisors.push_back(i); 59 | divisors.push_back(n / i); 60 | 61 | if (i * i == n) 62 | divisors.pop_back(); 63 | } 64 | } 65 | 66 | sort(divisors.begin(), divisors.end()); // O(nLog(n)) 67 | return divisors; 68 | } 69 | ``` 70 | 71 | ## Count divisors 72 | 73 | ### Count divisors for one number 74 | 75 | ```cpp 76 | int countDivisors(int n) // O(sqrt(n)) 77 | { 78 | int cnt = 0, i; 79 | for (i = 1; i * i <= n; i++) 80 | if (n % i == 0) 81 | cnt += 2; 82 | 83 | // 25 => 5 * 5, 36 = 6 * 6, ...etc 84 | if (isPerfectSquare(n)) cnt--; 85 | return cnt; 86 | } 87 | ``` 88 | 89 | ### Count divisors for all numbers from 1 to n 90 | 91 | ```cpp 92 | void divCountAllBruteForce(int n) // O(n * Sqrt(n)) 93 | { 94 | vector divisors(n + 1); 95 | for (int i = 1; i <= n; i++) 96 | { 97 | divisors[i] = countDivisors(i); // O(sqrt(n)) 98 | } 99 | } 100 | ``` 101 | 102 | #### Backward thinking 103 | ```cpp 104 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 .... 105 | 106 | 1 divides 1, 2, 3, 4, 5 ... n 107 | 2 divides 2, 4, 6, 8, 10 ... n 108 | 3 divides 3, 6, 9, 12, 15 ... n 109 | 4 divides 4, 8, 12, 16, 20 ... n 110 | 5 divides 5, 10, 15, 20, 25 ... n 111 | 112 | Complexity = n/1 + n/2 + n/3 + .... n/n 113 | => Harmonic series => n log(n) 114 | 115 | ``` 116 | 117 | ```cpp 118 | void divCountAllOptimized(int n) 119 | { 120 | vector divisors(n + 1); 121 | for (int i = 1; i <= n; i++) 122 | { 123 | for (int j = i; j <= n; j += i) 124 | divisors[j]++; 125 | } 126 | } 127 | ``` 128 | 129 | - - - 130 | 131 | ## Problems 132 | 133 | ### Multiplication Table 134 | 135 | Link: 136 | 137 | ![times-table](https://user-images.githubusercontent.com/40190772/115963677-ade5ff00-a520-11eb-8f3c-432eee5ad139.png) 138 | 139 | 140 | #### Brute force solution 141 | 142 | ```cpp 143 | void MultiplicationTableBruteForce() 144 | { 145 | ll n, x; 146 | cin >> n >> x; 147 | int cnt = 0; 148 | for (int i = 1; i <= n; i++) // iterate over all the rows 149 | if (x % i == 0 && x / i <= n) // check if this row is multiple of x, check the other multiple exists 150 | cnt++; 151 | } 152 | ``` 153 | 154 | - If n was big (1 <= n <= 1e9) 155 | - We can use sieve with small modification but HOW ? 156 | 157 | #### Optimized solution 158 | 159 | ```cpp 160 | void MultiplicationTableOptimized() 161 | { 162 | ll n, x; 163 | cin >> n >> x; 164 | vector divisors = generateDivisorsSqrt(x); 165 | int cnt = 0; 166 | 167 | for(auto divisor : divisors) { 168 | if (divisor <= n && (x / divisor) <= n) 169 | cnt++; 170 | } 171 | 172 | cout << cnt << endl; 173 | } 174 | ``` 175 | 176 | ### Bear and Poker 177 | 178 | Link: 179 | 180 | ```cpp 181 | void bearAndPoker() { 182 | int n; 183 | cin >> n; 184 | vector arr(n); 185 | 186 | for (int i = 0; i < n; i++) 187 | { 188 | cin >> arr[i]; 189 | 190 | while (arr[i] % 2 == 0) arr[i] /= 2; 191 | while (arr[i] % 3 == 0) arr[i] /= 3; 192 | 193 | if (arr[i] != arr[0]) 194 | { 195 | cout << "NO\n"; 196 | return 0; 197 | } 198 | } 199 | 200 | cout << "YES\n"; 201 | } 202 | ``` 203 | -------------------------------------------------------------------------------- /Factorization/README.md: -------------------------------------------------------------------------------- 1 | ![Component 48](https://user-images.githubusercontent.com/40190772/115973401-f4ede780-a554-11eb-981c-752044a0c036.png) 2 | -------------------------------------------------------------------------------- /Graph Theory/2022/DFS and BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /** 6 | * * Practice Problems: 7 | * 8 | * ^ [1] Dfs: 9 | * ^ 1-connected component (dfs) 10 | * & Problem: https://cses.fi/problemset/task/1192 11 | * & Solution: https://www.ideone.com/b76j2z 12 | * 13 | * ^ [2] 2-bfs 14 | * & Problem: https://codeforces.com/contest/242/problem/C 15 | * & Solution: https://www.ideone.com/Z3OiwY 16 | * 17 | * ^ [3] 3-check bipartite graph (bfs) 18 | * & Problem: https://cses.fi/problemset/task/1668 19 | * & Solution: https://www.ideone.com/vmiIyy 20 | */ 21 | 22 | 23 | /* 24 | ? adjacent list vs adjacent matrix 25 | Adjacency List: An Adjacency list is an array consisting of the address of all the linked lists. 26 | Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. 27 | 28 | !example:https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/ 29 | 30 | int adjmatrix[][3]={{0,0,0}, 31 | {0,1,0}, 32 | {0,1,1}}; 33 | 34 | vector adjlist[100]; 35 | 36 | ?Depth First Search (DFS) 37 | Depth-first search is an algorithm for traversing or searching tree or graph data structures. 38 | 39 | !cool simulation:https://www.cs.usfca.edu/~galles/visualization/DFS.html 40 | 41 | ?implementation: Time Complexity: O(V+E) 42 | */ 43 | 44 | vectoradjlist[100]; 45 | bool vis[100]; 46 | 47 | void dfs(int cur) 48 | { 49 | vis[cur]=1; 50 | for(int i=0;iadjlist[100]; 67 | bool vis[100]; 68 | 69 | void bfs(int cur) 70 | { 71 | queuequeue; 72 | queue.push(cur); 73 | while(queue.size()!=0) 74 | { 75 | int node =queue.front(); 76 | queue.pop(); 77 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int n, m; cin >> n >> m; 9 | vector vis(n, false); 10 | vector > adj(n); 11 | while(m--) { 12 | int u, v; cin >> u >> v; 13 | u--, v--; 14 | adj[u].push_back(v); 15 | adj[v].push_back(u); 16 | } 17 | /* queue q; 18 | q.push(0); 19 | vis[0] = true; 20 | while(!q.empty()) { 21 | auto cur = q.front(); 22 | q.pop(); 23 | cout << cur+1 << ' '; 24 | for (auto &i : adj[cur]) { 25 | if (!vis[i]) { 26 | vis[i] = true; 27 | q.push(i); 28 | } 29 | } 30 | }*/ 31 | vector dist(n, -1); 32 | dist[0] = 0; 33 | queue q; 34 | q.push(0); 35 | vector par(n, -1); 36 | while(!q.empty()) { 37 | auto cur = q.front(); 38 | q.pop(); 39 | for (auto &i : adj[cur]) { 40 | if (!~dist[i]) { // dist[i] == -1 41 | q.push(i); 42 | par[i] = cur; 43 | dist[i] = dist[cur]+1; 44 | } 45 | } 46 | } 47 | cout << dist[n-1]+1 << '\n'; 48 | vector path; 49 | int cur = n-1; 50 | while(~cur) { // cur != -1 51 | path.push_back(cur); 52 | cur = par[cur]; 53 | } 54 | reverse(path.begin(), path.end()); 55 | for (auto &i : path) cout << i+1 << ' '; 56 | cout << '\n'; 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Graph Theory/2023/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector vis; 6 | vector > adj; 7 | 8 | void dfs(int cur) 9 | { 10 | vis[cur] = true; 11 | for (auto &i : adj[cur]) { 12 | if (vis[i]) continue; 13 | dfs(i); 14 | } 15 | cout << cur+1 << ' '; 16 | } 17 | 18 | int main() 19 | { 20 | int n, m; cin >> n >> m; 21 | adj.assign(n, {}); 22 | vis.assign(n, false); 23 | while(m--) { 24 | int u, v; cin >> u >> v; 25 | u--, v--; 26 | adj[u].push_back(v); 27 | adj[v].push_back(u); 28 | } 29 | dfs(0); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Graph Theory/2023/adjacency matrix and list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n, m; cin >> n >> m; 8 | vector > adj_list(n); 9 | // vector > adj_mat(n); 10 | vector > adj_mat(n, vector (n, false)); 11 | while(m--) { 12 | int u, v; cin >> u >> v; 13 | u--, v--; 14 | adj_list[u].push_back(v); 15 | adj_list[v].push_back(u); 16 | adj_mat[u][v] = true; 17 | adj_mat[v][u] = true; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Material/Momentum Library.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/Material/Momentum Library.pdf -------------------------------------------------------------------------------- /Material/Sayed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/Material/Sayed.pdf -------------------------------------------------------------------------------- /Material/coach_library.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/Material/coach_library.pdf -------------------------------------------------------------------------------- /Material/math_cheat_sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/Material/math_cheat_sheet.pdf -------------------------------------------------------------------------------- /Primes/README.md: -------------------------------------------------------------------------------- 1 | ![Component 48](https://user-images.githubusercontent.com/40190772/115973401-f4ede780-a554-11eb-981c-752044a0c036.png) 2 | -------------------------------------------------------------------------------- /Primes/primes.md: -------------------------------------------------------------------------------- 1 | # Primes section 2 | 3 | ## Content 4 | 5 | 1. [Primes definition and test](#1) 6 | 2. [Counting primes in range](#2) 7 | 3. [Prime divisors](#3) 8 | 4. [Problems](#4) 9 | 10 | ## Primes definition and test
11 | 12 | ### Definition 13 | 14 | - N is a prime IFF it divides only by 1 and N 15 | 16 | ### Primality test 17 | 18 | ```cpp 19 | // slow version 20 | bool isPrime(int n) // O(n) 21 | { 22 | if (n < 2) return false; 23 | 24 | for (int i = 2; i < n; i++) 25 | { 26 | if (n % i == 0) 27 | return false; 28 | } 29 | 30 | return true; 31 | } 32 | ``` 33 | 34 | - **Do we need to loop until n** 35 | 36 | ```cpp 37 | 16 = 1 * 16 38 | 16 = 2 * 8 39 | 16 = 4 * 4 => mirror 40 | 16 = 8 * 2 41 | 16 = 16 * 1 42 | ``` 43 | 44 | ```txt 45 | IF n = x * y && x <= y 46 | 47 | THEN x <= sqrt(n) and y >= sqrt(n) 48 | ``` 49 | 50 | ```cpp 51 | bool isPrimeSqrt(int n) // O(sqrt(n)) 52 | { 53 | if (n < 2) return false; 54 | 55 | // i <= sqrt(n) -> it's a bad practice cuz of double 56 | // so to get rid of it -> power 2 sides 57 | // i <= sqrt(n) -> i *i <= n 58 | for (int i = 2; i* i <= n; i++) 59 | { 60 | if (n % i == 0) 61 | return false; 62 | } 63 | return true; 64 | } 65 | ``` 66 | 67 | ### Small optimization 68 | 69 | - **Any even number isn't a prime except *2*** 70 | 71 | ```cpp 72 | bool isPrimeSqrt2(int n) // O(sqrt(n)) 73 | { 74 | if (n == 2) return true; 75 | if (n < 2 || n % 2 == 0) return false; 76 | 77 | for (int i = 3; i * i <= n; i += 2) 78 | { 79 | if (n % i == 0) 80 | return false; 81 | } 82 | return true; 83 | } 84 | ``` 85 | 86 | ## Counting primes in a range
87 | 88 | ```cpp 89 | // brute force 90 | int CountPrimes(int n) // O(n * sqrt(n) ) 91 | { 92 | int cnt = 0; 93 | for (int i = 1; i <= n; ++i) 94 | if (isPrime_sqrt2(i)) 95 | cnt++; 96 | 97 | return cnt; 98 | } 99 | ``` 100 | 101 | ### Backward thinking 🔙 102 | 103 | ```cpp 104 | 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 .... 105 | 106 | Let 2 removes 4, 6, 8, 10 ... n 107 | Let 3 removes 6, 9, 12, 15 ... n 108 | 4 is removed 109 | Let 5 remove 10, 15, 20 ... n 110 | 6 is removed 111 | Could 7 be removed? NEVER, no one before it could divides it! 112 | Let 7 removes 14, 21 ... n 113 | 114 | ``` 115 | 116 | ```cpp 117 | int CountPrimesSeive(int n) // O(n log(log(n))) 118 | { 119 | vector primes(n + 1, 1); 120 | 121 | int cnt = 0; 122 | 123 | primes[0] = primes[1] = 0; 124 | for (int i = 2; i <= n; i++) 125 | { 126 | if (primes[i]) 127 | { 128 | cnt++; 129 | for (int j = i + i; j <= n; j += i) 130 | primes[j] = 0; 131 | } 132 | } 133 | 134 | return cnt; 135 | } 136 | ``` 137 | 138 | ## Prime divisors
139 | 140 | ### Fact 141 | 142 | - Any number can be represented by products of some primes 143 | 144 | ### Ways 145 | 146 | 1. Use sieve to gen. primes then count how many of them divides n 147 | 2. Loop to **sqrt(n)** and get rid of primes as much as U can **(more efficient)** 148 | 149 | ```cpp 150 | 200 / 2 = 100 151 | 100 / 2 = 50 152 | 50 / 2 = 25 153 | 154 | 25 % 8 != 0 but 200 % 8 == 0 155 | ``` 156 | 157 | ```cpp 158 | vector PrimeDivisors(int n) // sqrt(n) or less 159 | { 160 | vector divisors; 161 | for (int i = 2; i * i <= n; i++) 162 | { 163 | while (n % i == 0) 164 | { 165 | n /= i; 166 | divisors.push_back(i); 167 | } 168 | } 169 | // let n = 12 170 | // n / 2 = 6 171 | // 6 / 2 = 3 ==> get out from the loop with n = 3 172 | 173 | // if n != 1 -> it must be a prime 174 | // think in n as a sub-problem, we will begin from 2 and try to divide it 175 | // but we already get rid of all 2's and 3's ... so it must be a prime 176 | if (n != 1) divisors.push_back(n); 177 | 178 | return divisors; 179 | } 180 | ``` 181 | 182 | - - - 183 | 184 | ## Problems
185 | 186 | 1. [Almost Prime](https://codeforces.com/contest/26/problem/A) 187 | - Count numbers that has exactly two distinct prime divisors from 1 to n 188 | 189 | ```cpp 190 | int AlmostPrime() // Forward Thinking 191 | { 192 | int n, ans = 0; 193 | cin >> n; 194 | for (int i = 1; i <= n; i++) 195 | { 196 | int primeDivs = 0, tempi = i; 197 | 198 | for (int j = 2; j * j <= tempi; j++) 199 | { 200 | if (tempi % j == 0) primeDivs++; 201 | 202 | while (tempi % j == 0) 203 | { 204 | tempi /= j; 205 | } 206 | } 207 | if (tempi != 1) primeDivs++; 208 | 209 | ans += (primeDivs == 2); 210 | } 211 | return ans; 212 | } 213 | ``` 214 | 215 | - If n was big (1 <= n <= 1000000) 216 | - We can use sieve with small modification but HOW ? 217 | 218 | ```cpp 219 | int AlmostPrimeSeive(int n) // Backward Thinking 220 | { 221 | vector primes(n + 1, 1); 222 | vector no_primes(n + 1); 223 | 224 | primes[0] = primes[1] = 0; 225 | for (int i = 2; i <= n; i++) 226 | { 227 | if (primes[i]) 228 | { 229 | for (int j = i + i; j <= n; j += i) 230 | primes[j] = 0, 231 | no_primes[j]++; 232 | } 233 | } 234 | 235 | int ans = 0; 236 | for (int i = 0; i <= n; i++) 237 | if (no_primes[i] == 2) 238 | ans++; 239 | 240 | return ans; 241 | } 242 | ``` 243 | 244 | 2. [Duff in Love](https://codeforces.com/problemset/problem/588/B) 245 | 246 | - Find max divisor of ***n*** that hasn't a perfect square divisor 247 | - Perfect square numbers: 1, 4, 9, 16, 25, ... 248 | 249 | ```cpp 250 | bool isPerfectSquare(long double x) 251 | { 252 | long double sr = sqrt(x); 253 | return ((sr - floor(sr)) == 0); 254 | } 255 | 256 | int DuffInLoveBruteForce(int n) 257 | { 258 | vector divisors = getDivisors(n); 259 | sort(divisors.rbegin(), divisors.rend()); 260 | 261 | for (auto divisor: divisors) { 262 | vector tempDivisors = getDivisors(divisor); 263 | 264 | bool fail = 0; 265 | for (auto j : tempDivisors) { 266 | if (j != 1 && isPerfectSquare(j)) 267 | fail = 1; 268 | } 269 | 270 | if (!fail) { 271 | return divisor; 272 | } 273 | } 274 | 275 | } 276 | ``` 277 | 278 | ```cpp 279 | void Duff_In_Love_2() 280 | { 281 | ll n; cin >> n; 282 | ll ans = 1; 283 | set d; 284 | 285 | while (n % 2 == 0) n /= 2, d.insert(2); 286 | 287 | for (ll i = 3; i * i <= n; i += 2) 288 | { 289 | if (n % i == 0) d.insert(i); 290 | while (n % i == 0) n /= i; 291 | } 292 | 293 | if (n > 1) // means the remainder of n is prime 294 | d.insert(n); 295 | for (auto i : d) 296 | ans *= i; 297 | 298 | cout << ans << '\n'; 299 | } 300 | ``` 301 | -------------------------------------------------------------------------------- /Recursion/problems/23 out of 5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int a[5]; 6 | 7 | bool solve(int idx, int ans) { 8 | if (idx == 5) { 9 | return ans == 23; 10 | } 11 | 12 | return solve(idx + 1, ans * a[idx]) || 13 | solve(idx + 1, ans + a[idx]) || 14 | solve(idx + 1, ans - a[idx]); 15 | } 16 | 17 | int main() { 18 | while(1) { 19 | int cntZeros = 0; 20 | for (int i = 0; i < 5; i++) { 21 | cin >> a[i]; 22 | } 23 | 24 | if (a[0] == 0) 25 | break; 26 | 27 | sort(a, a + 5); 28 | bool possible = false; 29 | 30 | do { 31 | possible = solve(1, a[0]); 32 | 33 | if (possible) { 34 | cout << "Possible\n"; 35 | break; 36 | } 37 | 38 | } while (next_permutation(a, a + 5)); 39 | 40 | if (!possible) 41 | cout << "Impossible\n"; 42 | } 43 | } -------------------------------------------------------------------------------- /Recursion/problems/The 3n + 1 problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int solve(int n) { 6 | if (n == 1) 7 | return 1; 8 | 9 | if (n % 2 == 0) 10 | return 1 + solve(n / 2); 11 | 12 | return 1 + solve(3 * n + 1); 13 | } 14 | 15 | int main() { 16 | int a, b; 17 | while(cin >> a >> b) { 18 | int maxi = 0; 19 | for (int i = min(a, b); i <= max(a, b); i++) { 20 | maxi = max(maxi, solve(i)); 21 | } 22 | 23 | cout << a << ' ' << b << ' ' << maxi << endl; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Recursion/recursion.cpp: -------------------------------------------------------------------------------- 1 | // ^ Recursion ^ \\ 2 | // * a function is said to be recursive if it calls itself. 3 | 4 | /* 5 | & for better comment highlighting 6 | & https://marketplace.visualstudio.com/items?itemName=ParthR2031.colorful-comments 7 | */ 8 | 9 | 10 | 11 | 12 | #include 13 | 14 | using namespace std; 15 | 16 | // & Let us revise some functions basics 17 | 18 | int func(int b) { 19 | // do any thing 20 | } 21 | 22 | int decrease(int a) { 23 | // ? what is the difference between the next 2 lines ? 24 | func(a - 1); // func(4) 25 | func(--a); // func(4) 26 | 27 | // Any extra code here 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | // ^ Example 1 37 | 38 | 39 | // ? what is the next 3 functions do? 40 | // ? f3(5) 41 | 42 | int f1(int n) 43 | { 44 | return n * 1; 45 | } 46 | 47 | int f2(int n) 48 | { 49 | // ! calc: n * f1(n - 1) => n * (n - 1) 50 | return n * f1(n-1); 51 | } 52 | 53 | int f3(int n) 54 | { 55 | // ! calc: n * f2(n-1) => n * (n - 1) * f1(n - 2) => n * (n - 1) * (n - 2) 56 | return n * f2(n-1); 57 | } 58 | 59 | 60 | 61 | // the above 3 functions are doing the same thing 62 | // ? so, do we need to repeat the same code again in every function ? 63 | // ^ no, let the function just call itself like the following 64 | 65 | // f(5) 66 | 67 | int f(int n) { 68 | return n * f(n - 1); 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | /* 81 | ^ the above function will behave like the following: 82 | ! calc: n * f(n - 1) 83 | ! => n * (n - 1) * f(n - 2) 84 | ! => n * (n - 1) * (n - 2) * f(n - 3) => ....forever 85 | ^ so, we need to stop it, hmmmmmm, let's try putting a condition at the start of it 86 | */ 87 | 88 | // f(5) 89 | // 5 * 4 * 3 * 2 * 1 90 | 91 | int f(int n) { 92 | if (n <= 1) 93 | return 1; 94 | 95 | return n * f(n - 1); 96 | } 97 | 98 | 99 | /* 100 | ^ with this modification, the function will call itself until reaching 1 101 | ^ after reaching 1, it will stop calling itself and return 1 102 | 103 | & and this condition is called "base condition": the condition that stops the recursion calls 104 | & and function parameters (n) are called "state": the current state of our variables in the recursion calls 105 | 106 | & consider each call as a new copy of the function 107 | */ 108 | 109 | 110 | 111 | // ^ Example 2 112 | 113 | 114 | /* 115 | 116 | * 117 | ** 118 | *** 119 | **** 120 | ***** 121 | 122 | ? how can we draw this shape? 123 | ^ we can use some loops to draw it, but we want to use recursion 124 | */ 125 | 126 | int draw_triangle_iterative(int depth = 5) { 127 | for(int i = 1; i <= depth; i++) { 128 | for (int j = 1; j <= i ; j++) { 129 | cout << '*'; 130 | } 131 | 132 | cout << endl; 133 | } 134 | } 135 | 136 | 137 | 138 | void draw_triangle(int depth = 5) { 139 | // ^ base condition 140 | if (depth <= 0) { 141 | return; 142 | } 143 | 144 | for (int i = 0; i < depth; i++) { 145 | cout << "*"; 146 | } 147 | cout << endl; 148 | 149 | // ! draw_triangle(depth--); 150 | draw_triangle(depth - 1); 151 | 152 | } 153 | 154 | 155 | 156 | 157 | 158 | 159 | // * wrong implementation * \\ 160 | 161 | void draw_triangle(int depth = 5) { 162 | // ^ base condition 163 | if (depth <= 0) { 164 | return; 165 | } 166 | 167 | while(depth--) { 168 | cout << "*"; 169 | } 170 | cout << endl; 171 | 172 | draw_triangle(depth - 1); 173 | } 174 | 175 | 176 | 177 | // ^ Example 3 178 | 179 | 180 | // ^ recursion with more than one call 181 | /* 182 | & starting from the top left corner, return the path with the max sum reaching the bottom right corner\ 183 | & you can go right and down 184 | 512 185 | 678 186 | 189 187 | 188 | ? base condition? 189 | ? next calls? 190 | ? calcaulate results? 191 | */ 192 | 193 | 194 | const int n = 3, m = 3; 195 | int grid[n][m]; 196 | int maxPathSum(int r, int c) 197 | { 198 | // ^ Base condition 199 | if (r == n-1 && c == m-1) 200 | return grid[r][c]; 201 | 202 | // ^ Next calls 203 | int path1 = maxPathSum(r, c+1); // right 204 | int path2 = maxPathSum(r+1, c); // down 205 | 206 | // ^ Calculate results 207 | return grid[r][c] + max(path1, path2); 208 | } 209 | 210 | // ! we have bug here 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | // * correct implementation * \\ 220 | 221 | bool valid(int r, int c) { 222 | return r < n && r >= 0 && c < m && c >= 0; 223 | } 224 | 225 | int maxPathSum(int r, int c) 226 | { 227 | // ^ inside or outside the grid 228 | if (!valid(r, c)) 229 | return 0; 230 | 231 | if (r == n-1 && c == m-1) 232 | return grid[r][c]; // base 233 | 234 | int path1 = maxPathSum(r, c+1); // right 235 | int path2 = maxPathSum(r+1, c); // down 236 | 237 | return grid[r][c] + max(path1, path2); 238 | } 239 | 240 | 241 | 242 | 243 | 244 | 245 | // ^ Example 4: Flood Fill 246 | 247 | /* 248 | 249 | ...x. 250 | ..x.. 251 | .x... 252 | x.... 253 | 254 | */ 255 | 256 | int floodFill(int r, int c) { 257 | if (!valid(r, c) || grid[r][c] == 'x') 258 | return 0; 259 | 260 | int ans = 0; 261 | ans += floodFill(r + 1, c); // down 262 | ans += floodFill(r - 1, c); // up 263 | ans += floodFill(r, c + 1); // right 264 | ans += floodFill(r, c - 1); // left 265 | 266 | return 1 + ans; 267 | } 268 | 269 | // ! we have bug here 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | // * correct implementation * 279 | bool vis[n][m]; 280 | 281 | int floodFill(int r, int c) { 282 | if (!valid(r, c) || grid[r][c] == 'x' || vis[r][c]) 283 | return 0; 284 | 285 | vis[r][c] = true; 286 | 287 | int ans = 0; 288 | ans += floodFill(r + 1, c); // down 289 | ans += floodFill(r - 1, c); // up 290 | ans += floodFill(r, c + 1); // right 291 | ans += floodFill(r, c - 1); // left 292 | 293 | return 1 + ans; 294 | } 295 | 296 | 297 | 298 | // ^ Problems 299 | 300 | // 1. https://vjudge.net/problem/UVA-10344 301 | // 2. https://vjudge.net/problem/UVA-100 302 | -------------------------------------------------------------------------------- /Recursion/recursion_part1.md: -------------------------------------------------------------------------------- 1 | # Recursion 2 | ## Definition 3 | - a function is said to be recursive if it calls itself. 4 | 5 | ---------------- 6 | 7 | ## Let us revise some functions basics 8 | 9 | ```c++ 10 | int print(int b) { 11 | cout << b << endl; 12 | } 13 | 14 | int decrease(int a) { 15 | // ? what is the difference between the next 2 lines ? 16 | print(a - 1); // print(4) 17 | print(--a); // print(4) 18 | 19 | // Any extra code here 20 | } 21 | 22 | // function call 23 | decrease(5); 24 | ``` 25 | ---------------- 26 | 27 | ## Exmaple 1: Factorial 28 | 29 | ```c++ 30 | factorial(5) = 1 * 2 * 3 * 4 * 5 31 | factorial(4) = 1 * 2 * 3 * 4 32 | factorial(3) = 1 * 2 * 3 33 | factorial(2) = 1 * 2 34 | factorial(1) = 1 35 | ``` 36 | ## relation between factorial(5) and factorial(4) ? 37 | ```c++ 38 | if we know answer for factorial(4) we can calculate factorial(5) by multiplying it by 5 39 | 40 | factorial(5) = factorial(4) * 5 41 | ``` 42 | 43 | ## iterative solution 44 | ```c++ 45 | int factorial(int n) { 46 | int result = 1; 47 | for (int i = 1; i <= n; i++) { 48 | result *= i; 49 | } 50 | return result; 51 | } 52 | ``` 53 | 54 | ## let the problem is to calculate factorial(5) 55 | 56 | ```c++ 57 | int f1(){ 58 | return 1; 59 | } 60 | // f1 = 1 61 | 62 | int f2(){ 63 | return 2*f1(); 64 | } 65 | // f2 = 2 * f1 66 | // = 2 * 1 = 2 67 | 68 | int f3(){ 69 | return 3*f2(); 70 | } 71 | // f3 = 3 * f2 72 | // = 3 * 2 = 6 73 | 74 | int f4(){ 75 | return 4*f3(); 76 | } 77 | // f4 = 4 * f3 78 | // = 4 * 6 = 24 79 | 80 | int f5(){ 81 | return 5*f4(); 82 | } 83 | // f5 = 5 * f4 84 | // = 5 * 24 = 120 85 | ``` 86 | 87 | ## recursive solution 88 | ```c++ 89 | int factorial(int n) { 90 | if (n == 1) { 91 | return 1; 92 | } 93 | return n * factorial(n - 1); 94 | } 95 | ``` 96 | ## trace the recursive solution 97 | ```c++ 98 | factorial(5) = 5 * factorial(4) 99 | factorial(4) = 4 * factorial(3) 100 | factorial(3) = 3 * factorial(2) 101 | factorial(2) = 2 * factorial(1) 102 | factorial(1) = 1 103 | ``` 104 | 105 | ## what is the base case ? 106 | ## function parameter (n) are called state the current state of our variables in the recursion calls 107 | 108 | ---------------- 109 | 110 | ## Example 2: print array 111 | 112 | ```c++ 113 | int arr[] = {1, 2, 3, 4}; 114 | int n=4; 115 | ``` 116 | 117 | ## iterative solution 118 | ```c++ 119 | void printArray(int arr[], int n) { 120 | for (int i = 0; i < n; i++) { 121 | cout << arr[i] << " "; 122 | } 123 | cout << endl; 124 | } 125 | ``` 126 | 127 | ## recursive solution 128 | ```c++ 129 | void printArray(int i) { 130 | if (i == n) { 131 | return; 132 | } 133 | cout << arr[i] << " "; 134 | printArray(i + 1); 135 | } 136 | ``` 137 | ## trace the recursive solution 138 | ```c++ 139 | printArray(0) -> cout << arr[0] << " "; 140 | printArray(1); 141 | 142 | printArray(1) -> cout << arr[1] << " "; 143 | printArray(2); 144 | ... 145 | printArray(3) -> cout << arr[3] << " "; 146 | printArray(4); 147 | 148 | printArray(4) -> return; 149 | 150 | output: 1 2 3 4 151 | ``` 152 | 153 | ## what this code output ? 154 | ```c++ 155 | void printArray(int i) { 156 | if (i == n) { 157 | return; 158 | } 159 | printArray(i + 1); 160 | cout << arr[i] << " "; 161 | //return; 162 | } 163 | ``` 164 | ## trace 165 | ```c++ 166 | printArray(0) -> printArray(1); 167 | printArray(1) -> printArray(2); 168 | printArray(2) -> printArray(3); 169 | printArray(3) -> printArray(4); 170 | 171 | printArray(4) -> return; 172 | 173 | // i = 3 174 | cout << arr[3] << " "; 175 | return; 176 | 177 | // i = 2 178 | cout << arr[2] << " "; 179 | return; 180 | 181 | // i = 1 182 | cout << arr[1] << " "; 183 | return; 184 | 185 | // i = 0 186 | cout << arr[0] << " "; 187 | return; 188 | ``` 189 | 190 | ---------------- 191 | ## Example 3: Fibonacci 192 | ## what is fibonacci series ? 193 | ```c++ 194 | 0 1 1 2 3 5 8 13 21 ...... 195 | ``` 196 | ```c++ 197 | f(0) = 0 198 | f(1) = 1 199 | f(n) = f(n-1) + f(n-2) 200 | ``` 201 | 202 | ```c++ 203 | int fibonacci(int n) { 204 | if (n == 0) { 205 | return 0; 206 | } 207 | if (n == 1) { 208 | return 1; 209 | } 210 | return fibonacci(n - 1) + fibonacci(n - 2); 211 | } 212 | ``` 213 | ## trace 214 | ```c++ 215 | if we call fibonacci(5) we will get the following calls 216 | 5 217 | / \ 218 | 4 3 219 | / \ / \ 220 | 3 2 2 1 221 | /\ /\ /\ /\ 222 | 2 1 1 0 1 0 0 223 | /\ 224 | 1 0 225 | ``` 226 | 227 | ```c++ 228 | int fibonacci(int n) { 229 | if (n == 0) { 230 | return 0; 231 | } 232 | if (n == 1) { 233 | return 1; 234 | } 235 | // first call 236 | int f1 = fibonacci(n - 1); 237 | // second call 238 | int f2 = fibonacci(n - 2); 239 | // return the sum of the two calls 240 | return f1 + f2; 241 | } 242 | ``` 243 | 244 | ## what is the time complexity of this solution ? 245 | 246 | ---------------- 247 | 248 | ## problem 1 249 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/K 250 | ## sol : https://www.ideone.com/XDr2Eh 251 | 252 | ---------------- 253 | 254 | ## problem 2 255 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/Q 256 | ## sol : https://www.ideone.com/mAP8gE 257 | 258 | ---------------- 259 | 260 | ## problem 3 261 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/R 262 | ## sol : https://www.ideone.com/ALaabZ 263 | 264 | ---------------- 265 | ---------------- 266 | ---------------- 267 | ---------------- 268 | 269 | # Contest 270 | ## Rigster here https://codeforces.com/group/MWSDmqGsZm/contests 271 | ## Contest link https://codeforces.com/group/MWSDmqGsZm/contest/223339 -------------------------------------------------------------------------------- /Recursion/recursion_part2.md: -------------------------------------------------------------------------------- 1 | # Recursion 2 | ## Definition 3 | - a function is said to be recursive if it calls itself. 4 | 5 | ---------------- 6 | 7 | ## Let us revise some functions basics 8 | 9 | ```c++ 10 | int print(int b) { 11 | cout << b << endl; 12 | } 13 | 14 | int decrease(int a) { 15 | // ? what is the difference between the next 2 lines ? 16 | print(a - 1); // print(4) 17 | print(--a); // print(4) 18 | 19 | // Any extra code here 20 | } 21 | // function call 22 | decrease(5); 23 | ``` 24 | 25 | ## Exmaple 1: print numbers up to n bits in binary 26 | 27 | ```c++ 28 | int n=3 29 | ``` 30 | 31 | ```c++ 32 | void printBinary(int bit,string num){ 33 | if(bit == n){ 34 | cout << num << endl; 35 | return; 36 | } 37 | printBinary(bit+1,num+"0"); 38 | printBinary(bit+1,num+"1"); 39 | } 40 | ``` 41 | ```c++ 42 | printBinary(0,""); 43 | ``` 44 | ## Trace the recursive solution 45 | ```c++ 46 | "" 47 | / \ 48 | "0" "1" 49 | / \ / \ 50 | "00" "01" "10" "11" 51 | 52 | ``` 53 | 54 | ## Use only one string 55 | ```c++ 56 | string num=""; 57 | void printBinary(int bit){ 58 | if(bit == n){ 59 | cout << num << endl; 60 | return; 61 | } 62 | num+="0"; 63 | printBinary(bit+1); 64 | num.pop_back(); 65 | 66 | num+="1"; 67 | printBinary(bit+1); 68 | num.pop_back(); 69 | } 70 | ``` 71 | ## The second solution is more efficient in terms of memory usage 72 | 73 | 74 | -------------------- 75 | -------------------- 76 | 77 | 78 | ## Example 2: Knapsack 79 | ```c++ 80 | There are N items, each has a weight w and a value v 81 | You have a knapsack with capacity W 82 | What is the maximum value you can put in the knapsack? 83 | for example: 84 | N = 3 85 | W = 4 86 | w = {2, 1, 3} 87 | v = {4, 2, 3} 88 | the Answer is 6 -> take first and second items 89 | ``` 90 | ## N is small N<=20! 91 | . 92 | ## brute force? 93 | . 94 | ## try all subsets! 95 | . 96 | 97 | ``` 98 | 100 ->take first item 99 | 010 ->take second item 100 | 110 ->take first and second items 101 | 001 ->take third item 102 | 101 ->take first and third items 103 | and so on 104 | ``` 105 | ## How many subsets? 106 | ```c++ 107 | 2^N 108 | ``` 109 | ## we can use First code 110 | ```c++ 111 | int n = 3, W = 4; 112 | int w[] = {2, 1, 3}; 113 | int v[] = {4, 2, 3}; 114 | int mx=0 115 | 116 | void genAllSubsets(int bit,string num){ 117 | if(bit == n){ 118 | int sumW=0,sumV=0; 119 | for(int i=0;i= w[i]) 147 | take = v[i] + slv(i+1, remWight - w[i]); 148 | return max(leave, take); 149 | } 150 | ``` 151 | -------------------- 152 | -------------------- 153 | 154 | ## Example 3: 155 | ```c++ 156 | starting from the top left corner, return the path with the max sum reaching the bottom right corner & you can go right and down 157 | 5 1 2 158 | 6 7 8 159 | 1 8 9 160 | the answer is 5+6+7+8+9 = 35 161 | ``` 162 | * base condition? 163 | * next calls? 164 | * calcaulate results? 165 | 166 | 167 | ```c++ 168 | const int n = 3, m = 3; 169 | int grid[n][m]; 170 | int maxPathSum(int r, int c) 171 | { 172 | // ^ Base condition 173 | if (r == n-1 && c == m-1) 174 | return grid[r][c]; 175 | 176 | // ^ Next calls 177 | int path1 = maxPathSum(r, c+1); // right 178 | int path2 = maxPathSum(r+1, c); // down 179 | 180 | // ^ Calculate results 181 | return grid[r][c] + max(path1, path2); 182 | } 183 | ``` 184 | 185 | ## ! we have bug here 186 | 187 | ## . 188 | ## . 189 | ## . 190 | 191 | 192 | 193 | ## Correct implementation 194 | 195 | ```c++ 196 | bool valid(int r, int c) { 197 | return r < n && r >= 0 && c < m && c >= 0; 198 | } 199 | ``` 200 | ```c++ 201 | int maxPathSum(int r, int c) 202 | { 203 | // ^ inside or outside the grid 204 | if (!valid(r, c)) 205 | return 0; 206 | 207 | if (r == n-1 && c == m-1) 208 | return grid[r][c]; // base 209 | 210 | int path1 = maxPathSum(r, c+1); // right 211 | int path2 = maxPathSum(r+1, c); // down 212 | 213 | return grid[r][c] + max(path1, path2); 214 | } 215 | ``` 216 | 217 | ## problem 1 218 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/W 219 | ## sol 220 | https://www.ideone.com/QTbE43 221 | 222 | ---------------- 223 | 224 | ## problem 2 225 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/V 226 | ## sol 227 | sol 1: https://www.ideone.com/QTbE43 228 | 229 | sol 2: https://www.ideone.com/aG2jrA 230 | 231 | ---------------- 232 | 233 | ## problem 3 234 | https://codeforces.com/group/MWSDmqGsZm/contest/223339/problem/Y 235 | ## sol 236 | https://www.ideone.com/HP92qB 237 | 238 | ---------------- 239 | 240 | ## problem 4 241 | https://codeforces.com/group/gA8A93jony/contest/270592/problem/C 242 | ## sol 243 | 244 | 245 | ---------------- 246 | ---------------- 247 | ---------------- 248 | ---------------- 249 | 250 | # Contest 251 | https://codeforces.com/group/gA8A93jony/contests 252 | -------------------------------------------------------------------------------- /STL/README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | ![image](https://user-images.githubusercontent.com/40190772/107051994-cffb9b00-67d5-11eb-821f-71a7939a99d0.png) 5 | 6 | 7 |

8 | 9 |

STL

10 |
11 | 12 | [![GitHub contributors](https://img.shields.io/github/contributors/AbdallahHemdan/ACM-Time-Complexity-STL)](https://github.com/AbdallahHemdan/ACM-Time-Complexity-STL/contributors) 13 | [![GitHub issues](https://img.shields.io/github/issues/AbdallahHemdan/ACM-Time-Complexity-STL)](https://github.com/AbdallahHemdan/ACM-Time-Complexity-STL/issues) 14 | [![GitHub forks](https://img.shields.io/github/forks/AbdallahHemdan/ACM-Time-Complexity-STL)](https://github.com/AbdallahHemdan/ACM-Time-Complexity-STL/network) 15 | [![GitHub stars](https://img.shields.io/github/stars/AbdallahHemdan/ACM-Time-Complexity-STL)](https://github.com/AbdallahHemdan/ACM-Time-Complexity-STL/stargazers) 16 | [![GitHub license](https://img.shields.io/github/license/AbdallahHemdan/ACM-Time-Complexity-STL)](https://github.com/AbdallahHemdan/ACM-Time-Complexity-STL/blob/master/LICENSE) 17 | 18 |
19 | 20 | ## About ([live](https://abdallahhemdan.github.io/ACM-Time-Complexity-STL/)) 21 | > Introduction to Time complexity and STL in c++ as a part of ACM-FECU training 22 | 23 | ## Screenshots 24 | ![image](https://user-images.githubusercontent.com/40190772/107051994-cffb9b00-67d5-11eb-821f-71a7939a99d0.png) 25 | ![image](https://user-images.githubusercontent.com/40190772/107052015-d558e580-67d5-11eb-8fd2-13d80291738a.png) 26 | ![image](https://user-images.githubusercontent.com/40190772/107052030-d8ec6c80-67d5-11eb-8ea2-a8f43d889fde.png) 27 | ![image](https://user-images.githubusercontent.com/40190772/107052046-ddb12080-67d5-11eb-96fa-a857c9e6e7c8.png) 28 | ![image](https://user-images.githubusercontent.com/40190772/107052092-ea357900-67d5-11eb-8a65-452e2bffba3d.png) 29 | ![image](https://user-images.githubusercontent.com/40190772/107052102-edc90000-67d5-11eb-9ebd-36ccb64d5ae5.png) 30 | ![image](https://user-images.githubusercontent.com/40190772/107052109-f0c3f080-67d5-11eb-823a-67e8a4be6875.png) 31 | ![image](https://user-images.githubusercontent.com/40190772/107052130-f588a480-67d5-11eb-8199-18d5d18853b2.png) 32 | ![image](https://user-images.githubusercontent.com/40190772/107052140-f91c2b80-67d5-11eb-9ea5-8f4a29f1ff0f.png) 33 | ![image](https://user-images.githubusercontent.com/40190772/107052151-fcafb280-67d5-11eb-8a67-40e0b5eea9ab.png) 34 | ![image](https://user-images.githubusercontent.com/40190772/107052178-01746680-67d6-11eb-8984-d9657b21e6be.png) 35 | 36 | 37 | -------------------------------------------------------------------------------- /STL/STL.cpp: -------------------------------------------------------------------------------- 1 | // Author : Abdallah Hemdan 2 | #include 3 | 4 | using namespace std; 5 | using ll = long long int; 6 | using ld = long double; 7 | 8 | void learn__about__vectors() { 9 | /** 10 | * ^ declare an array with fixed size n 11 | * template 12 | * | 13 | * * vector name 14 | */ 15 | 16 | int n = 4; 17 | vector a(n); 18 | 19 | for (int i = 0; i < n; i++) { 20 | cin >> a[i]; 21 | } 22 | 23 | /** 24 | * ^ fancy way of reading vector data 25 | */ 26 | for (int& i : a) { 27 | cin >> i; 28 | } 29 | 30 | /** 31 | * ^ declare an vector with dynamic size 32 | */ 33 | vector b; 34 | 35 | /** 36 | * ^ push_back() : push the elements into a vector from the back 37 | */ 38 | for (int i = 0; i < n; i++) { 39 | int x; 40 | cin >> x; 41 | 42 | b.push_back(x); 43 | } 44 | 45 | /** 46 | * ^ pop_back() : pop or remove elements from a vector from the back 47 | */ 48 | a.pop_back(); // a = [1, 2, 3] 49 | 50 | /** 51 | * ^ size() : returns the number of elements in the vector 52 | */ 53 | cout << a.size() << endl; 54 | 55 | /** 56 | * ^ empty : returns whether the container is empty 57 | */ 58 | if (a.empty() == true) { 59 | cout << "our vector is empty" << endl; 60 | } else { 61 | cout << "we have some elements 🎉🎉" << endl; 62 | } 63 | 64 | /** 65 | * ^ front() : returns the first element in the vector 66 | * 67 | * e.g. 68 | * a = [1, 2, 3, 4] 69 | */ 70 | cout << a.front() << endl; // 1 71 | 72 | /** 73 | * ^ back() : returns the last element in the vector 74 | * 75 | * e.g. 76 | * a = [1, 2, 3, 4] 77 | */ 78 | cout << a.back() << endl; // 10 79 | 80 | /** 81 | * ^ begin() : returns an iterator pointing to the first element in the vector 82 | */ 83 | auto it = a.begin(); 84 | cout << *it << endl; // access its value 85 | 86 | /** 87 | * ^ end() : returns an iterator pointing to the theoretical element that 88 | * follows the last element in the vector 89 | */ 90 | for (auto i = a.begin(); i != a.end(); ++i) { 91 | cout << *i << " "; 92 | } 93 | 94 | /** 95 | * ^ erase() : removes elements from the specified position 96 | */ 97 | a.erase(a.begin() + i); 98 | 99 | /** 100 | * ^ clear() : removes all the elements of the vector container 101 | */ 102 | a.clear(); 103 | } 104 | 105 | void learn__about__stacks() { 106 | /** 107 | * ^ declaring a new stack object 108 | * 109 | * template 110 | * | 111 | * * stack name; 112 | */ 113 | stack s__int; 114 | stack s__double; 115 | 116 | /** 117 | * ^ push(x) : adds the element ‘x’ at the top of the stack 118 | */ 119 | 120 | int x; 121 | cin >> x; 122 | s__int.push(x); 123 | 124 | /** 125 | * ^ pop() : deletes the top most element of the stack 126 | * 127 | * e.g. 128 | * s = [1, 2, 3, 4, 5, 6] 129 | */ 130 | 131 | s__int.pop(); // s = [1, 2, 3, 4, 5] 132 | 133 | /** 134 | * ^ size() : returns the number of elements in the stack 135 | */ 136 | cout << s__int.size() << endl; 137 | 138 | /** 139 | * ^ empty() : returns whether the stack is empty or not 140 | */ 141 | if (s__int.empty()) { 142 | cout << "our stack is empty" << endl; 143 | } else { 144 | cout << "we have some elements 🎉🎉" << endl; 145 | } 146 | 147 | /** 148 | * ^ top() : returns a reference to the top most element of the stack 149 | */ 150 | 151 | cout << s__int.top() << endl; // 5 152 | 153 | /** 154 | * ^ to clear the stack content (there is no clear function) 155 | */ 156 | while (!s__int.empty()) { 157 | int t = s__int.top(); 158 | s__int.pop(); 159 | } 160 | } 161 | 162 | void learn__about__queues() { 163 | /** 164 | * ^ declaring a new queue object 165 | * template 166 | * | 167 | * * queue name; 168 | */ 169 | 170 | queue q__int; 171 | queue q__double; 172 | 173 | /** 174 | * 175 | * ^ push(x) : adds the element ‘x’ at the end of the queue 176 | */ 177 | int x; 178 | cin >> x; 179 | q__int.push(x); 180 | 181 | /** 182 | * 183 | * ^ pop() : deletes the first element of the queue. 184 | * 185 | * ! before 186 | * q__int : [1, 2, 3, 4, 5, 6, 7] 187 | * | 188 | * start 189 | * 190 | * * after 191 | * q__int : [2, 3, 4, 5, 6, 7] 192 | * | 193 | * start 194 | */ 195 | q__int.pop(); 196 | 197 | /** 198 | * ^ empty() : returns whether the queue is empty 199 | */ 200 | if (q__int.empty()) { 201 | cout << "our queue is empty" << endl; 202 | } else { 203 | cout << "we have some elements 🎉🎉" << endl; 204 | } 205 | 206 | /** 207 | * ^ size() : returns the number of elements in the queue 208 | */ 209 | cout << q__int.size() << endl; 210 | 211 | /** 212 | * ^ front() : returns the first element of the queue 213 | */ 214 | cout << q__int.front() << endl; 215 | 216 | /** 217 | * ^ back() : returns the last element of the queue 218 | */ 219 | cout << q__int.back() << endl; 220 | } 221 | 222 | void learn__about__sets() { 223 | /** 224 | * ^ declaring a new set object 225 | * 226 | * template 227 | * | 228 | * * set name; 229 | */ 230 | set st__int; 231 | set st_double; 232 | set> st__int__greater; 233 | 234 | /** 235 | * ^ insert(x) : adds a new element ‘x’ to the set 236 | */ 237 | int x; 238 | cin >> x; 239 | st__int.insert(x); 240 | 241 | /** 242 | * ^ erase(x) : Removes the value ‘x’ from the set 243 | */ 244 | st__int.erase(15); 245 | 246 | /** 247 | * ^ erase(x) : Removes the value ‘x’ from the set 248 | */ 249 | st__int.erase(st__int.begin()); 250 | 251 | /** 252 | * ^ empty() : returns whether the set is empty 253 | */ 254 | if (st__int.empty()) { 255 | cout << "our set is empty" << endl; 256 | } else { 257 | cout << "we have some elements 🎉🎉" << endl; 258 | } 259 | 260 | /** 261 | * ^ size() : returns the number of elements in the set 262 | */ 263 | cout << st__int.size() << endl; 264 | 265 | /** 266 | * ^ begin() : returns an iterator pointing to the first element in the set 267 | */ 268 | auto it = st__int.begin(); 269 | cout << *it << endl; // access its value 270 | 271 | /** 272 | * ^ end() : returns an iterator pointing to the theoretical element that 273 | * ^ follows the last element in the set 274 | * 275 | */ 276 | for (auto i = st__int.begin(); i != st__int.end(); ++i) { 277 | cout << *i << " "; 278 | } 279 | 280 | /** 281 | * ^ easy way to iterator over set 282 | */ 283 | 284 | for (auto it : st__int) { 285 | cout << it << endl; 286 | } 287 | } 288 | 289 | void learn__about__maps() { 290 | /** 291 | * ^ declaring a new map object 292 | * 293 | * template template 294 | * | | 295 | * * map name; 296 | */ 297 | map mp__int__int; 298 | map mp__int__double; 299 | map> mp__int__vector; 300 | map> mp__int__vector; 301 | map mp__string__int; 302 | 303 | /** 304 | * ^ insert(make_pair(key, value)) : adds a new element pair to 305 | * ^ the map 306 | */ 307 | int k, v; 308 | cin >> k >> v; 309 | mp__int__int.insert(make_pair(k, v)); 310 | mp__int__int.insert({k, v}); 311 | 312 | /** 313 | * ? alternative way to add key-value pair 314 | */ 315 | mp__int__int[k] = v; 316 | mp__string__int["Hemdan"] = 22; 317 | 318 | /** 319 | * ^ size() : returns the number of elements in the map 320 | */ 321 | cout << mp__int__int.size() << endl; 322 | 323 | /** 324 | * ^ empty() : returns whether the map is empty 325 | */ 326 | if (mp__int__int.empty()) { 327 | cout << "our map is empty" << endl; 328 | } else { 329 | cout << "we have some elements 🎉🎉" << endl; 330 | } 331 | 332 | /** 333 | * ^ begin() : returns an iterator pointing to the first element in the map 334 | */ 335 | 336 | auto it = mp__int__int.begin(); 337 | 338 | cout << it->first << endl; // access its key 339 | cout << it->second << endl; // access its value 340 | 341 | /** 342 | * ^ end() : returns an iterator pointing to the theoretical element that 343 | * ^ follows the last element in the map 344 | * 345 | */ 346 | for (auto it = mp__int__int.begin(); it != mp__int__int.end(); ++it) { 347 | cout << it->first << endl; // access its key 348 | cout << it->second << endl; // access its key 349 | } 350 | 351 | for (auto it : mp__int__int) { 352 | cout << it.first << endl; 353 | cout << it.second << endl; 354 | } 355 | 356 | /** 357 | * ^ clear() : removes all the elements of the map 358 | */ 359 | mp__int__int.clear(); 360 | } 361 | 362 | void learn__about__stl__algorithms() { 363 | /** 364 | * ^ sort(start it, end it) : sort your array / vector 365 | */ 366 | const int N = 100000; 367 | int a[N]; 368 | 369 | sort(a, a + N); 370 | 371 | int n; 372 | vector b(n); 373 | 374 | sort(b.begin(), b.end()); 375 | sort(b.rbegin(), b.rend()); 376 | 377 | /** 378 | * ^ reverse(start it, end it) : reverse your array / vector 379 | */ 380 | reverse(a, a + N); // Array 381 | reverse(b.begin(), b.end()); // Vector 382 | } 383 | 384 | void solve__problem__1() { 385 | // https://codeforces.com/group/zvsr84He8w/contest/315331/problem/A 386 | 387 | int n; 388 | cin >> n; 389 | 390 | vector arr(n); 391 | for (int i =0 ; i< n; i++) { 392 | cin >> arr[i]; 393 | } 394 | 395 | sort(arr.begin(), arr.end()); 396 | 397 | for (auto i : arr) { 398 | cout << i << ' '; 399 | } 400 | } 401 | 402 | void solve__problem__2() { 403 | // Link: https://codeforces.com/group/k43ChfKeWS/contest/353135/problem/C 404 | int n, k; 405 | cin >> n >> k; 406 | 407 | map freq; 408 | for (int i = 0, x; i < n; i++) { 409 | cin >> x; 410 | freq[x] = i + 1; 411 | } 412 | 413 | if (freq.size() >= k) { 414 | cout << "YES" << endl; 415 | 416 | auto it = freq.begin(); 417 | for (int i = 0; i < k; i++) 418 | cout << (it++->second) << ' '; 419 | } else { 420 | cout << "NO"; 421 | } 422 | } 423 | 424 | void solve__problem__3() { 425 | // https://codeforces.com/group/zvsr84He8w/contest/315331/problem/B 426 | int n; 427 | cin >> n; 428 | 429 | string s; 430 | map freq; 431 | for (int i = 0; i < n; i++) { 432 | cin >> s; 433 | if (freq[s] == 0) {// freq.count(s) 434 | cout << "OK" << endl; 435 | freq[s] = 1; 436 | } else { 437 | cout << s << freq[s]++ << endl; 438 | } 439 | } 440 | } 441 | 442 | int main() { 443 | #ifndef ONLINE_JUDGE 444 | freopen("In.txt", "r", stdin); 445 | freopen("Out.txt", "w", stdout); 446 | #else 447 | // freopen("input.in","r",stdin); 448 | // freopen("output.out","w",stdout); 449 | #endif 450 | learn__about__vectors(); 451 | learn__about__queues(); 452 | learn__about__sets(); 453 | learn__about__maps(); 454 | learn__about__stacks(); 455 | learn__about__priority__queue(); 456 | learn__about__stl__algorithms(); 457 | solve__problem__one(); 458 | solve__problem__two(); 459 | solve__problem__three(); 460 | } 461 | -------------------------------------------------------------------------------- /STL/STL_intro.md: -------------------------------------------------------------------------------- 1 | # STL Introduction 2 | 3 | 1. We were having a **problem** so we got data structures 4 | 2. **STL** is just an **implementation** for theses data structures 5 | 3. All of the data structures: 6 | - Have almost the same **interface** (functions) 7 | - But different in the concept of usage 8 | 9 | 10 | ## Vector 11 | 12 | 13 | 14 | ## Stack 15 | 16 | 17 | 18 | ## Queue 19 | 20 | 21 | 22 | ## Map 23 | 24 | -------------------------------------------------------------------------------- /STL/assets/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/STL/assets/map.png -------------------------------------------------------------------------------- /STL/assets/queue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/STL/assets/queue.png -------------------------------------------------------------------------------- /STL/assets/stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/STL/assets/stack.png -------------------------------------------------------------------------------- /STL/assets/vector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ACM-FE-CU-Community/Sessions-Codes/1a8747851459cc8ab60de3b54056d74938b305a7/STL/assets/vector.png -------------------------------------------------------------------------------- /Time and Space Complexity/Space complexity.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Memory Analysis 3 | // Check if any number is repeated: arr[i] < N 4 | 5 | // Memory O(1) & Time O(N^2) 6 | bool Ex8(vector& arr) 7 | { 8 | int N = arr.size(); 9 | for (int i = 0; i < N; ++i) { 10 | for (int j = i + 1; j < N; ++j) { 11 | if (arr[i] == arr[j]) { 12 | return 1; 13 | } 14 | } 15 | } 16 | return 0; 17 | } 18 | 19 | // Another Solution: Shows Memory & Time compromising 20 | // Memory O(N) & Time O(N) 21 | bool Ex8(vector& arr) 22 | { 23 | int N = arr.size(); 24 | 25 | vector exist(N, 0); // Allocate N location 26 | 27 | for (int i = 0; i < N; ++i) { 28 | if (exist[arr[i]] == 1) { 29 | return 1; 30 | } 31 | exist[arr[i]] = 1; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Time and Space Complexity/Time complexity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /** 5 | * 6 | * * Problem 7 | * [1] Our main problem is to measure the algorithm performance => Space + Time. 8 | * [2] We need to compare these algorithms to get the best algorithm. 9 | * 10 | */ 11 | 12 | /** 13 | * * Solution 14 | * [1] Complexity analysis using Exact time. 15 | * • It depends on the machine performance. 16 | * • IPC (Instructions per cycle) is machine dependent. 17 | * • CPU frequency varies even with same IPC. 18 | * 19 | * 20 | * [2] Complexity analysis using no. of steps and no. of operations. 21 | * • It is not practical in calculations. 22 | * 23 | * 24 | * [3] Complexity analysis using Worst, Average, Best case. 25 | * • We are interested in the worst case scenario 26 | * • e.g. Search for a number in an array of size n. 27 | * • 5 9 3 4 2 98 7 81 62 10 28 | * 29 | * 30 | * • Best case => 5 | 9 31 | * • Average case => 98 | 7 | 2 32 | * • Worst case => 62 | 10 33 | **/ 34 | 35 | 36 | /** 37 | * * Note 38 | * 1s takes 10^8 operations 39 | **/ 40 | 41 | 42 | // * General Rule 43 | // Worst Case = The highest degree if we have a polynomial equation. 44 | // Time = 3 * N^2 + 5 * N + 10 -> O(N^2) 45 | 46 | 47 | // Constant Order 48 | // O(1) 49 | int checkSign(int a) 50 | { 51 | if (a > 0) { 52 | return 1; 53 | } 54 | if (a < 0) { 55 | return -1; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | // Logarithmic Order 62 | /* 63 | Ex.: 64 | 16 -> 8 -> 4 -> 2 -> 1 Okay 65 | 20 -> 10 -> 5 (Ops..!) 66 | x <= 2^(20) 67 | O(log n) 68 | */ 69 | bool checkIfPowerOfTwo(int n) 70 | { 71 | while (n > 1) { 72 | if (n % 2 != 0) { 73 | return 0; 74 | } 75 | 76 | n /= 2; // Here is the magic 77 | } 78 | return 1; 79 | } 80 | 81 | // Linear Order 82 | // O(n) 83 | bool checkIfNumberExists(int arr[], int N, int num) 84 | { 85 | for (int i = 0; i < N; ++i) { 86 | if (arr[i] == num) { 87 | return 1; 88 | } 89 | } 90 | 91 | return 0; 92 | } 93 | 94 | // Quadratic order 95 | /* 96 | For the equation: y = mx + c 97 | Check if (m,c) exist in the array for such (x,y) 98 | */ 99 | bool checkLineEquation(vector arr, int x, int y) 100 | { 101 | int N = arr.size(); 102 | for (int i = 0; i < N; ++i) { 103 | for (int j = 0; j < N; ++j) { 104 | if (arr[i] * x + arr[j] == y) { 105 | return 1; 106 | } 107 | } 108 | } 109 | return 0; 110 | } 111 | 112 | // Cubic order 113 | /* 114 | Check if there are three numbers which their sum is d (i.e. a + b + c = d) 115 | */ 116 | bool checkThreeNumbersEquation(vector arr, int d) 117 | { 118 | int N = arr.size(); 119 | for (int i = 0; i < N; ++i) { 120 | for (int j = 0; j < N; ++j) { 121 | for (int k = 0; k < N; ++k) { 122 | if (arr[i] + arr[j] + arr[k] == d) { 123 | return 1; 124 | } 125 | } 126 | } 127 | } 128 | return 0; 129 | } 130 | 131 | // Factorial Order 132 | bool getNumberofPermutations(vector arr, int lower, int upper) 133 | { 134 | int counter = 0; 135 | do { 136 | counter++; 137 | } while (next_permutation(arr.begin(), arr.end())); 138 | 139 | return counter; 140 | } 141 | 142 | // Exponential Order 143 | /* 144 | Generates all binary sequences of size N (Recursively) 145 | Ex.: N = 2 => {00,01,10,11} 146 | */ 147 | void generateBinary(string s, int N) 148 | { 149 | if (s.size() == N) { 150 | cout << s << endl; 151 | return; 152 | } 153 | 154 | generateBinary(s + '0', N); 155 | generateBinary(s + '1', N); 156 | } 157 | 158 | // Examples 159 | // Arithmetic Sequence 160 | // 1 + 2 + 3 + ... + N = N*(N+1)/2 = N^2 / 2 + N / 2 161 | // O(N^2) 162 | void Ex1(int N) 163 | { 164 | for (int i = 1; i <= N; ++i) { 165 | // N 166 | for (int j = 1; j <= i; ++j) { 167 | // Do something 168 | } 169 | } 170 | } 171 | 172 | // Geometic Sequence 173 | /* 174 | 1 + x + x^2 + x^3 + .... + x^(N-1) 175 | = (x^N - 1) / (x - 1) 176 | O(x^N) 177 | */ 178 | void Ex2(int N, int x) 179 | { 180 | for (int i = 0; i < N; ++i) { 181 | int M = pow(x, i); 182 | for (int j = 1; j <= M; ++j) { 183 | // Do something 184 | } 185 | } 186 | } 187 | 188 | // Fractional Geometic Sequence 189 | // O(N) 190 | void Ex3(int N) 191 | { 192 | // N = 16 193 | for (int i = 1; i <= N; i *= 2) { 194 | // i = 1, 2, 4, 8, ... 16 195 | for (int j = 1; j <= N; j += i) { 196 | // Do something 197 | } 198 | /* 199 | N + N/2 + N/4 + N/8 + N/16 200 | = N * (1/1 + 1/2 + 1/4 + 1/8 + 1/16) 201 | 202 | (1 + 1/2 + 1/4 + 1/8 + 1/16)(decimal) -> 1.11111111 (binary) <= 2 203 | so Time = 2*N -> O(N) 204 | */ 205 | } 206 | } 207 | 208 | // Harmonic Sequence 209 | void Ex4(int N) 210 | { 211 | for (int i = 1; i < N; ++i) { 212 | // i = 1, 2, 3, ..., N 213 | for (int j = 1; j <= N; j += i) { 214 | // Do something 215 | } 216 | /* 217 | N + N/2 + N/3 + .... 218 | N * (1/1 + 1/2 + 1/3 + ... ) = O(N log(N)) [Best estimate so far] 219 | */ 220 | } 221 | } 222 | 223 | // O(N) 224 | void Ex5(int N) 225 | { 226 | int i = 0; 227 | while (i < N) { 228 | int j = i; 229 | while (j < N) { 230 | // Do something 231 | ++j; 232 | ++i; 233 | } 234 | ++i; 235 | } 236 | } 237 | 238 | // infinity!! -> i=0 239 | void Ex6(int N) 240 | { 241 | int i = 0; 242 | while (i < N) { 243 | // Do something 244 | i *= 2; 245 | } 246 | } 247 | 248 | RecEx7(x, N, 0); 249 | // O(x^N) 250 | void RecEx7(int x, int N, int i) 251 | { 252 | if (i == N) { 253 | return; 254 | } 255 | // Do something 256 | 257 | for (int j = 0; j < x; ++j) 258 | RecEx7(x, N, i + 1); 259 | } 260 | 261 | // O(N^2) 262 | void Ex(vector s, int N) 263 | { 264 | vector v; 265 | for (int i = 0; i < N; ++i) { 266 | for (int j = 0; j < N; ++j) { 267 | // Do something 268 | } 269 | } 270 | } 271 | 272 | // O((N^2) * (N^2)) -> O(N^4) 273 | void Ex10(int N) 274 | { 275 | vector a; 276 | // Fill a 277 | for (int i = 0; i < N; ++i) { 278 | for (int j = 0; j < N; ++j) { 279 | // O(N^2) 280 | Ex(a, N); 281 | // A line of code doesn't mean O(1) 282 | } 283 | } 284 | } 285 | --------------------------------------------------------------------------------