├── CODE_OF_CONDUCT.md ├── LICENSE ├── Mega_Contest_1 ├── Editorials │ ├── Problem-1_Sell-Candies │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ ├── Problem-2_Again-Silly-Mistake │ │ ├── solution.cpp │ │ └── solution2.cpp │ ├── Problem-3_Maximum-Sum │ │ ├── solution.cpp │ │ ├── solution.py │ │ ├── solution2.cpp │ │ └── solution2.py │ └── Problem-4_DJ-WALE-BABU │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp └── README.md ├── Mega_Contest_2 ├── Editorials │ ├── problem_1 │ │ ├── solution.cpp │ │ └── solution2.cpp │ ├── problem_2 │ │ ├── solution.cpp │ │ └── solution2.cpp │ ├── problem_3 │ │ ├── solution.cpp │ │ └── solution2.cpp │ ├── problem_4 │ │ ├── solution.cpp │ │ └── solution2.cpp │ └── problem_5 │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp └── README.md ├── Mega_Contest_3 ├── Editorials │ ├── Problem_1 │ │ └── solution_1.cpp │ ├── Problem_2 │ │ └── solution_1.cpp │ ├── Problem_3 │ │ └── solution_1.cpp │ ├── Problem_4 │ │ └── solution_1.cpp │ └── Problem_5 │ │ └── solution_1.py └── readme.md ├── Mega_Contest_4 ├── Editorials │ ├── Problem-1_Print-the-Char │ │ ├── solution.cpp │ │ └── solution1.cpp │ ├── Problem_1 │ │ └── solution.cpp │ ├── Problem_2 │ │ └── solution.cpp │ ├── Problem_3 │ │ └── solution.cpp │ ├── Problem_4 │ │ └── solution.cpp │ └── Problem_5 │ │ └── solution.cpp └── README.md ├── README.md ├── Week-1 ├── Editorials │ ├── Contest_1 │ │ ├── problem-1 │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ ├── solution.py │ │ │ └── solution2.cpp │ │ ├── problem-2 │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ ├── solution.py │ │ │ ├── solution2.cpp │ │ │ └── solution3.cpp │ │ ├── problem-3 │ │ │ ├── Unique Number 3.py │ │ │ ├── solution-1.cpp │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ ├── solution.py │ │ │ ├── solution2.cpp │ │ │ └── solution_countsort.cpp │ │ ├── problem-4 │ │ │ ├── O(n)_solution.cpp │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ ├── solution.py │ │ │ └── solution2.cpp │ │ └── problem-5 │ │ │ ├── solution.cpp │ │ │ ├── solution.java │ │ │ ├── solution.py │ │ │ ├── solution2.cpp │ │ │ ├── solution_usingMod.cpp │ │ │ └── using-array.cpp │ └── Contest_2 │ │ ├── problem-1 │ │ ├── soln.java │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ ├── problem-2 │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ ├── problem-3 │ │ ├── solution.cpp │ │ ├── solution.py │ │ ├── solution1.java │ │ └── solution2-vector.cpp │ │ ├── problem-4 │ │ ├── solnsortstr.java │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ ├── problem-5 │ │ ├── soln.java │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ └── problem-6 │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp └── README.md ├── Week-2 ├── Editorials │ ├── Contest_1 │ │ ├── problem-1 │ │ │ ├── solution.cpp │ │ │ ├── solution.py │ │ │ └── solution2.cpp │ │ ├── problem-2 │ │ │ ├── O(log n)_solution.cpp │ │ │ ├── solution.cpp │ │ │ ├── solution.py │ │ │ └── solution2.cpp │ │ ├── problem-3 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ ├── problem-4 │ │ │ ├── solution.cpp │ │ │ ├── solution.py │ │ │ └── solution2.cpp │ │ └── problem-5 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ └── Contest_2 │ │ ├── problem-1 │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ ├── problem-2 │ │ ├── solution.cpp │ │ ├── solution.py │ │ └── solution2.cpp │ │ ├── problem-3 │ │ ├── solution.cpp │ │ └── solution2.cpp │ │ └── problem-4 │ │ ├── solution.cpp │ │ └── solution2.cpp └── README.md ├── Week-3 ├── Editorials │ ├── Contest_1 │ │ ├── problem-1 │ │ │ └── Solution.py │ │ ├── problem-2 │ │ │ └── Solution.py │ │ ├── problem-3 │ │ │ └── Solution.py │ │ ├── problem-4 │ │ │ └── Solution.py │ │ ├── problem-5 │ │ │ └── Solutions.py │ │ └── problem-6 │ │ │ └── Solutions.py │ ├── PracticeContest_1 │ │ ├── Problem-1 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ ├── Problem-2 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ ├── Problem-3 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ ├── Problem-4 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ ├── Problem-5 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ │ └── Problem-6 │ │ │ ├── solution.cpp │ │ │ └── solution2.cpp │ └── PracticeContest_2 │ │ ├── Problem_1.cpp │ │ ├── Problem_1_solution2.cpp │ │ ├── Problem_2.cpp │ │ ├── Problem_2_solution2.cpp │ │ ├── Problem_3.cpp │ │ ├── Problem_3_solution2.cpp │ │ ├── Problem_4.cpp │ │ ├── Problem_4_solution2.cpp │ │ ├── Problem_5.cpp │ │ ├── Problem_5_solution2.cpp │ │ ├── Problem_6.cpp │ │ ├── Problem_6_solution2.cpp │ │ ├── Problem_7.cpp │ │ ├── Problem_7_solution2.cpp │ │ ├── Problem_8.cpp │ │ ├── Problem_8_solution2.cpp │ │ ├── Problem_9.cpp │ │ └── Problem_9_solution2.cpp └── README.md ├── Week-4 ├── Editorials │ └── Contest_1 │ │ ├── problem_1.cpp │ │ ├── problem_1_solution2.cpp │ │ ├── problem_2.cpp │ │ ├── problem_2_solution2.cpp │ │ ├── problem_3.cpp │ │ ├── problem_3_solution2.cpp │ │ ├── problem_4.cpp │ │ ├── problem_4_solution2.cpp │ │ ├── problem_5.cpp │ │ └── problem_5_solution2.cpp └── readme.md ├── Week-5 ├── Editorials │ └── Contest-1 │ │ ├── problem-1.cpp │ │ ├── problem-2.cpp │ │ ├── problem-3.cpp │ │ ├── problem-4.cpp │ │ ├── problem-5.cpp │ │ └── problem-6.cpp └── readme.md ├── Week-6 ├── Editorials │ └── Contest_1 │ │ ├── Problem-1 │ │ ├── solution.cpp │ │ └── solution_1.py │ │ ├── Problem-2 │ │ ├── solution.cpp │ │ └── solution_1.py │ │ ├── Problem-3 │ │ ├── solution.cpp │ │ └── solution_1.py │ │ ├── Problem-4 │ │ ├── solution.cpp │ │ └── solution_1.py │ │ └── Problem-5 │ │ ├── solution.cpp │ │ └── solution_1.py └── readme.md ├── Week-7 └── readme.md ├── Week-8 └── readme.md └── Week-Template.md /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at brainlesscodersofficial@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Brainless-Coders 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-1_Sell-Candies/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define ll long long int 5 | using namespace std; 6 | int main(){ 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | ll n; 11 | cin >> n; 12 | vector v; 13 | for(ll i = 0; i < n; i++){ 14 | ll a; 15 | cin >> a; 16 | v.push_back(a); 17 | } 18 | sort(v.begin(),v.end(),greater()); 19 | ll profit = 0; 20 | ll zero = 0; 21 | for(ll i = 0; i < n; i++){ 22 | v[i] = v[i] - i; 23 | profit = profit + max(v[i],zero); 24 | } 25 | cout << profit << endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-1_Sell-Candies/solution.py: -------------------------------------------------------------------------------- 1 | #! bin/bash/python3 2 | 3 | # Solution to Mega Contest 1 Problem: Sell Candies 4 | 5 | for testcase in range(int(input())): 6 | net_revenue = 0 7 | n = int(input()) 8 | vals = list(map(int, input().split())) 9 | vals.sort(reverse=True) 10 | cost_reduction = 0 11 | for val in vals: 12 | net_revenue += max(val-cost_reduction, 0) 13 | net_revenue %= int(1e9+7) 14 | cost_reduction += 1 15 | print(net_revenue) -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-1_Sell-Candies/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | int n; 15 | cin >> n; 16 | int arr[n]; 17 | for(int i=0;i> arr[i]; 19 | } 20 | sort(arr, arr+n, greater()); 21 | int sum = 0; 22 | int yr = 0; 23 | int temp = 0; 24 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | int totalUpper = 1; 8 | for(int i = 0; i < s.length(); i++){ 9 | if(!isupper(s[i])){ 10 | totalUpper = 0; 11 | break; 12 | } 13 | } 14 | if(totalUpper){ 15 | transform(s.begin(),s.end(),s.begin(), ::tolower); 16 | cout << s; 17 | }else{ 18 | if(islower(s[0])){ 19 | int upper = 1; 20 | for(int i = 1; i < s.length(); i++){ 21 | if(!isupper(s[i])){ 22 | upper = 0; 23 | break; 24 | } 25 | } 26 | if(upper){ 27 | string a = s.substr(1); 28 | s[0] = toupper(s[0]); 29 | transform(a.begin(),a.end(), a.begin(), ::tolower); 30 | cout << s[0]; 31 | cout << a; 32 | }else{ 33 | cout << s; 34 | } 35 | }else{ 36 | cout << s; 37 | } 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-2_Again-Silly-Mistake/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | string input; 12 | cin >> input; 13 | int n = input.size(); 14 | int ct = 0; 15 | int first_upper = 0; 16 | 17 | for(int i=0; i 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin >> t; 6 | while(t--){ 7 | int n,k; 8 | cin >> n >> k; 9 | int a[n],b[n]; 10 | for(int i = 0; i < n; i++){ 11 | cin >> a[i]; 12 | } 13 | for(int i = 0; i < n; i++){ 14 | cin >> b[i]; 15 | } 16 | sort(a,a+n); 17 | sort(b,b+n); 18 | int i = 0, j = n-1; 19 | while((k > 0) && (a[i] < b[j])){ 20 | a[i] = b[j]; 21 | i = i + 1; 22 | j = j - 1; 23 | k = k - 1; 24 | } 25 | long long int s = 0; 26 | for(int i = 0; i < n; i++){ 27 | s = s + a[i]; 28 | } 29 | cout << s << endl; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-3_Maximum-Sum/solution.py: -------------------------------------------------------------------------------- 1 | # Solution to Mega Contest 1 Problem: Maximum Sum 2 | 3 | for testcase in range(int(input())): 4 | n, k = map(int, input().split()) 5 | array_a = list(map(int, input().split())) 6 | array_b = list(map(int, input().split())) 7 | if k>0: 8 | array_a.sort() 9 | array_b.sort() 10 | # max_sum = sum(array_a[k:]+array_b[n-k:]) 11 | values = array_a[k:] 12 | for val in range(k): 13 | values.append(max(array_a[val], array_b[-val-1])) 14 | max_sum = sum(values) 15 | else: max_sum = sum(array_a) 16 | print(max_sum) -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-3_Maximum-Sum/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | int n, k; 15 | cin >> n >> k; 16 | int a[n], b[n]; 17 | for(int i=0;i> a[i]; 19 | for(int i=0;i> b[i]; 21 | sort(a, a+n); 22 | sort(b, b+n); 23 | int i = 0; 24 | int sum = 0; 25 | if(b[n-1] <= a[0]) { 26 | for(i=0;i a[i]) { 33 | a[i] = b[n-i-1]; 34 | } 35 | else { 36 | break; 37 | } 38 | } 39 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | string s; 5 | cin >> s; 6 | string k = ""; 7 | for(int i = 0; i < s.length(); i++){ 8 | string a = ""; 9 | a = a + s[i] + s[i+1] + s[i+2]; 10 | if(a == "WUB"){ 11 | // cout << "in" << endl; 12 | i = i + 2; 13 | }else{ 14 | for(int j = i; j < s.length(); j++){ 15 | string x = ""; 16 | x = x + s[j] + s[j+1] + s[j+2]; 17 | if(x == "WUB"){ 18 | i = i + 2; 19 | break; 20 | }else{ 21 | k = k + s[j]; 22 | i = i + 1; 23 | } 24 | } 25 | k = k + " "; 26 | } 27 | } 28 | cout << k; 29 | } 30 | -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-4_DJ-WALE-BABU/solution.py: -------------------------------------------------------------------------------- 1 | # Solution to Mega Contest 1 Problem: DJ WALE BABU 2 | 3 | string = input() 4 | while "WUBWUB" in string: string = string.replace("WUBWUB", "WUB") 5 | string = string.replace("WUB", " ") 6 | print(string.strip()) -------------------------------------------------------------------------------- /Mega_Contest_1/Editorials/Problem-4_DJ-WALE-BABU/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | string str; 13 | cin >> str; 14 | 15 | string s1 ="WUB"; 16 | 17 | size_t ind = string::npos; 18 | while((ind =str.find(s1)) != string::npos) { 19 | str.erase(ind, s1.length()); 20 | if(str.substr(ind, 3) != "WUB") 21 | str.insert(ind," "); 22 | } 23 | size_t beg = str.find_first_not_of(" \t"); 24 | str.erase(0, beg); 25 | 26 | beg = str.find_last_not_of(" \t"); 27 | if (string::npos != beg) 28 | str.erase(beg+1); 29 | 30 | cout << str; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Mega_Contest_1/README.md: -------------------------------------------------------------------------------- 1 | ### Mega Contest 1 2 | 3 | 4 | * Contest Link: https://www.hackerrank.com/contests/cpbuddyseries-contest1/challenges/ 5 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_1/solution.cpp: -------------------------------------------------------------------------------- 1 | // This code was taken from GeeksforGeeks 2 | // Link for the detailed editorial : https://www.geeksforgeeks.org/find-ways-integer-can-expressed-sum-n-th-power-unique-natural-numbers/ 3 | 4 | // C++ program to find number of ways to express 5 | // a number as sum of n-th powers of numbers. 6 | #include 7 | using namespace std; 8 | 9 | int res = 0; 10 | int checkRecursive(int num, int x, int k, int n) 11 | { 12 | if (x == 0) 13 | res++; 14 | 15 | int r = (int)floor(pow(num, 1.0 / n)); 16 | 17 | for (int i = k + 1; i <= r; i++) 18 | { 19 | int a = x - (int)pow(i, n); 20 | if (a >= 0) 21 | checkRecursive(num, x - 22 | (int)pow(i, n), i, n); 23 | } 24 | return res; 25 | } 26 | 27 | // Wrapper over checkRecursive() 28 | int check(int x, int n) 29 | { 30 | return checkRecursive(x, x, 0, n); 31 | } 32 | 33 | // Driver Code 34 | int main() 35 | { 36 | cout << (check(10, 2)); 37 | return 0; 38 | } 39 | 40 | // This code is contributed by mits 41 | 42 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_1/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int helper(int x, int n, int number); 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | int x, n; 15 | cin >> x; 16 | cin >> n; 17 | cout << helper(x, n, 1) << endl; 18 | } 19 | return 0; 20 | } 21 | 22 | int helper(int x, int n, int number) { 23 | int curr = x - pow(number, n); 24 | if(curr < 0) 25 | return 0; 26 | else if(curr==0) 27 | return 1; 28 | else { 29 | return helper(curr, n, number+1) + helper(x, n, number+1); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_2/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | #define mp make_pair 5 | #define pb push_back 6 | 7 | bool comp(ll a, ll b) { 8 | return a>b; 9 | } 10 | 11 | void solve(){ 12 | // Solve here 13 | string s; 14 | cin>>s; 15 | ll l = s.size(); 16 | ll t = 0; 17 | vector v; 18 | for(ll i=0;i0) v.pb(t); 27 | t = 0; 28 | } 29 | 30 | sort(v.begin(), v.end(), comp); 31 | ll ans = 0; 32 | for(int i=0;i>t; 45 | while(t--){ 46 | solve(); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_2/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | string s; 15 | cin >> s; 16 | s+='0'; 17 | int ctr = 0; 18 | vector v; 19 | for(int i=0;i= 0) { 32 | max_ones += v[max_index]; 33 | max_index -= 2; 34 | } 35 | cout << max_ones << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_3/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | while(t--){ 9 | stack a; 10 | string s; 11 | cin >> s; 12 | int c = 0; 13 | for(int i = 0; i < s.length(); i++){ 14 | if(s[i] == '<'){ 15 | a.push(s[i]); 16 | } 17 | else{ 18 | if(!a.empty() && a.top() == '<'){ 19 | a.pop(); 20 | c = c + 2; 21 | }else{ 22 | break; 23 | } 24 | } 25 | } 26 | s = s.substr(0,c); 27 | stack b; 28 | int d = 0; 29 | for(int i = 0; i < s.length(); i++){ 30 | if(s[i] == '<'){ 31 | b.push(s[i]); 32 | } 33 | else{ 34 | if(!b.empty() && b.top() == '<'){ 35 | b.pop(); 36 | d = d + 2; 37 | }else{ 38 | break; 39 | } 40 | } 41 | } 42 | if(c == d) 43 | cout << c << endl; 44 | else 45 | cout << "0" << endl; 46 | } 47 | return 0; 48 | } 49 | 50 | // Credits : Vanamala Srikanth 51 | 52 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_3/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | string s; 16 | int ctr = 0, val = 0; 17 | cin >> s; 18 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int t; 10 | cin >> t; 11 | while(t--) { 12 | string str; 13 | cin >> str; 14 | int ans = 0; 15 | for (int i = 0 ; i < str.length() - 1; i++) { 16 | if (str[i] == str[i + 1]) { 17 | // If two consecutive characters are the same, delete one of them. 18 | ans++; 19 | } 20 | } 21 | cout << ans << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_4/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the alternatingCharacters function below. 6 | int alternatingCharacters(string s) { 7 | int ctr = 0; 8 | for(int i=0;i> q; 21 | cin.ignore(numeric_limits::max(), '\n'); 22 | 23 | for (int q_itr = 0; q_itr < q; q_itr++) { 24 | string s; 25 | getline(cin, s); 26 | 27 | int result = alternatingCharacters(s); 28 | 29 | fout << result << "\n"; 30 | } 31 | 32 | fout.close(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_5/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--){ 14 | int n, m; 15 | cin >> n; 16 | cin >>m; 17 | int a[n], p[m]; 18 | for(int i=0;i> a[i]; 20 | } 21 | for(int i=0;i> p[i]; 23 | } 24 | sort(p, p+m); 25 | for(int i=0;i a[pi]: 8 | a[pi], a[pi-1] = a[pi-1], a[pi] 9 | print("YES" if sorted(a[:]) == a else "NO") 10 | 11 | # Credits : Aaditya Upadhyay 12 | -------------------------------------------------------------------------------- /Mega_Contest_2/Editorials/problem_5/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--){ 14 | int n, m; 15 | cin >> n; 16 | cin >>m; 17 | int a[n], p[m]; 18 | for(int i=0;i> a[i]; 20 | } 21 | for(int i=0;i> p[i]; 23 | } 24 | sort(p, p+m); 25 | for(int i=0;i 4 | #include 5 | #define ll long long int 6 | 7 | 8 | using namespace std; 9 | 10 | void prime_seive(int *p) { 11 | for(ll i=3;i<=1000000;i+=2) p[i]=1; 12 | 13 | p[2]=1; 14 | p[1]=p[0]=0; 15 | 16 | for(ll i=0;i<=1000000;i++) 17 | { 18 | if(p[i]==1) 19 | { 20 | for(ll j=i * i;j<=1000000;j+=i) 21 | { 22 | p[j]=0; 23 | } 24 | } 25 | } 26 | 27 | 28 | } 29 | 30 | int main(){ 31 | int T; 32 | cin>>T; 33 | int p[1000005] = {0}; 34 | prime_seive(p); 35 | int no_of_prime[1000000] = {0}; 36 | for(int i=1;i<=1000000;i++){ 37 | no_of_prime[i] = no_of_prime[i-1]+p[i]; 38 | } 39 | while(T--){ 40 | int a, b; 41 | cin>>a>>b; 42 | if(a!=0) cout< 4 | 5 | #define pb push_back 6 | #define mp make_pair 7 | #define fr first 8 | #define sc second 9 | #define MOD 1e9 + 7 10 | #define INF 1e9 + 7 11 | #define len(x) x.size() 12 | #define min(a, b) ((a) < (b) ? (a) : (b)) 13 | #define max(a, b) ((a) > (b) ? (a) : (b)) 14 | #define min3(a, b, c) min(a, min(b, c)) 15 | #define max3(a, b, c) max(a, max(b, c)) 16 | #define FOR(i, n) for (ll i = 0; i < n; i++) 17 | #define FORR(i, n) for (ll i = n - 1; i >= 0; i--) 18 | #define all(v) v.begin(), v.end() 19 | #define alla(a, n) a, a + n 20 | #define endl "\n"; 21 | #define fast \ 22 | ios_base::sync_with_stdio(false); \ 23 | cin.tie(NULL); \ 24 | cout.tie(NULL); 25 | 26 | using namespace std; 27 | 28 | typedef long long ll; 29 | typedef unsigned long long ull; 30 | typedef pair pll; 31 | typedef vector vll; 32 | typedef vector vi; 33 | typedef vector vpll; 34 | typedef vector vvll; 35 | typedef vector vs; 36 | typedef unordered_map ump; 37 | 38 | int par[3000]; 39 | vector gr[3000]; 40 | 41 | int a[2048]; 42 | 43 | int cnt(int v) 44 | { 45 | if (a[v] == -1) 46 | return 1; 47 | return 1 + cnt(a[v]); 48 | } 49 | 50 | int main() 51 | { 52 | int n; 53 | cin >> n; 54 | for (int i = 1; i <= n; i++) 55 | { 56 | cin >> a[i]; 57 | } 58 | int ans = 0; 59 | for (int i = 1; i <= n; i++) 60 | { 61 | ans = max(ans, cnt(i)); 62 | } 63 | cout << ans << endl; 64 | } 65 | -------------------------------------------------------------------------------- /Mega_Contest_3/Editorials/Problem_3/solution_1.cpp: -------------------------------------------------------------------------------- 1 | /* If the total number of occurrences of some character c is not a multiple of n, then it is impossible to make all n strings equal — because then it is impossible for all n strings to have the same number of c. 2 | 3 | So we have to count the number of occurences of the character and check if it is a multiple of n or not 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | typedef long long int ll; 9 | #define mp make_pair 10 | #define pb push_back 11 | 12 | void solve(){ 13 | // Solve here 14 | int n; 15 | cin>>n; 16 | 17 | map mp; 18 | 19 | for(int i=0;i>s; 22 | 23 | for(auto i:s) { 24 | mp[i]++; 25 | } 26 | } 27 | 28 | bool flag = false; 29 | for(auto i:mp) { 30 | if(i.second%n) { 31 | cout<<"NO\n"; 32 | flag = true; 33 | break; 34 | } 35 | } 36 | if(!flag) cout<<"YES\n"; 37 | } 38 | 39 | int main() { 40 | int t; 41 | cin>>t; 42 | while(t--){ 43 | solve(); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Mega_Contest_3/Editorials/Problem_4/solution_1.cpp: -------------------------------------------------------------------------------- 1 | /* Here idea is to count the number of set bits in the binary representation of given number 2 | 3 | Example if the number is 5 its binary form is 101 so the ans would be 2 4 | */ 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define ll long long 11 | #define mp make_pair 12 | #define pb push_back 13 | #define mod 1000000007 14 | #define endl "\n" 15 | #define len(x) x.size() 16 | #define gcd(a, b) __gcd(a, b) 17 | #define ste(v) v.begin(), v.end() 18 | #define stea(arr, n) arr, arr+n 19 | 20 | 21 | void solve(){ 22 | // Solve here 23 | int n; 24 | cin>>n; 25 | int ans=0; 26 | while(n) { 27 | if(n&1) ans++; 28 | n>>=1; 29 | } 30 | 31 | cout<>t; 37 | while(t--){ 38 | solve(); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Mega_Contest_3/Editorials/Problem_5/solution_1.py: -------------------------------------------------------------------------------- 1 | #Code By Aaditya Upadhyay (aadiupadhyay) 2 | from sys import stdin,stdout 3 | 4 | #Fast input output 5 | st=lambda:list(stdin.readline().strip()) 6 | li=lambda:list(map(int,stdin.readline().split())) 7 | mp=lambda:map(int,stdin.readline().split()) 8 | inp=lambda:int(stdin.readline()) 9 | pr=lambda n: stdout.write(str(n)+"\n") 10 | 11 | mod=1000000007 12 | 13 | def solve(): 14 | n,k=mp() #Taking space separted input 15 | i=1 16 | ma=float('-inf') #assigning minimum value to ma 17 | 18 | #calculating maximum factor of n which is less than or equal to k 19 | while i*i<=n: 20 | if n%i==0: 21 | if i!=(n//i): 22 | if i<=k: 23 | ma=max(ma,i) 24 | 25 | if n//i <=k: 26 | ma=max(ma,n//i) 27 | else: 28 | if i<=k: 29 | ma=max(ma,i) 30 | i+=1 31 | #ma stores the maximum factor of n which is less than or equal to k 32 | pr(n//ma) 33 | 34 | #test cases 35 | for _ in range(inp()): 36 | solve() 37 | -------------------------------------------------------------------------------- /Mega_Contest_3/readme.md: -------------------------------------------------------------------------------- 1 | ### Mega Contest 3 2 | 3 | - Contest's problems link : https://www.hackerrank.com/contests/cpbuddyseriesmegacontest3/challenges 4 | -------------------------------------------------------------------------------- /Mega_Contest_4/Editorials/Problem-1_Print-the-Char/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | map m; 4 | bool fun(char a, char b){ 5 | if(m[a] > m[b]) 6 | return true; 7 | else if(m[a] < m[b]) 8 | return false; 9 | else if(a < b) 10 | return true; 11 | return false; 12 | } 13 | int main(){ 14 | string s; 15 | cin >> s; 16 | set se; 17 | for(auto i : s){ 18 | m[i] = m[i] + 1; 19 | se.insert(i); 20 | } 21 | s = ""; 22 | for(auto i : se) 23 | s = s + i; 24 | sort(s.begin(),s.end(),fun); 25 | cout << s; 26 | } 27 | -------------------------------------------------------------------------------- /Mega_Contest_4/Editorials/Problem-1_Print-the-Char/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | map m; 8 | for(auto i : s) 9 | m[i] += 1; 10 | map a; 11 | for(auto i = m.begin(); i != m.end(); i++){ 12 | a[i->second] += i->first; 13 | } 14 | for(auto i = a.begin(); i != a.end(); i++){ 15 | sort(a[i->first].begin(), a[i->first].end()); 16 | } 17 | string ans = ""; 18 | for(auto i = a.rbegin(); i != a.rend(); i++){ 19 | ans = ans + i->second; 20 | } 21 | cout << ans; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Mega_Contest_4/Editorials/Problem_1/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | bool cmp(pair a, pair b) 6 | { 7 | if(a.first==b.first) 8 | return a.secondb.first; 10 | } 11 | 12 | void solve() 13 | { 14 | ll i,j; 15 | string s; 16 | cin>>s; 17 | vector> v(26); 18 | for(i=0;i<26;i++) 19 | v[i].second=i; 20 | for(auto i:s) 21 | (v[i-'a'].first)++; 22 | sort(v.begin(),v.end(),cmp); 23 | for(auto i:v) 24 | { 25 | if(i.first==0) 26 | break; 27 | char c = i.second+'a'; 28 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | void solve() 6 | { 7 | ll i,j; 8 | ll n,m,k,l; 9 | cin>>n>>m>>k>>l; 10 | ll temp = l+k; 11 | temp = (temp+m-1)/m; 12 | if(temp*m>n) 13 | cout<<"-1\n"; 14 | else 15 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | void solve() 6 | { 7 | ll i,j; 8 | ll h,m; 9 | char c; 10 | cin>>h>>c>>m; 11 | if(h==0) 12 | { 13 | cout<<"01:10"<5&&h<10) 51 | { 52 | cout<<"10:01"<15&&h<20) 97 | { 98 | cout<<"20:02"< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | vector phi(1000000+ 1); 6 | 7 | void phi_1_to_n(int n) { 8 | phi[0] = 0; 9 | phi[1] = 1; 10 | for (int i = 2; i <= n; i++) 11 | phi[i] = i; 12 | 13 | for (int i = 2; i <= n; i++) { 14 | if (phi[i] == i) { 15 | for (int j = i; j <= n; j += i) 16 | phi[j] -= phi[j] / i; 17 | } 18 | } 19 | } 20 | 21 | void solve() 22 | { 23 | ll i,j; 24 | ll x,y; 25 | cin>>x>>y; 26 | if(phi[y]>=phi[x]) 27 | cout<<"Aditya lost it"<>t; 38 | for(ll tt=1;tt<=t;tt++) 39 | { 40 | cout<<"Case "< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | void solve() 6 | { 7 | ll i,j; 8 | ll y,k,n; 9 | cin>>y>>k>>n; 10 | ll temp = y%k; 11 | i=k-temp; 12 | bool ans = false; 13 | for(i;i+y<=n&&i>0;i+=k) 14 | { 15 | cout< 16 | 17 | ## Our week-wise plan: 18 | 19 | | **Week** | **Topics** | 20 | | -------- | ------------------------------ | 21 | | Week 1 | Asymptotic Analysis | 22 | | | Arrays | 23 | | | Strings | 24 | | Week 2 | Sorting and searching | 25 | | | Recursion | 26 | | Week 3 | Linked List | 27 | | | Doubly Linked List | 28 | | Week 4 | Stacks | 29 | | | Queues | 30 | | | Doubly-ended queue | 31 | | Week 5 | Trees | 32 | | | Heaps | 33 | | Week 6 | General Mathematics | 34 | | | Number Theory | 35 | | | Bitwise Operations | 36 | | Week 7 | Divide and Conquer | 37 | | | Binary Search Related Problems | 38 | | Week 8 | Backtracking | 39 | | | Greedy Algorithms | 40 | | Week 9 | Graphs and its Traversal | 41 | | | Graph Related Algorithms | 42 | | Week 10 | Basic Dynamic Programming | 43 | | Week 11 | Advanced Dynamic Programming | 44 | | Week 12 | Segement Trees | 45 | | | Binary Index Tree | 46 | | Week 13 | Disjoint Sets | 47 | | | String Related ALgorithms | 48 | | | Bitwise Operations | 49 | | Week 14 | Miscellaneous Topics | 50 | | | Problem Solving | 51 | | | Tries | 52 | 53 | [Doc file of week wise plan](https://docs.google.com/document/d/1MPwoRmqIs0GA_z9xL8pTyc2lhOHRYwWY_BNPAxKhSW8/edit) 54 | 55 | **Feel free to improve this repo by creating PR 😉** 56 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-1/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; // Take the size of array 6 | cin>>n; 7 | int sum = 0; // Initialize the sum as 0 8 | 9 | for(int i=0;i>x; 12 | sum += x; 13 | } 14 | 15 | cout< 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | /* 8 | * Complete the simpleArraySum function below. 9 | */ 10 | int simpleArraySum(vector ar) { 11 | /* 12 | * Write your code here. 13 | */ 14 | int sum = 0; 15 | for(auto it=ar.begin(); it!=ar.end(); it++) { 16 | sum += *it; 17 | } 18 | return sum; 19 | } 20 | 21 | int main() 22 | { 23 | ofstream fout(getenv("OUTPUT_PATH")); 24 | 25 | int ar_count; 26 | cin >> ar_count; 27 | cin.ignore(numeric_limits::max(), '\n'); 28 | 29 | string ar_temp_temp; 30 | getline(cin, ar_temp_temp); 31 | 32 | vector ar_temp = split_string(ar_temp_temp); 33 | 34 | vector ar(ar_count); 35 | 36 | for (int ar_itr = 0; ar_itr < ar_count; ar_itr++) { 37 | int ar_item = stoi(ar_temp[ar_itr]); 38 | 39 | ar[ar_itr] = ar_item; 40 | } 41 | 42 | int result = simpleArraySum(ar); 43 | 44 | fout << result << "\n"; 45 | 46 | fout.close(); 47 | 48 | return 0; 49 | } 50 | 51 | vector split_string(string input_string) { 52 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 53 | return x == y and x == ' '; 54 | }); 55 | 56 | input_string.erase(new_end, input_string.end()); 57 | 58 | while (input_string[input_string.length() - 1] == ' ') { 59 | input_string.pop_back(); 60 | } 61 | 62 | vector splits; 63 | char delimiter = ' '; 64 | 65 | size_t i = 0; 66 | size_t pos = input_string.find(delimiter); 67 | 68 | while (pos != string::npos) { 69 | splits.push_back(input_string.substr(i, pos - i)); 70 | 71 | i = pos + 1; 72 | pos = input_string.find(delimiter, i); 73 | } 74 | 75 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 76 | 77 | return splits; 78 | } 79 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-2/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; // Take the number of elements in array 6 | cin>>n; 7 | 8 | int arr[n]; // Declare the array of size n 9 | 10 | for(int i=0;i>arr[i]; // Input the array 12 | } 13 | 14 | // Traverse the array in reverse direction and print it 15 | for(int i=n-1;i>=0;i--) { 16 | cout<=0;i--) 17 | { 18 | arr[j]=a[i]; 19 | j++; 20 | 21 | } 22 | return arr; 23 | 24 | 25 | 26 | } 27 | 28 | private static final Scanner scanner = new Scanner(System.in); 29 | 30 | public static void main(String[] args) throws IOException { 31 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 32 | 33 | int arrCount = scanner.nextInt(); 34 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 35 | 36 | int[] arr = new int[arrCount]; 37 | 38 | String[] arrItems = scanner.nextLine().split(" "); 39 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 40 | 41 | for (int i = 0; i < arrCount; i++) { 42 | int arrItem = Integer.parseInt(arrItems[i]); 43 | arr[i] = arrItem; 44 | } 45 | 46 | int[] res = reverseArray(arr); 47 | 48 | for (int i = 0; i < res.length; i++) { 49 | bufferedWriter.write(String.valueOf(res[i])); 50 | 51 | if (i != res.length - 1) { 52 | bufferedWriter.write(" "); 53 | } 54 | } 55 | 56 | bufferedWriter.newLine(); 57 | 58 | bufferedWriter.close(); 59 | 60 | scanner.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-2/solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the reverseArray function below. 10 | def reverseArray(a): 11 | #slicing to reverse list. Alternately use: "list.reverse()" 12 | return a[::-1] 13 | 14 | if __name__ == '__main__': 15 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 16 | 17 | arr_count = int(input()) 18 | 19 | arr = list(map(int, input().rstrip().split())) 20 | 21 | res = reverseArray(arr) 22 | 23 | fptr.write(' '.join(map(str, res))) 24 | fptr.write('\n') 25 | 26 | fptr.close() 27 | 28 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-2/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the reverseArray function below. 8 | vector reverseArray(vector a) { 9 | vector v1(a.size()); 10 | v1.clear(); 11 | for(int it=a.size()-1; it>=0; it--) { 12 | v1.push_back(a[it]); 13 | } 14 | return v1; 15 | } 16 | 17 | int main() 18 | { 19 | ofstream fout(getenv("OUTPUT_PATH")); 20 | 21 | int arr_count; 22 | cin >> arr_count; 23 | cin.ignore(numeric_limits::max(), '\n'); 24 | 25 | string arr_temp_temp; 26 | getline(cin, arr_temp_temp); 27 | 28 | vector arr_temp = split_string(arr_temp_temp); 29 | 30 | vector arr(arr_count); 31 | 32 | for (int i = 0; i < arr_count; i++) { 33 | int arr_item = stoi(arr_temp[i]); 34 | 35 | arr[i] = arr_item; 36 | } 37 | 38 | vector res = reverseArray(arr); 39 | 40 | for (int i = 0; i < res.size(); i++) { 41 | fout << res[i]; 42 | 43 | if (i != res.size() - 1) { 44 | fout << " "; 45 | } 46 | } 47 | 48 | fout << "\n"; 49 | 50 | fout.close(); 51 | 52 | return 0; 53 | } 54 | 55 | vector split_string(string input_string) { 56 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 57 | return x == y and x == ' '; 58 | }); 59 | 60 | input_string.erase(new_end, input_string.end()); 61 | 62 | while (input_string[input_string.length() - 1] == ' ') { 63 | input_string.pop_back(); 64 | } 65 | 66 | vector splits; 67 | char delimiter = ' '; 68 | 69 | size_t i = 0; 70 | size_t pos = input_string.find(delimiter); 71 | 72 | while (pos != string::npos) { 73 | splits.push_back(input_string.substr(i, pos - i)); 74 | 75 | i = pos + 1; 76 | pos = input_string.find(delimiter, i); 77 | } 78 | 79 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 80 | 81 | return splits; 82 | } 83 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-2/solution3.cpp: -------------------------------------------------------------------------------- 1 | // Analysis 2 | 3 | // Time : Linear => O(N), since we are traversing the complete array only once 4 | // Space: Constant => (1), we are only using the given array such idea is also called inplace algorithms. 5 | 6 | #include 7 | using namespace std; 8 | 9 | vector split_string(string); 10 | 11 | // Complete the reverseArray function below. 12 | vector reverseArray(vector a) 13 | { 14 | 15 | // setting two pointers 16 | // "start" pointer at the beginning element of the array 17 | // "end" pointer at the last element of the array 18 | int start = 0, end = a.size() - 1; 19 | int temp; 20 | while (start < end) 21 | { 22 | // logic for swapping elements at start and end indexes 23 | temp = a[start]; 24 | a[start] = a[end]; 25 | a[end] = temp; 26 | 27 | start++; // increment start index 28 | end--; // decrement end index 29 | } 30 | 31 | return a; // return the reversed array 32 | } 33 | 34 | int main() 35 | { 36 | ofstream fout(getenv("OUTPUT_PATH")); 37 | 38 | int arr_count; 39 | cin >> arr_count; 40 | cin.ignore(numeric_limits::max(), '\n'); 41 | 42 | string arr_temp_temp; 43 | getline(cin, arr_temp_temp); 44 | 45 | vector arr_temp = split_string(arr_temp_temp); 46 | 47 | vector arr(arr_count); 48 | 49 | for (int i = 0; i < arr_count; i++) 50 | { 51 | int arr_item = stoi(arr_temp[i]); 52 | 53 | arr[i] = arr_item; 54 | } 55 | 56 | vector res = reverseArray(arr); 57 | 58 | for (int i = 0; i < res.size(); i++) 59 | { 60 | fout << res[i]; 61 | 62 | if (i != res.size() - 1) 63 | { 64 | fout << " "; 65 | } 66 | } 67 | 68 | fout << "\n"; 69 | 70 | fout.close(); 71 | 72 | return 0; 73 | } 74 | 75 | vector split_string(string input_string) 76 | { 77 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [](const char &x, const char &y) { 78 | return x == y and x == ' '; 79 | }); 80 | 81 | input_string.erase(new_end, input_string.end()); 82 | 83 | while (input_string[input_string.length() - 1] == ' ') 84 | { 85 | input_string.pop_back(); 86 | } 87 | 88 | vector splits; 89 | char delimiter = ' '; 90 | 91 | size_t i = 0; 92 | size_t pos = input_string.find(delimiter); 93 | 94 | while (pos != string::npos) 95 | { 96 | splits.push_back(input_string.substr(i, pos - i)); 97 | 98 | i = pos + 1; 99 | pos = input_string.find(delimiter, i); 100 | } 101 | 102 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 103 | 104 | return splits; 105 | } 106 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-3/Unique Number 3.py: -------------------------------------------------------------------------------- 1 | t=int(input()) #To accept number of testcases 2 | while(t!=0): 3 | n,k=input().split() #accept the input 4 | n=int(n) 5 | k=int(k) 6 | arr=list(map(int,input().split())) # to get the array of elements 7 | dict={} 8 | for i in arr: 9 | if i in dict: 10 | dict[i]+=1 #if element present in dict increment by one 11 | else: 12 | dict[i]=1 #if element not present the dict intialise with 1 13 | #store the key and values of dict in lists 14 | key_list = list(dict.keys()) 15 | val_list = list(dict.values()) 16 | for i in dict: 17 | if dict[i]==1: 18 | print(key_list[val_list.index(dict[i])]) #print the key of the unique element present the given arr of elements 19 | t-=1 #decrement the number of test cases by 1 20 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-3/solution-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | while(t--){ 8 | map m; 9 | int n, k; 10 | cin >> n >> k; 11 | int a; 12 | for(int i = 0; i < n; i++){ 13 | cin >> a; 14 | m[a] = m[a] + 1; 15 | } 16 | for(auto i = m.begin(); i != m.end(); i++){ 17 | if(i->second == 1){ 18 | cout << i->first << endl; 19 | break; 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-3/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | int t; // Input the test cases 7 | cin>>t; 8 | 9 | while(t--) { 10 | int n,k; // input n and k (ex 5 and 2) 11 | cin>>n>>k; 12 | vector v; // Initialize the vector of integers 13 | for(int i=0;i>x; 16 | v.push_back(x); // Input the vector elements (ex 6 2 1 1 6) 17 | } 18 | 19 | sort(v.begin(), v.end()); // Sort the vector ( 1 1 2 6 6) 20 | 21 | int count = 1; // Intilize the count as 1 22 | 23 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | int n, k; 16 | cin>>n>>k; 17 | int arr[n]; 18 | unordered_map m1; 19 | for(int i=0;i> arr[i]; 21 | m1[arr[i]]++; 22 | } 23 | for(auto it:m1) { 24 | if(it.second==1) 25 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int t; //t is used for test cases 11 | cin>>t; 12 | while(t--){ 13 | int n,k; 14 | cin>>n>>k; // Array is initialized to zero 15 | int a[10000]={0}; // the size of array depends on contraints of array given in the problem 16 | /* 17 | Count sort takes extra space of size equal to constraints but this algorithm works in O(n) 18 | Here we consider indices as numbers and increment them according to the input 19 | */ 20 | for(int i=0;i>x; // Now, here we are getting the value from user and 23 | a[x]++; //incrementing the value at that particular index 24 | } 25 | for(int i=0;i<10000;i++){ 26 | if(a[i]!=k&&a[i]!=0){ //Here comes the condition and important part 27 | /* 28 | Now, according to question we know that the element should have frequency equal to k 29 | If we analyze this array we have saved the frequencies of indices and if a number does not exist then it will be having 0 as value 30 | */ 31 | cout< 24 | using namespace std; 25 | 26 | int main() 27 | { 28 | int n; // Size of strings 29 | cin>>n; 30 | unordered_map u; // Initialize the unordered_map 31 | for(int i=0; i>s; 35 | u[s]++ // Similar to u[s]=u[s]+1; 36 | // Frequency of a string is incremented each time it is repeated. 37 | } 38 | 39 | int q; // Size of query 40 | cin>>q; 41 | int arr[q]; // Initialize array of frequency of occurence of query in strings 42 | 43 | for(int i=0; i>s; // Take query string 47 | arr[i] = u[s]; // Frquency of the string s is u[s]. 48 | } 49 | 50 | // Print the array of occurence 51 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int n; // Size of strings 6 | cin>>n; 7 | vector strings; // Initialize the vector of strings; 8 | 9 | for(int i=0;i>s; 12 | strings.push_back(s); // Storing values in strings 13 | } 14 | 15 | int q; // Size of query 16 | cin>>q; 17 | 18 | int arr[q]; // Initialize array of frequency of occurence of query in strings 19 | 20 | for(int i=0;i>s; // Take query string 23 | int freq = 0; // Initialize frequency of occurence as 0 24 | 25 | for(int j=0;j 2 | 3 | using namespace std; 4 | 5 | // Complete the matchingStrings function below. 6 | vector matchingStrings(vector strings, vector queries) { 7 | unordered_map m1; 8 | vector v1; 9 | for(int i=0;i> strings_count; 24 | cin.ignore(numeric_limits::max(), '\n'); 25 | 26 | vector strings(strings_count); 27 | 28 | for (int i = 0; i < strings_count; i++) { 29 | string strings_item; 30 | getline(cin, strings_item); 31 | 32 | strings[i] = strings_item; 33 | } 34 | 35 | int queries_count; 36 | cin >> queries_count; 37 | cin.ignore(numeric_limits::max(), '\n'); 38 | 39 | vector queries(queries_count); 40 | 41 | for (int i = 0; i < queries_count; i++) { 42 | string queries_item; 43 | getline(cin, queries_item); 44 | 45 | queries[i] = queries_item; 46 | } 47 | 48 | vector res = matchingStrings(strings, queries); 49 | 50 | for (int i = 0; i < res.size(); i++) { 51 | fout << res[i]; 52 | 53 | if (i != res.size() - 1) { 54 | fout << "\n"; 55 | } 56 | } 57 | 58 | fout << "\n"; 59 | 60 | fout.close(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-5/solution.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Editorial for the last problem in week 1 contest - Left Rotation 3 | * 4 | * Problem: 5 | * 6 | * A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]. 7 | * Given an array of n integers and a number, d, perform d left rotations on the array. Then print the updated array as a single line of space-separated integers. 8 | * 9 | * Input Format: 10 | * 11 | * The first line contains two space-separated integers denoting the respective values of n (the number of integers) and d (the number of left rotations you must perform). 12 | * The second line contains n space-separated integers describing the respective elements of the array's initial state. 13 | * 14 | * Constraints: 15 | * 1<=n<=10^5 16 | * 1<=d<=n 17 | * 1<=ai<=10^6 18 | * 19 | * Output Format: 20 | * 21 | * Print a single line of n space-separated integers denoting the final state of the array after performing d left rotations. 22 | * 23 | */ 24 | // Importing required library and defining scope 25 | #include 26 | using namespace std; 27 | 28 | // Driver method of the program 29 | int main(){ 30 | 31 | // Input 32 | int n,d; 33 | cin >> n >> d; 34 | int arr[n]; 35 | for(int i=0;i> arr[i]; 37 | } 38 | vector result; 39 | 40 | /*Rotating d times to left is same as collecting and printing from d+1th to nth elements 41 | and 1st to dth elements on close observation.*/ 42 | 43 | //Collecting d+1th to nth elements 44 | for(int i=d;i 2 | #include 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | 8 | 9 | int main() 10 | { 11 | string nd_temp; 12 | getline(cin, nd_temp); 13 | 14 | vector nd = split_string(nd_temp); 15 | 16 | int n = stoi(nd[0]); 17 | 18 | int d = stoi(nd[1]); 19 | 20 | string a_temp_temp; 21 | getline(cin, a_temp_temp); 22 | 23 | vector a_temp = split_string(a_temp_temp); 24 | 25 | vector a(n); 26 | 27 | for (int i = 0; i < n; i++) { 28 | int a_item = stoi(a_temp[i]); 29 | 30 | a[i] = a_item; 31 | } 32 | 33 | rotate(a.begin(), a.begin() + d, a.end()); 34 | 35 | for(auto it=a.begin(); it!=a.end(); it++) { 36 | cout << *it << " "; 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | vector split_string(string input_string) { 43 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 44 | return x == y and x == ' '; 45 | }); 46 | 47 | input_string.erase(new_end, input_string.end()); 48 | 49 | while (input_string[input_string.length() - 1] == ' ') { 50 | input_string.pop_back(); 51 | } 52 | 53 | vector splits; 54 | char delimiter = ' '; 55 | 56 | size_t i = 0; 57 | size_t pos = input_string.find(delimiter); 58 | 59 | while (pos != string::npos) { 60 | splits.push_back(input_string.substr(i, pos - i)); 61 | 62 | i = pos + 1; 63 | pos = input_string.find(delimiter, i); 64 | } 65 | 66 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 67 | 68 | return splits; 69 | } 70 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_1/problem-5/solution_usingMod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int n,d; 9 | cin>>n>>d; // Taking input of size of array and number of left rotations 10 | int a[n+1],b[n+1]; 11 | for(int i=0;i>a[i]; 13 | } 14 | for(int i=0;i 26 | using namespace std; 27 | 28 | int main() 29 | { int n; 30 | int r; 31 | cin>>n>>r; 32 | int arr[n]; 33 | 34 | for(int i=0;i>arr[i]; 37 | } 38 | 39 | while(r--){ 40 | int temp = arr[1]; 41 | int one = arr[0]; 42 | for(int i=0;i 33 | using namespace std; 34 | 35 | int main() { 36 | string s; 37 | cin >> s; 38 | map m; 39 | for(int i = 0; i < s.length(); i++){ 40 | m[s[i]]++; 41 | } 42 | string r = ""; 43 | for(auto i = m.begin(); i != m.end(); i++){ 44 | if(i -> second % 2 != 0){ 45 | r = r + i -> first; 46 | } 47 | } 48 | if(r.length() != 0){ 49 | cout << r; 50 | }else{ 51 | cout << "Empty String"; 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-1/solution.py: -------------------------------------------------------------------------------- 1 | str1 = input() 2 | 3 | for z1 in range(3): 4 | record = [] 5 | for i in range((len(str1))-1): 6 | if str1[i] == str1[i+1] and (i-1) not in record: 7 | record.append(i) 8 | 9 | x = 0 10 | #print(record) 11 | for j in range(len(record)): 12 | record[j] -= x 13 | x += 2 14 | 15 | for w in record: 16 | str1 = str1[0: w:] + str1[w+1 + 1::] 17 | 18 | 19 | if(len(str1)) > 0: 20 | print(str1) 21 | else: 22 | print("Empty String") 23 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-1/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | string input; 12 | cin >> input; 13 | 14 | vector v1; 15 | v1.push_back('#'); 16 | 17 | for(int i=0; i 30 | #include 31 | using namespace std; 32 | int main(){ 33 | int t; 34 | cin >> t; 35 | while(t--){ 36 | string s; 37 | cin >> s; 38 | int n = s.length(); 39 | int f = 0; 40 | int size = 1; 41 | while(size != n){ 42 | string prefix = ""; 43 | string suffix = ""; 44 | prefix = s.substr(0,size); 45 | suffix = s.substr(n-size,size); 46 | // cout << "prefix : " << prefix << " suffix : " << suffix << endl; 47 | if(prefix == suffix){ 48 | f = size; 49 | } 50 | size = size + 1; 51 | } 52 | cout << f << endl; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-2/solution.py: -------------------------------------------------------------------------------- 1 | def lp(str1) : 2 | n = len(str1) 3 | 4 | for part in range(n-1, 0, -1) : 5 | #string slicing 6 | prefix = str1[0: part] 7 | suffix = str1[-part:] 8 | 9 | if (prefix == suffix) : 10 | return part 11 | return 0 12 | 13 | tc = input() 14 | #print(tc) 15 | s = [] 16 | i = 0 17 | while i < int(tc): 18 | z = str(input()) 19 | i +=1 20 | print(lp(z)) 21 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-2/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | string input; 15 | cin >> input; 16 | int n = input.size(); 17 | int lps[n]; 18 | 19 | int length = 0; 20 | lps[0] = 0; 21 | int i=1; 22 | while(i 37 | using namespace std; 38 | int main(){ 39 | int t; 40 | cin >> t; 41 | while(t--){ 42 | int n; 43 | cin >> n; 44 | int a[n]; 45 | for(int i = 0; i < n; i++){ 46 | cin >> a[i]; 47 | } 48 | int k; 49 | cin >> k; 50 | sort(a,a+n); 51 | cout << a[k-1] << endl; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-3/solution.py: -------------------------------------------------------------------------------- 1 | tc = int(input()) 2 | l = [] 3 | ar = [] 4 | k = [] 5 | for i in range(tc): 6 | l.append(int(input())) 7 | ar.append(input()) 8 | k.append(int(input())) 9 | #print(l) 10 | #print(ar) 11 | #print(k) 12 | for i in range(tc): 13 | ar[i] = ar[i].split(" ",) 14 | #print(ar[i]) 15 | for j in range(l[i]): 16 | ar[i][j] = int(ar[i][j]) 17 | ar[i] = sorted(ar[i]) 18 | print(ar[i][k[i]-1]) 19 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-3/solution1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | 8 | Scanner sc=new Scanner(System.in); 9 | int t=sc.nextInt(); //Take no of test cases 10 | while(t!=0) 11 | { 12 | int n=sc.nextInt(); //take no of element in array 13 | int arr[]=new int[n]; 14 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--) { 14 | int n; 15 | cin >> n; 16 | vector v1; 17 | int input, k; 18 | for(int i=0;i> input; 20 | v1.push_back(input); 21 | } 22 | cin >> k; 23 | sort(v1.begin(), v1.end()); 24 | cout << v1[k-1] << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-4/solnsortstr.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc=new Scanner(System.in); 8 | int n=sc.nextInt(); 9 | sc.nextLine(); 10 | String s=sc.nextLine(); 11 | String l=""; 12 | String u=""; 13 | for(int i=0;i=97 && k<=122) 17 | { 18 | l=l+s.charAt(i); 19 | } 20 | else 21 | { 22 | u=u+s.charAt(i); 23 | } 24 | } 25 | char[] charl = l.toCharArray(); 26 | char[] charu = u.toCharArray(); 27 | Arrays.sort(charl); 28 | Arrays.sort(charu); 29 | String sl = new String(charl); 30 | String su = new String(charu); 31 | int a=0,b=0; 32 | for(int i=0;i=97 && k<=122) 36 | { 37 | System.out.print(sl.charAt(a)); 38 | a=a+1; 39 | } 40 | else{ 41 | System.out.print(su.charAt(b)); 42 | b=b+1; 43 | } 44 | } 45 | 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-4/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase letters separately such that if the ith place in the original string had an Uppercase character then it should not have a lowercase character after being sorted and vice versa. 3 | 4 | Input Format 5 | 6 | First line will contain N length of the string. Next line will be the string. 7 | 8 | Constraints 9 | 10 | 1 ≤ N ≤ 10^3 11 | 12 | Output Format 13 | 14 | Print the sorted string 15 | 16 | Sample Input 0 17 | 18 | 12 19 | defRTSersUXI 20 | Sample Output 0 21 | 22 | deeIRSfrsTUX 23 | Explanation 0 24 | 25 | Sorted form of given string with the same case of character as that in original string is deeIRSfrsTUX 26 | 27 | Sample Input 1 28 | 29 | 6 30 | srbDKi 31 | Sample Output 1 32 | 33 | birDKs 34 | Explanation 1 35 | 36 | Sorted form of given string with the same case of character will result in output as birDKs. 37 | */ 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | using namespace std; 45 | int main() { 46 | int n; 47 | cin >> n; 48 | string s; 49 | cin >> s; 50 | string l,u; 51 | for(int i = 0; i < s.length(); i++){ 52 | int k = s[i]; 53 | if(k >= 97 && k <= (97+26)) 54 | l = l + s[i]; 55 | else 56 | u = u + s[i]; 57 | } 58 | sort(l.begin(),l.end()); 59 | sort(u.begin(),u.end()); 60 | // cout << l << endl << u; 61 | int a = 0,b = 0; 62 | for(int i = 0; i < s.length(); i++){ 63 | int k = s[i]; 64 | if(k >= 97 && k <= (97+26)){ 65 | cout << l[a]; 66 | a = a + 1; 67 | }else{ 68 | cout << u[b]; 69 | b = b + 1; 70 | } 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-4/solution.py: -------------------------------------------------------------------------------- 1 | len1 = input() 2 | str1 = input() 3 | low = [] 4 | up = [] 5 | for i in str1: 6 | if i.islower(): 7 | low.append(i) 8 | elif i.isupper(): 9 | up.append(i) 10 | up.sort() 11 | low.sort() 12 | 13 | newstr = "" 14 | j = 0 15 | u = 0 16 | l = 0 17 | for i in str1: 18 | #print((newstr),j) 19 | if i.isupper(): 20 | #print(newstr,j,up,u) 21 | newstr = newstr + up[u] 22 | u += 1 23 | elif i.islower(): 24 | newstr = newstr + low[l] 25 | l += 1 26 | j += 1 27 | print(newstr) 28 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-4/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int n; 13 | cin >> n; 14 | 15 | string input; 16 | cin >> input; 17 | 18 | vector v1, v2, ret(n); 19 | vector i1, i2; 20 | 21 | char curr; 22 | for(int i=0;i0){ 35 | String str = s.next(); 36 | if(isPalindrome(str)){ 37 | System.out.println("Yes"); 38 | }else{ 39 | System.out.println("No"); 40 | } 41 | } 42 | //System.out.println("Hello World!"); 43 | } 44 | } -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-5/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Arnab is given a string, but being the evil Lord he is, he has his own whims and fantasies about the string he wants to keep with himself. If he can convert a string into a palindrome by rearranging the characters, he will keep that string, otherwise he will discard that string. You’ve to tell if Arnab will keep that string or not. 3 | 4 | Input Format 5 | 6 | The first line of input contains the number of test cases T. Each test case contains a single string S. 7 | 8 | Constraints 9 | 10 | 1 <= Number of test cases, T <= 10 11 | 12 | 1<=|Length of String S|<=100 13 | 14 | Output Format 15 | 16 | For each test case print Yes if Arnab can convert string into a palindrome by rearranging the characters else print No. 17 | 18 | Sample Input 0 19 | 20 | 3 21 | code 22 | pop 23 | abb 24 | Sample Output 0 25 | 26 | No 27 | Yes 28 | Yes 29 | */ 30 | 31 | 32 | #include 33 | using namespace std; 34 | int main() { 35 | int t; 36 | cin >> t; 37 | while(t--){ 38 | string s; 39 | cin >> s; 40 | map m; 41 | for(int i = 0; i < s.length(); i++){ 42 | m[s[i]]++; 43 | m[s[i]] = m[s[i]] % 2; 44 | } 45 | // cout << "--------" << endl; 46 | int count = 0; 47 | for(auto i = m.begin(); i != m.end(); i++){ 48 | // cout << i->first << " : " << i->second << endl; 49 | count = count + i -> second; 50 | } 51 | // cout << endl; 52 | if(count == 1 || count == 0){ 53 | cout << "Yes" << endl; 54 | }else{ 55 | cout << "No" << endl; 56 | } 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-5/solution.py: -------------------------------------------------------------------------------- 1 | def pal(str1) : 2 | count = [0]*(256) 3 | for i in range( 0, len(str1)) : 4 | count[ord(str1[i])] += 1 5 | od = 0 6 | for i in range(0, 256): 7 | if (count[i] & 1): 8 | od += 1 9 | if (od > 1) : 10 | return False 11 | return True 12 | 13 | 14 | tc = int(input()) 15 | l = [] 16 | for i in range(tc): 17 | #l.append(input()) 18 | if (pal(input())): 19 | print("Yes") 20 | else: 21 | print("No") 22 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-5/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | string s; 16 | cin >> s; 17 | int n = s.size(); 18 | unordered_map m1; 19 | 20 | for(int i=0;i 1) { 27 | ctr++; 28 | } 29 | } 30 | 31 | if(ctr >= floor(n/2)) { 32 | cout<<"Yes"< 36 | using namespace std; 37 | int main(){ 38 | int t; 39 | cin >> t; 40 | while(t--){ 41 | map m; 42 | string s; 43 | cin >> s; 44 | for(int i = 0; i < s.length(); i++){ 45 | m[s[i]]++; 46 | } 47 | // cout << "-----" << endl; 48 | int count = 0; 49 | for(auto i = m.begin(); i != m.end(); i++){ 50 | // cout << i->first << " : " << i->second << endl; 51 | count = count + i->second -1; 52 | } 53 | cout << count << endl; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-6/solution.py: -------------------------------------------------------------------------------- 1 | def check(str1): 2 | z = [] 3 | count1 = 0 4 | for i in str1: 5 | if i not in z: 6 | z.append(i) 7 | k = str1.count(i) 8 | count1 = count1 + k - 1 9 | print(count1) 10 | 11 | tc = int(input()) 12 | l = [] 13 | for i in range(tc): 14 | check(input()) 15 | -------------------------------------------------------------------------------- /Week-1/Editorials/Contest_2/problem-6/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | string s; 16 | cin >> s; 17 | 18 | int ctr = 0; 19 | unordered_map m1; 20 | 21 | for(int i=0;i 1) { 27 | ctr += it.second-1; 28 | } 29 | } 30 | 31 | cout << ctr << endl; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Week-1/README.md: -------------------------------------------------------------------------------- 1 | # WEEK 1 [Aug 03 - Aug 09] 2 | 3 | ## Topics 4 | 5 | 1. Asymptotic analysis (Big-O notation) 6 | 2. Arrays 7 | 3. Strings 8 | 9 | ## Resources 10 | 11 | ##### Asymptotic Notations 12 | 13 | ###### Videos 14 | 15 | 1. https://www.youtube.com/watch?v=fZc3ijGM0aM 16 | 2. https://www.youtube.com/watch?v=vgSKOMsjLbc 17 | 3. https://www.youtube.com/watch?v=P7frcB_-g4w&list=PLUl4u3cNGP61Oq3tWYp6V_F-5jb5L2iHb&index=25 18 | 4. https://www.youtube.com/watch?v=1OTX-WXQHCQ&list=PLu0W_9lII9ahIappRPN0MCAgtOu3lQjQi&index=3 19 | 20 | 21 | ###### Articles 22 | 23 | 1. https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/asymptotic-notation 24 | 2. https://www.geeksforgeeks.org/analysis-of-algorithms-set-1-asymptotic-analysis/?ref=lbp 25 | 3. https://runestone.academy/runestone/books/published/pythonds/AlgorithmAnalysis/BigONotation.html 26 | 4. https://www.iarcs.org.in/inoi/online-study-material/topics/efficiency.php 27 | 28 | ##### Strings and Arrays 29 | 30 | **C++** 31 | 32 | ###### Videos 33 | 34 | 1. https://youtu.be/AT14lCXuMKI 35 | 2. https://youtu.be/PocJ5jXv8No 36 | 3. https://youtu.be/5u6Oq06Iask 37 | 4. https://youtu.be/DJs9ewAVDEg 38 | 39 | ###### Articles 40 | 41 | 1. https://www.guru99.com/array-data-structure.html 42 | 2. https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm 43 | 3. https://www.geeksforgeeks.org/array-data-structure 44 | 45 | **Java** 46 | 47 | ###### Videos 48 | 49 | 1. https://www.youtube.com/watch?v=xiqHjwBb5-E 50 | 2. https://www.youtube.com/watch?v=7r5ZX0ix5BU 51 | 3. https://www.youtube.com/watch?v=WRkSW_Apvp4 52 | 4. https://www.youtube.com/watch?v=d1NPEb1MC5U 53 | 54 | ###### Articles 55 | 56 | 1. https://www.tutorialspoint.com/java/java_arrays.htm 57 | 2. https://www.tutorialspoint.com/java/java_strings.htm 58 | 59 | **Python** 60 | 61 | ###### Videos 62 | 63 | 1. Lists in Python https://www.youtube.com/watch?v=tw7ror9x32s 64 | 2. Strings in Python https://www.youtube.com/watch?v=Ctqi5Y4X-jA 65 | 66 | ###### Articles 67 | 68 | 1. Lists in Python https://www.geeksforgeeks.org/python-list/ 69 | 2. Strings in Python https://www.tutorialspoint.com/python/python_strings.htm 70 | 71 | ## Links to Practice Contests 72 | 73 | ### Wednesday 74 | * Quiz : https://forms.gle/a9idNxYK2mS7KnS5A 75 | * Practice contest #1: https://www.hackerrank.com/cpbuddyseries-week1-contest1 76 | 77 | ### Sunday 78 | * Practice contest #2: https://www.hackerrank.com/cpbuddyseries-week1-practicecontest2 79 | 80 | ## Announcements 81 | 82 | - Our first Orientation meetup has been held on 2nd August. Here's the [link](https://meet.google.com/yeg-jnke-qnb) 83 | to it. 84 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-1/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Big Sorting 3 | */ 4 | #include 5 | using namespace std; 6 | bool fun(string a,string b){ 7 | int x,y; 8 | x = a.length(); 9 | y = b.length(); 10 | if(x == y){ 11 | return (a < b); 12 | } 13 | return (x < y); 14 | } 15 | int main(){ 16 | long n; 17 | cin >> n; 18 | vector v; 19 | for(long i = 0; i < n; i++){ 20 | string s; 21 | cin >> s; 22 | v.push_back(s); 23 | } 24 | sort(v.begin(),v.end(),fun); 25 | for(long i = 0; i < n; i++){ 26 | cout << v[i] << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-1/solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the bigSorting function below. 10 | def bigSorting(unsorted): 11 | unsorted.sort(key = lambda x:(len(x), x)) 12 | return unsorted 13 | 14 | if __name__ == '__main__': 15 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 16 | 17 | n = int(input()) 18 | 19 | unsorted = [] 20 | 21 | for _ in range(n): 22 | unsorted_item = input() 23 | unsorted.append(unsorted_item) 24 | 25 | result = bigSorting(unsorted) 26 | 27 | fptr.write('\n'.join(result)) 28 | fptr.write('\n') 29 | 30 | fptr.close() 31 | 32 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-1/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool comparator (string &left, string &right) { 7 | if(left.size() == right.size()) 8 | return left < right; 9 | else 10 | return left.size() < right.size(); 11 | } 12 | 13 | // Complete the bigSorting function below. 14 | vector bigSorting(vector unsorted) { 15 | sort(unsorted.begin(), unsorted.end(), comparator); 16 | return unsorted; 17 | } 18 | 19 | int main() 20 | { 21 | ofstream fout(getenv("OUTPUT_PATH")); 22 | 23 | int n; 24 | cin >> n; 25 | cin.ignore(numeric_limits::max(), '\n'); 26 | 27 | vector unsorted(n); 28 | 29 | for (int i = 0; i < n; i++) { 30 | string unsorted_item; 31 | getline(cin, unsorted_item); 32 | 33 | unsorted[i] = unsorted_item; 34 | } 35 | 36 | vector result = bigSorting(unsorted); 37 | 38 | for (int i = 0; i < result.size(); i++) { 39 | fout << result[i]; 40 | 41 | if (i != result.size() - 1) { 42 | fout << "\n"; 43 | } 44 | } 45 | 46 | fout << "\n"; 47 | 48 | fout.close(); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-2/O(log n)_solution.cpp: -------------------------------------------------------------------------------- 1 | // Since the input array is already sorted, we can use binary search to find the index of V 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int k; 9 | cin>>k; 10 | int n; 11 | cin>>n; 12 | vectorv(n); 13 | for(int i=0;i>v[i]; 15 | int l=0,r=n-1; //Left and right limits 16 | while(l<=r) 17 | { 18 | int mid = l+(r-l)/2; //Formula for mid (To avoid overflow) 19 | if(v[mid]==k) 20 | { 21 | cout< 2 | using namespace std; 3 | int main(){ 4 | int k; 5 | cin >> k; 6 | int n; 7 | cin >> n; 8 | for(int i = 0; i < n; i++){ 9 | int a; 10 | cin >> a; 11 | if(a == k){ 12 | cout << i; 13 | return 0; 14 | } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-2/solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the introTutorial function below. 10 | def introTutorial(V, arr): 11 | return(arr.index(V)) 12 | 13 | if __name__ == '__main__': 14 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 15 | 16 | V = int(input()) 17 | 18 | n = int(input()) 19 | 20 | arr = list(map(int, input().rstrip().split())) 21 | 22 | result = introTutorial(V, arr) 23 | 24 | fptr.write(str(result) + '\n') 25 | 26 | fptr.close() 27 | 28 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-2/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the introTutorial function below. 8 | int introTutorial(int V, vector arr) { 9 | for(int i=0;i> V; 22 | cin.ignore(numeric_limits::max(), '\n'); 23 | 24 | int n; 25 | cin >> n; 26 | cin.ignore(numeric_limits::max(), '\n'); 27 | 28 | string arr_temp_temp; 29 | getline(cin, arr_temp_temp); 30 | 31 | vector arr_temp = split_string(arr_temp_temp); 32 | 33 | vector arr(n); 34 | 35 | for (int i = 0; i < n; i++) { 36 | int arr_item = stoi(arr_temp[i]); 37 | 38 | arr[i] = arr_item; 39 | } 40 | 41 | int result = introTutorial(V, arr); 42 | 43 | fout << result << "\n"; 44 | 45 | fout.close(); 46 | 47 | return 0; 48 | } 49 | 50 | vector split_string(string input_string) { 51 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 52 | return x == y and x == ' '; 53 | }); 54 | 55 | input_string.erase(new_end, input_string.end()); 56 | 57 | while (input_string[input_string.length() - 1] == ' ') { 58 | input_string.pop_back(); 59 | } 60 | 61 | vector splits; 62 | char delimiter = ' '; 63 | 64 | size_t i = 0; 65 | size_t pos = input_string.find(delimiter); 66 | 67 | while (pos != string::npos) { 68 | splits.push_back(input_string.substr(i, pos - i)); 69 | 70 | i = pos + 1; 71 | pos = input_string.find(delimiter, i); 72 | } 73 | 74 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 75 | 76 | return splits; 77 | } 78 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-3/solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Adi and the rocks 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | void fun(vector>& mat) 9 | { 10 | int row = mat.size(); 11 | int col = mat[0].size(); 12 | 13 | vector Neg[row]; 14 | vector Pos[col]; 15 | 16 | for (int i = 0; i < row; i++) { 17 | for (int j = 0; j < col; j++) { 18 | if (j < i) { 19 | Neg[i - j].push_back(mat[i][j]); 20 | } 21 | else if (j > i) { 22 | Pos[j - i].push_back(mat[i][j]); 23 | } 24 | else { 25 | Pos[0].push_back(mat[i][j]); 26 | } 27 | } 28 | } 29 | for (int i = 0; i < row; i++) { 30 | sort(Neg[i].begin(), Neg[i].end(),greater()); 31 | } 32 | for (int i = 0; i < col; i++) { 33 | sort(Pos[i].begin(), Pos[i].end(),greater()); 34 | } 35 | 36 | for (int i = 0; i < row; i++) { 37 | for (int j = 0; j < col; j++) { 38 | if (j < i) { 39 | int d = i - j; 40 | int l = Neg[d].size(); 41 | mat[i][j] = Neg[d][l - 1]; 42 | Neg[d].pop_back(); 43 | } 44 | else if (j > i) { 45 | int d = j - i; 46 | int l = Pos[d].size(); 47 | mat[i][j] = Pos[d][l - 1]; 48 | Pos[d].pop_back(); 49 | } 50 | else { 51 | int l = Pos[0].size(); 52 | mat[i][j] = Pos[0][l - 1]; 53 | Pos[0].pop_back(); 54 | } 55 | } 56 | } 57 | } 58 | 59 | int main() 60 | { 61 | int t; 62 | cin >> t; 63 | while(t--){ 64 | int m,n; 65 | cin >> m >> n; 66 | vector> a; 67 | for(int i = 0; i < m; i++){ 68 | vector t; 69 | for(int j = 0; j < n; j++){ 70 | int x; 71 | cin >> x; 72 | t.push_back(x); 73 | } 74 | a.push_back(t); 75 | } 76 | fun(a); 77 | for(int i = 0; i < a.size(); i++){ 78 | for(int j = 0; j < a[i].size(); j++){ 79 | cout << a[i][j] << ' '; 80 | } 81 | cout << endl; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-3/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | int m, n; 16 | cin >> m >> n; 17 | int arr[m][n]; 18 | 19 | unordered_map> diagonal; 20 | 21 | for(int i=0;i>arr[i][j]; 24 | diagonal[i-j].push_back(arr[i][j]); 25 | } 26 | } 27 | 28 | for(int i=-(n-1);i=0;i--) { 33 | for(int j=n-1;j>=0;j--) { 34 | arr[i][j] = diagonal[i-j].back(); 35 | diagonal[i-j].pop_back(); 36 | } 37 | } 38 | 39 | for(int i=0;i 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | int n; 8 | cin >> n; 9 | vector v; 10 | for(int i = 0; i < n; i++){ 11 | int a; 12 | cin >> a; 13 | v.push_back(a); 14 | } 15 | 16 | sort(v.begin(),v.end()); 17 | int size = v.size(); 18 | int k = size/2; 19 | cout << v[k]; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-4/solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the findMedian function below. 10 | def findMedian(arr): 11 | if len(arr) == 0: 12 | return 13 | if(len(arr) == 1): 14 | return arr[0] 15 | arr = sorted(arr) 16 | return(arr[int((len(arr))/2)]) 17 | 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | n = int(input()) 23 | 24 | arr = list(map(int, input().rstrip().split())) 25 | 26 | result = findMedian(arr) 27 | 28 | fptr.write(str(result) + '\n') 29 | 30 | fptr.close() 31 | 32 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-4/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the findMedian function below. 8 | int findMedian(vector arr) { 9 | sort(arr.begin(), arr.end()); 10 | return arr[arr.size()/2]; 11 | } 12 | 13 | int main() 14 | { 15 | ofstream fout(getenv("OUTPUT_PATH")); 16 | 17 | int n; 18 | cin >> n; 19 | cin.ignore(numeric_limits::max(), '\n'); 20 | 21 | string arr_temp_temp; 22 | getline(cin, arr_temp_temp); 23 | 24 | vector arr_temp = split_string(arr_temp_temp); 25 | 26 | vector arr(n); 27 | 28 | for (int i = 0; i < n; i++) { 29 | int arr_item = stoi(arr_temp[i]); 30 | 31 | arr[i] = arr_item; 32 | } 33 | 34 | int result = findMedian(arr); 35 | 36 | fout << result << "\n"; 37 | 38 | fout.close(); 39 | 40 | return 0; 41 | } 42 | 43 | vector split_string(string input_string) { 44 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 45 | return x == y and x == ' '; 46 | }); 47 | 48 | input_string.erase(new_end, input_string.end()); 49 | 50 | while (input_string[input_string.length() - 1] == ' ') { 51 | input_string.pop_back(); 52 | } 53 | 54 | vector splits; 55 | char delimiter = ' '; 56 | 57 | size_t i = 0; 58 | size_t pos = input_string.find(delimiter); 59 | 60 | while (pos != string::npos) { 61 | splits.push_back(input_string.substr(i, pos - i)); 62 | 63 | i = pos + 1; 64 | pos = input_string.find(delimiter, i); 65 | } 66 | 67 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 68 | 69 | return splits; 70 | } 71 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-5/solution.cpp: -------------------------------------------------------------------------------- 1 | /* Lily's Homework */ 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int swappy(vector a, vector s){ 8 | map m; 9 | for(int i = 0; i < a.size(); i++){ 10 | m[a[i]] = i; 11 | } 12 | int c = 0; 13 | for(int i = 0; i < a.size(); i++){ 14 | if(a[i] != s[i]){ 15 | c = c + 1; 16 | int t = a[i]; 17 | a[i] = s[i]; 18 | int x = m[s[i]]; 19 | a[m[s[i]]] = t; 20 | m[t] = x; 21 | } 22 | } 23 | return c; 24 | } 25 | 26 | int lilysHomework(vector a) { 27 | vector v; 28 | v = a; 29 | sort(a.begin(),a.end()); 30 | int ac = swappy(v,a); 31 | reverse(a.begin(),a.end()); 32 | int bc = swappy(v,a); 33 | int ans = min(ac,bc); 34 | return ans; 35 | } 36 | 37 | int main() 38 | { 39 | int n; 40 | cin >> n; 41 | vector arr(n); 42 | 43 | for (int i = 0; i < n; i++) { 44 | int a; 45 | cin >> a; 46 | arr[i] = a; 47 | } 48 | 49 | int result = lilysHomework(arr); 50 | 51 | cout << result << "\n"; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_1/problem-5/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the lilysHomework function below. 8 | int lilysHomework(vector arr) { 9 | map m1, m2; 10 | for(int i=0;i ascending_vector_v1, v2, descending_vector_v3, v4; 15 | ascending_vector_v1 = arr; 16 | v2 = arr; 17 | descending_vector_v3 = arr; 18 | v4 = arr; 19 | sort(ascending_vector_v1.begin(), ascending_vector_v1.end()); 20 | sort(descending_vector_v3.begin(), descending_vector_v3.end(), greater()); 21 | int swapctr_1 = 0, swapctr_2 = 0; 22 | for(int i=0;i> n; 49 | cin.ignore(numeric_limits::max(), '\n'); 50 | 51 | string arr_temp_temp; 52 | getline(cin, arr_temp_temp); 53 | 54 | vector arr_temp = split_string(arr_temp_temp); 55 | 56 | vector arr(n); 57 | 58 | for (int i = 0; i < n; i++) { 59 | int arr_item = stoi(arr_temp[i]); 60 | 61 | arr[i] = arr_item; 62 | } 63 | 64 | int result = lilysHomework(arr); 65 | 66 | fout << result << "\n"; 67 | 68 | fout.close(); 69 | 70 | return 0; 71 | } 72 | 73 | vector split_string(string input_string) { 74 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 75 | return x == y and x == ' '; 76 | }); 77 | 78 | input_string.erase(new_end, input_string.end()); 79 | 80 | while (input_string[input_string.length() - 1] == ' ') { 81 | input_string.pop_back(); 82 | } 83 | 84 | vector splits; 85 | char delimiter = ' '; 86 | 87 | size_t i = 0; 88 | size_t pos = input_string.find(delimiter); 89 | 90 | while (pos != string::npos) { 91 | splits.push_back(input_string.substr(i, pos - i)); 92 | 93 | i = pos + 1; 94 | pos = input_string.find(delimiter, i); 95 | } 96 | 97 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 98 | 99 | return splits; 100 | } 101 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-1/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int swappy(vector a, vector s){ 6 | map m; 7 | for(int i = 0; i < a.size(); i++){ 8 | m[a[i]] = i; 9 | } 10 | int c = 0; 11 | for(int i = 0; i < a.size(); i++){ 12 | if(a[i] != s[i]){ 13 | c = c + 1; 14 | int t = a[i]; 15 | a[i] = s[i]; 16 | int x = m[s[i]]; 17 | a[m[s[i]]] = t; 18 | m[t] = x; 19 | } 20 | } 21 | return c; 22 | } 23 | 24 | int fun(vector a) { 25 | vector v; 26 | v = a; 27 | sort(a.begin(),a.end()); 28 | int ac = swappy(v,a); 29 | return ac; 30 | } 31 | 32 | int main() 33 | { 34 | int t; 35 | cin >> t; 36 | while(t--){ 37 | int n; 38 | cin >> n; 39 | vector arr(n); 40 | 41 | for (int i = 0; i < n; i++) { 42 | int a; 43 | cin >> a; 44 | arr[i] = a; 45 | } 46 | 47 | int result = fun(arr); 48 | 49 | cout << result << "\n"; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-1/solution.py: -------------------------------------------------------------------------------- 1 | tc = int(input()) 2 | count = [] 3 | li = [] 4 | ans = [] 5 | for i in range(tc): 6 | count.append(input()) 7 | li.append(input()) 8 | for x1 in li: 9 | x1 = x1.replace(' ','') 10 | x = list(x1) 11 | s = sorted(x) 12 | cout = 0 13 | while (True): 14 | for i in range(len(x)): 15 | if(s[i] != x[i]): 16 | cout += 1 17 | t = s.index(x[i]) 18 | x[i],x[t] = x[t],x[i] 19 | #print(x, s, i,x[i],x[s.index(x[i])]) 20 | else: 21 | continue 22 | if(s == x): 23 | #print(s,x) 24 | break 25 | if(s == x): 26 | break 27 | ans.append(cout) 28 | for q in ans: 29 | print(q) 30 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-1/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >>t; 14 | while(t--) { 15 | int n; 16 | cin >> n; 17 | vector arr; 18 | unordered_map m1; 19 | int input; 20 | for(int i=0;i> input; 22 | arr.push_back(input); 23 | m1[arr[i]] = i; 24 | } 25 | 26 | vector v1, v2; 27 | v1 = arr; 28 | v2 = arr; 29 | sort(v1.begin(), v1.end()); 30 | 31 | int ctr = 0; 32 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define ll long long int 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int t; 12 | cin >> t; 13 | while(t--){ 14 | ll n; 15 | cin >> n; 16 | vector v; 17 | for(ll i = 0; i < n; i++){ 18 | ll a; 19 | cin >> a; 20 | v.push_back(a); 21 | } 22 | ll c = 0; 23 | for(ll i = 0;i < n; i++){ 24 | for(ll j = i+1; j < n; j++){ 25 | if(v[i] > v[j]){ 26 | c = c + 1; 27 | } 28 | } 29 | } 30 | cout << c << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-2/solution.py: -------------------------------------------------------------------------------- 1 | tc = int(input()) 2 | for z in range(tc): 3 | n = int(input()) 4 | x = input() 5 | x = x.replace(' ','') 6 | arr = list(x) 7 | #print(arr) 8 | inv = 0 9 | for i in range(n): 10 | for j in range(i + 1, n): 11 | if (arr[i] > arr[j]): 12 | inv += 1 13 | 14 | print(inv) 15 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-2/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin >> t; 13 | while(t--){ 14 | int n; 15 | cin >>n; 16 | int arr[n]; 17 | for(int i=0;i> arr[i]; 19 | } 20 | int ctr = 0; 21 | for(int i=0;i 2 | #define ll long long int 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | int n; 11 | cin >> n; 12 | vector v; 13 | ll maxi = -999; 14 | for(int i = 0; i < n; i++){ 15 | ll a; 16 | cin >> a; 17 | v.push_back(a); 18 | maxi = max(maxi,a); 19 | } 20 | vector c; 21 | c = v; 22 | map m; 23 | sort(c.begin(), c.end()); 24 | for(int i = 0; i < v.size(); i++){ 25 | m[c[i]] = i; 26 | } 27 | for(int i = 0; i < n; i++){ 28 | if(v[i] != maxi){ 29 | cout << c[m[v[i]]+1] << " "; 30 | }else{ 31 | cout << "_ "; 32 | } 33 | } 34 | cout << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-3/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | while(t--) { 15 | int n; 16 | cin >>n; 17 | vector arr; 18 | int input; 19 | 20 | for(int i=0;i> input; 22 | arr.push_back(input); 23 | } 24 | 25 | vector v1, v2; 26 | v1 = arr; 27 | unordered_map m1; 28 | sort(v1.begin(), v1.end()); 29 | for(int i=0;i0) 36 | cout << temp << " "; 37 | else 38 | cout << "_" << " "; 39 | } 40 | cout << endl; 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-4/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define ll long long int 7 | int p = 1; 8 | using namespace std; 9 | void fun(ll x, ll n){ 10 | cout << x << " "; 11 | x = p > 0 ? x - 5 : x + 5; 12 | if(x != n){ 13 | if(x <= 0){ 14 | p = 0; 15 | } 16 | fun(x,n); 17 | }else{ 18 | cout << x << endl; 19 | } 20 | } 21 | 22 | int main() { 23 | ll t; 24 | cin >> t; 25 | while(t--){ 26 | ll n; 27 | cin >> n; 28 | p = 1; 29 | if(n == 0){ 30 | cout << n << endl; 31 | }else{ 32 | fun(n,n); 33 | } 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Week-2/Editorials/Contest_2/problem-4/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int flag, orig; 8 | 9 | void func(int n) { 10 | if(n<=0) { 11 | printf("%d ", n); 12 | return; 13 | } 14 | printf("%d ", n); 15 | func(n-5); 16 | printf("%d ", n); 17 | } 18 | 19 | 20 | int main() { 21 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 22 | int t; 23 | cin >> t; 24 | while(t--) { 25 | int n; 26 | cin >> n; 27 | func(n); 28 | cout < data << endl; 15 | head = head -> next; 16 | } 17 | } 18 | 19 | // Credits : Vanamala Srikanth 20 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-1/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the printLinkedList function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | void printLinkedList(SinglyLinkedListNode* head) { 13 | while(head != NULL) { 14 | cout << head->data << endl; 15 | head = head->next; 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-2/solution.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtTail function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { 13 | if(head == NULL){ 14 | SinglyLinkedListNode* node = new SinglyLinkedListNode(data); 15 | return node; 16 | } 17 | SinglyLinkedListNode* s = head; 18 | while(s->next != NULL){ 19 | s = s -> next; 20 | } 21 | SinglyLinkedListNode* node = new SinglyLinkedListNode(data); 22 | s -> next = node; 23 | return head; 24 | } 25 | 26 | /* Credits : Vanamala Srikanth */ 27 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-2/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtTail function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { 13 | SinglyLinkedListNode* n1 = new SinglyLinkedListNode(data); 14 | if(head==NULL) { 15 | head = n1; 16 | return head; 17 | } 18 | SinglyLinkedListNode* temp=head; 19 | while(temp->next != NULL) { 20 | temp = temp->next; 21 | } 22 | temp->next = n1; 23 | return head; 24 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-3/solution.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtHead function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { 13 | SinglyLinkedListNode* node = new SinglyLinkedListNode(data); 14 | if(llist == NULL){ 15 | return node; 16 | } 17 | node -> next = llist; 18 | llist = node; 19 | return llist; 20 | } 21 | 22 | // Credits : Vanamala Srikanth 23 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-3/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtHead function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { 13 | SinglyLinkedListNode* temp = new SinglyLinkedListNode(data); 14 | temp->next = llist; 15 | llist = temp; 16 | return llist; 17 | 18 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-4/solution.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtPosition function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) { 13 | SinglyLinkedListNode* node = new SinglyLinkedListNode(data); 14 | SinglyLinkedListNode* s = head; 15 | int c = 1; 16 | while(s && c < position){ 17 | s = s -> next; 18 | c = c + 1; 19 | } 20 | node->next = s->next; 21 | s->next = node; 22 | return head; 23 | } 24 | 25 | 26 | // Credits : Vanamala Srikanth 27 | 28 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-4/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the insertNodeAtPosition function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) { 13 | SinglyLinkedListNode* temp = new SinglyLinkedListNode(data); 14 | if(position == 0) { 15 | temp->next = head; 16 | head = temp; 17 | return head; 18 | } 19 | SinglyLinkedListNode* temp2 = head; 20 | for(int i=0;inext; 22 | } 23 | temp ->next = temp2->next; 24 | temp2->next = temp; 25 | return head; 26 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-5/solution.cpp: -------------------------------------------------------------------------------- 1 | // Complete the deleteNode function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) { 13 | SinglyLinkedListNode* s = head; 14 | if(position == 0){ 15 | head = head -> next; 16 | return head; 17 | } 18 | int c = 1; 19 | while(s && c < position){ 20 | s = s -> next; 21 | c = c + 1; 22 | } 23 | if(s != NULL){ 24 | s->next = s -> next -> next; 25 | } 26 | return head; 27 | } 28 | 29 | // Credits : Vanamala Srikanth 30 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-5/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the deleteNode function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) { 13 | if(head == NULL) { 14 | return head; 15 | } 16 | SinglyLinkedListNode* temp = head; 17 | if(position==0) { 18 | head = head->next; 19 | free(temp); 20 | return head; 21 | } 22 | for(int i=0;inext; 24 | } 25 | SinglyLinkedListNode* temp2 = temp->next; 26 | temp->next = temp2->next; 27 | free(temp2); 28 | return head; 29 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-6/solution.cpp: -------------------------------------------------------------------------------- 1 | // Complete the reversePrint function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | void reversePrint(SinglyLinkedListNode* head) { 13 | vector v; 14 | while(head){ 15 | v.push_back(head->data); 16 | head = head -> next; 17 | } 18 | for(auto i = v.rbegin(); i != v.rend(); i++){ 19 | cout << *i << endl; 20 | } 21 | } 22 | 23 | 24 | // Credits : Vanamala Srikanth 25 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_1/Problem-6/solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the reversePrint function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | void reversePrint(SinglyLinkedListNode* head) { 13 | if(head==NULL) 14 | return; 15 | reversePrint(head->next); 16 | cout << head->data << endl; 17 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_1.cpp: -------------------------------------------------------------------------------- 1 | SinglyLinkedListNode* reverse(SinglyLinkedListNode* head) 2 | { 3 | if(head==nullptr||head->next==nullptr) 4 | return head; 5 | SinglyLinkedListNode* ans=new SinglyLinkedListNode(0); 6 | SinglyLinkedListNode* ans2 = ans; 7 | stacks; 8 | while(head!=nullptr) 9 | { 10 | s.push(head->data); 11 | head= head->next; 12 | } 13 | while(!s.empty()) 14 | { 15 | ans->next = new SinglyLinkedListNode(s.top()); 16 | s.pop(); 17 | ans=ans->next; 18 | } 19 | return ans2->next; 20 | } 21 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_1_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the reverse function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* reverse(SinglyLinkedListNode* head) { 13 | if(head==NULL) 14 | return head; 15 | SinglyLinkedListNode* prev = NULL; 16 | SinglyLinkedListNode* curr = head; 17 | SinglyLinkedListNode* temp=head; 18 | while(curr!=NULL) { 19 | temp = curr->next; 20 | curr->next=prev; 21 | prev = curr; 22 | curr = temp; 23 | } 24 | head = prev; 25 | return head; 26 | 27 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_2.cpp: -------------------------------------------------------------------------------- 1 | bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) 2 | { 3 | while(head1!=nullptr&&head2!=nullptr) 4 | { 5 | if(head1->data!=head2->data) 6 | return false; 7 | head1=head1->next; 8 | head2=head2->next; 9 | } 10 | if(head1==nullptr&&head2==nullptr) 11 | return true; 12 | return false; 13 | } 14 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_2_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the compare_lists function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { 13 | while(head1!=NULL && head2!=NULL) { 14 | if(head1->data != head2->data) { 15 | return false; 16 | } 17 | head1 = head1->next; 18 | head2 = head2->next; 19 | } 20 | if(head1==NULL && head2==NULL){ 21 | return true; 22 | } 23 | return false; 24 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_3.cpp: -------------------------------------------------------------------------------- 1 | SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) 2 | { 3 | SinglyLinkedListNode* ans = new SinglyLinkedListNode(0); 4 | SinglyLinkedListNode* temp = ans; 5 | while(head1!=nullptr||head2!=nullptr) 6 | { 7 | int t1 = 1001; 8 | int t2 = 1001; 9 | if(head1!=nullptr) 10 | t1=head1->data; 11 | if(head2!=nullptr) 12 | t2=head2->data; 13 | if(min(t1,t2)==t1) 14 | { 15 | ans->next = new SinglyLinkedListNode(t1); 16 | ans=ans->next; 17 | head1=head1->next; 18 | } 19 | else 20 | { 21 | ans->next = new SinglyLinkedListNode(t2); 22 | ans=ans->next; 23 | head2=head2->next; 24 | } 25 | } 26 | return temp->next; 27 | } 28 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_3_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the mergeLists function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { 13 | SinglyLinkedListNode* temp = head1; 14 | SinglyLinkedListNode* newhead; 15 | if(head1->data <= head2->data) { 16 | temp = head1; 17 | head1=head1->next; 18 | } 19 | else { 20 | temp = head2; 21 | head2 = head2->next; 22 | } 23 | newhead = temp; 24 | 25 | while(head1!=NULL && head2!=NULL) { 26 | if(head1->data <= head2->data) { 27 | temp->next = head1; 28 | head1=head1->next; 29 | temp = temp->next; 30 | } 31 | else { 32 | temp->next = head2; 33 | head2=head2->next; 34 | temp = temp->next; 35 | } 36 | } 37 | 38 | while(head1!=NULL) { 39 | temp->next = head1; 40 | head1=head1->next; 41 | temp = temp->next; 42 | } 43 | 44 | 45 | while(head2!=NULL) { 46 | temp->next = head2; 47 | head2=head2->next; 48 | temp = temp->next; 49 | } 50 | 51 | temp ->next = NULL; 52 | return newhead; 53 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_4.cpp: -------------------------------------------------------------------------------- 1 | int getNode(SinglyLinkedListNode* head, int positionFromTail) 2 | { 3 | int count=0; 4 | SinglyLinkedListNode* temp = head; 5 | while(head!=nullptr) 6 | { 7 | count++; 8 | head=head->next; 9 | } 10 | positionFromTail =count-positionFromTail; 11 | count=0; 12 | while(true) 13 | { 14 | count++; 15 | if(count==positionFromTail) 16 | return temp->data; 17 | temp=temp->next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_4_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the getNode function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | int getNode(SinglyLinkedListNode* head, int positionFromTail) { 13 | if(head==NULL) 14 | return -1; 15 | vector v1; 16 | v1.push_back(head->data); 17 | while(head->next != NULL) { 18 | head=head->next; 19 | v1.push_back(head->data); 20 | } 21 | return v1[v1.size()-1-positionFromTail]; 22 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_5.cpp: -------------------------------------------------------------------------------- 1 | SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* head) 2 | { 3 | SinglyLinkedListNode* ans = head; 4 | while(head!=nullptr) 5 | { 6 | while(head->next!=nullptr&&head->next->data==head->data) 7 | { 8 | head->next=head->next->next; 9 | } 10 | head=head->next; 11 | } 12 | return ans; 13 | } 14 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_5_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the removeDuplicates function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* head) { 13 | if(head==NULL) 14 | return head; 15 | SinglyLinkedListNode* temp = head; 16 | SinglyLinkedListNode* prev = NULL; 17 | unordered_map m1; 18 | while(temp != NULL) { 19 | m1[temp->data]++; 20 | if(m1[temp->data] > 1) { 21 | m1[temp->data]--; 22 | prev->next = temp->next; 23 | temp = temp->next; 24 | } 25 | else { 26 | prev=temp; 27 | temp = temp->next; 28 | } 29 | } 30 | return head; 31 | } 32 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_6.cpp: -------------------------------------------------------------------------------- 1 | int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) 2 | { 3 | int count1=0,count2=0; 4 | SinglyLinkedListNode* temp = head1; 5 | while(head1!=nullptr) 6 | { 7 | count1++; 8 | head1=head1->next; 9 | } 10 | head1=temp; 11 | temp=head2; 12 | while(head2!=nullptr) 13 | { 14 | count2++; 15 | head2=head2->next; 16 | } 17 | head2=temp; 18 | while(count1>count2) 19 | { 20 | head1=head1->next; 21 | count1--; 22 | } 23 | while(count2>count1) 24 | { 25 | head2=head2->next; 26 | count2--; 27 | } 28 | while(true) 29 | { 30 | if(head1==head2) 31 | { 32 | return head1->data; 33 | } 34 | head1=head1->next; 35 | head2=head2->next; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_6_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the findMergeNode function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | #include 13 | int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { 14 | SinglyLinkedListNode* cur1 = head1; 15 | SinglyLinkedListNode* cur2 = head2; 16 | while(!(cur1 == cur2)) { 17 | if(cur1->next == NULL) 18 | cur1 = head2; 19 | else 20 | cur1 = cur1->next; 21 | if(cur2->next == NULL) 22 | cur2 = head1; 23 | else 24 | cur2 = cur2->next; 25 | } 26 | return cur1->data; 27 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_7.cpp: -------------------------------------------------------------------------------- 1 | bool has_cycle(SinglyLinkedListNode* head) 2 | { 3 | int count=0; 4 | while(head!=nullptr) 5 | { 6 | count++; 7 | head=head->next; 8 | if(count>1000) 9 | return true; 10 | } 11 | return false; 12 | } 13 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_7_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the has_cycle function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode* next; 9 | * }; 10 | * 11 | */ 12 | bool has_cycle(SinglyLinkedListNode* head) { 13 | SinglyLinkedListNode* slow = head; 14 | SinglyLinkedListNode* fast = head; 15 | 16 | while(slow && fast && fast->next) { 17 | slow = slow->next; 18 | fast = fast->next->next; 19 | if(slow == fast) { 20 | return true; 21 | } 22 | } 23 | return false; 24 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_8.cpp: -------------------------------------------------------------------------------- 1 | DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) 2 | { 3 | if(head==nullptr) 4 | { 5 | head = new DoublyLinkedListNode(data); 6 | return head; 7 | } 8 | if(head->data>=data) 9 | { 10 | DoublyLinkedListNode* temp = new DoublyLinkedListNode(data); 11 | temp->next = head; 12 | temp->prev = nullptr; 13 | head->prev = temp; 14 | return temp; 15 | } 16 | DoublyLinkedListNode* temp = head; 17 | while(head->next!=nullptr&&head->next->datanext; 19 | DoublyLinkedListNode* firstnext = head->next; 20 | head->next = new DoublyLinkedListNode(data); 21 | head->next->prev = head; 22 | head->next->next = firstnext; 23 | if(head->next->next != nullptr) 24 | head->next->next->prev = head->next; 25 | return temp; 26 | } 27 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_8_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the sortedInsert function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * DoublyLinkedListNode { 7 | * int data; 8 | * DoublyLinkedListNode* next; 9 | * DoublyLinkedListNode* prev; 10 | * }; 11 | * 12 | */ 13 | DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) { 14 | DoublyLinkedListNode* n1 = new DoublyLinkedListNode(data); 15 | DoublyLinkedListNode* temp = head; 16 | 17 | if(data < head->data) { 18 | n1->next = head; 19 | head->prev = n1; 20 | head = n1; 21 | return head; 22 | } 23 | 24 | int flag = 0; 25 | 26 | while(temp->data <= data) { 27 | if(temp->next==NULL) { 28 | flag=1; 29 | break; 30 | } 31 | else { 32 | temp = temp->next; 33 | } 34 | } 35 | 36 | if(flag==1) { 37 | temp->next = n1; 38 | n1->prev = temp; 39 | return head; 40 | } 41 | 42 | temp = temp->prev; 43 | DoublyLinkedListNode* nx = temp->next; 44 | 45 | temp->next = n1; 46 | n1->prev = temp; 47 | nx->prev = n1; 48 | n1->next = nx; 49 | 50 | return head; 51 | 52 | } -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_9.cpp: -------------------------------------------------------------------------------- 1 | DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) 2 | { 3 | if(head==nullptr) 4 | return head; 5 | while(true) 6 | { 7 | swap(head->next, head->prev); 8 | if(head->prev == nullptr) 9 | break; 10 | head = head->prev; 11 | } 12 | return head; 13 | } 14 | -------------------------------------------------------------------------------- /Week-3/Editorials/PracticeContest_2/Problem_9_solution2.cpp: -------------------------------------------------------------------------------- 1 | // Complete the reverse function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * DoublyLinkedListNode { 7 | * int data; 8 | * DoublyLinkedListNode* next; 9 | * DoublyLinkedListNode* prev; 10 | * }; 11 | * 12 | */ 13 | DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) { 14 | if(head==NULL) 15 | return head; 16 | DoublyLinkedListNode* temp = head; 17 | DoublyLinkedListNode* prev = NULL; 18 | DoublyLinkedListNode* nx = head; 19 | 20 | while(temp->next) { 21 | nx = temp->next; 22 | temp->next = prev; 23 | temp->prev = nx; 24 | prev = temp; 25 | temp = temp->prev; 26 | } 27 | temp->next = prev; 28 | temp->prev = NULL; 29 | head = temp; 30 | return head; 31 | 32 | } -------------------------------------------------------------------------------- /Week-3/README.md: -------------------------------------------------------------------------------- 1 | # WEEK [Aug 17 - Aug 23] 2 | 3 | ## Topics 4 | 5 | 1. Linked List 6 | 2. Doubly Linked List 7 | 8 | ## Resources 9 | 10 | **Resources for Linked List in CPP** 11 | - Videos 12 | 1. https://www.youtube.com/watch?v=utc8bwTDjLk&list=PLqM7alHXFySH41ZxzrPNj2pAYPOI8ITe7 13 | 2. https://www.youtube.com/watch?v=92S4zgXN17o&list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P 14 | 3. https://www.youtube.com/watch?v=moQzUFqaV8Q 15 | 4. https://www.youtube.com/watch?v=hzQuDi4yf40 16 | 17 | - Articles 18 | 1. https://www.geeksforgeeks.org/data-structures/linked-list/ 19 | 20 | **Resources for Linked List in JAVA** 21 | 22 | - Videos: 23 | 1. https://www.youtube.com/watch?v=SMIq13-FZSE 24 | 2. https://www.youtube.com/watch?v=6KKIZL1wt8o 25 | 3. https://www.youtube.com/playlist?list=PL6Zs6LgrJj3ssQEe2VasrL9MWpjp0kHVl 26 | 27 | - Articles: 28 | 1. https://www.javatpoint.com/java-linkedlist 29 | 2. https://www.geeksforgeeks.org/data-structures/linked-list/ 30 | 31 | ## Links to Practice Contests 32 | 33 | ### Wednesday 34 | * Practice contest #1: https://www.hackerrank.com/cpbuddyseries-week3practicecontest1 35 | 36 | ### Thursday 37 | * Practice contest #2: https://www.hackerrank.com/cpbuddyseries-week3practicecontest2 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | int t; 12 | cin>>t; 13 | stacks; 14 | stacksmax; 15 | s.push(0); 16 | smax.push(0); 17 | while(t--) 18 | { 19 | int type; 20 | cin>>type; 21 | if(type==1) 22 | { 23 | int x; 24 | cin>>x; 25 | s.push(x); 26 | if(smax.top()<=x) 27 | smax.push(x); 28 | } 29 | else if(type==2) 30 | { 31 | if(smax.top()==s.top()) 32 | smax.pop(); 33 | s.pop(); 34 | } 35 | else 36 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | 12 | int main() { 13 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 14 | int n; 15 | cin >> n; 16 | stack s1; 17 | stack max; 18 | max.push(0); 19 | while(n--) { 20 | int x; 21 | cin >> x; 22 | if(x==1) { 23 | cin >> x; 24 | if(x>=max.top()) 25 | max.push(x); 26 | s1.push(x); 27 | } 28 | else if(x==2) { 29 | if(s1.top() == max.top()) 30 | max.pop(); 31 | s1.pop(); 32 | } 33 | else { 34 | cout << max.top() << endl; 35 | } 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_2.cpp: -------------------------------------------------------------------------------- 1 | int equalStacks(vector h1, vector h2, vector h3) 2 | { 3 | stacks1,s2,s3; 4 | s1.push(0); 5 | s2.push(0); 6 | s3.push(0); 7 | for(int i=h1.size()-1;i>=0;i--) 8 | s1.push(s1.top()+h1[i]); 9 | for(int i=h2.size()-1;i>=0;i--) 10 | s2.push(s2.top()+h2[i]); 11 | for(int i=h3.size()-1;i>=0;i--) 12 | s3.push(s3.top()+h3[i]); 13 | while(true) 14 | { 15 | if(s1.top()==s2.top()&&s1.top()==s3.top()) 16 | return s1.top(); 17 | int temp = min(s1.top(),min(s2.top(),s3.top())); 18 | while(s1.top()>temp) 19 | s1.pop(); 20 | while(s2.top()>temp) 21 | s2.pop(); 22 | while(s3.top()>temp) 23 | s3.pop(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_2_solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | /* 8 | * Complete the equalStacks function below. 9 | */ 10 | map m1; 11 | void helper(vector &v){ 12 | int sum = 0; 13 | m1[sum]++; 14 | for(int i=v.size()-1;i>=0;--i){ 15 | sum+=v[i]; 16 | m1[sum]++; 17 | } 18 | } 19 | int equalStacks(vector h1, vector h2, vector h3) { 20 | /* 21 | * Write your code here. 22 | */ 23 | helper(h1); 24 | helper(h2); 25 | helper(h3); 26 | int solution = 0; 27 | for(auto itr:m1) 28 | if(itr.second==3) 29 | solution = itr.first; 30 | return solution; 31 | } 32 | 33 | int main() 34 | { 35 | ofstream fout(getenv("OUTPUT_PATH")); 36 | 37 | string n1N2N3_temp; 38 | getline(cin, n1N2N3_temp); 39 | 40 | vector n1N2N3 = split_string(n1N2N3_temp); 41 | 42 | int n1 = stoi(n1N2N3[0]); 43 | 44 | int n2 = stoi(n1N2N3[1]); 45 | 46 | int n3 = stoi(n1N2N3[2]); 47 | 48 | string h1_temp_temp; 49 | getline(cin, h1_temp_temp); 50 | 51 | vector h1_temp = split_string(h1_temp_temp); 52 | 53 | vector h1(n1); 54 | 55 | for (int h1_itr = 0; h1_itr < n1; h1_itr++) { 56 | int h1_item = stoi(h1_temp[h1_itr]); 57 | 58 | h1[h1_itr] = h1_item; 59 | } 60 | 61 | string h2_temp_temp; 62 | getline(cin, h2_temp_temp); 63 | 64 | vector h2_temp = split_string(h2_temp_temp); 65 | 66 | vector h2(n2); 67 | 68 | for (int h2_itr = 0; h2_itr < n2; h2_itr++) { 69 | int h2_item = stoi(h2_temp[h2_itr]); 70 | 71 | h2[h2_itr] = h2_item; 72 | } 73 | 74 | string h3_temp_temp; 75 | getline(cin, h3_temp_temp); 76 | 77 | vector h3_temp = split_string(h3_temp_temp); 78 | 79 | vector h3(n3); 80 | 81 | for (int h3_itr = 0; h3_itr < n3; h3_itr++) { 82 | int h3_item = stoi(h3_temp[h3_itr]); 83 | 84 | h3[h3_itr] = h3_item; 85 | } 86 | 87 | int result = equalStacks(h1, h2, h3); 88 | 89 | fout << result << "\n"; 90 | 91 | fout.close(); 92 | 93 | return 0; 94 | } 95 | 96 | vector split_string(string input_string) { 97 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 98 | return x == y and x == ' '; 99 | }); 100 | 101 | input_string.erase(new_end, input_string.end()); 102 | 103 | while (input_string[input_string.length() - 1] == ' ') { 104 | input_string.pop_back(); 105 | } 106 | 107 | vector splits; 108 | char delimiter = ' '; 109 | 110 | size_t i = 0; 111 | size_t pos = input_string.find(delimiter); 112 | 113 | while (pos != string::npos) { 114 | splits.push_back(input_string.substr(i, pos - i)); 115 | 116 | i = pos + 1; 117 | pos = input_string.find(delimiter, i); 118 | } 119 | 120 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 121 | 122 | return splits; 123 | } 124 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_3.cpp: -------------------------------------------------------------------------------- 1 | string isBalanced(string s) 2 | { 3 | stackst; 4 | for(auto i:s) 5 | { 6 | if(i=='('||i=='{'||i=='[') 7 | st.push(i); 8 | else 9 | { 10 | if(st.size()==0) 11 | return "NO"; 12 | char c = st.top(); 13 | if(i==']'&&c!='[') 14 | return "NO"; 15 | if(i==')'&&c!='(') 16 | return "NO"; 17 | if(i=='}'&&c!='{') 18 | return "NO"; 19 | st.pop(); 20 | } 21 | } 22 | if(st.size()==0) 23 | return "YES"; 24 | return "NO"; 25 | } 26 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_3_solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the isBalanced function below. 6 | string isBalanced(string s) { 7 | stack s1; 8 | int flag = 0; 9 | mapm1; 10 | m1[')'] ='('; 11 | m1[']'] = '['; 12 | m1['}'] = '{'; 13 | for(int i=0;s[i]!='\0';i++) { 14 | if(s[i]=='{' || s[i]=='[' || s[i]=='(') { 15 | s1.push(s[i]); 16 | } 17 | else { 18 | if(!s1.empty() && m1[s[i]]==s1.top()) 19 | s1.pop(); 20 | else { 21 | flag=1; 22 | break; 23 | } 24 | } 25 | } 26 | 27 | if(!flag && s1.empty()) { 28 | return "YES"; 29 | } 30 | else { 31 | return "NO"; 32 | } 33 | 34 | } 35 | 36 | int main() 37 | { 38 | ofstream fout(getenv("OUTPUT_PATH")); 39 | 40 | int t; 41 | cin >> t; 42 | cin.ignore(numeric_limits::max(), '\n'); 43 | 44 | for (int t_itr = 0; t_itr < t; t_itr++) { 45 | string s; 46 | getline(cin, s); 47 | 48 | string result = isBalanced(s); 49 | 50 | fout << result << "\n"; 51 | } 52 | 53 | fout.close(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vectordp(1000001,1000001); 6 | void precompute() 7 | { 8 | dp[0]=0; 9 | dp[1]=1; 10 | for(int i=2;i<1000001;i++) 11 | { 12 | dp[i]=min(dp[i],dp[i-1]+1); 13 | for(int j=1;j*i<1000001&&j<=i;j+=1) 14 | { 15 | dp[i*j]=min(dp[i*j],dp[i]+1); 16 | } 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | precompute(); 23 | int q; 24 | cin >> q; 25 | while(q--) 26 | { 27 | int n; 28 | cin>>n; 29 | cout< 2 | 3 | using namespace std; 4 | 5 | /* 6 | * Complete the downToZero function below. 7 | */ 8 | int downToZero(int n) { 9 | /* 10 | * Write your code here. 11 | */ 12 | if(n==0) { 13 | return 0; 14 | } 15 | int ctr[1000007]; 16 | queue q; 17 | q.push(n); 18 | ctr[n] = 1; 19 | while(!q.empty()) { 20 | int curr = q.front(); 21 | q.pop(); 22 | if(ctr[curr-1] == 0) { 23 | ctr[curr-1] = ctr[curr] + 1; 24 | if(curr-1 == 0) 25 | break; 26 | q.push(curr-1); 27 | } 28 | for(int i=2;i*i<=curr;i++) { 29 | if(curr%i == 0) { 30 | int factor = max(i, curr/i); 31 | if(ctr[factor] == 0) { 32 | ctr[factor] = ctr[curr] + 1; 33 | q.push(factor); 34 | } 35 | } 36 | } 37 | } 38 | int tmp = ctr[0] - 1; 39 | memset(ctr, 0, sizeof(ctr)); 40 | return tmp; 41 | } 42 | 43 | int main() 44 | { 45 | ofstream fout(getenv("OUTPUT_PATH")); 46 | 47 | int q; 48 | cin >> q; 49 | cin.ignore(numeric_limits::max(), '\n'); 50 | 51 | for (int q_itr = 0; q_itr < q; q_itr++) { 52 | int n; 53 | cin >> n; 54 | cin.ignore(numeric_limits::max(), '\n'); 55 | 56 | int result = downToZero(n); 57 | 58 | fout << result << "\n"; 59 | } 60 | 61 | fout.close(); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Week-4/Editorials/Contest_1/problem_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int t; 12 | cin>>t; 13 | stacks1,s2; 14 | while(t--) 15 | { 16 | int type; 17 | cin>>type; 18 | if(type==1) 19 | { 20 | int val; 21 | cin>>val; 22 | s1.push(val); 23 | } 24 | else 25 | { 26 | if(s2.empty()) 27 | { 28 | while(!s1.empty()) 29 | { 30 | s2.push(s1.top()); 31 | s1.pop(); 32 | } 33 | } 34 | if(type==2) 35 | s2.pop(); 36 | else 37 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | void helper(stack &s1, stack &s2) { 10 | while(!s1.empty()) { 11 | s2.push(s1.top()); 12 | s1.pop(); 13 | } 14 | } 15 | 16 | 17 | int main() { 18 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 19 | int q; 20 | cin >>q; 21 | stack s1; 22 | stack s2; 23 | 24 | while(q--) { 25 | int x; 26 | cin >>x; 27 | switch(x) { 28 | case 1: 29 | cin >> x; 30 | s1.push(x); 31 | break; 32 | case 2: 33 | if(s2.empty()) 34 | helper(s1, s2); 35 | s2.pop(); 36 | break; 37 | case 3: 38 | if(s2.empty()) { 39 | helper(s1, s2); 40 | } 41 | cout << s2.top() << endl; 42 | break; 43 | default: 44 | break; 45 | } 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Week-4/readme.md: -------------------------------------------------------------------------------- 1 | # WEEK 4 [24/08/2020 - 30/08/2020] 2 | 3 | ## Topics 4 | 5 | 1. Stack 6 | 2. Queue 7 | 3. Doubly-ended queue 8 | 9 | ## Resources 10 | 11 | **Video Lectures** 12 | 13 | 1. Stacks: 14 | - https://www.youtube.com/playlist?list=PLqM7alHXFySF7Lap-wi5qlaD8OEBx9RMV 15 | 16 | 2. Queue: 17 | - https://www.youtube.com/playlist?list=PLqM7alHXFySG6wgjVeEat_ouTIi0IBQ6D 18 | 19 | *Some Playlist of Data Structure which will help you: We have written the number of videos which contains topics of this week in bracket.* 20 | 21 | - https://www.youtube.com/playlist?list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P (Watch from Video no 14 to 21) 22 | - https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU 23 | *(watch from video no 28 to 38 and 41 to 48)* 24 | 25 | ## Links to Practice Contests 26 | 27 | ### Wednesday - https://www.hackerrank.com/cpbuddyseries-week4contest1 28 | 29 | 30 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-1.cpp: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node *left; 8 | Node *right; 9 | Node(int d) { 10 | data = d; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | */ 17 | 18 | void preOrder(Node *root) { 19 | if(root == NULL) 20 | return; 21 | cout << root->data << " "; 22 | if(root->left) { 23 | preOrder(root->left); 24 | } 25 | if(root->right) { 26 | preOrder(root->right); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-2.cpp: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node *left; 8 | Node *right; 9 | Node(int d) { 10 | data = d; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | */ 17 | 18 | 19 | void postOrder(Node *root) { 20 | if(root == NULL) 21 | return; 22 | if(root->left) { 23 | postOrder(root->left); 24 | } 25 | if(root->right) { 26 | postOrder(root->right); 27 | } 28 | cout << root->data << " "; 29 | } 30 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-3.cpp: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node *left; 8 | Node *right; 9 | Node(int d) { 10 | data = d; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | */ 17 | 18 | void inOrder(Node *root) { 19 | if(root == NULL) 20 | return; 21 | if(root->left) { 22 | inOrder(root->left); 23 | } 24 | cout << root->data << " "; 25 | if(root->right) { 26 | inOrder(root->right); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-4.cpp: -------------------------------------------------------------------------------- 1 | 2 | /*The tree node has data, left child and right child 3 | class Node { 4 | int data; 5 | Node* left; 6 | Node* right; 7 | }; 8 | 9 | */ 10 | int height(Node* root) { 11 | // Write your code here. 12 | if(root == NULL) { 13 | return -1; 14 | } 15 | int l = height(root->left); 16 | int r = height(root->right); 17 | return 1+max(l, r); 18 | } 19 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | public: 4 | int data; 5 | Node *left; 6 | Node *right; 7 | Node(int d) { 8 | data = d; 9 | left = NULL; 10 | right = NULL; 11 | } 12 | }; 13 | 14 | */ 15 | 16 | void topView(Node * root) { 17 | queue> q; 18 | map unique_level; 19 | q.push(make_pair(0, root)); 20 | for(auto i = q.front(); !q.empty(); q.pop(), i=q.front()) { 21 | if(!i.second) 22 | continue; 23 | unique_level.insert(i); 24 | q.push(make_pair(i.first+1, i.second->right)); 25 | q.push(make_pair(i.first-1, i.second->left)); 26 | } 27 | for(auto i:unique_level) { 28 | cout << i.second->data << " "; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Week-5/Editorials/Contest-1/problem-6.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class Node { 4 | public: 5 | int data; 6 | Node *left; 7 | Node *right; 8 | Node(int d) { 9 | data = d; 10 | left = NULL; 11 | right = NULL; 12 | } 13 | }; 14 | */ 15 | 16 | void levelOrder(Node * root) { 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()){ 20 | Node* x = q.front(); 21 | q.pop(); 22 | cout << x->data << " "; 23 | if(x->left) { 24 | q.push(x->left); 25 | } 26 | if(x->right) { 27 | q.push(x->right); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Week-5/readme.md: -------------------------------------------------------------------------------- 1 | # WEEK 5 [31/08/2020 - 05/09/2020] 2 | 3 | ## Topics 4 | 5 | 1. Trees 6 | 2. Heaps 7 | 8 | ## Resources 9 | 10 | **Video Lectures** 11 | 12 | 1. TREES 13 | 14 | - Video Resources 15 | https://www.youtube.com/playlist?list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk 16 | 17 | - Articles 18 | https://www.programiz.com/dsa/trees 19 | 20 | 21 | 2. HEAPS 22 | 23 | - Video Resources 24 | https://www.youtube.com/watch?v=hW8PrQrvMNc&list=PL_z_8CaSLPWdtY9W22VjnPxG30CXNZpI9 25 | 26 | 27 | - Articles 28 | https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/tutorial/ 29 | 30 | ## Links to Practice Contests 31 | 32 | ### Wednesday - https://www.hackerrank.com/cpbuddyseries-practicecontest2 33 | ### Friday - https://www.hackerrank.com/cpbuddyseries-week5contest2 34 | 35 | 36 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-1/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int t; 12 | cin>>t; 13 | while(t--) 14 | { 15 | int p; 16 | cin>>p; 17 | int a=2*p-2; 18 | cout< 2 | 3 | using namespace std; 4 | 5 | int lowestTriangle(int base, int area){ 6 | // Complete this function 7 | int h; 8 | double ht; 9 | ht=(double)2*area/base; 10 | //cout<> base >> area; 19 | int height = lowestTriangle(base, area); 20 | cout << height << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-2/solution_1.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | import math 3 | import sys 4 | 5 | def lowestTriangle(base, area): 6 | return math.ceil(2*area/base) 7 | 8 | base, area = input().strip().split(' ') 9 | base, area = [int(base), int(area)] 10 | height = lowestTriangle(base, area) 11 | print(height) 12 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-3/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | * Complete the handshake function below. 7 | */ 8 | int handshake(int n) { 9 | /* 10 | * Write your code here. 11 | */ 12 | int p=n*(n-1)/2; 13 | return p; 14 | 15 | } 16 | 17 | int main() 18 | { 19 | ofstream fout(getenv("OUTPUT_PATH")); 20 | 21 | int t; 22 | cin >> t; 23 | cin.ignore(numeric_limits::max(), '\n'); 24 | 25 | for (int t_itr = 0; t_itr < t; t_itr++) { 26 | int n; 27 | cin >> n; 28 | cin.ignore(numeric_limits::max(), '\n'); 29 | 30 | int result = handshake(n); 31 | 32 | fout << result << "\n"; 33 | } 34 | 35 | fout.close(); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-3/solution_1.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | # 7 | # Complete the handshake function below. 8 | # 9 | def handshake(n): 10 | return int(n * (n-1) / 2) 11 | 12 | if __name__ == '__main__': 13 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 14 | 15 | t = int(input()) 16 | 17 | for t_itr in range(t): 18 | n = int(input()) 19 | 20 | result = handshake(n) 21 | 22 | fptr.write(str(result) + '\n') 23 | 24 | fptr.close() 25 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-4/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | * Complete the maximumDraws function below. 7 | */ 8 | int maximumDraws(int n) { 9 | /* 10 | * Write your code here. 11 | */ 12 | return n+1; 13 | 14 | } 15 | 16 | int main() 17 | { 18 | ofstream fout(getenv("OUTPUT_PATH")); 19 | 20 | int t; 21 | cin >> t; 22 | cin.ignore(numeric_limits::max(), '\n'); 23 | 24 | for (int t_itr = 0; t_itr < t; t_itr++) { 25 | int n; 26 | cin >> n; 27 | cin.ignore(numeric_limits::max(), '\n'); 28 | 29 | int result = maximumDraws(n); 30 | 31 | fout << result << "\n"; 32 | } 33 | 34 | fout.close(); 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-4/solution_1.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | # 7 | # Complete the maximumDraws function below. 8 | # 9 | def maximumDraws(n): 10 | return n+1 11 | 12 | if __name__ == '__main__': 13 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 14 | 15 | t = int(input()) 16 | 17 | for t_itr in range(t): 18 | n = int(input()) 19 | 20 | result = maximumDraws(n) 21 | 22 | fptr.write(str(result) + '\n') 23 | 24 | fptr.close() 25 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-5/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | /* 8 | * Complete the findPoint function below. 9 | */ 10 | vector findPoint(int px, int py, int qx, int qy) { 11 | /* 12 | * Write your code here. 13 | */ 14 | 15 | vector ans; 16 | ans.push_back(2*qx-px); 17 | ans.push_back(2*qy-py); 18 | return ans; 19 | } 20 | 21 | int main() 22 | { 23 | ofstream fout(getenv("OUTPUT_PATH")); 24 | 25 | int n; 26 | cin >> n; 27 | cin.ignore(numeric_limits::max(), '\n'); 28 | 29 | for (int n_itr = 0; n_itr < n; n_itr++) { 30 | string pxPyQxQy_temp; 31 | getline(cin, pxPyQxQy_temp); 32 | 33 | vector pxPyQxQy = split_string(pxPyQxQy_temp); 34 | 35 | int px = stoi(pxPyQxQy[0]); 36 | 37 | int py = stoi(pxPyQxQy[1]); 38 | 39 | int qx = stoi(pxPyQxQy[2]); 40 | 41 | int qy = stoi(pxPyQxQy[3]); 42 | 43 | vector result = findPoint(px, py, qx, qy); 44 | 45 | for (int result_itr = 0; result_itr < (int)result.size(); result_itr++) { 46 | fout << result[result_itr]; 47 | 48 | if (result_itr != (int)result.size() - 1) { 49 | fout << " "; 50 | } 51 | } 52 | 53 | fout << "\n"; 54 | } 55 | 56 | fout.close(); 57 | 58 | return 0; 59 | } 60 | 61 | vector split_string(string input_string) { 62 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 63 | return x == y and x == ' '; 64 | }); 65 | 66 | input_string.erase(new_end, input_string.end()); 67 | 68 | while (input_string[input_string.length() - 1] == ' ') { 69 | input_string.pop_back(); 70 | } 71 | 72 | vector splits; 73 | char delimiter = ' '; 74 | 75 | size_t i = 0; 76 | size_t pos = input_string.find(delimiter); 77 | 78 | while (pos != string::npos) { 79 | splits.push_back(input_string.substr(i, pos - i)); 80 | 81 | i = pos + 1; 82 | pos = input_string.find(delimiter, i); 83 | } 84 | 85 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 86 | 87 | return splits; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Week-6/Editorials/Contest_1/Problem-5/solution_1.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | import sys 5 | 6 | # 7 | # Complete the findPoint function below. 8 | # 9 | def findPoint(px, py, qx, qy): 10 | return 2*qx - px, 2*qy-py 11 | 12 | if __name__ == '__main__': 13 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 14 | 15 | n = int(input()) 16 | 17 | for n_itr in range(n): 18 | pxPyQxQy = input().split() 19 | 20 | px = int(pxPyQxQy[0]) 21 | 22 | py = int(pxPyQxQy[1]) 23 | 24 | qx = int(pxPyQxQy[2]) 25 | 26 | qy = int(pxPyQxQy[3]) 27 | 28 | result = findPoint(px, py, qx, qy) 29 | 30 | fptr.write(' '.join(map(str, result))) 31 | fptr.write('\n') 32 | 33 | fptr.close() 34 | 35 | -------------------------------------------------------------------------------- /Week-6/readme.md: -------------------------------------------------------------------------------- 1 | # WEEK 6 [06/09/2020 - 13/09/2020] 2 | 3 | ## Topics 4 | 5 | 1. Bit Manipulation 6 | 2. Number Theory 7 | 8 | ## Resources 9 | 10 | **Video Lectures** 11 | 12 | 1. BIT Manupulation 13 | - https://www.youtube.com/playlist?list=PLqM7alHXFySF8B9KqOy6yz4vggu4tiNMP 14 | 15 | 2. Number Theory 16 | - https://www.youtube.com/playlist?list=PL2q4fbVm1Ik4liHX78IRslXzUr8z5QxsG 17 | 18 | 19 | 20 | ## Links to Practice Contests 21 | 22 | ### Wednesday - https://www.hackerrank.com/cpbuddyseriesweek6contest1 23 | 24 | -------------------------------------------------------------------------------- /Week-7/readme.md: -------------------------------------------------------------------------------- 1 | # WEEK 7 [14/09/2020 - 21/09/2020] 2 | 3 | ## Topics 4 | 5 | 1. Divide and conquer 6 | 2. Binary Search 7 | 8 | ## Resources 9 | 10 | 1. Video Lectures 11 | #### Divide and Conquer: 12 | - https://youtu.be/EzeYI7p9MjU 13 | - https://youtu.be/iTMn0Kt18tg 14 | - https://youtu.be/hmReJCupbNU 15 | - https://www.youtube.com/playlist?list=PLTZbNwgO5ebr2Xh9ensrgvzEY1eQAn48J 16 | 17 | #### Binary Search with its implementation: 18 | - https://www.youtube.com/playlist?list=PL2_aWCzGMAwL3ldWlrii6YeLszojgH77j 19 | - https://www.youtube.com/playlist?list=PL_z_8CaSLPWeYfhtuKHj-9MpYb6XQJ_f2 20 | - https://youtu.be/LcWPKR1uef4 21 | 22 | 2. Some Articles: 23 | - https://www.geeksforgeeks.org/divide-and-conquer-algorithm-introduction/ 24 | - https://www.geeksforgeeks.org/binary-search/ 25 | 26 | 3. Some questions for your practice: https://practice.geeksforgeeks.org/explore/?category%5B%5D=Binary%20Search&category%5B%5D=Divide%20and%20Conquer&problemType=full&page=1 27 | 28 | 29 | ## Links to Practice Contests 30 | 31 | ### Contest 1 Problems: 32 | 1. https://practice.geeksforgeeks.org/problems/merge-sort/1 33 | 2. https://practice.geeksforgeeks.org/problems/find-the-highest-number2259/1 34 | 3. https://practice.geeksforgeeks.org/problems/k-sorted-array1610/1 35 | 4. https://practice.geeksforgeeks.org/problems/array-subset-of-another-array/0 36 | 5. https://practice.geeksforgeeks.org/problems/the-nth-fibonnaci/0 37 | -------------------------------------------------------------------------------- /Week-8/readme.md: -------------------------------------------------------------------------------- 1 | # WEEK 8 [21/09/2020 - 28/09/2020] 2 | 3 | ## Topics 4 | 5 | 1. Backtracking 6 | 2. Greedy Algorithms 7 | 8 | ## Resources 9 | 10 | 1. Video Lectures 11 | #### Backtracking: 12 | - https://youtu.be/WW5u8RTu44Y 13 | - https://youtu.be/adF61qWX20o 14 | - https://www.youtube.com/playlist?list=PLX4N9vQU5pGZsIjhkOmCExawJMOTsP2WI 15 | - https://www.youtube.com/playlist?list=PLTZbNwgO5ebpqWBmBx0lpy9IYFMSQcrL- 16 | 17 | #### Greedy Algorithms: 18 | - https://youtu.be/-QcPo_DWJk4 19 | - https://youtu.be/tKwnms5iRBU 20 | - https://www.youtube.com/playlist?list=PLqM7alHXFySESatj68JKWHRVhoJ1BxtLW 21 | - https://youtu.be/jwEUdGPBqJshttps://www.youtube.com/playlist?list=PLfFeAJ-vQopt_S5XlayyvDFL_mi2pGJE3 22 | 23 | 24 | ## Some questions for your practice: 25 | https://practice.geeksforgeeks.org/explore/?category%5B%5D=Greedy&category%5B%5D=Backtracking&difficulty%5B%5D=0&difficulty%5B%5D=1&difficulty%5B%5D=2&page=1 26 | 27 | 28 | -------------------------------------------------------------------------------- /Week-Template.md: -------------------------------------------------------------------------------- 1 | # WEEK X [Mon DD - Mon DD] 2 | 3 | ## Topics 4 | 5 | 1. Topic-1 6 | 2. Topic-2 7 | 3. Topic-3 8 | 9 | ## Resources 10 | 11 | - [Resource 1](#) 12 | - [Resource 2](#) 13 | - [Resource 3](#) 14 | 15 | ## Links to Practice Contests 16 | 17 | ### [Wednesday](#) 18 | 19 | ### [Saturday](#) 20 | 21 | ### [Sunday](#) 22 | 23 | ## Announcements 24 | 25 | - [announcement-1](#) 26 | - [announcement-2](#) 27 | - [announcement-3](#) 28 | --------------------------------------------------------------------------------