├── .gitignore ├── README.md ├── algorithms ├── bitwise.cpp ├── graphs │ └── dfs.cpp ├── kadanes.cpp ├── math │ └── prime-factors.cpp ├── sieve-of-eratosthenes.cpp ├── sorts │ └── insertion.cpp └── subsets.cpp ├── atcoder └── abc │ └── a │ ├── ABC001.cpp │ ├── ABC002.cpp │ ├── ABC003.cpp │ ├── ABC004.cpp │ ├── ABC005.cpp │ ├── ABC006.cpp │ ├── ABC007.cpp │ ├── ABC008.cpp │ ├── ABC009.cpp │ ├── ABC010.cpp │ ├── ABC011.cpp │ ├── ABC012.cpp │ ├── ABC013.cpp │ ├── ABC014.cpp │ ├── ABC015.cpp │ ├── ABC016.cpp │ ├── ABC017.cpp │ ├── ABC018.cpp │ ├── ABC019.cpp │ ├── ABC020.cpp │ ├── ABC021.cpp │ ├── ABC023.cpp │ ├── ABC160.cpp │ ├── ABC161.cpp │ ├── ABC162.cpp │ ├── ABC163.cpp │ ├── ABC164.cpp │ ├── ABC165.cpp │ ├── ABC167.cpp │ ├── ABC168.cpp │ ├── ABC169.cpp │ ├── ABC170.cpp │ ├── ABC171.cpp │ └── ABC172.cpp ├── codechef ├── CHN15A.cpp ├── CNOTE.cpp ├── FRGTNLNG.cpp ├── LEARNDSA │ ├── LRNDSA01 │ │ ├── FLOW007.cpp │ │ ├── LAPIN.cpp │ │ ├── TEST.cpp │ │ └── ZCO14003.cpp │ └── LRNDSA02 │ │ └── STFOOD.cpp ├── LECANDY.cpp ├── PCSUB.cpp ├── RAINBOWA.cpp └── SALARY.cpp ├── codeforces ├── A │ ├── P1015A.cpp │ ├── P1030A.cpp │ ├── P110A.cpp │ ├── P112A.cpp │ ├── P116A.cpp │ ├── P122A.cpp │ ├── P129A.cpp │ ├── P1325A.cpp │ ├── P1328A.cpp │ ├── P1335A.cpp │ ├── P133A.cpp │ ├── P136A.cpp │ ├── P1370A.cpp │ ├── P1397A.cpp │ ├── P144A.cpp │ ├── P155A.cpp │ ├── P158A.cpp │ ├── P200B.cpp │ ├── P208A.cpp │ ├── P228A.cpp │ ├── P230A.cpp │ ├── P231A.cpp │ ├── P233A.cpp │ ├── P258A.cpp │ ├── P263A.cpp │ ├── P265A.cpp │ ├── P266A.cpp │ ├── P266B.cpp │ ├── P268A.cpp │ ├── P271A.cpp │ ├── P275A.cpp │ ├── P282A.cpp │ ├── P313A.cpp │ ├── P318A.cpp │ ├── P337A.cpp │ ├── P339A.cpp │ ├── P344A.cpp │ ├── P381A.cpp │ ├── P405A.cpp │ ├── P427A.cpp │ ├── P431A.cpp │ ├── P432A.cpp │ ├── P451A.cpp │ ├── P459A.cpp │ ├── P466A.cpp │ ├── P467A.cpp │ ├── P469A.cpp │ ├── P472A.cpp │ ├── P476A.cpp │ ├── P479A.cpp │ ├── P486A.cpp │ ├── P490A.cpp │ ├── P509A.cpp │ ├── P50A.cpp │ ├── P520A.cpp │ ├── P546A.cpp │ ├── P58A.cpp │ ├── P59A.cpp │ ├── P617A.cpp │ ├── P61A.cpp │ ├── P621A.cpp │ ├── P677A.cpp │ ├── P686A.cpp │ ├── P702A.cpp │ ├── P705A.cpp │ ├── P709A.cpp │ ├── P732A.cpp │ ├── P734A.cpp │ ├── P749A.cpp │ ├── P770A.cpp │ ├── P787A.cpp │ ├── P791A.cpp │ ├── P807A.cpp │ ├── P80A.cpp │ ├── P977A.cpp │ ├── P988A.cpp │ ├── P996A.cpp │ └── P9A.cpp ├── B │ └── P1353B.cpp └── practice │ └── binary-search │ ├── A1.cpp │ ├── B1.cpp │ └── C1.cpp ├── cses ├── alon20 │ ├── P1165.cpp │ └── P1166.cpp ├── dt │ ├── P312.cpp │ ├── P313.cpp │ ├── P315.cpp │ ├── P317.cpp │ └── P318.cpp └── problemset │ ├── P1068.cpp │ ├── P1069.cpp │ ├── P1070.cpp │ └── P1083.cpp ├── datastructures ├── binary-tree.cpp ├── graphs.cpp ├── list.cpp ├── queues.cpp ├── segment-tree.cpp ├── set.cpp ├── singly-linked-list.cpp ├── stack-linked-list.cpp ├── strings.cpp └── vectors.cpp ├── hackerrank └── algorithms │ ├── apple-and-orange.cpp │ ├── breaking-best-and-worst-records.cpp │ ├── grading.cpp │ ├── kangaroo.cpp │ ├── mini-max-sum.cpp │ └── time-conversion.cpp ├── kickstart ├── 2016 │ └── A │ │ └── A.cpp └── 2020 │ └── A │ └── A.cpp ├── leetcode ├── P1.cpp ├── P1019.cpp ├── P1207.cpp ├── P1252.cpp ├── P136.cpp ├── P1403.cpp ├── P144.cpp ├── P145.cpp ├── P1450.cpp ├── P1460.cpp ├── P1475.cpp ├── P1512.cpp ├── P167.cpp ├── P189.cpp ├── P203.cpp ├── P206.cpp ├── P217.cpp ├── P234.cpp ├── P287.cpp ├── P496.cpp ├── P520.cpp ├── P704.cpp ├── P705.cpp ├── P706.cpp ├── P75.cpp ├── P78.cpp ├── P804.cpp ├── P832.cpp ├── P90.cpp ├── P938.cpp ├── P94.cpp └── P961.cpp ├── onlinejudge ├── P36.cpp └── mathematics │ ├── adhoc │ ├── P1012.cpp │ └── P996.cpp │ └── prime-numbers │ └── P543.cpp ├── problems ├── arrays │ ├── check-subarray-with-0-sum-exists-not.cpp │ └── find-pair-with-given-sum-array.cpp ├── backtracking │ └── maze.cpp ├── bitwise │ ├── detect-if-two-integers-have-opposite-signs.cpp │ └── find-the-element-that-appears-once.cpp ├── dynamic-programming │ └── knapsack.cpp ├── graphs │ └── so-np.cpp └── recursion │ ├── factorial.cpp │ └── prefix-sum.cpp ├── spoj ├── ADAGCD.cpp ├── TDPRIMES.cpp └── simple-math │ └── ADDREV.cpp └── template.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | cmake-build-debug/* 3 | CMakeLists.txt 4 | *.txt 5 | main.cpp 6 | template.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Competitive Programing 2 | Check the [Trello Board](https://trello.com/b/TWSfC3uS) for a better understanding of topics and repo for code reference. 3 | 4 | ![Competitive Programing Trello Board](https://img.techpowerup.org/200713/rizimore-competitve-programming.png) 5 | 6 | 1. Data Structures 7 | 2. Algorithms 8 | 3. Mathematics 9 | 4. Dynamic Programming 10 | 5. Miscellaneous -------------------------------------------------------------------------------- /algorithms/bitwise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /** 5 | * Why Bitwise? 6 | * 7 | * 1) It's very fast, faster then any operator 8 | * 2) % operator O(n cube) but in bitwise we can do in O(1d) 9 | */ 10 | int main() { 11 | int a, b; 12 | 13 | // AND (&) Operator 14 | // OR Operator 15 | // XOR Operator (^) 16 | 17 | // Left Shift (<<) 18 | // a * 2 power n 19 | cout << (2 << 1) << endl; // 4 20 | 21 | // Right Shift (>>) 22 | // a / 2 power n 23 | cout << (5 >> 1) << endl; // 2 24 | 25 | // Not/Complement Operator (~) 26 | cout << (~4) << endl; 27 | 28 | // even/odd number 29 | a = 1; 30 | if (a & 1) 31 | cout << "Odd" << endl; 32 | else 33 | cout << "Even" << endl; 34 | 35 | // min number 36 | a = 4, b = 3; 37 | if (a ^ b) 38 | cout << a << endl; 39 | else 40 | cout << b << endl; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /algorithms/graphs/dfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define SIZE 100 7 | 8 | vector v[SIZE]; 9 | vector visited(SIZE, false); 10 | 11 | void dfs(int i) { 12 | if (visited[i]) 13 | return; 14 | 15 | visited[i] = true; 16 | cout << i << " "; 17 | for (int j=0; j> n; 26 | for (int i=0; i> x >> y; 29 | v[x].push_back(y); 30 | v[y].push_back(x); 31 | } 32 | dfs(1); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /algorithms/kadanes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRRazvi/competitive-programming/28da03e598df3f4c37bdbd06fc2ba256daf1cb69/algorithms/kadanes.cpp -------------------------------------------------------------------------------- /algorithms/math/prime-factors.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRRazvi/competitive-programming/28da03e598df3f4c37bdbd06fc2ba256daf1cb69/algorithms/math/prime-factors.cpp -------------------------------------------------------------------------------- /algorithms/sieve-of-eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPrime(int num){ 6 | bool flag = true; 7 | for(int i=2; i<=num/2; i++) { 8 | if(num%i == 0) { 9 | flag = false; 10 | break; 11 | } 12 | } 13 | return flag; 14 | } 15 | 16 | int main() { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | cout.tie(NULL); 20 | int n = 100000000; 21 | vector v(n+1, true); 22 | v[0] = v[1] = false; 23 | for (int i=2; (long long)i*i 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector insertion(vector& v) { 7 | int i, j, key; 8 | for (i=1; i= 0 && v[j] > key) { // < for decreasing order 12 | v[j+1] = v[j]; 13 | j--; 14 | } 15 | v[j+1] = key; 16 | } 17 | return v; 18 | } 19 | 20 | void display(const vector& v) { 21 | for (auto num: v) 22 | cout << num << " "; 23 | cout << endl; 24 | } 25 | 26 | int main() { 27 | vector tc1 = {5, 2, 4, 6, 1, 3}; 28 | insertion(tc1); 29 | display(tc1); 30 | } -------------------------------------------------------------------------------- /algorithms/subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // generating subsets 7 | // [1,2,3] 8 | // [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] 9 | vector> subsets(vector& nums) { 10 | vector> subsets; 11 | int n = nums.size(); 12 | for (int i=0; i<(1< sub; 14 | for (int j=0; j> subsetsWithDup(vector& nums) { 27 | vector> subsets; 28 | int n = nums.size(); 29 | for (int i=0; i<(1< sub; 31 | for (int j=0; j 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b; 6 | cin >> a >> b; 7 | cout << a - b << endl; 8 | } 9 | -------------------------------------------------------------------------------- /atcoder/abc/a/ABC002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | cout << max(a, b) << endl; 9 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double n; 6 | cin >> n; 7 | double result = 0; 8 | for (int i=1; i<=n; i++) { 9 | result += (10000 * i) / n; 10 | } 11 | cout << result << endl; 12 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | cout << n * 2 << endl; 8 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int x, y; 6 | cin >> x >> y; 7 | cout << y / x << endl; 8 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | if (n%3==0) 8 | cout << "YES"; 9 | else 10 | cout << "NO"; 11 | cout << endl; 12 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string n; 6 | cin >> n; 7 | int result; 8 | if (n == "One") result = 0; 9 | else if (n == "Two") result = 1; 10 | else if (n == "Three") result = 2; 11 | else if (n == "Four") result = 3; 12 | else if (n == "Five") result = 4; 13 | else if (n == "Six") result = 5; 14 | else if (n == "Seven") result = 6; 15 | else if (n == "Eight") result = 7; 16 | else if (n == "Nine") result = 8; 17 | else if (n == "Ten") result = 19; 18 | else result = stoi(n) - 1; 19 | cout << result << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int s, t; 6 | cin >> s >> t; 7 | int count = 0; 8 | for (int i=s; i<=t; i++) { 9 | if (i<=t) 10 | count++; 11 | } 12 | if (count==0) 13 | cout << 1; 14 | else 15 | cout << count; 16 | cout << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | int count = 0; 12 | 13 | if (n <= 2) { 14 | cout << 1 << endl; 15 | return 0; 16 | } 17 | 18 | while (n > 0) { 19 | n -= 2; 20 | count++; 21 | } 22 | cout << count << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | string s; 10 | cin >> s; 11 | cout << s << "pp" << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | if (n == 12) { 12 | cout << 1 << endl; 13 | return 0; 14 | } 15 | cout << ++n << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int a, b; 10 | cin >> a >> b; 11 | cout << b << " " << a << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | char x; 10 | cin >> x; 11 | int result = 0; 12 | if (x=='A') result = 1; 13 | else if (x=='B') result = 2; 14 | else if (x=='C') result = 3; 15 | else if (x=='D') result = 4; 16 | else if (x=='E') result = 5; 17 | cout << result << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int a, b; 10 | cin >> a >> b; 11 | if (a <= b) { 12 | cout << b - a << endl; 13 | return 0; 14 | } else { 15 | int count = 0; 16 | while (a%b!=0) { 17 | count++; 18 | a++; 19 | } 20 | cout << count << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | string a, b; 11 | cin >> a >> b; 12 | if (a.length() > b.length()) 13 | cout << a; 14 | else 15 | cout << b; 16 | cout << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC016.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int m, d; 10 | cin >> m >> d; 11 | if (m%d==0) 12 | cout << "YES"; 13 | else 14 | cout << "NO"; 15 | cout << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC017.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | int s1, s2, s3; 11 | int e1, e2, e3; 12 | cin >> s1 >> e1; 13 | cin >> s2 >> e2; 14 | cin >> s3 >> e3; 15 | cout << (s1 * (e1 * 0.1)) + (s2 * (e2 * 0.1)) + (s3 * (e3 * 0.1)) << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | vector v(3); 11 | for (int i=0; i<3; i++) 12 | cin >> v[i]; 13 | vector temp(v); 14 | sort(temp.rbegin(), temp.rend()); 15 | for (int i=0; i<3; i++) { 16 | auto it = find(temp.begin(), temp.end(), v[i]); 17 | cout << distance(temp.begin(), it) + 1 << endl; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | if (n==1) 12 | cout << "ABC"; 13 | else 14 | cout << "chokudai"; 15 | cout << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC020.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | if (n==1) 12 | cout << "ABC"; 13 | else 14 | cout << "chokudai"; 15 | cout << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC021.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | int n; 11 | cin >> n; 12 | if (n <= 1) { 13 | cout << 1 << endl << 1 << endl; 14 | return 0; 15 | } else { 16 | vector v; 17 | while (n > 1) { 18 | n -= 2; 19 | v.push_back(2); 20 | } 21 | v.push_back(1); 22 | cout << v.size() << endl; 23 | for (int i=v.size() - 1; i>=0; i--) 24 | cout << v[i] << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC023.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | int n; 11 | cin >> n; 12 | int remainder = 0, result = 0; 13 | while (n>0) { 14 | remainder = n%10; 15 | result += remainder; 16 | n /= 10; 17 | } 18 | cout << result << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC160.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | string s; 8 | cin >> s; 9 | if (s[2] == s[3] && s[4] == s[5]) 10 | cout << "Yes"; 11 | else 12 | cout << "No"; 13 | cout << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC161.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int a, b, c; 8 | cin >> a >> b >> c; 9 | swap(a, b); 10 | swap(a, c); 11 | printf("%d %d %d\n", a, b, c); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC162.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | string n; 8 | cin >> n; 9 | if (n.find('7') != string::npos) 10 | cout << "Yes"; 11 | else 12 | cout << "No"; 13 | cout << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC163.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int r; 10 | cin >> r; 11 | cout << setprecision(20) << 2*M_PI*r << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC164.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int s, w; 8 | cin >> s >> w; 9 | if (w >= s) 10 | cout << "unsafe"; 11 | else 12 | cout << "safe"; 13 | cout << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC165.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int k, a, b; 8 | cin >> k >> a >> b; 9 | int count = 1, mul; 10 | while (true) { 11 | mul = k*count; 12 | if (mul >= a && mul <= b) { 13 | cout << "OK" << endl; 14 | break; 15 | } else if (mul>b) { 16 | cout << "NG" << endl; 17 | break; 18 | } 19 | count++; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC167.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | string s, t; 8 | cin >> s >> t; 9 | if (s.length() + 1 != t.length()) { 10 | cout << "No" << endl; 11 | return 0; 12 | } 13 | t.pop_back(); 14 | if (s == t) 15 | cout << "Yes" << endl; 16 | else 17 | cout << "No" << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC168.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | switch (n%10) { 10 | case 2: 11 | case 4: 12 | case 5: 13 | case 7: 14 | case 9: 15 | cout << "hon" << endl; 16 | return 0; 17 | case 0: 18 | case 1: 19 | case 6: 20 | case 8: 21 | cout << "pon" << endl; 22 | return 0; 23 | case 3: 24 | cout << "bon" << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC169.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int a, b; 8 | cin >> a >> b; 9 | cout << a*b << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC170.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | set s = {1, 2, 3, 4, 5, 0}; 9 | for (int i=0; i<5; i++) { 10 | int num; 11 | cin >> num; 12 | s.erase(num); 13 | } 14 | cout << *s.begin() << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC171.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | char a; 8 | cin >> a; 9 | if (isupper(a)) 10 | cout << "A"; 11 | else 12 | cout << "a"; 13 | cout << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /atcoder/abc/a/ABC172.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int a; 9 | cin >> a; 10 | cout << pow(a, 1) + pow(a, 2) + pow(a, 3) << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /codechef/CHN15A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main () { 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, k; 11 | cin >> n >> k; 12 | vector v(n); 13 | int input; 14 | for (int i=0; i> input; 16 | v[i] = input + k; 17 | } 18 | 19 | int count = 0; 20 | for (auto num: v) { 21 | if (num%7 == 0) 22 | count++; 23 | } 24 | cout << count << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /codechef/CNOTE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int x, y, k, n; 11 | cin >> x >> y >> k >> n; 12 | int arr[n][2]; 13 | for (int i=0; i> arr[i][0]; 15 | cin >> arr[i][1]; 16 | } 17 | 18 | // solve 19 | int flag = 0; 20 | for (int i=0; i= x-y && arr[i][1] <= k) 22 | flag = 1; 23 | } 24 | 25 | if (flag) 26 | cout << "LuckyChef"; 27 | else 28 | cout << "UnluckyChef"; 29 | cout << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /codechef/FRGTNLNG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n, k; 12 | cin >> n >> k; 13 | vector forgotten(n); 14 | for (auto &input: forgotten) 15 | cin >> input; 16 | set modern; 17 | for (int i=0; i> l; 20 | for (int j=0; j> s; 23 | modern.insert(s); 24 | } 25 | } 26 | 27 | for (const auto& word: forgotten) { 28 | if (modern.count(word)) 29 | cout << "YES "; 30 | else 31 | cout << "NO "; 32 | } 33 | cout << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /codechef/LEARNDSA/LRNDSA01/FLOW007.cpp: -------------------------------------------------------------------------------- 1 | // Reverse The Number 2 | // https://www.codechef.com/LRNDSA01/problems/FLOW007 3 | 4 | #include 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | string s; 17 | cin >> s; 18 | 19 | reverse(s.begin(), s.end()); 20 | bool flag = false; 21 | for (int i=0; i 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | string s; 17 | cin >> s; 18 | 19 | int mid = s.size()/2; 20 | string s1 = s.substr(0, mid); 21 | if (s.size()%2 != 0) 22 | mid += 1; 23 | string s2 = s.substr(mid, s.size()); 24 | 25 | sort(s1.begin(), s1.end()); 26 | sort(s2.begin(), s2.end()); 27 | 28 | if (s1 == s2) 29 | cout << "YES"; 30 | else 31 | cout << "NO"; 32 | cout << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /codechef/LEARNDSA/LRNDSA01/TEST.cpp: -------------------------------------------------------------------------------- 1 | // Life, the Universe, and Everything 2 | // https://www.codechef.com/LRNDSA01/problems/TEST 3 | 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | freopen("output.txt", "w", stdout); 11 | 12 | int input; 13 | while (cin >> input) { 14 | if (input == 42) 15 | break; 16 | cout << input << endl; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /codechef/LEARNDSA/LRNDSA01/ZCO14003.cpp: -------------------------------------------------------------------------------- 1 | // Smart Phone (ZCO14003) 2 | // Zonal Computing Olympiad 2014, 30 Nov 2013 3 | // https://www.codechef.com/LRNDSA01/problems/ZCO14003 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | ll n; 16 | cin >> n; 17 | vector v(n); 18 | for (auto& i: v) 19 | cin >> i; 20 | 21 | vector profit; 22 | for (ll i=0; i= v[i]) 26 | max += v[i]; 27 | } 28 | profit.push_back(max); 29 | } 30 | 31 | cout << *max_element(profit.begin(), profit.end()) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /codechef/LEARNDSA/LRNDSA02/STFOOD.cpp: -------------------------------------------------------------------------------- 1 | // Chef and Street Food 2 | // https://www.codechef.com/LRNDSA02/problems/STFOOD 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | vector profit; 20 | for (int i=0; i> s >> p >> v; 23 | s++; 24 | profit.push_back((p/s) * v); 25 | } 26 | cout << *max_element(profit.begin(), profit.end()) << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /codechef/LECANDY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | while (t--) { 9 | int n, c; 10 | cin >> n >> c; 11 | int sum = 0; 12 | for (int i=0; i> input; 15 | sum += input; 16 | } 17 | 18 | if (sum <= c) 19 | cout << "Yes"; 20 | else 21 | cout << "No"; 22 | cout << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /codechef/PCSUB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n; 8 | cin >> t; 9 | while (t--) { 10 | cin >> n; 11 | string s; cin >> s; 12 | long long int count = 0; 13 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n; 12 | cin >> n; 13 | vector a(n); 14 | for (auto &input: a) 15 | cin >> input; 16 | 17 | map mp; 18 | for (int i=0; i<7; i++) 19 | mp.insert({i+1, 0}); 20 | 21 | int flag = 1; 22 | for (int i=0; i<=n/2; i++) { 23 | mp[a[i]]++; 24 | if (a[i] != a[n-i-1]) 25 | flag = 0; 26 | } 27 | 28 | for (auto m: mp) { 29 | if (m.second == 0) 30 | flag = 0; 31 | } 32 | 33 | if (flag) 34 | cout << "yes"; 35 | else 36 | cout << "no"; 37 | cout << endl; 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /codechef/SALARY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n; 12 | cin >> n; 13 | vector w(n); 14 | for (auto &input: w) 15 | cin >> input; 16 | 17 | // solve 18 | int temp = w[0]; 19 | int flag = 0; 20 | for (int i=1; i 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n, m; 15 | cin >> n >> m; 16 | int arr[n][2]; 17 | for (int i=0; i> arr[i][j]; 20 | } 21 | } 22 | 23 | vector ans(m, false); 24 | for (int i=0; i 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int n; 10 | cin >> n; 11 | vector v(n); 12 | for (auto &input: v) 13 | cin >> input; 14 | for (auto num: v) { 15 | if (num == 1) { 16 | cout << "HARD" << endl; 17 | return 0; 18 | } 19 | } 20 | cout << "EASY" << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /codeforces/A/P110A.cpp: -------------------------------------------------------------------------------- 1 | // A. Nearly Lucky Number 2 | // https://codeforces.com/problemset/problem/110/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | string n; 9 | cin >> n; 10 | 11 | 12 | int count=0; 13 | for (char i : n) { 14 | if (i == '4' || i == '7') 15 | count++; 16 | } 17 | if (count == 4 || count == 7) 18 | cout << "YES"; 19 | else 20 | cout << "NO"; 21 | cout << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /codeforces/A/P112A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | string s1, s2; 9 | cin >> s1 >> s2; 10 | transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 11 | transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 12 | if (s1.compare(s2) < 0) 13 | cout << -1; 14 | else if (s1.compare(s2) > 0) 15 | cout << 1; 16 | else 17 | cout << 0; 18 | cout << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /codeforces/A/P116A.cpp: -------------------------------------------------------------------------------- 1 | // A. Tram 2 | // https://codeforces.com/problemset/problem/116/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n; 9 | cin >> n; 10 | int arr[n][2]; 11 | for (int i=0; i> arr[i][j]; 14 | } 15 | } 16 | 17 | 18 | int max = arr[0][1]; 19 | for (int i=1; i max) 23 | max = capacity; 24 | } 25 | cout << max << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /codeforces/A/P122A.cpp: -------------------------------------------------------------------------------- 1 | // A. Lucky Division 2 | // https://codeforces.com/problemset/problem/122/A 3 | // 10 mint avg 4 | // mod math, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef int64_t ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int n; 16 | cin >> n; 17 | vector lucky = {4, 7, 47, 74, 477, 744}; 18 | bool flag = false; 19 | for (auto num: lucky) { 20 | if (n%num == 0) { 21 | flag = true; 22 | break; 23 | } 24 | } 25 | if (flag) 26 | cout << "YES"; 27 | else 28 | cout << "NO"; 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /codeforces/A/P129A.cpp: -------------------------------------------------------------------------------- 1 | // A. Cookies 2 | // https://codeforces.com/problemset/problem/129/A 3 | // 5 mint avg 4 | // array, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef int64_t ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int n; 17 | cin >> n; 18 | vector a(n); 19 | for (auto &i: a) 20 | cin >> i; 21 | int count = 0; 22 | for (int i=0; i 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | cout << 1 << " " << n-1 << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /codeforces/A/P1328A.cpp: -------------------------------------------------------------------------------- 1 | // A. Divisibility Problem 2 | // https://codeforces.com/problemset/problem/1328/A 3 | // 2 mint avg 4 | // math, brute force 5 | 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int t; 17 | cin >> t; 18 | while (t--) { 19 | int a, b; 20 | cin >> a >> b; 21 | 22 | if (a%b == 0) 23 | cout << 0; 24 | else 25 | cout << b-a%b; 26 | cout << endl; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /codeforces/A/P1335A.cpp: -------------------------------------------------------------------------------- 1 | // A. Candies and Two Sisters 2 | // https://codeforces.com/problemset/problem/1335/A 3 | // 5 mint avg 4 | // math, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int t; 17 | cin >> t; 18 | while (t--) { 19 | int n; 20 | cin >> n; 21 | 22 | if (n%2==0) { 23 | cout << ((n/2)-1); 24 | } else { 25 | cout << n/2; 26 | } 27 | cout << endl; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /codeforces/A/P133A.cpp: -------------------------------------------------------------------------------- 1 | // A. HQ9+ 2 | // https://codeforces.com/problemset/problem/133/A 3 | // 5 mint avg 4 | // string 5 | 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | string p; 15 | cin >> p; 16 | 17 | bool status = false; 18 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // https://codeforces.com/problemset/problem/136/A 8 | // Codeforces Beta Round #97 (Div. 2) 9 | // A. Presents 10 | int main() { 11 | int n; 12 | cin >> n; 13 | vector v(n); 14 | for (int i=0; i> v[i]; 16 | 17 | vector result(n); 18 | for (int j=0; j 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n; 18 | cin >> n; 19 | 20 | if (n%2 != 0) 21 | n -= 1; 22 | cout << __gcd(n/2, n) << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /codeforces/A/P1397A.cpp: -------------------------------------------------------------------------------- 1 | // A. Juggling Letters 2 | // https://codeforces.com/contest/1397/problem/A 3 | 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | int t; 11 | cin >> t; 12 | while (t--) { 13 | int n; 14 | cin >> n; 15 | vector s(n); 16 | for (auto& i: s) 17 | cin >> i; 18 | 19 | map mp; 20 | for (int i=0; i 4 | #include 5 | typedef long long ll; 6 | using namespace std; 7 | 8 | int main() { 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | 12 | int n; 13 | cin >> n; 14 | vector a(n); 15 | int min, max; 16 | min = max = 0; 17 | int index = 0; 18 | for (auto &input: a) { 19 | cin >> input; 20 | 21 | if (input <= a[min]) 22 | min = index; 23 | if (input > a[max]) 24 | max = index; 25 | index++; 26 | } 27 | 28 | if (max > min) 29 | cout << (max - 1) + (n - min) - 1 << endl; 30 | else 31 | cout << (max - 1) + (n - min) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /codeforces/A/P155A.cpp: -------------------------------------------------------------------------------- 1 | // A. I_love_%username% 2 | // https://codeforces.com/contest/155/problem/A 3 | #include 4 | #include 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | int main() { 11 | freopen("input.txt", "r", stdin); 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | vector a(n); 17 | for (auto &input: a) 18 | cin >> input; 19 | 20 | int count = 0; 21 | for (int i=0; i a[i]) { 23 | count++; 24 | } else if (*max_element(a.begin(), a.begin()+i) < a[i]) { 25 | count++; 26 | } 27 | } 28 | cout << count << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /codeforces/A/P158A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // A. Next Round 6 | // https://codeforces.com/problemset/problem/158/A 7 | int main() { 8 | int n, k; 9 | cin >> n >> k; 10 | vector a(n); 11 | for (int i=0; i> a[i]; 13 | 14 | int count = 0; 15 | for (int i=0; i= a[k-1] && a[i]!=0) 17 | count++; 18 | } 19 | cout << count << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/A/P200B.cpp: -------------------------------------------------------------------------------- 1 | // B. Drinks 2 | // https://codeforces.com/problemset/problem/200/B 3 | #include 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | freopen("input.txt", "r", stdin); 11 | freopen("output.txt", "w", stdout); 12 | 13 | int n, sum=0; 14 | cin >> n; 15 | for (int i=0; i> input; 18 | sum += input; 19 | } 20 | 21 | cout << setprecision(12) << (double) sum/n << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /codeforces/A/P208A.cpp: -------------------------------------------------------------------------------- 1 | // A. Dubstep 2 | // https://codeforces.com/problemset/problem/208/A 3 | // 10 mint avg 4 | // string, implementation 5 | 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | string s; 15 | cin >> s; 16 | 17 | bool space = false; 18 | int count = 0; 19 | for (int i=0; i 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int count = 0; 15 | map mp; 16 | for (int i=0; i<4; i++) { 17 | int input; 18 | cin >> input; 19 | mp.insert({input, 0}); 20 | mp[input]++; 21 | 22 | if (mp[input] > 1) 23 | count++; 24 | } 25 | cout << count << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /codeforces/A/P230A.cpp: -------------------------------------------------------------------------------- 1 | // A. Dragons 2 | // https://codeforces.com/contest/230/problem/A 3 | // 20 mint avg 4 | // array, sorting, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef int64_t ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int s, n; 16 | cin >> s >> n; 17 | array arr[n]; 18 | for (int i=0; i> arr[i][0] >> arr[i][1]; 20 | 21 | sort(arr, arr+n); 22 | 23 | bool status = true; 24 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, count, result = 0, temp; 7 | cin >> n; 8 | for (int i=0; i> temp; 12 | if (temp == 1) 13 | count++; 14 | } 15 | 16 | if (count >= 2) 17 | result++; 18 | } 19 | cout << result << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/A/P233A.cpp: -------------------------------------------------------------------------------- 1 | // A. Perfect Permutation 2 | // https://codeforces.com/problemset/problem/233/A 3 | #include 4 | 5 | typedef long long ll; 6 | using namespace std; 7 | 8 | int main() { 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | 12 | int n; 13 | cin >> n; 14 | 15 | if (n%2==1) { 16 | cout << -1; 17 | } else { 18 | cout << "2 1 "; 19 | for (int i=3; i 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | freopen("output.txt", "w", stdout); 11 | 12 | string a; 13 | cin >> a; 14 | 15 | bool flag = true; 16 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | // https://codeforces.com/problemset/problem/263/A 6 | // Codeforces Round #161 (Div. 2) 7 | // A. Beautiful Matrix 8 | int main() { 9 | int matrix[5][5]; 10 | int x, y, n; 11 | for (int i=0; i<5; i++) { 12 | for (int j=0; j<5; j++) { 13 | cin >> n; 14 | matrix[i][j] = n; 15 | if (n == 1) { 16 | x = i; 17 | y = j; 18 | } 19 | } 20 | } 21 | 22 | cout << abs(2 - x) + abs(2 - y) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /codeforces/A/P265A.cpp: -------------------------------------------------------------------------------- 1 | // A. Colorful Stones (Simplified Edition) 2 | // https://codeforces.com/contest/265/problem/A 3 | // 5 mint avg 4 | 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | string s, t; 16 | cin >> s >> t; 17 | int index = 0; 18 | for (int i=0; i 2 | using namespace std; 3 | 4 | // A. Stones on the Table 5 | // https://codeforces.com/problemset/problem/266/A 6 | int main() { 7 | int n; 8 | cin >> n; 9 | string s; 10 | cin >> s; 11 | int count = 0; 12 | for (int i=0; i 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n, t; 9 | cin >> n >> t; 10 | string s; 11 | cin >> s; 12 | 13 | while (t--) { 14 | for (int i=0; i 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | int arr[n][2]; 17 | for (int i=0; i> arr[i][0] >> arr[i][1]; 19 | 20 | int count = 0; 21 | for (int i=0; i 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int y; 9 | cin >> y; 10 | while (true) { 11 | y++; 12 | string s = to_string(y); 13 | bool flag = false; 14 | for (int i=0; i<4; i++) { 15 | for (int j=0; j<4; j++) { 16 | if(s[i] == s[j] && i != j) { 17 | flag = true; 18 | break; 19 | } 20 | } 21 | 22 | if (flag) 23 | break; 24 | } 25 | 26 | if (!flag) { 27 | cout << y << endl; 28 | return 0; 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /codeforces/A/P275A.cpp: -------------------------------------------------------------------------------- 1 | // A. Lights Out 2 | // https://codeforces.com/problemset/problem/275/A 3 | #include 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | freopen("input.txt", "r", stdin); 11 | freopen("output.txt", "w", stdout); 12 | 13 | int grid[5][5]; 14 | int toggled[5][5]; 15 | for (int i=1; i<=3; i++) { 16 | for (int j=1; j<=3; j++) { 17 | cin >> grid[i][j]; 18 | toggled[i][j] = grid[i][j]; 19 | } 20 | } 21 | 22 | for (int i=1; i<=3; i++) { 23 | for (int j=1; j<=3; j++) { 24 | if (grid[i][j] != 0) { 25 | toggled[i][j-1] += grid[i][j]; 26 | toggled[i][j+1] += grid[i][j]; 27 | toggled[i-1][j] += grid[i][j]; 28 | toggled[i+1][j] += grid[i][j]; 29 | } 30 | } 31 | } 32 | 33 | for (int i=1; i<=3; i++) { 34 | for (int j=1; j<=3; j++) { 35 | if (toggled[i][j]%2==0) 36 | cout << 1; 37 | else 38 | cout << 0; 39 | } 40 | cout << endl; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /codeforces/A/P282A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A. Bit++ 5 | // https://codeforces.com/problemset/problem/282/A 6 | int main() { 7 | int n, count = 0; 8 | cin >> n; 9 | string x; 10 | for (int i=0; i> x; 12 | if (x.find("++") != string::npos) 13 | count++; 14 | else if (x.find("--") != string::npos) 15 | count--; 16 | } 17 | cout << count; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /codeforces/A/P313A.cpp: -------------------------------------------------------------------------------- 1 | // A. Ilya and Bank Account 2 | // https://codeforces.com/problemset/problem/313/A 3 | // 8 mint avg 4 | // string, math, implementation 5 | 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | ll n; 15 | cin >> n; 16 | 17 | if (n > 0) { 18 | cout << n << endl; 19 | return 0; 20 | } 21 | 22 | string s = to_string(n); 23 | string s1, s2; 24 | for (int i=0; i 7 | #include 8 | 9 | typedef int64_t ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int64_t n, k; 16 | cin >> n >> k; 17 | 18 | // prime case 19 | if ((n+1)/2 >= k) { 20 | cout << (k*2-1) << endl; 21 | } else { 22 | k = abs(k-(n+1)/2); 23 | cout << k*2 << endl; 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /codeforces/A/P337A.cpp: -------------------------------------------------------------------------------- 1 | // A. Puzzles 2 | // https://codeforces.com/contest/337/problem/A 3 | // 15 mint avg 4 | // array, sort, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef int64_t ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int n, m; 17 | cin >> n >> m; 18 | vector f(m); 19 | for (auto &i: f) 20 | cin >> i; 21 | sort(f.begin(), f.end()); 22 | 23 | int best = f[n-1] - f[0]; 24 | for (int i=1; i<=m-n; i++) { 25 | best = min(best, f[i+n-1]-f[i]); 26 | } 27 | cout << best << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /codeforces/A/P339A.cpp: -------------------------------------------------------------------------------- 1 | // A. Helpful Maths 2 | // https://codeforces.com/contest/339/problem/A 3 | // implementation, string, array 4 | // 2 mint avg 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | string s; 17 | cin >> s; 18 | 19 | vector v; 20 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n; 9 | cin >> n; 10 | vector v(n); 11 | for (int i=0; i> v[i]; 13 | 14 | int count = 0; 15 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n; 9 | cin >> n; 10 | vector v(n); 11 | for (int i=0; i> v[i]; 13 | int s, d, l, r; 14 | s = d = 0; 15 | l = 0; 16 | r = n-1; 17 | for (int i=0; i v[r]) { 20 | s += v[l++]; 21 | } else { 22 | s += v[r--]; 23 | } 24 | } else { 25 | if (v[l] > v[r]) { 26 | d += v[l++]; 27 | } else { 28 | d += v[r--]; 29 | } 30 | } 31 | } 32 | 33 | if (s > d) 34 | cout << s << " " << d; 35 | else 36 | cout << d << " " << s; 37 | cout << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /codeforces/A/P405A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long ll; 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | cout.tie(nullptr); 11 | int n; 12 | cin >> n; 13 | multiset s; 14 | int num; 15 | for (int i=0; i> num; 17 | s.insert(num); 18 | } 19 | for (auto num: s) 20 | cout << num << " "; 21 | cout << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /codeforces/A/P427A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n; 9 | cin >> n; 10 | vector v(n); 11 | for (int i=0; i> v[i]; 13 | int police = 0; 14 | int crime = 0; 15 | for (int i=0; i 0) 17 | police += v[i]; 18 | else if (v[i] < 0 && police > 0) 19 | police--; 20 | else 21 | crime++; 22 | } 23 | cout << crime << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /codeforces/A/P431A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int a1, a2, a3, a4; 9 | cin >> a1 >> a2 >> a3 >> a4; 10 | string s; 11 | cin >> s; 12 | vector v = {a1, a2, a3, a4}; 13 | int sum = 0; 14 | for (int i=0; i 7 | #include 8 | #include 9 | #include 10 | 11 | typedef long long ll; 12 | using namespace std; 13 | 14 | int main() { 15 | freopen("output.txt", "w", stdout); 16 | 17 | int n, k; 18 | cin >> n >> k; 19 | vector y(n); 20 | for (auto &i: y) 21 | cin >> i; 22 | // sort(y.begin(), y.end()); 23 | int count = 0; 24 | for (int i=0; i 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n, m; 15 | cin >> n >> m; 16 | 17 | int move = 1; 18 | while (n != 0 && m != 0) { 19 | n--; 20 | m--; 21 | move++; 22 | } 23 | 24 | if (move%2 != 0) 25 | cout << "Malvika"; 26 | else 27 | cout << "Akshat"; 28 | cout << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /codeforces/A/P459A.cpp: -------------------------------------------------------------------------------- 1 | // A. Pashmak and Garden 2 | // https://codeforces.com/contest/459/problem/A 3 | // 30 mint avg 4 | // math, geometry, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int x1, y1, x2, y2, x3, y3, x4, y4; 16 | cin >> x1 >> y1 >> x2 >> y2; 17 | if (x1 == x2) { 18 | int side = abs(y2-y1); 19 | x3 = x1+side; 20 | x4 = x2+side; 21 | y3 = y1; 22 | y4 = y2; 23 | } else if (y1 == y2) { 24 | int side = abs(x2-x1); 25 | x3 = x1; 26 | x4 = x2; 27 | y3 = y1+side; 28 | y4 = y2+side; 29 | } else if (abs(x2-x1) == abs(y2-y1)) { 30 | x3 = x1; 31 | y3 = y2; 32 | x4 = x2; 33 | y4 = y1; 34 | } else { 35 | cout << -1 << endl; 36 | return 0; 37 | } 38 | printf("%d %d %d %d\n", x3, y3, x4, y4); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /codeforces/A/P466A.cpp: -------------------------------------------------------------------------------- 1 | // A. Cheap Travel 2 | // https://codeforces.com/problemset/problem/466/A 3 | // 10 mint avg 4 | // math 5 | 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n, m, a, b; 15 | cin >> n >> m >> a >> b; 16 | 17 | if (m*a <= b) 18 | cout << n*a; 19 | else 20 | cout << (n/m)*b + min((n%m)*a, b); 21 | cout << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /codeforces/A/P467A.cpp: -------------------------------------------------------------------------------- 1 | // A. George and Accommodation 2 | // https://codeforces.com/problemset/problem/467/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n; 9 | cin >> n; 10 | int arr[n][2]; 11 | for (int i=0; i> arr[i][j]; 14 | } 15 | } 16 | 17 | int count = 0; 18 | for (int i=0; i= 2) 20 | count++; 21 | } 22 | 23 | cout << count << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /codeforces/A/P469A.cpp: -------------------------------------------------------------------------------- 1 | // A. I Wanna Be the Guy 2 | // https://codeforces.com/problemset/problem/469/A 3 | // 8 mint avg 4 | // set, array, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef int64_t ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | set s; 17 | 18 | int n; 19 | cin >> n; 20 | int x, y; 21 | cin >> x; 22 | for (int i=0; i> input; 25 | s.insert(input); 26 | } 27 | cin >> y; 28 | for (int i=0; i> input; 31 | s.insert(input); 32 | } 33 | 34 | for (int i=1; i<=n; i++) { 35 | if (s.count(i) == 0) { 36 | cout << "Oh, my keyboard!" << endl; 37 | return 0; 38 | } 39 | } 40 | 41 | cout << "I become the guy." << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /codeforces/A/P472A.cpp: -------------------------------------------------------------------------------- 1 | // A. Design Tutorial: Learn from Math 2 | // https://codeforces.com/problemset/problem/472/A 3 | // 10 mint avg 4 | // math, composite number, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | bool is_composite(int n) { 13 | if (n <= 1) return false; 14 | if (n <= 3) return false; 15 | if (n%2 == 0 || n%3 == 0) 16 | return true; 17 | 18 | for (int i=5; i*i<=n; i=i+6) 19 | if (n%i == 0 || n%(i+2) == 0) 20 | return true; 21 | 22 | return false; 23 | } 24 | 25 | int main() { 26 | freopen("output.txt", "w", stdout); 27 | 28 | int n; 29 | cin >> n; 30 | 31 | for (int i=2; i<=n; i++) { 32 | for (int j=2; j<=n; j++) { 33 | if (is_composite(i) && is_composite(j)) { 34 | if (i+j == n) { 35 | cout << i << " " << j << endl; 36 | return 0; 37 | } 38 | } 39 | } 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /codeforces/A/P476A.cpp: -------------------------------------------------------------------------------- 1 | // A. Dreamoon and Stairs 2 | // https://codeforces.com/problemset/problem/476/A 3 | // 10 mint avg 4 | // math, implementation 5 | 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n, m, x; 15 | cin >> n >> m; 16 | if (m > n) 17 | x = -1; 18 | else { 19 | if (n%2 == 0) 20 | x = n/2; 21 | else 22 | x = (n/2)+1; 23 | while (x%m != 0) 24 | x++; 25 | } 26 | 27 | cout << x << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/A/P479A.cpp: -------------------------------------------------------------------------------- 1 | // A. Expression 2 | // https://codeforces.com/problemset/problem/479/A 3 | // 3 mint avg 4 | // basi math, array, sort, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef int64_t ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int a, b, c; 17 | cin >> a >> b >> c; 18 | vector v; 19 | v.push_back(a+b+c); 20 | v.push_back(a+b*c); 21 | v.push_back(a*b+c); 22 | v.push_back(a*(b+c)); 23 | v.push_back((a+b)*c); 24 | v.push_back(a*b*c); 25 | sort(v.begin(), v.end()); 26 | cout << v[v.size()-1] << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /codeforces/A/P486A.cpp: -------------------------------------------------------------------------------- 1 | // A. Calculating Function 2 | // https://codeforces.com/problemset/problem/486/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | long long int n; 9 | cin >> n; 10 | if (n%2==0) { 11 | cout << n/2; 12 | } else { 13 | cout << (n/2+1) * -1; 14 | } 15 | cout << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /codeforces/A/P490A.cpp: -------------------------------------------------------------------------------- 1 | // A. Team Olympiad 2 | // https://codeforces.com/contest/490/problem/A 3 | // 15 mint avg 4 | // greedy, array, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | typedef long long ll; 12 | using namespace std; 13 | 14 | int main() { 15 | freopen("output.txt", "w", stdout); 16 | 17 | int n; 18 | cin >> n; 19 | vector v(n); 20 | map mp; 21 | for (auto &i: v) { 22 | cin >> i; 23 | mp.insert({i, 0}); 24 | mp[i]++; 25 | } 26 | 27 | int teams = 0; 28 | for (int i=0; i= 1 && mp[2] >= 1 && mp[3] >= 1) { 30 | mp[1]--, mp[2]--, mp[3]--; 31 | teams++; 32 | } 33 | } 34 | 35 | if (teams) { 36 | cout << teams << endl; 37 | for (int i=0; i 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | int matrix[n][n]; 17 | for (int i=0; i ans; 30 | for (int i=0; i 2 | using namespace std; 3 | 4 | // A. Domino piling 5 | // https://codeforces.com/problemset/problem/50/A 6 | int main() { 7 | int m, n; 8 | cin >> m >> n; 9 | cout << (m*n)/2 << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /codeforces/A/P520A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n; 9 | cin >> n; 10 | string s; 11 | cin >> s; 12 | if (s.length() < 26) { 13 | cout << "NO" << endl; 14 | return 0; 15 | } 16 | set st; 17 | for (int i=65; i<91; i++) 18 | st.insert(i); 19 | 20 | transform(s.begin(), s.end(), s.begin(), ::toupper); 21 | for (int i=0; i 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int k, n, w; 9 | cin >> k >> n >> w; 10 | 11 | int cost = 0; 12 | for (int i=0; i n) 16 | cout << abs(cost - n); 17 | else 18 | cout << 0; 19 | cout << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /codeforces/A/P58A.cpp: -------------------------------------------------------------------------------- 1 | // A. Chat room 2 | // https://codeforces.com/problemset/problem/58/A 3 | // 5 mint avg 4 | // string, implementation 5 | 6 | #include 7 | #include 8 | 9 | typedef int64_t ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | string s; 16 | cin >> s; 17 | 18 | string comp = "hello"; 19 | int index, count; 20 | index = count = 0; 21 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | string s; 9 | cin >> s; 10 | int l, u; 11 | l = u = 0; 12 | for (auto c: s) { 13 | if (islower(c)) 14 | l++; 15 | else 16 | u++; 17 | } 18 | if (u > l) 19 | transform(s.begin(), s.end(), s.begin(), ::toupper); 20 | else 21 | transform(s.begin(), s.end(), s.begin(), ::tolower); 22 | cout << s << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /codeforces/A/P617A.cpp: -------------------------------------------------------------------------------- 1 | // A. Elephant 2 | // https://codeforces.com/problemset/problem/617/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int x; 9 | cin >> x; 10 | if (x%5 != 0) 11 | cout << x/5 + 1; 12 | else 13 | cout << x/5; 14 | cout << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /codeforces/A/P61A.cpp: -------------------------------------------------------------------------------- 1 | // A. Ultra-Fast Mathematician 2 | // https://codeforces.com/problemset/problem/61/A 3 | #include 4 | 5 | typedef long long ll; 6 | using namespace std; 7 | 8 | int main() { 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | 12 | string num1, num2; 13 | cin >> num1 >> num2; 14 | 15 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n; 9 | cin >> n; 10 | vector v(n); 11 | for (auto &input: v) 12 | cin >> input; 13 | 14 | long long sum = 0; 15 | int odd = INT_MAX; 16 | for (int i=0; i 2 | #include 3 | #include 4 | typedef long long ll; 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | cout.tie(nullptr); 11 | int n, h; 12 | cin >> n >> h; 13 | vector v(n); 14 | for (auto &num: v) 15 | cin >> num; 16 | int count = 0; 17 | for (int i=0; i h) { 23 | v[i] /= 2; 24 | temp += 2; 25 | } 26 | count += temp; 27 | } 28 | } 29 | cout << count << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /codeforces/A/P686A.cpp: -------------------------------------------------------------------------------- 1 | // A. Free Ice Cream 2 | // https://codeforces.com/contest/686/problem/A 3 | // 5 mint avg 4 | // array, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | ll n, x; 17 | cin >> n >> x; 18 | vector> d(n); 19 | for (auto &i: d) 20 | cin >> i.first >> i.second; 21 | 22 | int count = 0; 23 | for (int i=0; i= d[i].second) { 28 | x -= d[i].second; 29 | } else 30 | count++; 31 | } 32 | } 33 | 34 | cout << x << ' ' << count << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /codeforces/A/P702A.cpp: -------------------------------------------------------------------------------- 1 | // A. Maximum Increase 2 | // https://codeforces.com/problemset/problem/702/A 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | vector a(n); 17 | for (auto &i: a) 18 | cin >> i; 19 | int count = 0, max = 0; 20 | for (int i=0; i a[i]) 22 | count++; 23 | else 24 | count = 0; 25 | 26 | if (count > max) 27 | max = count; 28 | } 29 | if (max == 0) 30 | cout << 1; 31 | else 32 | cout << (max + 1); 33 | cout << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /codeforces/A/P705A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | int n; 8 | cin >> n; 9 | for (int i=0; i 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | // no. of oranges 17 | // max size of the orange 18 | int n, b, d; 19 | cin >> n >> b >> d; 20 | vector a(n); 21 | for (auto &i: a) 22 | cin >> i; 23 | 24 | int sum = 0, count = 0; 25 | for (int i=0; i d) 31 | count++, sum = 0; 32 | } 33 | cout << count << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /codeforces/A/P732A.cpp: -------------------------------------------------------------------------------- 1 | // A. Buy a Shove 2 | // https://codeforces.com/contest/732/problem/A 3 | // 15 mint avg 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | 14 | int main() { 15 | freopen("output.txt", "w", stdout); 16 | 17 | int k, r; 18 | cin >> k >> r; 19 | 20 | if (k%10==r) { 21 | cout << 1 << endl; 22 | } else { 23 | int i = 0; 24 | while (true) { 25 | i++; 26 | if ((k*i)%10 == 0 || (k*i)%10 == r) 27 | break; 28 | } 29 | cout << i << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /codeforces/A/P734A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef long long ll; 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | cout.tie(nullptr); 11 | int n; 12 | cin >> n; 13 | string s; 14 | cin >> s; 15 | int a, d; 16 | a = d = 0; 17 | for (int i=0; i d) 24 | cout << "Anton"; 25 | else if (a < d) 26 | cout << "Danik"; 27 | else 28 | cout << "Friendship"; 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /codeforces/A/P749A.cpp: -------------------------------------------------------------------------------- 1 | // A. Bachgold Problem 2 | // https://codeforces.com/problemset/problem/749/A 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | 17 | int count = 0; 18 | if (n%2 == 0) { 19 | count = n/2; 20 | cout << count << endl; 21 | for (int i=0; i 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int n, k; 17 | cin >> n >> k; 18 | char c = 'a'; 19 | vector ch; 20 | for (int i=0; i 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int a, b, c, d; 16 | cin >> a >> b >> c >> d; 17 | 18 | vector v1, v2; 19 | v1.push_back(b); 20 | v2.push_back(d); 21 | for (int i=1; i<=200; i++) { 22 | v1.push_back(b+(i*a)); 23 | v2.push_back(d+(i*c)); 24 | } 25 | 26 | for (int i=0; i 2 | #include 3 | #include 4 | typedef long long ll; 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | cout.tie(nullptr); 11 | int a, b; 12 | cin >> a >> b; 13 | int count = 0; 14 | do { 15 | count++; 16 | a *= 3; 17 | b *= 2; 18 | } while (a <= b); 19 | cout << count << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /codeforces/A/P807A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | int n; 10 | cin >> n; 11 | int arr[n][2]; 12 | for (int i=0; i> arr[i][0]; 14 | cin >> arr[i][1]; 15 | } 16 | 17 | for (int i=0; i arr[j][0]) { 27 | cout << "unrated" << endl; 28 | return 0; 29 | } 30 | } 31 | } 32 | 33 | cout << "maybe" << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /codeforces/A/P80A.cpp: -------------------------------------------------------------------------------- 1 | // A. Panoramix's Prediction 2 | // https://codeforces.com/problemset/problem/80/A 3 | #include 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | bool isPrime(int num){ 10 | bool flag = true; 11 | for(int i=2; i<=num/2; i++) { 12 | if(num%i == 0) { 13 | flag = false; 14 | break; 15 | } 16 | } 17 | return flag; 18 | } 19 | 20 | int main() { 21 | freopen("input.txt", "r", stdin); 22 | freopen("output.txt", "w", stdout); 23 | 24 | int n, m; 25 | cin >> n >> m; 26 | 27 | int count = 0; 28 | for (int i=n+1; i<=m; i++) { 29 | bool status = isPrime(i); 30 | if (status) 31 | count++; 32 | 33 | if (status && i == m && count == 1) { 34 | cout << "YES" << endl; 35 | return 0; 36 | } 37 | } 38 | 39 | cout << "NO" << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /codeforces/A/P977A.cpp: -------------------------------------------------------------------------------- 1 | // A. Wrong Subtraction 2 | // https://codeforces.com/problemset/problem/977/A 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n, k; 9 | cin >> n >> k; 10 | for (int i=0; i 6 | #include 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int n, k; 17 | cin >> n >> k; 18 | vector v(n); 19 | for (auto &i: v) 20 | cin >> i; 21 | 22 | vector> ans; 23 | for (int i=0; i 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int n; 17 | cin >> n; 18 | int target, count; 19 | target = count = 0; 20 | vector coins = {1, 5, 10, 20, 100}; 21 | while (target < n) { 22 | int max = 0; 23 | for (int i=0; i 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int y, w; 17 | cin >> y >> w; 18 | 19 | vector p = {"1/1", "5/6", "2/3", "1/2", "1/3", "1/6"}; 20 | cout << p[max(y, w)-1] << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /codeforces/B/P1353B.cpp: -------------------------------------------------------------------------------- 1 | // B. Two Arrays And Swaps 2 | // https://codeforces.com/problemset/problem/1353/B 3 | // 15 mint avg 4 | // greedy, array, sort, implementation 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | typedef long long ll; 11 | using namespace std; 12 | 13 | int main() { 14 | freopen("output.txt", "w", stdout); 15 | 16 | int t; 17 | cin >> t; 18 | while (t--) { 19 | int n, k; 20 | cin >> n >> k; 21 | vector a(n); 22 | for (auto &i: a) 23 | cin >> i; 24 | vector b(n); 25 | for (auto &i: b) 26 | cin >> i; 27 | 28 | sort(a.begin(), a.end()); 29 | sort(b.rbegin(), b.rend()); 30 | 31 | int ans = 0; 32 | for (int i=0; i 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | bool binary_search(vector& v, int num) { 12 | int L = 0, R = v.size() - 1; 13 | while (L <= R) { 14 | int M = L + (R - L) / 2; 15 | if (v[M] == num) 16 | return true; 17 | else if (v[M] > num) 18 | R = M-1; 19 | else 20 | L = M+1; 21 | } 22 | return false; 23 | } 24 | 25 | int main() { 26 | freopen("output.txt", "w", stdout); 27 | 28 | int n, k, input; 29 | cin >> n >> k; 30 | vector a(n); 31 | for (auto &i: a) 32 | cin >> i; 33 | for (int i=0; i> input; 35 | if (binary_search(a, input)) 36 | cout << "YES"; 37 | else 38 | cout << "NO"; 39 | cout << endl; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /codeforces/practice/binary-search/B1.cpp: -------------------------------------------------------------------------------- 1 | // B. Closest to the Left 2 | // https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/B 3 | 4 | #include 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | int binary_search(vector& v, int num) { 11 | int L = -1; 12 | int R = v.size(); 13 | while (R > L+1) { 14 | int M = (L + R) / 2; 15 | if (v[M] <= num) 16 | L = M; 17 | else 18 | R = M; 19 | } 20 | return R; 21 | } 22 | 23 | int main() { 24 | freopen("output.txt", "w", stdout); 25 | 26 | int n, k, input; 27 | cin >> n >> k; 28 | vector a(n); 29 | for (auto &i: a) 30 | cin >> i; 31 | for (int i=0; i> input; 33 | cout << binary_search(a, input) << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /codeforces/practice/binary-search/C1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/C 3 | 4 | #include 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | int binary_search(vector& v, int num) { 11 | int L = -1; 12 | int R = v.size(); 13 | while (R > L+1) { 14 | int M = (L + R) / 2; 15 | if (v[M] < num) 16 | L = M; 17 | else 18 | R = M; 19 | } 20 | return R+1; 21 | } 22 | 23 | int main() { 24 | freopen("output.txt", "w", stdout); 25 | 26 | int n, k, input; 27 | cin >> n >> k; 28 | vector a(n); 29 | for (auto &i: a) 30 | cin >> i; 31 | for (int i=0; i> input; 33 | cout << binary_search(a, input) << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /cses/alon20/P1165.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | 5 | int main() { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | cout.tie(nullptr); 9 | ll n; 10 | cin >> n; 11 | ll sum = 0; 12 | for (int i=1; i<=n; i++) { 13 | sum += i; 14 | } 15 | cout << sum << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /cses/alon20/P1166.cpp: -------------------------------------------------------------------------------- 1 | // List 2 | // https://cses.fi/alon20/task/1166/ 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | // freopen("output.txt", "w", stdout); 11 | 12 | // 1 2 3 4 5 13 | // 1 3 4 2 4 14 | int n; 15 | cin >> n; 16 | 17 | if (n == 2 || n == 3) { 18 | cout << "NO SOLUTION" << endl; 19 | return 0; 20 | } 21 | 22 | vector v; 23 | for (int i=0; i 2 | 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | ll a, b; 11 | cin >> a >> b; 12 | cout << a+b << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /cses/dt/P313.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | ll n; 11 | cin >> n; 12 | while (n!=1) { 13 | cout << n << " "; 14 | if (n%2==0) 15 | n = n/2; 16 | else 17 | n = (n*3) + 1; 18 | } 19 | cout << 1 << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /cses/dt/P315.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | cout.tie(nullptr); 10 | double r; 11 | cin >> r; 12 | printf("%.9lf\n", (4.0/3.0)*(M_PI*pow(r, 3))); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /cses/dt/P317.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | typedef long long ll; 6 | using namespace std; 7 | 8 | int main() { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(nullptr); 11 | cout.tie(nullptr); 12 | string s; 13 | cin >> s; 14 | int max = 0, count = 0; 15 | for (int i=0; i= max) { 20 | max = count; 21 | } 22 | count = 0; 23 | } 24 | } 25 | if (count > max) { 26 | max = count; 27 | } 28 | cout << max+1 << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /cses/dt/P318.cpp: -------------------------------------------------------------------------------- 1 | // Missing 2 | // https://cses.fi/dt/task/318/ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | 11 | int main() { 12 | // freopen("output.txt", "w", stdout); 13 | 14 | int n; 15 | cin >> n; 16 | 17 | vector nums(n); 18 | for (int i=0; i> input; 26 | nums[input-1] = true; 27 | } 28 | 29 | int ans = 0; 30 | for (int i=0; i 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | cout.tie(nullptr); 11 | 12 | long long int n; 13 | cin >> n; 14 | while (n != 1) { 15 | cout << n << " "; 16 | if (n%2 == 0) n /= 2; 17 | else n *= 3, n += 1; 18 | } 19 | cout << 1 << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /cses/problemset/P1069.cpp: -------------------------------------------------------------------------------- 1 | // Repetitions 2 | // https://cses.fi/problemset/task/1069/ 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | // freopen("output.txt", "w", stdout); 12 | 13 | string s; 14 | cin >> s; 15 | int max = 0, count = 0; 16 | for (int i=0; i max) 21 | max = count; 22 | count = 0; 23 | } 24 | } 25 | cout << max+1<< endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /cses/problemset/P1070.cpp: -------------------------------------------------------------------------------- 1 | // Permutations 2 | // https://cses.fi/problemset/task/1070/ 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | // freopen("output.txt", "w", stdout); 11 | 12 | int n; 13 | cin >> n; 14 | 15 | if (n == 2 || n == 3) { 16 | cout << "NO SOLUTION" << endl; 17 | return 0; 18 | } 19 | 20 | vector v; 21 | for (int i=0; i 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | // freopen("output.txt", "w", stdout); 12 | 13 | int n; 14 | cin >> n; 15 | vector v(n-1); 16 | for (auto &i: v) cin >> i; 17 | map mp; 18 | for (int i=0; i 2 | #include 3 | 4 | using namespace std; 5 | 6 | class TreeNode { 7 | public: 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 12 | }; 13 | 14 | void PreOrder(TreeNode *tree); 15 | void InOrder(TreeNode *tree); 16 | void Postrder(TreeNode *tree); 17 | TreeNode* Minimum(TreeNode *tree); 18 | TreeNode* Maximum(TreeNode *tree); 19 | TreeNode* Find(TreeNode *tree, int element); 20 | int Sum(TreeNode *tree); 21 | bool isSame(TreeNode *tree1, TreeNode *tree2); 22 | int sumLeft(TreeNode *tree); 23 | 24 | int main() { 25 | TreeNode *root = new TreeNode(4); 26 | root->left = new TreeNode(2); 27 | root->right = new TreeNode(10); 28 | 29 | root->left->left = new TreeNode(1); 30 | root->left->right = new TreeNode(3); 31 | 32 | root->right->left = new TreeNode(9); 33 | root->right->right = new TreeNode(16); 34 | 35 | PreOrder(root); cout << endl; 36 | InOrder(root); cout << endl; 37 | Postrder(root); cout << endl; 38 | cout << "Minimum: " << Minimum(root)->val << endl; 39 | cout << "Maximum: " << Maximum(root)->val << endl; 40 | cout << "Sum: " << Sum(root) << endl; 41 | cout << "SumLeft: " << sumLeft(root) << endl; 42 | } 43 | 44 | void PreOrder(TreeNode *tree) { 45 | if (tree) { 46 | printf("%d ", tree->val); 47 | PreOrder(tree->left); 48 | PreOrder(tree->right); 49 | } 50 | } 51 | 52 | void InOrder(TreeNode *tree) { 53 | if (tree) { 54 | InOrder(tree->left); 55 | printf("%d ", tree->val); 56 | InOrder(tree->right); 57 | } 58 | } 59 | 60 | void Postrder(TreeNode *tree) { 61 | if (tree) { 62 | Postrder(tree->left); 63 | Postrder(tree->right); 64 | printf("%d ", tree->val); 65 | } 66 | } 67 | 68 | TreeNode* Minimum(TreeNode *tree) { 69 | if (tree == nullptr) 70 | return nullptr; 71 | if (tree->left == nullptr) 72 | return tree; 73 | Minimum(tree->left); 74 | } 75 | 76 | TreeNode* Maximum(TreeNode *tree) { 77 | if (tree == nullptr) 78 | return nullptr; 79 | if (tree->right == nullptr) 80 | return tree; 81 | Maximum(tree->right); 82 | } 83 | 84 | TreeNode* Find(TreeNode *tree, int element) { 85 | if (tree) { 86 | if (tree->val == element) 87 | return tree; 88 | Find(tree->left, element); 89 | Find(tree->right, element); 90 | } 91 | 92 | return tree; 93 | } 94 | 95 | int Sum(TreeNode *tree) { 96 | if (tree == nullptr) 97 | return 0; 98 | return (tree->val + Sum(tree->left) + Sum(tree->right)); 99 | } 100 | 101 | bool isSame(TreeNode *tree1, TreeNode *tree2) { 102 | if (tree1 && tree2) { 103 | if (tree1->val != tree2->val) 104 | return false; 105 | isSame(tree1->left, tree2->left); 106 | isSame(tree1->right, tree2->right); 107 | } 108 | 109 | return true; 110 | } 111 | 112 | int sumLeft(TreeNode *tree) { 113 | if (tree == nullptr) 114 | return 0; 115 | return (tree->val + sumLeft(tree->left)); 116 | } -------------------------------------------------------------------------------- /datastructures/graphs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | class Graph { 10 | private: 11 | int V; 12 | list *l; 13 | public: 14 | Graph(int V) { 15 | this->V = V; 16 | this->l = new list[V]; 17 | } 18 | 19 | void addEdge(int x, int y) { 20 | this->l[x].push_back(y); 21 | this->l[y].push_back(x); 22 | } 23 | 24 | void traverse() { 25 | for (int i=0; il[i]) { 28 | cout << j << " "; 29 | } 30 | cout << endl; 31 | } 32 | cout << endl; 33 | } 34 | }; 35 | 36 | int main() { 37 | Graph g(5); 38 | g.addEdge(1, 2); 39 | g.addEdge(1, 3); 40 | g.addEdge(1, 5); 41 | g.addEdge(3, 4); 42 | g.traverse(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /datastructures/list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void display(list& l); 7 | 8 | int main() { 9 | list l; 10 | auto it = l.begin(); 11 | advance(it, 3); 12 | 13 | l.push_front(2); 14 | l.push_front(1); 15 | l.push_front(0); 16 | l.push_back(4); 17 | l.push_back(5); 18 | l.push_back(6); 19 | l.insert(it, 3); 20 | display(l); 21 | cout << l.size() << endl; 22 | } 23 | 24 | void display(list& l) { 25 | for (auto num: l) 26 | cout << num << " "; 27 | cout << endl; 28 | } -------------------------------------------------------------------------------- /datastructures/queues.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /datastructures/segment-tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class SegmentTree { 7 | private: 8 | int size; 9 | vector tree; 10 | public: 11 | explicit SegmentTree(const vector& arr) { 12 | this->size = arr.size(); 13 | this->tree.resize(2*arr.size()); 14 | this->build(arr, 0, arr.size()-1, 1); 15 | } 16 | 17 | void build(vector arr, int start, int end, int index) { 18 | if (start == end) { 19 | this->tree[index] = arr[start]; 20 | return; 21 | } 22 | 23 | int mid = (start+end)/2; 24 | this->build(arr, start, mid, 2*index); 25 | this->build(arr, mid+1, end, 2*index+1); 26 | this->tree[index] = this->tree[2*index] + this->tree[2*index+1]; 27 | } 28 | 29 | int sum(int start, int end, int l, int r, int index) { 30 | if (l > r) 31 | return 0; 32 | if (start == l && end == r) 33 | return this->tree[index]; 34 | int mid = (start+end)/2; 35 | return sum(start, mid, l, min(r, mid), 2*index) + sum(mid+1, end, max(l, mid+1), r, 2*index+1); 36 | } 37 | 38 | int sum(int l, int r) { 39 | return this->sum(0, this->size-1, l, r, 1); 40 | } 41 | 42 | void update(int i, int value, int start, int end, int index) { 43 | if (start == end) { 44 | this->tree[index] = value; 45 | return; 46 | } 47 | 48 | int mid = (start+end)/2; 49 | if (i <= mid) 50 | this->update(i, value, start, mid, 2*index); 51 | else 52 | this->update(i, value, mid+1, end, 2*index+1); 53 | this->tree[index] = this->tree[2*index] + this->tree[2*index+1]; 54 | } 55 | 56 | void update(int i, int value) { 57 | this->update(i, value, 0, this->size-1, 1); 58 | } 59 | 60 | void display() { 61 | for (int i=1; itree.size(); i++) 62 | cout << this->tree[i] << " "; 63 | cout << endl; 64 | } 65 | }; 66 | 67 | int main() { 68 | int n, m; 69 | cin >> n >> m; 70 | vector a(n); 71 | for (auto &input: a) 72 | cin >> input; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /datastructures/set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void display (set& s) { 8 | cout << "Elements: "; 9 | for (auto num: s) 10 | cout << num << " "; 11 | cout << endl; 12 | } 13 | 14 | void display (unordered_set& s) { 15 | cout << "Elements: "; 16 | for (auto num: s) 17 | cout << num << " "; 18 | cout << endl; 19 | } 20 | 21 | // set won't contain duplicates elements 22 | // set is automatically in order form 23 | // we can not use [] operator in set 24 | int main() { 25 | set s = {1, 2}; 26 | s.insert(4); 27 | s.insert(5); 28 | s.insert(5); // don't insert the element because of duplication 29 | s.insert(3); 30 | 31 | display(s); // print all the elements 32 | 33 | cout << "Count: " << s.count(5) << endl; // check specific element 34 | cout << "Size: " << s.size() << endl; // size of set 35 | cout << "Erase: " << s.erase(3) << endl; // erase the element 36 | cout << "Find: " << *s.find(3) << endl; // find the element and return index of it 37 | 38 | unordered_set us = {2, 1, 4, 3, 5}; // same as set but it not sort automatically 39 | display(us); 40 | 41 | multiset ms = {1, 1, 2, 2, 3, 3, 3}; // same as set but it contain duplicate value 42 | cout << "Multiset Count: " << ms.count(3) << endl; // check all the element and return count 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /datastructures/singly-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * It's my own implementation of linked list with all the standards 3 | * ADT's such as: 4 | * 5 | * - push_front, push_at, push_back 6 | * - pop_front, pop_at, pop_back 7 | * - get, find 8 | * - is_empty, size 9 | * - traverse 10 | * 11 | * @author Mubashir R. Razvi 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | typedef struct ListNode { 19 | int data; 20 | struct ListNode *next; 21 | } ListNode; 22 | 23 | ListNode* HEAD = NULL; // pointer to track position of first element 24 | 25 | void push_front(int data); // add element at front of list 26 | void push_at(int data, int index); // add element at specific index 27 | void push_back(int data); // add element at the last 28 | int pop_front(); // delete element from front and return the value 29 | int pop_at(int index); // delete element from specific index 30 | int pop_back(); // delete element from last 31 | int get(int index); // get the value from specific index 32 | bool find(int value); // check the value exist in list or not 33 | int size(); // list size 34 | bool is_empty(); // check list is empty 35 | void traverse(); // display all the elements in list 36 | 37 | void push_front(int data) { 38 | ListNode* node = (ListNode*) malloc(sizeof(ListNode)); 39 | node->data = data; 40 | node->next = HEAD; 41 | HEAD = node; 42 | } 43 | 44 | void push_at(int data, int index) { 45 | ListNode* node = (ListNode*) malloc(sizeof(ListNode)); 46 | node->data = data; 47 | if (HEAD == NULL) { 48 | node->next = HEAD; 49 | HEAD = node; 50 | } else { 51 | ListNode* temp = HEAD; 52 | ListNode* last; 53 | int count = 0; 54 | while (temp != NULL) { 55 | if (count == index) { 56 | node->next = temp; 57 | last->next = node; 58 | return; 59 | } 60 | 61 | count++; 62 | last = temp; 63 | temp = temp->next; 64 | } 65 | } 66 | } 67 | 68 | void push_back(int data) { 69 | ListNode* node = (ListNode*) malloc(sizeof(ListNode)); 70 | node->data = data; 71 | if (HEAD == NULL) { 72 | node->next = HEAD; 73 | } else { 74 | ListNode* temp = HEAD; 75 | ListNode* last; 76 | while (temp != NULL) { 77 | last = temp; 78 | temp = temp->next; 79 | } 80 | last->next = node; 81 | node->next = NULL; 82 | } 83 | } 84 | 85 | int pop_front() { 86 | ListNode* temp = HEAD; 87 | HEAD = temp->next; 88 | int value = temp->data; 89 | return value; 90 | } 91 | 92 | int pop_at(int index) { 93 | if (index == 0) { 94 | return pop_front(); 95 | } 96 | 97 | if (index == size()-1) { 98 | return pop_back(); 99 | } 100 | 101 | ListNode* temp = HEAD; 102 | ListNode* last; 103 | int count = 0; 104 | while(temp != NULL) { 105 | if (count == index) { 106 | last->next = temp->next; 107 | int value = temp->data; 108 | return value; 109 | } 110 | count++; 111 | last = temp; 112 | temp = temp->next; 113 | } 114 | return -1; 115 | } 116 | 117 | int pop_back() { 118 | ListNode* temp = HEAD; 119 | ListNode* last; 120 | while(temp->next != NULL) { 121 | last = temp; 122 | temp = temp->next; 123 | } 124 | last->next = NULL; 125 | int value = temp->data; 126 | return value; 127 | } 128 | 129 | int get(int index) { 130 | ListNode* temp = HEAD; 131 | int count = 0; 132 | while (temp != NULL) { 133 | if (count == index) { 134 | return temp->data; 135 | } 136 | count++; 137 | temp = temp->next; 138 | } 139 | free(temp); 140 | return -1; 141 | } 142 | 143 | bool find(int value) { 144 | ListNode* temp = HEAD; 145 | while(temp != NULL) { 146 | if (temp->data == value) { 147 | return true; 148 | } 149 | temp = temp->next; 150 | } 151 | return false; 152 | } 153 | 154 | int size() { 155 | ListNode* temp = HEAD; 156 | int count = 0; 157 | while(temp != NULL) { 158 | count++; 159 | temp = temp->next; 160 | } 161 | free(temp); 162 | return count; 163 | } 164 | 165 | bool is_empty() { 166 | return size() == 0; 167 | } 168 | 169 | void traverse() { 170 | ListNode* temp = HEAD; 171 | while (temp != NULL) { 172 | printf("%d ", temp->data); 173 | temp = temp->next; 174 | } 175 | printf("\n"); 176 | free(temp); 177 | } 178 | -------------------------------------------------------------------------------- /datastructures/stack-linked-list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef struct Node { 6 | int data; 7 | struct Node *next; 8 | } Node; 9 | 10 | Node *HEAD = nullptr; 11 | 12 | void push(int data); 13 | void pop(); 14 | int peek(); 15 | int size(); 16 | bool isEmpty(); 17 | 18 | int main() { 19 | push(1); 20 | push(2); 21 | push(3); 22 | push(4); 23 | push(5); 24 | pop(); 25 | pop(); 26 | cout << "Peek: " << peek() << endl; 27 | cout << "Size: " << size() << endl; 28 | cout << "Empty: " << isEmpty() << endl; 29 | } 30 | 31 | void push(int data) { 32 | Node *node = (Node*) malloc(sizeof(Node)); 33 | node->data = data; 34 | node->next = HEAD; 35 | HEAD = node; 36 | } 37 | 38 | void pop() { 39 | HEAD = HEAD->next; 40 | } 41 | 42 | int peek() { 43 | return HEAD->data; 44 | } 45 | 46 | int size() { 47 | Node *temp = HEAD; 48 | int count = 0; 49 | while (temp != nullptr) { 50 | count++; 51 | temp = temp->next; 52 | } 53 | return count; 54 | } 55 | 56 | bool isEmpty() { 57 | return HEAD == nullptr; 58 | } -------------------------------------------------------------------------------- /datastructures/strings.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Mubashir R. Razv on 7/22/2020. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /datastructures/vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void display(vector& v) { 8 | // print al the elements in vector 9 | cout << "Elements: "; 10 | for (int num: v) 11 | cout << num << " "; 12 | cout << endl; 13 | } 14 | 15 | int main() { 16 | // dynamic arrays 17 | vector v(20, 0); // constructor with 20 elements space, init with 0 18 | v.reserve(20); // same as above to start with 20 space 19 | v = {1, 2}; 20 | v.push_back(4); // inset element at start 21 | v.push_back(3); 22 | v.push_back(6); 23 | v.push_back(5); 24 | 25 | cout << "Size: " << v.size() << endl; // size of vector 26 | cout << "Capacity: " << v.capacity() << endl; // total allocate size 27 | cout << "Max Elements: " << v.max_size() << endl; // maximum numbers that vector can hold 28 | cout << "Last Element: " << v.back() << endl; // return last element 29 | cout << "First Element: " << v.front() << endl; // return first element 30 | cout << "Specific Value: " << v.at(2) << endl; // get value from specific index 31 | cout << "Specific Value: " << v[2] << endl; // get value from specific index 32 | cout << "Check Empty: " << v.empty() << endl; // check empty or not 33 | v.insert(v.begin(), 10); // insert at start 34 | v.insert(v.end(), 11); // insert at back 35 | v.push_back(12); // insert at back 36 | cout << "Insert at start: " << v.front() << endl; 37 | cout << "Insert at start: " << v.back() << endl; 38 | v.pop_back(); // remove element from end 39 | v.erase(v.end() - 1, v.end()); 40 | v.erase(v.begin(), v.begin() + 1); // remove first element from vector 41 | 42 | sort(v.begin(), v.end()); // sort the elements in asc order 43 | display(v); 44 | sort(v.rbegin(), v.rend()); // sort the elements in desc order 45 | display(v); 46 | 47 | // swap, to swap two vectors 48 | return 0; 49 | } -------------------------------------------------------------------------------- /hackerrank/algorithms/apple-and-orange.cpp: -------------------------------------------------------------------------------- 1 | // Apple and Orange 2 | // https://www.hackerrank.com/challenges/apple-and-orange/problem 3 | void countApplesAndOranges(int s, int t, int a, int b, vector apples, vector oranges) { 4 | int left = 0, right = 0; 5 | for (auto num: apples) { 6 | if (a+num >= s && a+num <= t) 7 | left++; 8 | } 9 | for (auto num: oranges) { 10 | if (b+num >= s && b+num <= t) 11 | right++; 12 | } 13 | cout << left << endl; 14 | cout << right << endl; 15 | } -------------------------------------------------------------------------------- /hackerrank/algorithms/breaking-best-and-worst-records.cpp: -------------------------------------------------------------------------------- 1 | // Breaking the Records 2 | // https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem 3 | vector breakingRecords(vector scores) { 4 | int max = scores[0], min = scores[0]; 5 | int maxCount = 0, minCount = 0; 6 | for (int i=1; i max) { 8 | max = scores[i]; 9 | maxCount++; 10 | } 11 | 12 | if (scores[i] < min) { 13 | min = scores[i]; 14 | minCount++; 15 | } 16 | } 17 | vector v; 18 | v.push_back(maxCount); 19 | v.push_back(minCount); 20 | return v; 21 | } -------------------------------------------------------------------------------- /hackerrank/algorithms/grading.cpp: -------------------------------------------------------------------------------- 1 | // Grading Students 2 | // https://www.hackerrank.com/challenges/grading/problem 3 | vector gradingStudents(vector grades) { 4 | vector ans; 5 | for (int i=0; i arr) { 4 | vector minmax; 5 | for (int i=0; i 6 | #include 7 | #include 8 | 9 | typedef long long ll; 10 | using namespace std; 11 | 12 | int main() { 13 | freopen("output.txt", "w", stdout); 14 | 15 | int t; 16 | cin >> t; 17 | int tc = 1; 18 | while (t--) { 19 | int n, b; 20 | cin >> n >> b; 21 | vector a(n); 22 | for (auto &i: a) 23 | cin >> i; 24 | sort(a.begin(), a.end()); 25 | int count = 0; 26 | for (int i=0; i= a[i]) { 28 | b -= a[i]; 29 | count++; 30 | } 31 | } 32 | printf("Case #%d: %d\n", tc, count); 33 | tc++; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /leetcode/P1.cpp: -------------------------------------------------------------------------------- 1 | // 1. Two Sum 2 | // https://leetcode.com/problems/two-sum/ 3 | class Solution { 4 | public: 5 | // Loop - O(n^2) 6 | vector twoSum(vector& nums, int target) { 7 | vector v; 8 | for (int i=0; i nextLargerNodes(ListNode* head) { 6 | vector ans; 7 | ListNode *tempi = head; 8 | while (tempi != NULL) { 9 | ListNode *tempj = tempi->next; 10 | int max = tempi->val; 11 | while (tempj != NULL) { 12 | if (tempj->val > max) { 13 | max = tempj->val; 14 | break; 15 | } 16 | tempj = tempj->next; 17 | } 18 | if (max == tempi->val) 19 | ans.push_back(0); 20 | else 21 | ans.push_back(max); 22 | tempi = tempi->next; 23 | } 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /leetcode/P1207.cpp: -------------------------------------------------------------------------------- 1 | // 1207. Unique Number of Occurrences 2 | // https://leetcode.com/problems/unique-number-of-occurrences/ 3 | class Solution { 4 | public: 5 | bool uniqueOccurrences(vector& arr) { 6 | map mp; 7 | for (auto num: arr) { 8 | mp.insert({num, 0}); 9 | mp[num]++; 10 | } 11 | 12 | set s; 13 | for (auto num: mp) { 14 | s.insert(num.second); 15 | } 16 | 17 | if (s.size() != mp.size()) 18 | return false; 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /leetcode/P1252.cpp: -------------------------------------------------------------------------------- 1 | // 1252. Cells with Odd Values in a Matrix 2 | // https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/ 3 | class Solution { 4 | public: 5 | int oddCells(int n, int m, vector>& indices) { 6 | int arr[n][m]; 7 | for (int i=0; i& nums) { 6 | unordered_map ump; 7 | for (auto num: nums) { 8 | ump.insert({num, 0}); 9 | ump[num]++; 10 | } 11 | 12 | for (auto num: ump) { 13 | if (num.second == 1) 14 | return num.first; 15 | } 16 | return 0; 17 | } 18 | }; -------------------------------------------------------------------------------- /leetcode/P1403.cpp: -------------------------------------------------------------------------------- 1 | // 1403. Minimum Subsequence in Non-Increasing Order 2 | // https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/ 3 | 4 | class Solution { 5 | public: 6 | vector minSubsequence(vector& nums) { 7 | vector ans; 8 | sort(nums.begin(), nums.end()); 9 | int sum = 0; 10 | for (int i=0; i=0; i--) { 15 | temp += nums[i]; 16 | sum -= nums[i]; 17 | ans.push_back(nums[i]); 18 | if (temp > sum) 19 | break; 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /leetcode/P144.cpp: -------------------------------------------------------------------------------- 1 | // 144. Binary Tree Preorder Traversal 2 | // https://leetcode.com/problems/binary-tree-preorder-traversal/ 3 | class Solution { 4 | public: 5 | vector ans; 6 | vector preorderTraversal(TreeNode* root) { 7 | if (root) { 8 | ans.push_back(root->val); 9 | preorderTraversal(root->left); 10 | preorderTraversal(root->right); 11 | } 12 | 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /leetcode/P145.cpp: -------------------------------------------------------------------------------- 1 | // 145. Binary Tree Postorder Traversal 2 | // https://leetcode.com/problems/binary-tree-postorder-traversal/ 3 | class Solution { 4 | public: 5 | vector ans; 6 | vector postorderTraversal(TreeNode* root) { 7 | if (root) { 8 | postorderTraversal(root->left); 9 | postorderTraversal(root->right); 10 | ans.push_back(root->val); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /leetcode/P1450.cpp: -------------------------------------------------------------------------------- 1 | // 1450. Number of Students Doing Homework at a Given Time 2 | // https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/ 3 | class Solution { 4 | public: 5 | int busyStudent(vector& startTime, vector& endTime, int queryTime) { 6 | int students = 0; 7 | for (int i=0; i= queryTime) 9 | students++; 10 | } 11 | return students; 12 | } 13 | }; -------------------------------------------------------------------------------- /leetcode/P1460.cpp: -------------------------------------------------------------------------------- 1 | // 1460. Make Two Arrays Equal by Reversing Sub-arrays 2 | // https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/ 3 | class Solution { 4 | public: 5 | bool canBeEqual(vector& target, vector& arr) { 6 | sort(target.begin(), target.end()); 7 | sort(arr.begin(), arr.end()); 8 | for (int i=0; i finalPrices(vector& prices) { 6 | for (int i=0; i& nums) { 6 | int count = 0; 7 | for (int i=0; i twoSum(vector& numbers, int target) { 6 | vector v; 7 | int l = 0, r = numbers.size()-1; 8 | while (left < right) { 9 | int sum = numbers[l] + numbers[r]; 10 | if (sum == target) { 11 | v.push_back(l+1); 12 | v.push_back(r+1); 13 | return v; 14 | } else if (sum < target) 15 | l++; 16 | else 17 | r--; 18 | } 19 | return v; 20 | } 21 | }; -------------------------------------------------------------------------------- /leetcode/P189.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | list l; 5 | for (auto num: nums) 6 | l.push_back(num); 7 | 8 | for (int i=0; i v; 8 | while (temp != NULL) { 9 | if (temp->val != val) 10 | v.push_back(temp->val); 11 | temp = temp->next; 12 | } 13 | 14 | // special case 15 | if (v.size() == 0) { 16 | head = NULL; 17 | return head; 18 | } 19 | 20 | temp = head; 21 | for (int i=0; ival = v[i]; 23 | if (i==v.size() - 1) { 24 | temp->next = NULL; 25 | break; 26 | } 27 | temp = temp->next; 28 | } 29 | return head; 30 | } 31 | }; -------------------------------------------------------------------------------- /leetcode/P206.cpp: -------------------------------------------------------------------------------- 1 | // 206. Reverse Linked List 2 | // https://leetcode.com/problems/reverse-linked-list 3 | 4 | class Solution { 5 | public: 6 | ListNode* reverseList(ListNode* head) { 7 | vector v; 8 | ListNode *temp = head; 9 | while (temp != NULL) { 10 | v.push_back(temp->val); 11 | temp = temp->next; 12 | } 13 | 14 | temp = head; 15 | int index = v.size() - 1; 16 | while (temp != NULL) { 17 | temp->val = v[index--]; 18 | temp = temp->next; 19 | } 20 | return head; 21 | } 22 | }; -------------------------------------------------------------------------------- /leetcode/P217.cpp: -------------------------------------------------------------------------------- 1 | // 217. Contains Duplicate 2 | // https://leetcode.com/problems/contains-duplicate/ 3 | class Solution { 4 | public: 5 | bool containsDuplicate(vector& nums) { 6 | unordered_set us; 7 | for (auto num: nums) { 8 | if (us.count(num) > 0) 9 | return true; 10 | us.insert(num); 11 | } 12 | return false; 13 | } 14 | }; -------------------------------------------------------------------------------- /leetcode/P234.cpp: -------------------------------------------------------------------------------- 1 | // 234. Palindrome Linked List 2 | // https://leetcode.com/problems/palindrome-linked-list/ 3 | class Solution { 4 | public: 5 | bool isPalindrome(ListNode* head) { 6 | vector v; 7 | ListNode *temp = head; 8 | while (temp != NULL) { 9 | v.push_back(temp->val); 10 | temp = temp->next; 11 | } 12 | 13 | temp = head; 14 | for (int i=v.size() - 1; i>=0; i--) { 15 | if (temp->val != v[i]) { 16 | return false; 17 | } 18 | temp = temp->next; 19 | } 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /leetcode/P287.cpp: -------------------------------------------------------------------------------- 1 | // 287. Find the Duplicate Number 2 | // https://leetcode.com/problems/find-the-duplicate-number/ 3 | class Solution { 4 | public: 5 | int findDuplicate(vector& nums) { 6 | sort(nums.begin(), nums.end()); 7 | for (int i=0; i v, int value) { 2 | auto it = find(v.begin(), v.end(), value); 3 | return distance(v.begin(), it); 4 | } 5 | 6 | class Solution { 7 | public: 8 | // iterative approach 9 | vector nextGreaterElement(vector& nums1, vector& nums2) { 10 | vector v; 11 | 12 | int flag, index; 13 | for (int i=0; i nums1[i]) { 18 | v.push_back(nums2[j]); 19 | flag = 1; 20 | break; 21 | } 22 | } 23 | if (!flag) 24 | v.push_back(-1); 25 | } 26 | return v; 27 | } 28 | }; -------------------------------------------------------------------------------- /leetcode/P520.cpp: -------------------------------------------------------------------------------- 1 | // 520. Detect Capital 2 | // https://leetcode.com/problems/detect-capital/ 3 | class Solution { 4 | public: 5 | bool detectCapitalUse(string word) { 6 | if (isupper(word[0]) && isupper(word[1])) { 7 | for (int i=2; i& nums, int target) { 7 | if (nums.size() == 0) 8 | return -1; 9 | 10 | int L=0, R=nums.size()-1; 11 | while (L <= R) { 12 | int M = L+(R-L)/2; 13 | if (nums[M] == target) 14 | return M; 15 | if (nums[M] > target) 16 | R = M-1; 17 | else 18 | L = M+1; 19 | } 20 | return -1; 21 | } 22 | }; -------------------------------------------------------------------------------- /leetcode/P705.cpp: -------------------------------------------------------------------------------- 1 | // 705. Design HashSet 2 | // https://leetcode.com/problems/design-hashset/ 3 | class MyHashSet { 4 | private: 5 | unordered_set us; 6 | public: 7 | void add(int key) { 8 | us.insert(key); 9 | } 10 | 11 | void remove(int key) { 12 | us.erase(key); 13 | } 14 | 15 | bool contains(int key) { 16 | return us.count(key) >= 1; 17 | } 18 | }; -------------------------------------------------------------------------------- /leetcode/P706.cpp: -------------------------------------------------------------------------------- 1 | // 706. Design HashMap 2 | // https://leetcode.com/problems/design-hashmap/ 3 | class MyHashMap { 4 | private: 5 | int map[1000000]; 6 | public: 7 | MyHashMap() { 8 | fill_n(map, 1000000, -1); 9 | } 10 | 11 | void put(int key, int value) { 12 | this->map[key] = value; 13 | } 14 | 15 | int get(int key) { 16 | return this->map[key]; 17 | } 18 | 19 | void remove(int key) { 20 | this->map[key] = -1; 21 | } 22 | }; -------------------------------------------------------------------------------- /leetcode/P75.cpp: -------------------------------------------------------------------------------- 1 | // 75. Sort Colors 2 | // https://leetcode.com/problems/sort-colors/ 3 | class Solution { 4 | public: 5 | void sortColors(vector& nums) { 6 | insertionSort(nums); 7 | } 8 | 9 | void insertionSort(vector& nums) { 10 | int i, j, k; 11 | for (int i=1; i= 0 && nums[j] > k) { 15 | nums[j+1] = nums[j]; 16 | j--; 17 | } 18 | nums[j+1] = k; 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /leetcode/P78.cpp: -------------------------------------------------------------------------------- 1 | // 78. Subsets 2 | // https://leetcode.com/problems/subsets/ 3 | class Solution { 4 | public: 5 | vector> subsets(vector& nums) { 6 | vector> subsets; 7 | int n = nums.size(); 8 | for (int i=0; i<(1< sub; 10 | for (int j=0; j& words) { 6 | vector v = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; 7 | map mp; 8 | char ch = 'a'; 9 | for (auto pattern: v) { 10 | mp[ch] = pattern; 11 | ch++; 12 | } 13 | 14 | set s; 15 | for (int i=0; i> flipAndInvertImage(vector>& A) { 6 | vector> v(A.size(), vector(A.size(), 0)); 7 | for (int i=0; i=0; j--) { 10 | v[i][index++] = abs(A[i][j] - 1); 11 | } 12 | } 13 | return v; 14 | } 15 | }; -------------------------------------------------------------------------------- /leetcode/P90.cpp: -------------------------------------------------------------------------------- 1 | // 90. Subsets II 2 | // https://leetcode.com/problems/subsets-ii/ 3 | class Solution { 4 | public: 5 | vector> subsetsWithDup(vector& nums) { 6 | vector> subsets; 7 | int n = nums.size(); 8 | for (int i=0; i<(1< sub; 10 | for (int j=0; jval >= L && root->val <= R) 9 | ans += root->val; 10 | rangeSumBST(root->left, L, R); 11 | rangeSumBST(root->right, L, R); 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /leetcode/P94.cpp: -------------------------------------------------------------------------------- 1 | // 94. Binary Tree Inorder Traversal 2 | // https://leetcode.com/problems/binary-tree-inorder-traversal/ 3 | class Solution { 4 | public: 5 | vector ans; 6 | vector inorderTraversal(TreeNode* root) { 7 | if (root) { 8 | inorderTraversal(root->left); 9 | ans.push_back(root->val); 10 | inorderTraversal(root->right); 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /leetcode/P961.cpp: -------------------------------------------------------------------------------- 1 | // N-Repeated Element in Size 2N Array 2 | // https://leetcode.com/problems/n-repeated-element-in-size-2n-array/ 3 | class Solution { 4 | public: 5 | int repeatedNTimes(vector& A) { 6 | map mp; 7 | for (auto num: A) { 8 | mp.insert({num, 0}); 9 | mp[num]++; 10 | } 11 | 12 | int maxLen, maxEle; 13 | maxLen = maxEle = 0; 14 | for (auto num: mp) { 15 | if (num.second > maxLen) { 16 | maxLen = num.second; 17 | maxEle = num.first; 18 | } 19 | } 20 | return maxEle; 21 | } 22 | }; -------------------------------------------------------------------------------- /onlinejudge/P36.cpp: -------------------------------------------------------------------------------- 1 | // 100 - The 3n + 1 problem 2 | // https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36 3 | #include 4 | #include 5 | #include 6 | 7 | typedef long long ll; 8 | using namespace std; 9 | 10 | ll countCyclicLength(ll num) { 11 | ll count = 1; 12 | while (num != 1) { 13 | if (num%2 == 0) num /= 2; 14 | else num = num*3+1; 15 | count++; 16 | } 17 | return count; 18 | } 19 | 20 | int main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | cout.tie(nullptr); 24 | 25 | #ifndef ONLINE_JUDGE 26 | freopen("input.txt", "r", stdin); 27 | freopen("output.txt", "w", stdout); 28 | #endif 29 | 30 | ll i, j; 31 | while (cin >> i >> j) { 32 | vector v; 33 | if (i < j) { 34 | for (ll k=i; k<=j; k++) { 35 | v.push_back(countCyclicLength(k)); 36 | } 37 | } else { 38 | for (ll k=i; k>=j; k--) { 39 | v.push_back(countCyclicLength(k)); 40 | } 41 | } 42 | sort(v.rbegin(), v.rend()); 43 | cout << i << " " << j << " " << v[0] << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /onlinejudge/mathematics/adhoc/P1012.cpp: -------------------------------------------------------------------------------- 1 | // 10071 - Back to High School Physics 2 | // https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1012 3 | 4 | #include 5 | #include 6 | 7 | typedef int64_t ll; 8 | using namespace std; 9 | 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | int v, t; 14 | while (cin >> v >> t) { 15 | cout << 2*v*t << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /onlinejudge/mathematics/adhoc/P996.cpp: -------------------------------------------------------------------------------- 1 | // 10055 - Hashmat the Brave Warrior 2 | // https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=996 3 | 4 | #include 5 | #include 6 | 7 | typedef int64_t ll; 8 | using namespace std; 9 | 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | ll a, b; 14 | while (cin >> a >> b) { 15 | cout << abs(b - a) << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /onlinejudge/mathematics/prime-numbers/P543.cpp: -------------------------------------------------------------------------------- 1 | // 543 - Goldbach's Conjecture 2 | // https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=484 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | vector nums; 12 | 13 | void sieve(ll num) { 14 | nums.assign(num+1, true); 15 | nums[0] = nums[1] = false; 16 | for (ll i=2; i*i<=num; i++) { 17 | if (!nums[i]) 18 | continue; 19 | for (ll j=i*i; j<=num; j+=i) 20 | nums[j] = false; 21 | } 22 | } 23 | 24 | int main() { 25 | freopen("output.txt", "w", stdout); 26 | 27 | ios::sync_with_stdio(false); 28 | cin.tie(nullptr); 29 | cout.tie(nullptr); 30 | sieve(1e6); 31 | ll n; 32 | while (cin >> n) { 33 | if (n == 0) 34 | break; 35 | 36 | for (ll i=3; i 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() { 10 | freopen("output.txt", "w", stdout); 11 | 12 | vector v = {3, 4, -7, 3, 1, 3, 1, -4, -2, -2}; 13 | 14 | bool answer = false; 15 | for (int i=0; i 4 | #include 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | pair findPair(vector v, int sum) { 10 | pair answer; 11 | for (int i=0; i testcase1 = findPair({8, 7, 2, 5, 3, 1}, 10); 26 | cout << testcase1.first << "+" << testcase1.second << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /problems/backtracking/maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef long long ll; 7 | typedef long long int lli; 8 | using namespace std; 9 | 10 | const int n = 3; 11 | bool path[n][n] = {false}; 12 | 13 | bool find_path(int matrix[n][n], int x, int y) { 14 | if (x < 0 || x > n || y < 0 || y > n) 15 | return false; 16 | 17 | if (x == n-1 && y == n-1) // reach the point 18 | return true; 19 | 20 | if (matrix[x][y] == 0 || path[x][y]) 21 | return false; 22 | 23 | // right 24 | path[x][y] = true; 25 | if (find_path(matrix, x, y+1)) { 26 | path[x][y] = false; 27 | return true; // right 28 | } 29 | if (find_path(matrix, x, y-1)) { 30 | path[x][y] = false; 31 | return true; // left 32 | } 33 | if (find_path(matrix, x+1, y)) { 34 | path[x][y] = false; 35 | return true; // bottom 36 | } 37 | if (find_path(matrix, x-1, y)) { 38 | path[x][y] = false; 39 | return true; // top 40 | } 41 | 42 | return false; 43 | } 44 | 45 | int main() { 46 | freopen("input.txt", "r", stdin); 47 | freopen("output.txt", "w", stdout); 48 | 49 | int matrix[n][n]; 50 | for (auto &i: matrix) { 51 | for (int &j: i) { 52 | cin >> j; 53 | } 54 | } 55 | 56 | cout << find_path(matrix, 0, 0) << endl; 57 | 58 | for (auto &i: path) { 59 | for (auto j: i) { 60 | cout << j << " "; 61 | } 62 | cout << endl; 63 | } 64 | cout << endl; 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /problems/bitwise/detect-if-two-integers-have-opposite-signs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool detect_opposite_signs(int num1, int num2) { 6 | return ((num1 ^ num2) < 0); 7 | } 8 | 9 | int main() { 10 | cout << detect_opposite_signs(-1, -1) << endl; 11 | cout << detect_opposite_signs(-1, 1) << endl; 12 | cout << detect_opposite_signs(1, 1) << endl; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /problems/bitwise/find-the-element-that-appears-once.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MRRazvi/competitive-programming/28da03e598df3f4c37bdbd06fc2ba256daf1cb69/problems/bitwise/find-the-element-that-appears-once.cpp -------------------------------------------------------------------------------- /problems/dynamic-programming/knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef long long ll; 5 | typedef long long int lli; 6 | using namespace std; 7 | 8 | int knapsack(vector w, vector v, int weight, int index) { 9 | if (weight == 0 || index == 0) 10 | return 0; 11 | 12 | if (w[index-1] > weight) 13 | return knapsack(w, v, weight, index-1); 14 | return max(v[index-1] + knapsack(w, v, weight-w[index-1], index-1), knapsack(w, v, weight, index-1)); 15 | } 16 | 17 | int main() { 18 | freopen("input.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | 21 | int t; 22 | cin >> t; 23 | while (t--) { 24 | int n, weight; 25 | cin >> n >> weight; 26 | vector w(n); 27 | vector v(n); 28 | 29 | for (auto &i: v) 30 | cin >> i; 31 | 32 | for (auto &i: w) 33 | cin >> i; 34 | 35 | cout << knapsack(w, v, weight, n) << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /problems/graphs/so-np.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Mubashir R. Razv on 8/6/2020. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /problems/recursion/factorial.cpp: -------------------------------------------------------------------------------- 1 | // Dynamic Programming for Coding Interviews 2 | // Question 1.1 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | /** 12 | * F(n) = n * F(n) if n>1 13 | * = 1 if n<1 14 | */ 15 | ll factorial(ll num) { 16 | if (num == 1 || num == 0) 17 | return 1; 18 | return factorial(num - 1) * num; 19 | } 20 | 21 | int main() { 22 | freopen("output.txt", "w", stdout); 23 | 24 | cout << factorial(5) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /problems/recursion/prefix-sum.cpp: -------------------------------------------------------------------------------- 1 | // Dynamic Programming for Coding Interviews 2 | // Question 1.2 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef long long ll; 9 | using namespace std; 10 | 11 | void prefix_sum(vector& arr, int start, int end) { 12 | if (start == end) 13 | return; 14 | 15 | arr[start] = arr[start] + arr[start-1]; 16 | prefix_sum(arr, start+1, end); 17 | } 18 | 19 | int main() { 20 | freopen("output.txt", "w", stdout); 21 | 22 | vector arr = {1, 2, 3, 4, 5, 6}; 23 | prefix_sum(arr, 1, arr.size()); 24 | 25 | for (auto n: arr) 26 | cout << n << " "; 27 | cout << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /spoj/ADAGCD.cpp: -------------------------------------------------------------------------------- 1 | // ADAGCD - Ada and GCD 2 | // https://www.spoj.com/problems/ADAGCD/ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | int t; 14 | cin >> t; 15 | 16 | vector nums; 17 | while (t--) { 18 | int n; 19 | cin >> n; 20 | vector m(n); 21 | for (auto &i: m) 22 | cin >> i; 23 | 24 | ll num = 1; 25 | for (ll i=0; i 2 | #include 3 | using namespace std; 4 | 5 | // https://www.spoj.com/problems/TDPRIMES/ 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | cout.tie(NULL); 10 | int n = 100000000; 11 | vector v(n+1, true); 12 | v[0] = v[1] = false; 13 | for (int i=2; (long long)i*i 5 | #include 6 | #include 7 | 8 | typedef int64_t ll; 9 | using namespace std; 10 | int main() { 11 | freopen("output.txt", "w", stdout); 12 | 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | int n1, n2; 17 | cin >> n1 >> n2; 18 | string num1, num2; 19 | num1 = to_string(n1); 20 | num2 = to_string(n2); 21 | reverse(num1.begin(), num1.end()); 22 | reverse(num2.begin(), num2.end()); 23 | n1 = stoi(num1); 24 | n2 = stoi(num2); 25 | string sum = to_string(n1+n2); 26 | reverse(sum.begin(), sum.end()); 27 | bool flag = true; 28 | for (int i=0; i 2 | 3 | typedef long long ll; 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | return 0; 9 | } --------------------------------------------------------------------------------