├── Step 1 - Learn the basics ├── Step 1.4 - Know Basic Maths │ ├── 4) GCD or HCF.cpp │ ├── 6) Sum of all divisors.cpp │ ├── 1) Count Digits.cpp │ ├── 3) Palindrome number.cpp │ ├── 7) Check Prime.cpp │ ├── 2) Reverse Bits.cpp │ └── 5) Check Armstrong.cpp ├── Step 1.1 - Things to Know in C++. │ ├── 3) if-else (Decision Making).cpp │ ├── 4) Switch Case statement.cpp │ ├── 1) Find Character Case.cpp │ ├── 2) Data Type.cpp │ ├── 6) Sum of even & odd.cpp │ ├── 7) FA-1 ECE.cpp │ └── 5) Nth Fibonacci Number.cpp ├── Step 1.5 - Learn Basic Recursion │ ├── 4) Sum Of First N Numbers.cpp │ ├── 1) 1 to N Without Loop.cpp │ ├── 2) Print n times.cpp │ ├── 3) N to 1 without loop.cpp │ ├── 7) Check Palindrome (recursive).cpp │ ├── 6) Reverse an Array.cpp │ ├── 5) Factorial Numbers Not Greater Than N.cpp │ └── 8) Print Fibonacci Series.cpp ├── Step 1.6 - Learn Basic Hashing │ ├── 1) Count Frequency in a range.cpp │ └── 2) Highest -Lowest Frequency Elements.cpp ├── Step 1.2 - Build-up Logical Thinking via Patterns. │ ├── 5) Pattern 5.cpp │ ├── 6) Pattern 6.cpp │ ├── 1) Pattern 1.cpp │ ├── 2) Pattern 2.cpp │ ├── 15) Pattern 15.cpp │ ├── 3) Pattern 3.cpp │ ├── 4) Pattern 4.cpp │ ├── 14) Pattern 14.cpp │ ├── 16) Pattern 16.cpp │ ├── 18) Pattern 18.cpp │ ├── 13) Pattern 13.cpp │ ├── 11) Pattern 11.cpp │ ├── 8) Pattern 8.cpp │ ├── 7) Pattern 7.cpp │ ├── 10) Pattern 10.cpp │ ├── 17) Pattern 17.cpp │ ├── 12) Pattern 12.cpp │ ├── 21) Pattern 21.cpp │ ├── 9) Pattern 9.cpp │ ├── 19) Pattern 19.cpp │ ├── 20) Pattern 20.cpp │ └── 22) Pattern 22.cpp └── Step 1.3 - Learn STL │ ├── 1) Pairs in C++ STL.cpp │ ├── 5) Queue in C++ STL.cpp │ ├── 4) Stack in C++ STL.cpp │ ├── 7) Priority Queue in C++ STL.cpp │ ├── 6) Deque in C++ STL.cpp │ ├── 12) Map in C++ STL.cpp │ ├── 8) Set in C++ STL.cpp │ ├── 10) Multiset in C++ STL.cpp │ ├── 3) List in C++ STL.cpp │ ├── 13) Unordered-map in C++ STL.cpp │ ├── 9) Unordered-set in C++ STL.cpp │ ├── 14) Multimap in C++ STL.cpp │ └── 11) Unordered-multiset in C++ STL.cpp ├── Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard] ├── Step 3.1 - Easy │ ├── 10) Missing Number.cpp │ ├── 12) Find The Single Element (UnsortedArray).cpp │ ├── 8) Linear Search.cpp │ ├── 6) Rotate array.cpp │ ├── 3) Check Sorted Array.cpp │ ├── 7) Move Zero's to End.cpp │ ├── 4) Remove Duplicates from Sorted Array.cpp │ ├── 1) Largest Element in the Array.cpp │ ├── 5) Left Rotate an Array by One.cpp │ ├── 11) Maximum Consecutive Ones.cpp │ ├── 15) Traffic.cpp │ ├── 13) Longest Subarray With Sum K.cpp │ ├── 14) Longest Subarray With Sum K..cpp │ ├── 2) Second Largest Number.cpp │ ├── 12) Find The Single Element (SortedArray).cpp │ └── 9) Merge 2 Sorted Array.cpp ├── Step 3.2 - Medium │ ├── 9) Next Greater Permutation.cpp │ ├── 3) Majority Element.cpp │ ├── 4) Maximum Subarray Sum.cpp │ ├── 1) Two Sum.cpp │ ├── 6) Best time to buy and sell stock.cpp │ ├── 13) Rotate The Matrix.cpp │ ├── 15) Count All Subarrays With Given Sum.cpp │ ├── 2) Sort An Array of 0s, 1s and 2s.cpp │ ├── 11) Longest Successive Elements.cpp │ ├── 7) Alternate Numbers (Variety-1).cpp │ ├── 5) Print Maximum Subarry Sum.cpp │ ├── 10) Superior Elements.cpp │ ├── 8) Alternate Numbers (Variety-2).cpp │ ├── 12) Zero Matrix.cpp │ └── 14) Spiral Matrix.cpp └── Step 3.3 - Hard │ ├── 8) Subarrays with XOR ‘K’.cpp │ ├── 2) Print Pascal’s Triangle (Variation 2).cpp │ ├── 1) Print Pascal’s Triangle (Variation 1).cpp │ ├── 14) Subarray With Maximum Product.cpp │ ├── 9) Merge All Overlapping Intervals.cpp │ ├── 7) Longest Subarray With Zero Sum.cpp │ ├── 3) Print Pascal’s Triangle (Variation 3).cpp │ ├── 11) Missing And Repeating Numbers.cpp │ ├── 5) Three Sum.cpp │ ├── 6) 4Sum.cpp │ ├── 12) Number of Inversions.cpp │ ├── 13) Team Contest.cpp │ ├── 4) Majority Element.cpp │ └── 10) Merge Two Sorted Arrays Without Extra Space.cpp ├── Step 2 - Learn Important Sorting Techniques ├── Step 2.1 - Sorting-I │ ├── 2) Bubble Sort.cpp │ ├── 1) Selection Sort.cpp │ └── 3) Insertion Sort.cpp └── Step 2.2 - Sorting-II │ ├── 2) Recursive Bubble Sort.cpp │ ├── 3) Recursive Insertion sort.cpp │ ├── 1) Merge Sort.cpp │ └── 4) Quick Sort.cpp ├── .gitignore ├── Step 5 - Strings [Basic and Medium] ├── Step 5.1 - Basic and Easy String Problems │ ├── 6) Check If One String Is A Rotation Of Another String.cpp │ ├── 3) Largest Odd Number in String.cpp │ ├── 7) Anagram Pairs.cpp │ ├── 1) Remove Outermost Parentheses.cpp │ ├── 4) Longest Common Prefix.cpp │ ├── 2) Reverse Words In A String.cpp │ └── 5) Isomorphic Strings.cpp └── Step 5.2 - Medium String Problems │ ├── 2) Maximum Nesting Depth Of The Parentheses.cpp │ ├── 7) Sum Of Beauty Of All Substrings.cpp │ ├── 8) Reverse Words.cpp │ ├── 3) Roman Numeral To Integer.cpp │ ├── 1) Sorting Characters By Frequency.cpp │ ├── 6) Longest Palindromic Substring (Without DP).cpp │ ├── 5) Count With K Different Characters.cpp │ └── 4) Implement Atoi Function.cpp ├── Step 4 - Binary Search [1D, 2D Arrays, Search Space] ├── Step 4.1 - BS on 1D Arrays │ ├── 8) Number of occurrence.cpp │ ├── 1) Binary Search.cpp │ ├── 12) Rotation.cpp │ ├── 11) Find Minimum in Rotated Sorted Array.cpp │ ├── 4) Search Insert Position.cpp │ ├── 6) Floor and Ceil (Sorted Version).cpp │ ├── 3) Implement Upper Bound.cpp │ ├── 2) Implement Lower Bound.cpp │ ├── 14) Find Peak Element.cpp │ ├── 13) Single Element in a Sorted Array.cpp │ ├── 5) Floor and Ceil (Unsorted Version).cpp │ ├── 10) Search In A Rotated Sorted Array II.cpp │ ├── 7) First and Last Position of an Element In Sorted Array.cpp │ └── 9) Search In Rotated Sorted Array.cpp ├── Step 4.3 - BS on 2D Arrays │ ├── 1) Row with max 1s.cpp │ ├── 3) Search In A Row Wise And Column Wise Sorted Matrix.cpp │ ├── 2) Search In A 2D Matrix.cpp │ ├── 4) Find A Peak Element.cpp │ └── 5) Median in a row-wise sorted Matrix.cpp └── Step 4.2 - BS on Answers │ ├── 1) Square Root of a number.cpp │ ├── 7) Kth Missing Positive Number.cpp │ ├── 12) Minimize Max Distance to Gas Station.cpp │ ├── 5) Smallest divisor.cpp │ ├── 2) Find Nth Root Of M.cpp │ ├── 3) Koko Eating Bananas.cpp │ ├── 4) Rose Garden.cpp │ ├── 10) Largest Subarray Sum Minimized.cpp │ ├── 11) Painter's Partition Problem.cpp │ ├── 8) Aggressive Cows.cpp │ ├── 6) Capacity To Ship Packages Within D Days.cpp │ ├── 13) K-th Element of Two Sorted Arrays.cpp │ ├── 9) Allocate Books.cpp │ └── 14) Median of two sorted arrays.cpp ├── Step 7 - Recursion [PatternWise] └── Step 7.1 - Get a Strong Hold │ ├── 2) Find X raised to power N.cpp │ └── 1) Implement Atoi Function (Recursion).cpp ├── Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems] ├── Step 6.1 - Learn 1D LinkedList │ ├── 2) Insert Node At The Beginning.cpp │ ├── 1) Introduction To Linked List.cpp │ ├── 3) Delete Node Of Linked List.cpp │ ├── 5) Search in a Linked List.cpp │ └── 4) Count nodes of linked list.cpp ├── Step 6.2 - Learn Doubly LinkedList │ ├── 1) Introduction To Doubly Linked List.cpp │ ├── 2) Insert at end of Doubly Linked List.cpp │ ├── 3) Delete Last Node of a Doubly Linked List.cpp │ └── 4) Reverse A Doubly Linked List.cpp ├── Step 6.3 - Medium Problems of LL │ ├── 1) Middle Of Linked List.cpp │ ├── 10) Delete Middle Node.cpp │ ├── 9) Delete Kth Node From End.cpp │ ├── 6) Find length of Loop.cpp │ ├── 8) Segregate Even And Odd Nodes In a Linked List.cpp │ ├── 4) Cycle Detection in a Singly Linked List.cpp │ ├── 2) Reverse Linked List (Iterative).cpp │ ├── 3) Reverse Linked List (Recursive).cpp │ ├── 7) Check If Linked List Is Palindrome.cpp │ ├── 12) Sort linked list of 0s 1s 2s.cpp │ ├── 5) Linked List Cycle II.cpp │ ├── 13) Intersection of Two Linked Lists.cpp │ ├── 14) Add one to a number represented as Linked List.cpp │ ├── 15) Add Two Numbers.cpp │ └── 11) Sort Linked List.cpp ├── Step 6.5 - Hard Problems of LL │ ├── 3) Flatten A Linked List.cpp │ ├── 2) Rotate Linked List.cpp │ ├── 4) Clone a Linked List with random pointers.cpp │ └── 1) Reverse List In K Groups.cpp └── Step 6.4 - Medium Problems of DLL │ ├── 2) Find pairs with given sum in doubly linked list.cpp │ ├── 3) Remove duplicates from a sorted Doubly Linked List.cpp │ └── 1) Delete all occurrences of a given key in a doubly linked list.cpp └── LICENSE /Step 1 - Learn the basics/Step 1.4 - Know Basic Maths/4) GCD or HCF.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/hcf-and-lcm_840448?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int calcGCD(int n, int m){ 4 | if(m == 0) return n; 5 | return calcGCD(m, n%m); 6 | } 7 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/10) Missing Number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/missing-number_6680467 2 | 3 | int missingNumber(vector&a, int N) { 4 | long long total = N*(N+1)/2; 5 | for(auto ele : a) 6 | total -= ele; 7 | return total; 8 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.4 - Know Basic Maths/6) Sum of all divisors.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sum-of-all-divisors_8360720?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int sumOfAllDivisors(int n){ 4 | int ans = 0; 5 | for(int i = 1; i<=n; i++) 6 | ans += i*(n/i); 7 | return ans; 8 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/9) Next Greater Permutation.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/next-greater-permutation_6929564?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | vector nextGreaterPermutation(vector &A) { 4 | next_permutation(A.begin(), A.end()); 5 | return A; 6 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/12) Find The Single Element (UnsortedArray).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/library/find-the-element-that-appears-once 2 | 3 | #include 4 | 5 | int getSingleElement(vector &arr){ 6 | int singleElement = 0; 7 | 8 | for(auto ele : arr) 9 | singleElement ^= ele; 10 | 11 | return singleElement; 12 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/8) Linear Search.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/linear-search_6922070?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int linearSearch(int n, int num, vector &arr) 4 | { 5 | for(int i = 0; i b) 8 | return "greater"; 9 | else 10 | return "equal"; 11 | } 12 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.1 - Things to Know in C++./4) Switch Case statement.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/switch-case-statement_8357244?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | double areaSwitchCase(int ch, vector a) { 4 | 5 | switch(ch){ 6 | case 1: 7 | return a[0]*a[0]*M_PI; 8 | case 2: 9 | return a[0]*a[1]; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/6) Rotate array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rotate-array_1230543?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector rotateArray(vectorarr, int k) { 4 | reverse(arr.begin(), arr.begin()+k); 5 | reverse(arr.begin()+k, arr.end()); 6 | reverse(arr.begin(), arr.end()); 7 | return arr; 8 | } 9 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.4 - Know Basic Maths/1) Count Digits.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/count-digits_8416387?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int countDigits(int n){ 4 | 5 | int count = 0, temp = n; 6 | 7 | while(temp != 0){ 8 | int digit = temp%10; 9 | if(digit!= 0 && n%digit == 0) count++; 10 | temp = temp/10; 11 | } 12 | 13 | return count; 14 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/3) Check Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/ninja-and-the-sorted-check_6581957?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | int isSorted(int n, vector a) { 4 | 5 | for(int i = 0; i a[i+1]) return false; 7 | 8 | return true; 9 | } 10 | -------------------------------------------------------------------------------- /Step 2 - Learn Important Sorting Techniques/Step 2.1 - Sorting-I/2) Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/bubble-sort_624380?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | void bubbleSort(vector &arr, int n) 4 | { 5 | for(int i = 0; i arr[j+1]) swap(arr[j], arr[j+1]); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.5 - Learn Basic Recursion/4) Sum Of First N Numbers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sum-of-first-n-numbers_8876068?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | long long solve(long long n){ 4 | if(n == 0) return 0; 5 | else return n+solve(n-1); 6 | } 7 | 8 | long long sumFirstN(long long n) { 9 | // return solve(n); 10 | return n*(n+1)/2; 11 | } -------------------------------------------------------------------------------- /Step 2 - Learn Important Sorting Techniques/Step 2.2 - Sorting-II/2) Recursive Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | // Question has no link 2 | 3 | void bubbleSortRecursion(vector &arr, int n){ 4 | if(n == 1) return; 5 | 6 | for(int i = 0; i arr[i+1]) swap(arr[i], arr[i+1]); 8 | 9 | bubbleSortRecursion(arr, n-1); 10 | } 11 | 12 | void bubbleSort(vector &arr, int n) 13 | { 14 | bubbleSortRecursion(arr, n); 15 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.4 - Know Basic Maths/3) Palindrome number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/palindrome-number_624662?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | bool palindrome(int n) 4 | { 5 | int rNumber = 0, temp = n; 6 | 7 | while(temp != 0){ 8 | rNumber *= 10; 9 | rNumber += temp%10; 10 | temp = temp/10; 11 | } 12 | 13 | return rNumber == n; 14 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.4 - Know Basic Maths/7) Check Prime.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/check-prime_624934?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | bool isPrime(int n) 4 | { 5 | if(n == 2 || n == 3) return true; 6 | if(n == 1 || n%2 == 0 || n%3 == 0) return false; 7 | 8 | for(int i = 5; i*i Medium -> Hard]/Step 3.1 - Easy/7) Move Zero's to End.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/ninja-and-the-zero-s_6581958?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector moveZeros(int n, vector a) { 4 | int zeroIndex = 0; 5 | 6 | for(int i = 0; i &ans){ 4 | if(curr > n) return; 5 | ans.push_back(curr); 6 | solve(curr+1, n, ans); 7 | } 8 | 9 | vector printNos(int x) { 10 | vector ans; 11 | solve(1, x, ans); 12 | return ans; 13 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/4) Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/remove-duplicates-from-sorted-array_1102307?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int removeDuplicates(vector &arr, int n) { 4 | int index = 0; 5 | for(int i = 1; i countFrequency(int n, int x, vector &nums){ 4 | vector frequency(n, 0); 5 | 6 | for(int i = 0; i &ans){ 4 | if(curr > n) return; 5 | ans.push_back("Coding Ninjas"); 6 | solve(curr+1, n, ans); 7 | } 8 | 9 | 10 | vector printNTimes(int n) { 11 | vector ans; 12 | solve(1, n, ans); 13 | return ans; 14 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.5 - Learn Basic Recursion/3) N to 1 without loop.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/n-to-1-without-loop_8357243?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | void solve(int curr, int n, vector &ans){ 4 | if(curr > n) return; 5 | solve(curr+1, n, ans); 6 | ans.push_back(curr); 7 | } 8 | 9 | vector printNos(int x) { 10 | vector ans; 11 | solve(1, x, ans); 12 | return ans; 13 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.5 - Learn Basic Recursion/7) Check Palindrome (recursive).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/check-palindrome-recursive_624386?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | bool solve(int start, int end, string &str){ 4 | if(start > end) return true; 5 | return str[start] != str[end] ? false : solve(start+1, end -1, str); 6 | } 7 | 8 | bool isPalindrome(string& str) { 9 | return solve(0, str.length()-1, str); 10 | } 11 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/1) Largest Element in the Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/largest-element-in-the-array-largest-element-in-the-array_5026279?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | #include 4 | int largestElement(vector &arr, int n) { 5 | int maxi = arr[0]; 6 | for(auto ele : arr) 7 | maxi = max(maxi, ele); 8 | 9 | return maxi; 10 | } 11 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/5) Left Rotate an Array by One.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/left-rotate-an-array-by-one_5026278?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | #include 4 | vector rotateArray(vector& arr, int n) { 5 | int temp = arr[0]; 6 | for(int i = 0; i=0; i--){ 8 | if((num[i]-48)%2 != 0){ 9 | ans = num.substr(0, i+1); 10 | break; 11 | } 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.1 - Things to Know in C++./1) Find Character Case.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-character-case_58513?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | char ch; 9 | cin>>ch; 10 | 11 | if(60 <= ch && ch <= 90) 12 | cout << 1 << endl; 13 | else if(97 <= ch && ch <= 122) 14 | cout << 0 << endl; 15 | else 16 | cout << -1 << endl; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.1 - Things to Know in C++./2) Data Type.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/data-type_8357232?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | int dataTypes(string type) { 4 | 5 | if(type.compare("Integer") == 0) 6 | return 4; 7 | else if (type.compare("Long") == 0) 8 | return 8; 9 | else if (type.compare("Float") == 0) 10 | return 4; 11 | else if (type.compare("Double") == 0) 12 | return 8; 13 | else 14 | return 1; 15 | 16 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/3) Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/majority-element_6783241?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int majorityElement(vector v) { 4 | int major = v[0], count = 1; 5 | for(auto ele : v){ 6 | if(ele == major) count++; 7 | else{ 8 | count--; 9 | if(count == 0){ 10 | major = ele; 11 | count = 1; 12 | } 13 | } 14 | } 15 | return major; 16 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.2 - Medium String Problems/2) Maximum Nesting Depth Of The Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/maximum-nesting-depth-of-the-parentheses_8144741?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int maxDepth(string s) { 4 | int ans = 0; 5 | int currCount = 0; 6 | for(auto ele : s){ 7 | if(ele == '(') 8 | currCount++; 9 | else if (ele == ')') 10 | currCount--; 11 | ans = max(ans, currCount); 12 | } 13 | return ans; 14 | } 15 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/11) Maximum Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/maximum-consecutive-ones_3843993 2 | 3 | #include 4 | int consecutiveOnes(vector& arr){ 5 | int maxiOnes = 0; 6 | 7 | int currOnes = 0; 8 | for(int i = 0; i&arr) { 4 | 5 | for(int i = 0; i 4 | void insertionSort(int n, vector &arr){ 5 | 6 | for(int i = 1; i < arr.size(); i++){ 7 | int temp = arr[i]; 8 | int j = i-1; 9 | for(; j>=0; j--){ 10 | if(arr[j] < temp) break; 11 | else 12 | arr[j+1] = arr[j]; 13 | } 14 | arr[j+1] = temp; 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/4) Maximum Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/maximum-subarray-sum_630526?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | long long maxSubarraySum(vector arr, int n) 4 | { 5 | long long maxSum = 0, currSum = INT_MIN; 6 | for(int i = 0; i 4 | using namespace std; 5 | 6 | int main() { 7 | int n; cin>>n; 8 | 9 | int esum = 0, osum = 0; 10 | 11 | while(n!=0){ 12 | if((n%10)%2 == 0) 13 | esum += n%10; 14 | else 15 | osum += n%10; 16 | 17 | n = n/10; 18 | } 19 | 20 | cout << esum << " " << osum << endl; 21 | 22 | return 0; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/8) Number of occurrence.cpp: -------------------------------------------------------------------------------- 1 | https://www.codingninjas.com/studio/problems/occurrence-of-x-in-a-sorted-array_630456?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | #include 4 | 5 | int count(vector& arr, int n, int x) { 6 | int start = lower_bound(arr.begin(), arr.end(), x)-arr.begin(); 7 | if(start == n || arr[start] != x) return 0; 8 | int end = upper_bound(arr.begin(), arr.end(), x) - arr.begin(); 9 | return end - start; 10 | } 11 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/1) Two Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reading_6845742?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | string read(int n, vector book, int target) 4 | { 5 | unordered_map mp; 6 | string ans = "NO"; 7 | for(auto ele : book){ 8 | if(mp.find(target-ele) != mp.end()){ 9 | ans = "YES"; 10 | break; 11 | } 12 | mp[ele]++; 13 | } 14 | 15 | return ans; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/6) Best time to buy and sell stock.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/best-time-to-buy-and-sell-stock_6194560?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int bestTimeToBuyAndSellStock(vector&prices) { 4 | int profit = 0, minSoFar = INT_MAX; 5 | 6 | for(int i = 0;i &nums){ 4 | if(nums.empty()) return; 5 | int element = nums[0]; 6 | nums.erase(nums.begin()); 7 | solve(n, nums); 8 | nums.push_back(element); 9 | } 10 | 11 | vector reverseArray(int n, vector &nums) 12 | { 13 | // solve(n, nums); 14 | reverse(nums.begin(), nums.end()); 15 | return nums; 16 | } 17 | -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.1 - Basic and Easy String Problems/7) Anagram Pairs.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/anagram-pairs_626517?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | bool isAnagram(string str1, string str2) 6 | { 7 | unordered_map mp; 8 | for(auto ele : str1) 9 | mp[ele]++; 10 | 11 | for(auto ele : str2) 12 | mp[ele]--; 13 | 14 | for(auto it : mp) 15 | if(it.second != 0) return false; 16 | 17 | return true; 18 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/13) Rotate The Matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rotate-the-matrix_6825090?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | void rotateMatrix(vector> &mat){ 6 | 7 | //transpose 8 | for(int i = 0; i &nums, int target) { 4 | int lower = 0, higher = nums.size()-1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher-lower)/2; 8 | 9 | if(nums[mid] == target) return mid; 10 | else if (nums[mid] < target) lower = mid+1; 11 | else higher = mid - 1; 12 | } 13 | 14 | return -1; 15 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/12) Rotation.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rotation_7449070?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int findKRotation(vector &arr){ 4 | 5 | if(arr.size()==1 || arr[0] < arr[arr.size()-1]) return 0; 6 | 7 | int low = 0, high = arr.size()-1; 8 | 9 | while(low <= high){ 10 | int mid = low + (high-low)/2; 11 | 12 | if(arr[mid] < arr[mid-1]) return mid; 13 | else if (arr[0] <= arr[mid]) low = mid+1; 14 | else high = mid-1; 15 | } 16 | return -1; 17 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/15) Count All Subarrays With Given Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/subarray-sums-i_1467103?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int findAllSubarraysWithGivenSum(vector < int > & arr, int k) { 4 | unordered_map mp; 5 | mp[0]++; 6 | 7 | int ans = 0, prefix = 0; 8 | 9 | for(int i=0; i Medium -> Hard]/Step 3.3 - Hard/8) Subarrays with XOR ‘K’.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/subarrays-with-xor-k_6826258?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | int subarraysWithSumK(vector < int > a, int b) { 6 | unordered_map mp; 7 | mp[0]++; 8 | 9 | int prefix = 0, ans = 0; 10 | for(int i=0; i Medium -> Hard]/Step 3.3 - Hard/2) Print Pascal’s Triangle (Variation 2).cpp: -------------------------------------------------------------------------------- 1 | // https://takeuforward.org/data-structure/program-to-generate-pascals-triangle/ 2 | 3 | // Variation 2: Given the row number n. Print the n-th row of Pascal’s triangle. 4 | 5 | void pascalTriangle(int n) { 6 | long long ans = 1; 7 | cout << ans << " "; // printing 1st element 8 | 9 | //Printing the rest of the part: 10 | for (int i = 1; i < n; i++) { 11 | ans = ans * (n - i); 12 | ans = ans / i; 13 | cout << ans << " "; 14 | } 15 | cout << endl; 16 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.5 - Learn Basic Recursion/5) Factorial Numbers Not Greater Than N.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/factorial-numbers-not-greater-than-n_8365435?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | void solve(int index, long long n, vector &ans){ 4 | if(ans[ans.size()-1]*index > n) return; 5 | ans.push_back(ans[ans.size()-1]*index); 6 | solve(index+1, n, ans); 7 | } 8 | 9 | vector factorialNumbers(long long n) { 10 | vector ans; 11 | ans.push_back(1); 12 | solve(2, n, ans); 13 | return ans; 14 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.3 - BS on 2D Arrays/1) Row with max 1s.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/row-of-a-matrix-with-maximum-ones_982768?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int rowWithMax1s(vector> &matrix, int n, int m) 4 | { 5 | int ans = -1; 6 | int row = 0, coln = m-1; 7 | 8 | while(row < n && coln >= 0){ 9 | if(matrix[row][coln] == 1){ 10 | ans = row; 11 | coln--; 12 | } 13 | else 14 | row++; 15 | } 16 | 17 | return ans; 18 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.3 - BS on 2D Arrays/3) Search In A Row Wise And Column Wise Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/search-in-a-row-wise-and-column-wise-sorted-matrix_839811 2 | 3 | #include 4 | pair search(vector> matrix, int x) 5 | { 6 | int n = matrix.size(); 7 | int row = 0, coln = n-1; 8 | 9 | while(row < n && coln >= 0){ 10 | if(matrix[row][coln] == x) return {row, coln}; 11 | else if(matrix[row][coln] > x) coln--; 12 | else row++; 13 | } 14 | 15 | return {-1, -1}; 16 | } -------------------------------------------------------------------------------- /Step 7 - Recursion [PatternWise]/Step 7.1 - Get a Strong Hold/2) Find X raised to power N.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-x-raised-to-power-n-_626560?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | double myPowPos(double x, int n){ 4 | if (n == 0) return 1; 5 | 6 | double temp = myPowPos(x, n/2); 7 | if(n&1) 8 | return x * temp * temp; 9 | else 10 | return temp * temp; 11 | } 12 | 13 | double myPow(double x, int n) { 14 | 15 | if(n > 0) 16 | return myPowPos(x, n); 17 | else 18 | return 1/myPowPos(x, n); 19 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/11) Find Minimum in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rotated-array_1093219?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int findMin(vector& arr) 4 | { 5 | if(arr.size()==1) return arr[0]; 6 | if(arr[0] < arr[arr.size()-1]) return arr[0]; 7 | 8 | int low = 0, high = arr.size()-1; 9 | 10 | while(low <= high){ 11 | int mid = low + (high-low)/2; 12 | 13 | if(arr[mid] < arr[mid-1]) return arr[mid]; 14 | else if (arr[0] <= arr[mid]) low = mid+1; 15 | else high = mid-1; 16 | } 17 | return -1; 18 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/2) Sort An Array of 0s, 1s and 2s.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sort-an-array-of-0s-1s-and-2s_892977?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | void sortArray(vector& arr, int n) 5 | { 6 | int index0 = 0, index2 = arr.size()-1; 7 | 8 | for(int i = 0; i<= index2; i++){ 9 | i = max(index0, i); 10 | if(arr[i] == 0) 11 | swap(arr[index0++], arr[i--]); 12 | else if (arr[i] == 2) 13 | swap(arr[index2--], arr[i--]); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/1) Print Pascal’s Triangle (Variation 1).cpp: -------------------------------------------------------------------------------- 1 | // https://takeuforward.org/data-structure/program-to-generate-pascals-triangle/ 2 | 3 | // Variation 1: Given row number r and column number c. Print the element at position (r, c) in Pascal’s triangle. 4 | 5 | int nCr(int n, int r) { 6 | long long res = 1; 7 | 8 | // calculating nCr: 9 | for (int i = 0; i < r; i++) { 10 | res = res * (n - i); 11 | res = res / (i + 1); 12 | } 13 | return res; 14 | } 15 | 16 | int pascalTriangle(int r, int c) { 17 | int element = nCr(r - 1, c - 1); 18 | return element; 19 | } -------------------------------------------------------------------------------- /Step 2 - Learn Important Sorting Techniques/Step 2.2 - Sorting-II/3) Recursive Insertion sort.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/insertion-sort_624381?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | void recursiveInsertionSort(int arr[], int index, int n){ 4 | if(index == n) return; 5 | 6 | int temp = arr[index]; 7 | int i = index-1; 8 | for(; i>=0; i--) 9 | if(arr[i] > temp) arr[i+1] = arr[i]; 10 | else break; 11 | arr[i+1] = temp; 12 | 13 | recursiveInsertionSort(arr, index+1, n); 14 | } 15 | 16 | void insertionSort(int arr[], int n) 17 | { 18 | recursiveInsertionSort(arr, 1, n); 19 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.1 - Basic and Easy String Problems/1) Remove Outermost Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-outermost-parentheses/description/ 2 | 3 | class Solution { 4 | public: 5 | string removeOuterParentheses(string s) { 6 | string ans = ""; 7 | int currCount = 0; 8 | 9 | for(auto ele : s){ 10 | if(ele == '('){ 11 | if(currCount != 0) 12 | ans += '('; 13 | currCount++; 14 | } 15 | else{ 16 | if(currCount != 1) 17 | ans += ')'; 18 | currCount--; 19 | } 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/4) Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/algorithm-to-find-best-insert-position-in-sorted-array_839813?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int searchInsert(vector& arr, int x) 4 | { 5 | int lower = 0, higher = arr.size()-1; 6 | int ans = arr.size(); 7 | 8 | while(lower <= higher){ 9 | int mid = lower + (higher-lower)/2; 10 | 11 | if(arr[mid] == x) return mid; 12 | if(arr[mid] < x) 13 | lower = mid+1; 14 | 15 | else{ 16 | ans = mid; 17 | higher = mid-1; 18 | } 19 | 20 | } 21 | 22 | return ans; 23 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/6) Floor and Ceil (Sorted Version).cpp: -------------------------------------------------------------------------------- 1 | // https://takeuforward.org/arrays/floor-and-ceil-in-sorted-array/ 2 | 3 | pair getFloorAndCeil(int arr[], int n, int x) { 4 | int first = -1, second = -1; 5 | int index = lower_bound(arr, arr+n, x) - arr; 6 | 7 | if(index == 0) 8 | second = arr[index]; 9 | 10 | else if (index == n) 11 | first = arr[n-1]; 12 | 13 | else if(arr[index] == x) 14 | first = second = arr[index]; 15 | 16 | else{ 17 | first = arr[index-1]; 18 | second = arr[index]; 19 | } 20 | 21 | return {first, second}; 22 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/14) Subarray With Maximum Product.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/subarray-with-maximum-product_6890008?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | #include 4 | #include 5 | 6 | int subarrayWithMaxProduct(vector &arr){ 7 | int prefix = 1, suffix = 1; 8 | int maxi = INT_MIN; 9 | 10 | for(int i = 0; i& arr,int n){ 4 | string ans = ""; 5 | 6 | for(int i = 0; i Medium -> Hard]/Step 3.1 - Easy/15) Traffic.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/traffic_6682625?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int traffic(int n, int m, vector vehicle) { 4 | vector posZero; 5 | 6 | for(int i=0; i Medium -> Hard]/Step 3.3 - Hard/9) Merge All Overlapping Intervals.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/merge-all-overlapping-intervals_6783452?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | vector> mergeOverlappingIntervals(vector> &arr){ 6 | sort(arr.begin(), arr.end()); 7 | 8 | vector> ans; 9 | ans.push_back(arr[0]); 10 | 11 | for(int i = 1; i mp; 8 | int mini = 1; 9 | int maxi = 1; 10 | 11 | for(int j = i; j= 0) { 8 | 9 | while (s >= 0 && str[s] == ' ') 10 | s--; 11 | 12 | string word = ""; 13 | 14 | while (s >= 0 && str[s] != ' ') 15 | word.push_back(str[s--]); 16 | 17 | for (int i = word.length() - 1; i >= 0; i--) 18 | ans.push_back(word[i]); 19 | 20 | if (s >= 0) 21 | ans.push_back(' '); 22 | } 23 | 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/1) Square Root of a number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/square-root-integral_893351?leftPanelTab=0&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int floorSqrt(int n) 4 | { 5 | int low = 0, high = n; 6 | int ans = -1; 7 | 8 | while(low <= high){ 9 | long long mid = low + (high - low)/2; 10 | 11 | if(mid*mid == n){ 12 | ans = mid; 13 | break; 14 | } 15 | else if (mid*mid < n){ 16 | ans = mid; 17 | low = mid + 1; 18 | } 19 | else 20 | high = mid - 1; 21 | } 22 | return ans; 23 | } 24 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/13) Longest Subarray With Sum K.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/longest-subarray-with-sum-k_6682399?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | int longestSubarrayWithSumK(vector a, long long k) { 4 | int maxLength = 0, startIndex = 0; 5 | long long currSum = 0; 6 | 7 | for(int i = 0; i k) 11 | currSum -= a[startIndex++]; 12 | 13 | if(currSum == k) 14 | maxLength = max(maxLength, i-startIndex+1); 15 | } 16 | 17 | return maxLength; 18 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/7) Longest Subarray With Zero Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/longest-subarray-with-zero-sum_6783450?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | #include 4 | #include 5 | 6 | int getLongestZeroSumSubarrayLength(vector &arr){ 7 | unordered_map mp; 8 | 9 | int prefix = 0, maxLength = 0; 10 | for(int i = 0; i vec, int n, int k) { 4 | int lower = 0, higher = n-1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher - lower)/2; 8 | 9 | if(vec[mid] - mid - 1 >= k) 10 | higher = mid - 1; 11 | else 12 | lower = mid + 1; 13 | } 14 | 15 | if(higher == -1) 16 | return k; 17 | else 18 | return vec[higher] + (k - (vec[higher] - higher - 1)); 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/3) Implement Upper Bound.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/implement-upper-bound_8165383?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | // The upper bound algorithm finds the first or the smallest index in a sorted array where the value at that index is greater than the given key i.e. x. 4 | 5 | int upperBound(vector &arr, int x, int n){ 6 | int lower = 0, higher = n-1; 7 | int ans = n; 8 | 9 | while(lower <= higher){ 10 | int mid = lower + (higher-lower)/2; 11 | 12 | if(arr[mid] > x){ 13 | ans = mid; 14 | higher = mid-1; 15 | } 16 | else 17 | lower = mid+1; 18 | } 19 | 20 | return ans; 21 | } 22 | -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.1 - Basic and Easy String Problems/2) Reverse Words In A String.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reverse-words-in-a-string_696444?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | string reverseString(string &str) { 4 | int s = str.length()- 1; 5 | string ans = ""; 6 | 7 | while (s >= 0) { 8 | 9 | while (s >= 0 && str[s] == ' ') 10 | s--; 11 | 12 | string word = ""; 13 | 14 | while (s >= 0 && str[s] != ' ') 15 | word.push_back(str[s--]); 16 | 17 | for (int i = word.length() - 1; i >= 0; i--) 18 | ans.push_back(word[i]); 19 | 20 | if (s >= 0) 21 | ans.push_back(' '); 22 | } 23 | 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.5 - Learn Basic Recursion/8) Print Fibonacci Series.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/print-fibonacci-series_7421617?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | void solve(int a, int b, int c, int n, vector &ans){ 4 | if(n == 0) return; 5 | c = a+b; 6 | a = b; 7 | b = c; 8 | ans.push_back(c); 9 | solve(a, b, c, n-1, ans); 10 | } 11 | 12 | vector generateFibonacciNumbers(int n) { 13 | int a = 0, b = 1, c; 14 | 15 | if(n == 1) return {0}; 16 | if(n == 2) return {0, 1}; 17 | 18 | vector ans; 19 | ans.push_back(0); 20 | ans.push_back(1); 21 | 22 | solve(a, b, c, n-2, ans); 23 | 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/2) Implement Lower Bound.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/lower-bound_8165382?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | // The lower bound algorithm finds the first or the smallest index in a sorted array where the value at that index is greater than or equal to a given key i.e. x. 4 | 5 | int upperBound(vector &arr, int x, int n){ 6 | int lower = 0, higher = n-1; 7 | int ans = n; 8 | 9 | while(lower <= higher){ 10 | int mid = lower + (higher-lower)/2; 11 | 12 | if(arr[mid] > x){ 13 | ans = mid; 14 | higher = mid-1; 15 | } 16 | else 17 | lower = mid+1; 18 | } 19 | 20 | return ans; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.3 - BS on 2D Arrays/2) Search In A 2D Matrix.cpp: -------------------------------------------------------------------------------- 1 | https://www.codingninjas.com/studio/problems/search-in-a-2d-matrix_980531?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | bool searchMatrix(vector>& mat, int target) { 4 | int lower = 0, higher = mat.size()*mat[0].size() - 1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher - lower)/2; 8 | 9 | if(mat[mid/mat[0].size()][mid%mat[0].size()] == target) 10 | return true; 11 | else if (mat[mid/mat[0].size()][mid%mat[0].size()] < target) 12 | lower = mid + 1; 13 | else 14 | higher = mid - 1; 15 | } 16 | 17 | return false; 18 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.2 - Medium String Problems/3) Roman Numeral To Integer.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/roman-numeral-to-integer_981308?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int romanToInt(string s) { 4 | unordered_map mp; 5 | mp['I'] = 1; 6 | mp['V'] = 5; 7 | mp['X'] = 10; 8 | mp['L'] = 50; 9 | mp['C'] = 100; 10 | mp['D'] = 500; 11 | mp['M'] = 1000; 12 | 13 | int ans = 0; 14 | 15 | for(int i=0; i mp[s[i]]) 17 | ans += mp[s[i+1]] - mp[s[i++]]; 18 | else 19 | ans += mp[s[i]]; 20 | } 21 | 22 | return ans; 23 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/11) Longest Successive Elements.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/longest-successive-elements_6811740?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | int longestSuccessiveElements(vector&a) { 4 | sort(a.begin(), a.end()); 5 | 6 | int noDups = 0; 7 | for(int i = 1; inext = list; 19 | list = temp; 20 | return list; 21 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/14) Find Peak Element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-peak-element_1081482?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int findPeakElement(vector &arr) { 4 | if(arr[0] > arr[1]) return 0; 5 | if(arr[arr.size()-1] > arr[arr.size()-2]) return arr.size()-1; 6 | 7 | int lower = 1, higher = arr.size()-1; 8 | 9 | while(lower <= higher){ 10 | int mid = lower + (higher - lower)/2; 11 | 12 | if(arr[mid-1] < arr[mid] && arr[mid] > arr[mid+1]) return mid; 13 | 14 | else if (arr[mid-1] < arr[mid] && arr[mid] < arr[mid+1]) lower = mid + 1; 15 | 16 | else higher = mid - 1; 17 | } 18 | return -1; 19 | } 20 | -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.2 - Medium String Problems/1) Sorting Characters By Frequency.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sorting-characters-by-frequency_1263699?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | string sortByFrequency(int n, string& s) 4 | { 5 | unordered_map mp; 6 | for(auto &ch : s) 7 | mp[ch]++; 8 | 9 | priority_queue> pq; 10 | for(auto it: mp) 11 | pq.push({it.second, it.first}); 12 | 13 | string ans = ""; 14 | 15 | while(!pq.empty()){ 16 | int noOfIterations = pq.top().first; 17 | char ch = pq.top().second; 18 | pq.pop(); 19 | 20 | for(int i = 0; i Medium -> Hard]/Step 3.3 - Hard/3) Print Pascal’s Triangle (Variation 3).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/print-pascal-s-triangle_6917910?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | // Variation 3: Given the number of rows n. Print the first n rows of Pascal’s triangle. 4 | 5 | vector> pascalTriangle(int N) { 6 | vector> ans(N, vector()); 7 | ans[0].push_back(1); 8 | 9 | for(int i = 1; i 4 | using namespace std; 5 | 6 | int Maximum(int x, int y) 7 | { 8 | if(x > y) return x; 9 | else return y; 10 | } 11 | 12 | void Swap(int &x, int &y) 13 | { 14 | x = x+y; 15 | y = x-y; 16 | x = x-y; 17 | } 18 | 19 | int main() 20 | { 21 | int test, a, b, r; 22 | cin >> test; 23 | cin >> a >> b; 24 | switch (test) 25 | { 26 | case 1: 27 | r = Maximum(a, b); 28 | cout << r; 29 | break; 30 | case 2: 31 | Swap(a, b); 32 | cout << a << " " << b; 33 | break; 34 | default: 35 | cout << "Invalid test option"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.2 - Medium String Problems/6) Longest Palindromic Substring (Without DP).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/longest-palindromic-substring_758900?leftPanelTab=0 2 | 3 | bool isPalindrome(string s, int start, int end){ 4 | if(start > end) return true; 5 | return s[start] == s[end] && isPalindrome(s, start+1, end-1); 6 | } 7 | 8 | string longestPalinSubstring(string str) { 9 | string ans = ""; 10 | 11 | for(int i = 0; i ans.length()) 16 | ans = temp; 17 | } 18 | } 19 | 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.1 - Things to Know in C++./5) Nth Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/nth-fibonacci-number_74156?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; cin>>n; 9 | 10 | if(n == 1) cout << 1 << endl; 11 | else if (n == 2) cout << 2 << endl; 12 | else{ 13 | int a = 1, b = 2, c; 14 | 15 | for(int i = 3; i Medium -> Hard]/Step 3.2 - Medium/7) Alternate Numbers (Variety-1).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/alternate-numbers_6783445?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | // In Variey-1, the number of positive and negative values are equal. 4 | 5 | vector alternateNumbers(vector&a) { 6 | vector ans(a.size()); 7 | int positiveIndex = 0, negativeIndex = 1; 8 | 9 | for(int i=0; i 0){ 11 | ans[positiveIndex] = a[i]; 12 | positiveIndex+=2; 13 | } 14 | else{ 15 | ans[negativeIndex] = a[i]; 16 | negativeIndex+=2; 17 | } 18 | 19 | } 20 | 21 | return ans; 22 | 23 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.6 - Learn Basic Hashing/2) Highest -Lowest Frequency Elements.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/k-most-occurrent-numbers_625382?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | vector getFrequencies(vector& v) { 4 | 5 | unordered_map mp; 6 | 7 | for(auto i : v) 8 | mp[i]++; 9 | 10 | int maxi = v[0], mini = v[0]; 11 | 12 | for(auto ele : mp){ 13 | if(ele.second > mp[maxi]) 14 | maxi = ele.first; 15 | else if (ele.second == mp[maxi]) 16 | maxi = min(ele.first, maxi); 17 | 18 | if(ele.second < mp[mini]) 19 | mini = ele.first; 20 | else if(ele.second == mp[mini]) 21 | mini = min(ele.first, mini); 22 | } 23 | 24 | return {maxi, mini}; 25 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/12) Minimize Max Distance to Gas Station.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/minimise-max-distance_7541449?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int checkDist(vector &arr, double minDist){ 4 | int totalAddedGasStation = 0; 5 | 6 | for(int i = 1; i &arr, int k){ 13 | double lower = 0; 14 | double higher = arr[arr.size()-1]; 15 | 16 | while(higher - lower > 1e-6){ 17 | double mid = lower + (higher - lower)/2; 18 | 19 | if(checkDist(arr, mid) > k) 20 | lower = mid; 21 | 22 | else 23 | higher = mid; 24 | } 25 | 26 | return higher; 27 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.1 - Learn 1D LinkedList/1) Introduction To Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/introduction-to-linked-list_8144737?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of linked list 5 | * class Node { 6 | * 7 | * public: 8 | * int data; 9 | * Node* next; 10 | * Node() : data(0), next(nullptr) {} 11 | * Node(int x) : data(x), next(nullptr) {} 12 | * Node(int x, Node* next) : data(x), next(next) {} 13 | * }; 14 | */ 15 | 16 | Node* constructLL(vector& arr) { 17 | Node *head = new Node(arr[0]); 18 | Node *temp = head; 19 | 20 | for(int i = 1; inext = new Node(arr[i]); 22 | temp = temp->next; 23 | } 24 | 25 | return head; 26 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.1 - Basic and Easy String Problems/5) Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/isomorphic-strings-_1117636?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | bool areIsomorphicHelper(string &str1, string &str2){ 4 | if(str1.length() != str2.length()) return 0; 5 | 6 | char check[26]; 7 | for(int i=0; i<26; i++) check[i] = ' '; 8 | 9 | for(int i=0; i mp; 6 | int ans = 0, start = 0, unique = 0; 7 | 8 | for (int i = 0; i < s.size(); i++) { 9 | mp[s[i]]++; 10 | if(mp[s[i]] == 1) unique++; 11 | while (unique > k) { 12 | mp[s[start]]--; 13 | if (mp[s[start]] == 0) 14 | unique--; 15 | start++; 16 | } 17 | ans += i-start+1; 18 | } 19 | return ans; 20 | } 21 | 22 | int countSubStrings(string str, int k) 23 | { 24 | return most_k_chars(str, k) - most_k_chars(str, k-1); 25 | } 26 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./5) Pattern 5.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_5 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution{ 10 | public: 11 | 12 | void printTriangle(int n) { 13 | 14 | for(int i = n; i>0; i--){ 15 | for(int j = i; j>0; j--) 16 | cout << "* "; 17 | cout << endl; 18 | } 19 | 20 | } 21 | }; 22 | 23 | //{ Driver Code Starts. 24 | 25 | int main() { 26 | int t; 27 | cin >> t; 28 | while (t--) { 29 | int n; 30 | cin >> n; 31 | 32 | Solution ob; 33 | ob.printTriangle(n); 34 | } 35 | return 0; 36 | } 37 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./6) Pattern 6.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-number-1661489840/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_6 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution{ 10 | public: 11 | 12 | void printTriangle(int n) { 13 | 14 | for(int i = n; i>0; i--){ 15 | for(int j = 1; j<=i; j++) 16 | cout << j << " "; 17 | cout << endl; 18 | } 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./1) Pattern 1.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/square-pattern/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_1 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printSquare(int n) { 12 | 13 | for(int i = 0; i> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printSquare(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./2) Pattern 2.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/right-triangle/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_2 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 0; i> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/14) Longest Subarray With Sum K..cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/longest-subarray-with-sum-k_5713505?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | #include 4 | int getLongestSubarray(vector& nums, int k){ 5 | unordered_map mp; 6 | unordered_set s; 7 | 8 | int maxLength = 0; 9 | 10 | int prefixSum = 0; 11 | s.insert(0); 12 | mp[0] = -1; 13 | 14 | for(int i = 0; inext == NULL) return NULL; 17 | 18 | Node *temp = list; 19 | 20 | while(temp->next->next != NULL) 21 | temp = temp->next; 22 | 23 | Node *todelete = temp->next; 24 | temp->next = NULL; 25 | free(todelete); 26 | 27 | return list; 28 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./15) Pattern 15.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1662285196/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_15 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i=n; i>0; i--){ 14 | for(char j = 'A'; j<'A'+i; j++) 15 | cout << j; 16 | cout << endl; 17 | } 18 | 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./3) Pattern 3.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-number/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_3 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 1; i<=n; i++){ 14 | for(int j = 1; j<=i; j++) 15 | cout << j << " "; 16 | cout << endl; 17 | } 18 | 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/5) Print Maximum Subarry Sum.cpp: -------------------------------------------------------------------------------- 1 | // No link 2 | 3 | long long maxSubarraySum(vector arr, int n) 4 | { 5 | long long maxSum = 0, currSum = INT_MIN; 6 | int start = 0, end = 0, finalStart = -1, finalEnd = -1; 7 | for(int i = 0; i arr[i]){ 9 | currSum = currSum + arr[i]; 10 | end = i; 11 | } 12 | else{ 13 | currSum = arr[i]; 14 | start = end = i; 15 | } 16 | 17 | if(maxSum < currSum){ 18 | maxSum = currSum; 19 | finalStart = start; 20 | finalEnd = end; 21 | } 22 | } 23 | 24 | if(finalStart != -1) 25 | for(int i = finalStart; i<=finalEnd; i++) 26 | cout << arr[i] << " "; 27 | cout << endl; 28 | 29 | return maxSum; 30 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/13) Single Element in a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/unique-element-in-sorted-array_1112654?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int singleNonDuplicate(vector& arr) 4 | { 5 | if(arr[0] != arr[1]) return arr[0]; 6 | if(arr[arr.size()-1] != arr[arr.size()-2]) return arr[arr.size()-1]; 7 | 8 | int start = 0, end = arr.size()-1; 9 | 10 | while(start <= end){ 11 | int mid = start + (end - start)/2; 12 | 13 | if(arr[mid] != arr[mid-1] && arr[mid] != arr[mid+1]) 14 | return arr[mid]; 15 | 16 | if(mid%2 == 0){ 17 | if(arr[mid] == arr[mid+1]) 18 | start = mid+1; 19 | else 20 | end = mid-1; 21 | } 22 | else{ 23 | if(arr[mid] == arr[mid-1]) 24 | start = mid+1; 25 | else 26 | end = mid-1; 27 | } 28 | } 29 | 30 | return -1; 31 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/5) Smallest divisor.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/smallest-divisor-with-the-given-limit_1755882?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | bool checkThreshold (vector& arr, int divisor, int limit){ 4 | long long totalSum = 0; 5 | 6 | for(int i =0; i& arr, int limit) 15 | { 16 | int lower = 1, higher = *max_element(arr.begin(), arr.end()); 17 | int ans = -1; 18 | 19 | while(lower<=higher){ 20 | int mid = lower + (higher - lower)/2; 21 | 22 | if(checkThreshold(arr, mid, limit)){ 23 | ans = mid; 24 | higher = mid - 1; 25 | } 26 | else 27 | lower = mid + 1; 28 | } 29 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./4) Pattern 4.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-number-1661428795/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_4 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 1; i<=n; i++){ 14 | for(int j = 0; j> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/11) Missing And Repeating Numbers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/missing-and-repeating-numbers_6828164?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector findMissingRepeatingNumbers(vector < int > a) { 4 | int pXorq = 0; 5 | for(int i = 0; i 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 1; i<=n; i++){ 14 | for(char j = 'A'; j < 'A'+i; j++) 15 | cout << j; 16 | cout << endl; 17 | } 18 | 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./16) Pattern 16.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1662285334/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_16 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 0; i> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/2) Second Largest Number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/ninja-and-the-second-order-elements_6581960?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector getSecondOrderElements(int n, vector a) { 4 | int maxiFirst = -1, maxiSecond = -1, miniFirst = INT_MAX, miniSecond = INT_MAX; 5 | 6 | for (auto ele : a){ 7 | 8 | if(maxiFirst < ele){ 9 | maxiSecond = maxiFirst; 10 | maxiFirst = ele; 11 | } 12 | else{ 13 | maxiSecond = max(maxiSecond, ele); 14 | } 15 | 16 | if(miniFirst > ele){ 17 | miniSecond = miniFirst; 18 | miniFirst = ele; 19 | } 20 | else{ 21 | miniSecond = min(miniSecond, ele); 22 | } 23 | } 24 | 25 | return {maxiSecond, miniSecond}; 26 | } 27 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./18) Pattern 18.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1662286302/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_18 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 0; i='A'+n-i-1; j--) 15 | cout << j << " "; 16 | cout << endl; 17 | } 18 | 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while (t--) { 28 | int n; 29 | cin >> n; 30 | 31 | Solution ob; 32 | ob.printTriangle(n); 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/12) Find The Single Element (SortedArray).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-the-single-element_6680465?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | int getSingleElement(vector &arr){ 6 | if(arr[0] != arr[1]) return arr[0]; 7 | if(arr[arr.size()-1] != arr[arr.size()-2]) return arr[arr.size()-1]; 8 | 9 | int start = 0, end = arr.size()-1; 10 | 11 | while(start <= end){ 12 | int mid = start + (end - start)/2; 13 | 14 | if(arr[mid] != arr[mid-1] && arr[mid] != arr[mid+1]) 15 | return arr[mid]; 16 | 17 | if(mid%2 == 0){ 18 | if(arr[mid] == arr[mid+1]) 19 | start = mid+1; 20 | else 21 | end = mid-1; 22 | } 23 | else{ 24 | if(arr[mid] == arr[mid-1]) 25 | start = mid+1; 26 | else 27 | end = mid-1; 28 | } 29 | } 30 | 31 | return -1; 32 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/10) Superior Elements.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/superior-elements_6783446?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | void insertLiner(vector &ans, int element){ 4 | ans.push_back(element); 5 | int temp = element; 6 | int i = ans.size()-2; 7 | for(; i>=0; i--){ 8 | if(ans[i] > ans[i+1]) 9 | ans[i+1] = ans[i]; 10 | else 11 | break; 12 | } 13 | ans[i+1] = temp; 14 | 15 | } 16 | 17 | vector superiorElements(vector&a) { 18 | vector ans; 19 | ans.push_back(a[a.size()-1]); 20 | 21 | int maxTillNow = a[a.size()-1]; 22 | for(int i = a.size()-2; i>=0; i--){ 23 | if(a[i] > maxTillNow){ 24 | insertLiner(ans, a[i]); 25 | maxTillNow = a[i]; 26 | } 27 | } 28 | 29 | return ans; 30 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/5) Floor and Ceil (Unsorted Version).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/ceiling-in-a-sorted-array_1825401?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | pair getFloorAndCeil(int arr[], int n, int x) { 4 | int lower = -1, higher = n; 5 | 6 | for(int i =0; i arr[i]) 20 | higher = i; 21 | } 22 | } 23 | 24 | int first = -1; 25 | int second = -1; 26 | 27 | if(lower != -1) 28 | first = arr[lower]; 29 | 30 | if(higher != n) 31 | second = arr[higher]; 32 | 33 | return {first, second}; 34 | } 35 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./13) Pattern 13.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1661718712/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_13 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | int count = 1; 14 | for(int i=1; i<=n; i++){ 15 | for(int j = 1; j<=i; j++) 16 | cout << count++ <<" "; 17 | cout << endl; 18 | } 19 | 20 | } 21 | }; 22 | 23 | //{ Driver Code Starts. 24 | 25 | int main() { 26 | int t; 27 | cin >> t; 28 | while (t--) { 29 | int n; 30 | cin >> n; 31 | 32 | Solution ob; 33 | ob.printTriangle(n); 34 | } 35 | return 0; 36 | } 37 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/2) Find Nth Root Of M.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/nth-root-of-m_1062679?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | long long power(int x, int n, int m){ 4 | if(n == 0) return 1; 5 | 6 | long long temp = power(x, n/2, m); 7 | 8 | if(temp > m) return temp; 9 | 10 | if(temp*temp > 1e9) 11 | return m+1; 12 | 13 | long long check = 0; 14 | 15 | if(n%2 == 0) 16 | return temp * temp; 17 | else 18 | return temp * temp * x; 19 | } 20 | 21 | int NthRoot(int n, int m) { 22 | int lower = 0, higher = m; 23 | 24 | while(lower <= higher){ 25 | int mid = lower + (higher - lower)/2; 26 | 27 | long long nthMultiple = power(mid, n, m); 28 | 29 | if(nthMultiple == m) return mid; 30 | else if (nthMultiple > m) higher = mid - 1; 31 | else lower = mid + 1; 32 | } 33 | 34 | return -1; 35 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./11) Pattern 11.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1661718455/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_11 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 0; i=0; j--){ 15 | if(j%2 == 0) cout << "1 "; 16 | else cout << "0 "; 17 | } 18 | cout << endl; 19 | } 20 | 21 | } 22 | }; 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int n; 31 | cin >> n; 32 | 33 | Solution ob; 34 | ob.printTriangle(n); 35 | } 36 | return 0; 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/10) Search In A Rotated Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/search-in-a-rotated-sorted-array-ii_7449547?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | bool searchInARotatedSortedArrayII(vector& arr, int target) { 4 | int lower = 0, higher = arr.size()-1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher - lower)/2; 8 | 9 | if(arr[mid] == target) return 1; 10 | else if (arr[lower] <= arr[mid]){ 11 | if(arr[lower] <= target && target <= arr[mid]) 12 | higher = mid - 1; 13 | else 14 | lower = mid + 1; 15 | } 16 | else{ 17 | if(arr[mid] <= target && target <= arr[higher]) 18 | lower = mid + 1; 19 | else 20 | higher = mid - 1; 21 | } 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/3) Koko Eating Bananas.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/minimum-rate-to-eat-bananas_7449064?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int calcHour(vector v, long long currM, int h){ 4 | 5 | int totalHr = 0; 6 | 7 | for(int i = 0; i h) return totalHr; 10 | } 11 | 12 | return totalHr; 13 | } 14 | 15 | int minimumRateToEatBananas(vector v, int h) { 16 | long long lower = 1, higher = accumulate(v.begin(), v.end(), 0LL); 17 | int ans = -1; 18 | 19 | while(lower <= higher){ 20 | long long mid = lower + (higher - lower)/2; 21 | 22 | if(calcHour(v, mid, h) <= h){ 23 | ans = mid; 24 | higher = mid-1; 25 | } 26 | else 27 | lower = mid + 1; 28 | } 29 | 30 | return ans; 31 | } -------------------------------------------------------------------------------- /Step 5 - Strings [Basic and Medium]/Step 5.2 - Medium String Problems/4) Implement Atoi Function.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/implement-atoi-function_981270?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int createAtoi(string s) { 4 | int start = 0; 5 | while(s[start] == ' ') start++; 6 | 7 | bool negative = false; 8 | if(s[start] == '-'){ 9 | negative = true; 10 | start++; 11 | } 12 | else if(s[start] == '+') start++; 13 | 14 | long long ans = 0; 15 | int count = 0; 16 | 17 | for(; start 10) break; 21 | } 22 | 23 | else break; 24 | } 25 | 26 | if(negative) 27 | ans = ans*-1; 28 | 29 | if(ans > INT_MAX) 30 | ans = INT_MAX; 31 | else if (ans < INT_MIN) 32 | ans = INT_MIN; 33 | 34 | return ans; 35 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.2 - Learn Doubly LinkedList/1) Introduction To Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/introduction-to-doubly-linked-list_8160413?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | * Definition for doubly-linked list. 5 | * class Node 6 | * { 7 | * public: 8 | * int data; 9 | * Node *next, *prev; 10 | * Node() : data(0), next(nullptr), prev(nullptr) {} 11 | * Node(int x) : data(x), next(nullptr), prev(nullptr) {} 12 | * Node(int x, Node *next, Node *prev) : data(x), next(next), prev(prev) {} 13 | * }; 14 | */ 15 | 16 | Node* constructDLL(vector& arr) { 17 | Node *head = new Node(arr[0]); 18 | 19 | Node *temp = head; 20 | 21 | for(int i = 1; iprev = temp; 24 | temp->next = newNode; 25 | temp = temp->next; 26 | } 27 | 28 | return head; 29 | } 30 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.3 - BS on 2D Arrays/4) Find A Peak Element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-peak-element_7449073?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | vector findPeakGrid(vector> &g){ 4 | int lower = 0, higher = g.size()-1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher - lower)/2; 8 | 9 | int coln = -1, maxi = -1; 10 | for(int i = 0; i= g[mid][coln]){ 17 | lower = mid + 1; 18 | continue; 19 | } 20 | else if(mid - 1 >= 0 && g[mid-1][coln] >= g[mid][coln]){ 21 | higher = mid - 1; 22 | continue; 23 | } 24 | 25 | return {mid, coln}; 26 | } 27 | 28 | return {-1, -1}; 29 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/4) Rose Garden.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rose-garden_2248080?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | int calcAdjacentRoses(vector &arr, int currDay, int k){ 4 | 5 | int totalBouquets = 0; 6 | int fCount = 0; 7 | 8 | for(int i = 0; i currDay) 10 | fCount = 0; 11 | 12 | else 13 | fCount++; 14 | 15 | if(fCount >= k){ 16 | totalBouquets++; 17 | fCount = 0; 18 | } 19 | } 20 | 21 | return totalBouquets; 22 | 23 | } 24 | 25 | int roseGarden(vector arr, int k, int m) 26 | { 27 | int lower = 1, higher = *max_element(arr.begin(), arr.end()); 28 | int ans = -1; 29 | 30 | while(lower <= higher){ 31 | int mid = lower + (higher - lower)/2; 32 | 33 | if(calcAdjacentRoses(arr, mid, k) >= m){ 34 | ans = mid; 35 | higher = mid-1; 36 | } 37 | else 38 | lower = mid + 1; 39 | } 40 | 41 | return ans; 42 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./8) Pattern 8.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1661493231/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_8 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution{ 10 | public: 11 | 12 | void printTriangle(int n) { 13 | 14 | for(int i = n; i>0; i--){ 15 | 16 | // Empty Spaces 17 | for(int j = 0; j> t; 36 | while (t--) { 37 | int n; 38 | cin >> n; 39 | 40 | Solution ob; 41 | ob.printTriangle(n); 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./7) Pattern 7.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1661492263/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_7 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i =1; i<=n; i++){ 14 | 15 | // Empty Spaces 16 | for(int j = 0; j> t; 34 | while (t--) { 35 | int n; 36 | cin >> n; 37 | 38 | Solution ob; 39 | ob.printTriangle(n); 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/10) Largest Subarray Sum Minimized.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/largest-subarray-sum-minimized_7461751?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int noOfSubarryPossible(vector a, int maxSubarrySize){ 4 | int totalSubarryPossible = 1; 5 | int currVal = 0; 6 | 7 | for(int i = 0; i maxSubarrySize){ 10 | totalSubarryPossible++; 11 | currVal = a[i]; 12 | } 13 | } 14 | 15 | return totalSubarryPossible; 16 | } 17 | 18 | int largestSubarraySumMinimized(vector a, int k) { 19 | int lower = *max_element(a.begin(), a.end()); 20 | int higher = accumulate(a.begin(), a.end(), 0); 21 | 22 | while(lower <= higher){ 23 | int mid = lower + (higher - lower)/2; 24 | 25 | if(noOfSubarryPossible(a, mid) > k) 26 | lower = mid + 1; 27 | 28 | else 29 | higher = mid - 1; 30 | } 31 | return lower; 32 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.3 - BS on 2D Arrays/5) Median in a row-wise sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/median-of-a-row-wise-sorted-matrix_1115473?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int countNumberLessthan(vector> &matrix, int value){ 4 | int ans = 0; 5 | for(int i= 0; i> &matrix, int m, int n) { 11 | int lower = INT_MAX, higher = INT_MIN; 12 | for(int i= 0; idata = 0; 14 | next = NULL; 15 | } 16 | Node(int data) 17 | { 18 | this->data = data; 19 | this->next = NULL; 20 | } 21 | Node(int data, Node* next) 22 | { 23 | this->data = data; 24 | this->next = next; 25 | } 26 | }; 27 | */ 28 | 29 | Node *findMiddle(Node *head) { 30 | if(head->next == NULL) return head; 31 | 32 | Node *curr = head, *forward = head->next; 33 | 34 | while(forward != NULL){ 35 | curr = curr->next; 36 | forward = forward->next; 37 | if(forward != NULL) 38 | forward = forward->next; 39 | } 40 | 41 | return curr; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Yash_Sampat 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 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.2 - Learn Doubly LinkedList/2) Insert at end of Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/insert-at-end-of-doubly-linked-list_8160464?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of doubly linked list: 5 | * 6 | * struct Node { 7 | * int value; 8 | * Node *prev; 9 | * Node *next; 10 | * Node() : value(0), prev(nullptr), next(nullptr) {} 11 | * Node(int val) : value(val), prev(nullptr), next(nullptr) {} 12 | * Node(int val, Node *p, Node *n) : value(val), prev(p), next(n) {} 13 | * }; 14 | * 15 | *************************************************************************/ 16 | 17 | Node * insertAtTail(Node *head, int k) { 18 | if(head == NULL) 19 | return new Node(k); 20 | 21 | Node *temp = head; 22 | 23 | while(temp->next != NULL){ 24 | temp = temp->next; 25 | } 26 | 27 | Node *newNode = new Node(k); 28 | temp->next = newNode; 29 | newNode->prev = temp; 30 | 31 | return head; 32 | } 33 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.2 - Learn Doubly LinkedList/3) Delete Last Node of a Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/delete-last-node-of-a-doubly-linked-list_8160469?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | /** 4 | * Definition of doubly linked list: 5 | * 6 | * struct Node { 7 | * int data; 8 | * Node *prev; 9 | * Node *next; 10 | * Node() : data(0), prev(nullptr), next(nullptr) {} 11 | * Node(int val) : data(val), prev(nullptr), next(nullptr) {} 12 | * Node(int val, Node *p, Node *n) : data(val), prev(p), next(n) {} 13 | * }; 14 | * 15 | *************************************************************************/ 16 | 17 | Node * deleteLastNode(Node *head) { 18 | if(head->next == NULL) return NULL; 19 | 20 | Node *temp = head; 21 | 22 | while(temp->next->next != NULL) 23 | temp = temp->next; 24 | 25 | Node* toDelete = temp->next; 26 | toDelete->prev = NULL; 27 | 28 | temp->next = NULL; 29 | 30 | free(toDelete); 31 | 32 | return head; 33 | } 34 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/11) Painter's Partition Problem.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/painter-s-partition-problem_1089557?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int partitionIntoK(vector &boards, int maxPaintableArea){ 4 | int totalPainters = 1; 5 | int currArea = 0; 6 | 7 | for(int i = 0; i maxPaintableArea){ 10 | totalPainters++; 11 | currArea = boards[i]; 12 | } 13 | } 14 | return totalPainters; 15 | } 16 | 17 | 18 | int findLargestMinDistance(vector &boards, int k) 19 | { 20 | int lower = *max_element(boards.begin(), boards.end()); 21 | int higher = accumulate(boards.begin(), boards.end(), 0); 22 | 23 | while(lower <= higher){ 24 | int mid = lower + (higher - lower)/2; 25 | 26 | if(partitionIntoK(boards, mid) > k) 27 | lower = mid + 1; 28 | 29 | else 30 | higher = mid - 1; 31 | } 32 | return lower; 33 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/10) Delete Middle Node.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/delete-middle-node_763267?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | Following is the class structure of the Node class: 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | Node() 12 | { 13 | this->data = 0; 14 | next = NULL; 15 | } 16 | Node(int data) 17 | { 18 | this->data = data; 19 | this->next = NULL; 20 | } 21 | Node(int data, Node* next) 22 | { 23 | this->data = data; 24 | this->next = next; 25 | } 26 | }; 27 | */ 28 | 29 | Node* deleteMiddle(Node* head){ 30 | if(head->next == NULL) return NULL; 31 | 32 | Node *prev = head, *slow = head, *fast = head; 33 | 34 | while(fast != NULL && fast->next != NULL){ 35 | prev = slow; 36 | slow = slow->next; 37 | fast = fast->next->next; 38 | } 39 | 40 | prev->next = slow->next; 41 | free(slow); 42 | 43 | return head; 44 | } 45 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/8) Alternate Numbers (Variety-2).cpp: -------------------------------------------------------------------------------- 1 | // https://takeuforward.org/arrays/rearrange-array-elements-by-sign/ 2 | 3 | // In Variey-2, the number of positive and negative values may or may not be equal. 4 | 5 | vector alternateNumbers(vector&a) { 6 | vector positive, negative, ans; 7 | 8 | for(int i=0; i 0) 10 | positive.push_back(a[i]); 11 | else 12 | negative.push_back(a[i]); 13 | } 14 | 15 | int positiveIndex = 0, negativeIndex = 0; 16 | bool turn = 1; 17 | 18 | while(positiveIndex < positive.size() && negativeIndex < negative.size()){ 19 | if(turn) 20 | ans.push_back(positive[positiveIndex++]); 21 | else 22 | ans.push_back(negative[negativeIndex++]); 23 | 24 | turn = !turn; 25 | } 26 | 27 | while(positiveIndex < positive.size()) 28 | ans.push_back(positive[positiveIndex++]); 29 | 30 | while(negativeIndex < negative.size()) 31 | ans.push_back(negative[negativeIndex++]); 32 | 33 | return ans; 34 | 35 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/8) Aggressive Cows.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/aggressive-cows_1082559?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | bool checkMinPos(vector &stalls, int minDist, int k){ 4 | 5 | int totalCowsPlaced = 1, lastPlacedCow = stalls[0]; 6 | 7 | for(int i = 1; i= lastPlacedCow + minDist){ 9 | totalCowsPlaced++; 10 | lastPlacedCow = stalls[i]; 11 | } 12 | } 13 | 14 | if(totalCowsPlaced >= k) 15 | return 1; 16 | else 17 | return 0; 18 | 19 | } 20 | 21 | int aggressiveCows(vector &stalls, int k) 22 | { 23 | sort(stalls.begin(), stalls.end()); 24 | 25 | int lower = 1, higher = stalls[stalls.size()-1]; 26 | int ans = -1; 27 | 28 | while(lower <= higher){ 29 | int mid = lower + (higher - lower)/2; 30 | 31 | if(checkMinPos(stalls, mid, k)){ 32 | ans = mid; 33 | lower = mid + 1; 34 | } 35 | else 36 | higher = mid - 1; 37 | } 38 | return ans; 39 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./10) Pattern 10.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1661718013/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_10 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | //Back-end complete function Template for C++ 10 | class Solution{ 11 | public: 12 | void printTriangle(int n) { 13 | 14 | // Top Pattern 15 | for(int i=1; i<=n; i++){ 16 | for(int j = 1; j<=i; j++) 17 | cout << "* "; 18 | cout << endl; 19 | } 20 | 21 | // Bottom Pattern 22 | for(int i=n-1; i>0; i--){ 23 | for(int j = 1; j<=i; j++) 24 | cout << "* "; 25 | cout << endl; 26 | } 27 | 28 | } 29 | }; 30 | 31 | //{ Driver Code Starts. 32 | 33 | int main() { 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | int n; 38 | cin >> n; 39 | 40 | Solution ob; 41 | ob.printTriangle(n); 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 2 - Learn Important Sorting Techniques/Step 2.2 - Sorting-II/1) Merge Sort.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/merge-sort_5846?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | void merge(vector & arr, int l, int mid, int r){ 4 | int lIndex = l, rIndex = mid+1; 5 | 6 | vector newArr(r-l+1); 7 | int newArrayIndex = 0; 8 | 9 | while(lIndex <= mid && rIndex <= r){ 10 | if(arr[lIndex] < arr[rIndex]) 11 | newArr[newArrayIndex++] = arr[lIndex++]; 12 | 13 | else 14 | newArr[newArrayIndex++] = arr[rIndex++]; 15 | } 16 | 17 | while(lIndex <= mid) 18 | newArr[newArrayIndex++] = arr[lIndex++]; 19 | 20 | while(rIndex <= r) 21 | newArr[newArrayIndex++] = arr[rIndex++]; 22 | 23 | newArrayIndex = 0; 24 | for(int i = l; i <= r; i++) 25 | arr[i] = newArr[newArrayIndex++]; 26 | 27 | return; 28 | } 29 | 30 | void mergeSort(vector& arr, int l, int r) { 31 | if(l >= r) return; 32 | 33 | int mid = l + (r-l)/2; 34 | mergeSort(arr, l, mid); 35 | mergeSort(arr, mid+1, r); 36 | merge(arr, l , mid, r); 37 | return; 38 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/5) Three Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/three-sum_6922132?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | vector> triplet(int n, vector &arr) 6 | { 7 | set> checkUniqueness; 8 | vector> ans; 9 | sort(arr.begin(), arr.end()); 10 | 11 | for(int i =0; i 0) break; 15 | 16 | int lower = i+1, higher = arr.size()-1; 17 | while(lower < higher){ 18 | if(arr[i] + arr[lower] + arr[higher] == 0){ 19 | if(checkUniqueness.count({arr[i], arr[lower], arr[higher]}) == 0){ 20 | checkUniqueness.insert({arr[i], arr[lower], arr[higher]}); 21 | ans.push_back({arr[i], arr[lower], arr[higher]}); 22 | } 23 | lower++; 24 | } 25 | else if(arr[i] + arr[lower] + arr[higher] < 0) 26 | lower++; 27 | else 28 | higher--; 29 | } 30 | } 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./17) Pattern 17.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/triangle-pattern-1662285911/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_17 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 0; i='A'; j--) 25 | cout << j; 26 | 27 | cout << endl; 28 | } 29 | 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int n; 40 | cin >> n; 41 | 42 | Solution ob; 43 | ob.printTriangle(n); 44 | } 45 | return 0; 46 | } 47 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.1 - Learn 1D LinkedList/5) Search in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/search-in-a-linked-list_975381?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | template 8 | class Node 9 | { 10 | public: 11 | T data; 12 | Node *next; 13 | Node() 14 | { 15 | this->data = 0; 16 | this->next = NULL; 17 | } 18 | Node(T data) 19 | { 20 | this->data = data; 21 | this->next = NULL; 22 | } 23 | Node(T data, T* next) 24 | { 25 | this->data = data; 26 | this->next = next; 27 | } 28 | }; 29 | 30 | *****************************************************************/ 31 | 32 | int searchInLinkedList(Node *head, int k) { 33 | Node *temp = head; 34 | 35 | while(temp != NULL){ 36 | if(temp->data == k) return 1; 37 | temp = temp->next; 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/6) Capacity To Ship Packages Within D Days.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/capacity-to-ship-packages-within-d-days_1229379?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | bool checkMinWeight (vector &weights, int minWeight, int d){ 4 | int totalDays = 1, weightTillNow = 0; 5 | 6 | for(int i = 0; i minWeight){ 9 | totalDays++; 10 | weightTillNow = weights[i]; 11 | } 12 | if(totalDays > d) return 0; 13 | } 14 | return 1; 15 | } 16 | 17 | int leastWeightCapacity(vector &weights, int d) 18 | { 19 | int lower = *max_element(weights.begin(), weights.end()); 20 | int higher = accumulate(weights.begin(), weights.end(), 0); 21 | int ans = -1; 22 | 23 | while(lower <= higher){ 24 | int mid = lower + (higher - lower)/2; 25 | 26 | if(checkMinWeight(weights, mid, d)){ 27 | ans = mid; 28 | higher = mid - 1; 29 | } 30 | else 31 | lower = mid + 1; 32 | } 33 | return ans; 34 | } -------------------------------------------------------------------------------- /Step 2 - Learn Important Sorting Techniques/Step 2.2 - Sorting-II/4) Quick Sort.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/quick-sort_5844?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | The function is called with the parameters: 5 | quickSort(input, 0, size - 1); 6 | 7 | */ 8 | 9 | int partitionArray(int input[], int start, int end) { 10 | 11 | int partitionIndex = start; 12 | 13 | int l = start + 1, r = end; 14 | 15 | while(l <= r){ 16 | 17 | while(l <= r && input[l] <= input[partitionIndex]) l++; 18 | 19 | while(l <= r && input[r] > input[partitionIndex]) r--; 20 | 21 | if(l < r) 22 | swap(input[l], input[r]); 23 | 24 | } 25 | 26 | swap(input[l-1], input[partitionIndex]); 27 | return l-1; 28 | } 29 | 30 | void quickSort(int input[], int start, int end) { 31 | /* 32 | Don't write main(). 33 | Don't read input, it is passed as function argument. 34 | Change in the given array itself. 35 | Taking input and printing output is handled automatically. 36 | */ 37 | 38 | if(start >= end) return; 39 | 40 | int partitionIndex = partitionArray(input, start, end); 41 | quickSort(input, start, partitionIndex-1); 42 | quickSort(input, partitionIndex+1, end); 43 | return; 44 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/9) Delete Kth Node From End.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/delete-kth-node-from-end_799912?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | Following is the class structure of the Node class: 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | Node() 12 | { 13 | this->data = 0; 14 | next = NULL; 15 | } 16 | Node(int data) 17 | { 18 | this->data = data; 19 | this->next = NULL; 20 | } 21 | Node(int data, Node* next) 22 | { 23 | this->data = data; 24 | this->next = next; 25 | } 26 | }; 27 | */ 28 | 29 | Node* removeKthNode(Node* head, int K) 30 | { 31 | Node *forward = head; 32 | 33 | while(K--) 34 | forward = forward->next; 35 | 36 | if(forward == NULL) 37 | return head->next; 38 | 39 | Node *prev = head, *curr = head; 40 | 41 | while(forward != NULL){ 42 | prev = curr; 43 | curr = curr->next; 44 | forward = forward->next; 45 | } 46 | 47 | prev->next = curr->next; 48 | free(curr); 49 | 50 | return head; 51 | } 52 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./12) Pattern 12.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/double-triangle-pattern-1662664259/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_12 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | for(int i = 1; i<=n; i++){ 14 | 15 | // Number Pattern 16 | for(int j = 1; j<=i; j++) 17 | cout << j << " "; 18 | 19 | // Space Pattern 20 | for(int j = 1; j<=2*(n-i); j++) 21 | cout << " "; 22 | 23 | // Number Pattern 24 | for(int j = i; j>0; j--) 25 | cout << j << " "; 26 | 27 | cout << endl; 28 | } 29 | 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int n; 40 | cin >> n; 41 | 42 | Solution ob; 43 | ob.printTriangle(n); 44 | } 45 | return 0; 46 | } 47 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./21) Pattern 21.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/square-pattern-1662287714/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_21 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printSquare(int n) { 12 | 13 | 14 | if(n == 1){ 15 | cout << "*" << endl; 16 | return; 17 | } 18 | 19 | for(int i = 0; i> t; 42 | while (t--) { 43 | int n; 44 | cin >> n; 45 | 46 | Solution ob; 47 | ob.printSquare(n); 48 | } 49 | return 0; 50 | } 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/6) Find length of Loop.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-length-of-loop_8160455?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of linked list: 5 | * 6 | * class Node { 7 | * public: 8 | * int data; 9 | * Node *next; 10 | * 11 | * Node(int data) { 12 | * this->data = data; 13 | * this->next = NULL; 14 | * } 15 | * }; 16 | * 17 | *************************************************************************/ 18 | 19 | int lengthOfLoop(Node *head) { 20 | Node *slow = head, *fast = head; 21 | 22 | bool checkCycle = false; 23 | 24 | while(fast != NULL){ 25 | slow = slow->next; 26 | fast = fast->next; 27 | if(fast != NULL) 28 | fast = fast->next; 29 | else return 0; 30 | 31 | if(slow == fast){ 32 | checkCycle = true; 33 | break; 34 | } 35 | } 36 | 37 | if(!checkCycle) 38 | return 0; 39 | 40 | int count = 1; 41 | fast = fast->next; 42 | 43 | while(slow != fast){ 44 | fast = fast->next; 45 | count++; 46 | } 47 | 48 | return count; 49 | } 50 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/12) Zero Matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/zero-matrix_1171153?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | vector> zeroMatrix(vector> &matrix, int n, int m) { 5 | 6 | bool make0Row0 = 0; 7 | for(int coln = 0; coln0; coln--){ 25 | if(matrix[0][coln] == 0){ 26 | for(int row = 0; rowdata = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | 30 | *****************************************************************/ 31 | 32 | int length(Node *head) 33 | { 34 | if(head == NULL) return 0; 35 | 36 | int count = 0; 37 | 38 | Node*temp = head; 39 | 40 | while(temp != NULL){ 41 | temp = temp->next; 42 | count++; 43 | } 44 | 45 | return count; 46 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/13) K-th Element of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/k-th-element-of-2-sorted-array_1164159?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int kthElement(vector &arr1, vector& arr2, int n, int m, int k){ 4 | int flower = 0, fhigher = arr1.size()-1, fmid; 5 | int slower = 0, shigher = arr2.size(), smid; 6 | 7 | while(flower <= fhigher && slower <= shigher){ 8 | fmid = flower + (fhigher - flower)/2; 9 | smid = slower + (shigher - slower)/2; 10 | 11 | int numberCoveredTillNow = fmid + smid + 1; 12 | 13 | if(arr1[fmid] > arr2[smid]){ 14 | 15 | if(numberCoveredTillNow >= k) 16 | fhigher = fmid - 1; 17 | 18 | else if(numberCoveredTillNow < k) 19 | slower = smid + 1; 20 | 21 | } 22 | 23 | else{ 24 | 25 | if(numberCoveredTillNow >= k) 26 | shigher = smid - 1; 27 | 28 | else 29 | flower = fmid + 1; 30 | 31 | } 32 | } 33 | 34 | if(flower > fhigher) 35 | return arr2[slower+(k-flower-slower)-1]; 36 | 37 | else 38 | return arr1[flower+(k-flower-slower)-1]; 39 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.5 - Hard Problems of LL/3) Flatten A Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/flatten-a-linked-list_1112655?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | * Definition for linked list. 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *next; 9 | * Node *child; 10 | * Node() : data(0), next(nullptr), child(nullptr){}; 11 | * Node(int x) : data(x), next(nullptr), child(nullptr) {} 12 | * Node(int x, Node *next, Node *child) : data(x), next(next), child(child) {} 13 | * }; 14 | */ 15 | 16 | Node* flattenLinkedList(Node* head) 17 | { 18 | if(head->next == NULL) 19 | return head; 20 | 21 | Node* newList = flattenLinkedList(head->next); 22 | head->next = NULL; 23 | 24 | Node* result = new Node(0); 25 | Node* resultItr = result; 26 | 27 | while(head != NULL && newList != NULL){ 28 | if(head->data < newList->data){ 29 | resultItr->child = head; 30 | resultItr = resultItr->child; 31 | head = head->child; 32 | } 33 | else{ 34 | resultItr->child = newList; 35 | resultItr = resultItr->child; 36 | newList = newList->child; 37 | } 38 | } 39 | 40 | if(head != NULL) 41 | resultItr->child = head; 42 | else 43 | resultItr->child = newList; 44 | 45 | return result->child; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/9) Allocate Books.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/allocate-books_1090540?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int checkMinPages(vector& arr, int minPages){ 4 | 5 | int totalChildren = 0; 6 | int currPages = 0; 7 | 8 | for(int i = 0; i minPages){ 11 | totalChildren++; 12 | currPages = arr[i]; 13 | } 14 | } 15 | if(currPages > 0) totalChildren++; 16 | return totalChildren; 17 | } 18 | 19 | int findPages(vector& arr, int n, int m) { 20 | int lower = *max_element(arr.begin(), arr.end()); 21 | int higher = accumulate(arr.begin(), arr.end(), 0); 22 | int ans = -1; 23 | 24 | while(lower <= higher){ 25 | 26 | int mid = lower + (higher - lower)/2; 27 | int totalChildren = checkMinPages(arr, mid); 28 | 29 | if(totalChildren <= m){ 30 | if(totalChildren == m) 31 | ans = mid; 32 | higher = mid - 1; 33 | } 34 | else{ 35 | ans = max(ans, mid); 36 | lower = mid + 1; 37 | } 38 | 39 | } 40 | if(ans == -1 && n > m) return *max_element(arr.begin(), arr.end()); 41 | return ans; 42 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.5 - Hard Problems of LL/2) Rotate Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/rotate-linked-list_920454?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *next; 9 | * Node() : data(0), next(nullptr) {} 10 | * Node(int x) : data(x), next(nullptr) {} 11 | * Node(int x, Node *next) : data(x), next(next) {} 12 | * }; 13 | */ 14 | 15 | int getLength(Node* head){ 16 | if(head == NULL) return 0; 17 | 18 | int count = 0; 19 | 20 | while(head != NULL){ 21 | count++; 22 | head = head->next; 23 | } 24 | 25 | return count; 26 | } 27 | 28 | Node *rotate(Node *head, int k) { 29 | 30 | if(head == NULL || head->next == NULL) return head; 31 | 32 | int length = getLength(head); 33 | 34 | k = k%length; 35 | k = length-k; 36 | 37 | Node* tail = head; 38 | Node* curr; 39 | 40 | int count = 0; 41 | 42 | while(tail->next != NULL){ 43 | count++; 44 | if(count == k) 45 | curr = tail; 46 | tail = tail->next; 47 | } 48 | 49 | tail->next = head; 50 | head = curr->next; 51 | curr->next = NULL; 52 | 53 | return head; 54 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.5 - Hard Problems of LL/4) Clone a Linked List with random pointers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/clone-a-linked-list-with-random-pointers_983604?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | * Definition for linked list. 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *next; 9 | * Node *random; 10 | * Node() : data(0), next(nullptr), random(nullptr){}; 11 | * Node(int x) : data(x), next(nullptr), random(nullptr) {} 12 | * Node(int x, Node *next, Node *random) : data(x), next(next), random(random) {} 13 | * }; 14 | */ 15 | 16 | Node *cloneLL(Node *head){ 17 | if(head == NULL) return NULL; 18 | 19 | Node* itr = head; 20 | 21 | while(itr != NULL){ 22 | Node* temp = new Node(itr->data); 23 | temp->next = itr->next; 24 | itr->next = temp; 25 | itr = itr->next->next; 26 | } 27 | 28 | itr = head; 29 | 30 | while(itr != NULL){ 31 | if(itr->random != NULL) 32 | itr->next->random = itr->random->next; 33 | itr = itr->next->next; 34 | } 35 | 36 | Node* result = new Node(0); 37 | Node* resultItr = result; 38 | itr = head; 39 | 40 | while(itr != NULL){ 41 | resultItr->next = itr->next; 42 | resultItr = resultItr->next; 43 | itr->next = itr->next->next; 44 | itr = itr->next; 45 | } 46 | 47 | return result->next; 48 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/6) 4Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/4sum_5713771?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector> fourSum(vector& nums, int target) { 4 | 5 | sort(nums.begin(), nums.end()); 6 | vector> ans; 7 | 8 | for(int i = 0; i target) 23 | higher--; 24 | 25 | else{ 26 | ans.push_back({nums[i], nums[j], nums[lower], nums[higher]}); 27 | lower++; 28 | higher--; 29 | 30 | while(lower < higher && nums[lower] == nums[lower-1]) lower++; 31 | while(lower < higher && nums[higher] == nums[higher+1]) higher--; 32 | } 33 | } 34 | } 35 | } 36 | 37 | return ans; 38 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/8) Segregate Even And Odd Nodes In a Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/segregate-even-and-odd-nodes-in-a-linked-list_1116100?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | * Definition for linked list. 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *next; 9 | * Node() : data(0), next(nullptr) {}; 10 | * Node(int x) : data(x), next(nullptr) {} 11 | * Node(int x, Node *next) : data(x), next(next) {} 12 | * }; 13 | */ 14 | 15 | Node* segregateEvenOdd(Node* head) 16 | { 17 | Node *evenHead = new Node(); 18 | Node *oddHead = new Node(); 19 | 20 | Node *evenIter = evenHead; 21 | Node *oddIter = oddHead; 22 | 23 | Node *iter = head; 24 | 25 | while(iter != NULL){ 26 | if(iter->data % 2 == 0){ 27 | evenIter->next = iter; 28 | evenIter = evenIter->next; 29 | } 30 | else{ 31 | oddIter->next = iter; 32 | oddIter = oddIter->next; 33 | } 34 | iter = iter->next; 35 | } 36 | oddIter->next = NULL; 37 | 38 | evenHead = evenHead->next; 39 | oddHead = oddHead->next; 40 | 41 | if(evenHead) 42 | evenIter->next = oddHead; 43 | else 44 | evenHead = oddHead; 45 | 46 | return evenHead; 47 | } 48 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.5 - Hard Problems of LL/1) Reverse List In K Groups.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reverse-list-in-k-groups_983644?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *next; 9 | * Node() : data(0), next(nullptr) {} 10 | * Node(int x) : data(x), next(nullptr) {} 11 | * Node(int x, Node *next) : data(x), next(next) {} 12 | * }; 13 | */ 14 | 15 | int getLength(Node* head){ 16 | if(head == NULL) return 0; 17 | 18 | int count = 0; 19 | 20 | while(head != NULL){ 21 | count++; 22 | head = head->next; 23 | } 24 | 25 | return count; 26 | } 27 | 28 | Node* kReverse(Node* head, int k) { 29 | 30 | if(head == NULL) return head; 31 | 32 | int length = getLength(head); 33 | 34 | if(length < k) return head; 35 | 36 | Node *prev = NULL; 37 | Node *curr = head; 38 | Node *forward = head->next; 39 | int count = 0; 40 | 41 | while(countnext = prev; 43 | 44 | prev = curr; 45 | curr = forward; 46 | if(forward) 47 | forward = forward->next; 48 | 49 | count++; 50 | } 51 | 52 | head->next = kReverse(curr, k); 53 | 54 | return prev; 55 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.2 - Medium/14) Spiral Matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/spiral-matrix_6922069?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | vector spiralMatrix(vector>&MATRIX) { 4 | int top = 0, right = MATRIX[0].size()-1; 5 | int bottom = MATRIX.size()-1, left = 0; 6 | 7 | vector ans; 8 | int count = MATRIX.size()*MATRIX[0].size(); 9 | 10 | while(count > 0){ 11 | 12 | // top row 13 | for(int i = left; i<=right; i++){ 14 | ans.push_back(MATRIX[top][i]); 15 | count--; 16 | } 17 | top++; 18 | 19 | 20 | if(count <= 0) break; 21 | // rightmost coln 22 | for(int i = top; i<=bottom; i++){ 23 | ans.push_back(MATRIX[i][right]); 24 | count--; 25 | } 26 | right--; 27 | 28 | 29 | if(count <= 0) break; 30 | // bottom row 31 | for(int i = right; i>=left; i--){ 32 | ans.push_back(MATRIX[bottom][i]); 33 | count--; 34 | } 35 | bottom--; 36 | 37 | 38 | if(count <= 0) break; 39 | // leftmost coln 40 | for(int i = bottom; i>=top; i--){ 41 | ans.push_back(MATRIX[i][left]); 42 | count--; 43 | } 44 | left++; 45 | } 46 | 47 | return ans; 48 | 49 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/4) Cycle Detection in a Singly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/cycle-detection-in-a-singly-linked-list_628974?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | 30 | *****************************************************************/ 31 | 32 | bool detectCycle(Node *head) 33 | { 34 | Node *slow = head, *fast = head; 35 | 36 | while(fast != NULL){ 37 | slow = slow->next; 38 | fast = fast->next; 39 | if(fast != NULL) 40 | fast = fast->next; 41 | else return 0; 42 | 43 | if(slow == fast) return 1; 44 | } 45 | 46 | return false; 47 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/7) First and Last Position of an Element In Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/first-and-last-position-of-an-element-in-sorted-array_1082549?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | int firstPosition(vector& arr, int k){ 4 | int lower = 0, higher = arr.size()-1; 5 | int ans = -1; 6 | 7 | while(lower <= higher){ 8 | 9 | int mid = lower + (higher - lower)/2; 10 | 11 | if(arr[mid] == k){ 12 | ans = mid; 13 | higher = mid - 1; 14 | } 15 | else if(arr[mid] < k) 16 | lower = mid + 1; 17 | else 18 | higher = mid - 1; 19 | } 20 | 21 | return ans; 22 | } 23 | 24 | int lastPosition(vector& arr, int k){ 25 | int lower = 0, higher = arr.size()-1; 26 | int ans = -1; 27 | 28 | while(lower <= higher){ 29 | 30 | int mid = lower + (higher - lower)/2; 31 | 32 | if(arr[mid] == k){ 33 | ans = mid; 34 | lower = mid + 1; 35 | } 36 | else if(arr[mid] < k) 37 | lower = mid + 1; 38 | else 39 | higher = mid - 1; 40 | } 41 | 42 | return ans; 43 | } 44 | 45 | pair firstAndLastPosition(vector& arr, int n, int k) 46 | { 47 | return {firstPosition(arr, k), lastPosition(arr, k)}; 48 | } 49 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/2) Reverse Linked List (Iterative).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reverse-linked-list_920513?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | 30 | *****************************************************************/ 31 | 32 | Node* reverseLinkedList(Node *head) 33 | { 34 | if(head->next == NULL) return head; 35 | 36 | Node *prev = NULL, *curr = head, *forward = head->next; 37 | 38 | while(forward!=NULL){ 39 | curr->next = prev; 40 | prev = curr; 41 | curr = forward; 42 | forward = forward->next; 43 | } 44 | curr->next = prev; 45 | return curr; 46 | } 47 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/3) Reverse Linked List (Recursive).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reverse-linked-list_920513?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | 30 | *****************************************************************/ 31 | 32 | Node* reverseLinkedListHelper(Node *prev, Node *curr){ 33 | if(curr == NULL) return prev; 34 | 35 | Node *forward = curr->next; 36 | curr->next = prev; 37 | prev = curr; 38 | curr = forward; 39 | 40 | return reverseLinkedListHelper(prev, curr); 41 | } 42 | 43 | Node* reverseLinkedList(Node *head) 44 | { 45 | return reverseLinkedListHelper(NULL, head); 46 | } 47 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.2 - Learn Doubly LinkedList/4) Reverse A Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/reverse-a-doubly-linked-list_1116098?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | /* 4 | Following is the class structure of the Node class: 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next,*prev; 11 | Node() 12 | { 13 | this->data = 0; 14 | next = NULL; 15 | prev= NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | this->prev= NULL; 22 | } 23 | Node(int data, Node* next, Node *prev) 24 | { 25 | this->data = data; 26 | this->next = next; 27 | this->prev= prev; 28 | } 29 | }; 30 | 31 | */ 32 | 33 | Node* reverseDLL(Node* head) 34 | { 35 | if(head->next == NULL) return head; 36 | 37 | Node *prev, *curr, *forward; 38 | prev = NULL; 39 | curr = head; 40 | forward = head->next; 41 | 42 | while(forward != NULL){ 43 | curr->next = prev; 44 | if(prev != NULL) 45 | prev->prev = curr; 46 | 47 | prev = curr; 48 | curr = forward; 49 | if(forward != NULL) 50 | forward = forward->next; 51 | } 52 | curr->next = prev; 53 | prev->prev = curr; 54 | curr->prev = NULL; 55 | 56 | head = curr; 57 | return head; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/1) Pairs in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | -> Pair is used to combine together two values that may be of different data types. 7 | -> Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples. 8 | -> The pair container is a simple container defined in header consisting of two data elements or objects. 9 | 10 | Syntax: pair Pair_name 11 | 12 | 13 | -> Most used functions : 14 | 15 | 1) make_pair() - It makes it possible to create a value pair without explicitly writing the data types. 16 | 17 | Syntax : pair1 = make_pair(data1, data2); 18 | 19 | 20 | 2) swap() - It swaps the contents of one pair object with the contents of another pair object. The pairs have to be of the same kind. 21 | 22 | Syntax : pair1.swap(pair2) 23 | 24 | */ 25 | 26 | int main() 27 | { 28 | pair p1 = {1, 3}; 29 | cout << p1.first << " " << p1.second << endl; 30 | 31 | 32 | pair> p2 = {1, {3, 4}}; 33 | cout << p2.first << " " << p2.second.first << " " << p2.second.second << endl; 34 | 35 | 36 | pair arr[] = { {1, 2}, {2, 5}, {5, 1}}; 37 | cout << arr[1].second << endl; 38 | 39 | 40 | pair < int, int > p3; 41 | p1 = make_pair(2, 4); 42 | 43 | p1.swap(p3); 44 | 45 | cout << p1.first << " " << p1.second << endl; 46 | cout << p3.first << " " << p3.second << endl; 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.1 - BS on 1D Arrays/9) Search In Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/search-in-rotated-sorted-array_1082554?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int searchSecondHalf(vector& arr, int k){ 4 | int lower = 0, higher = arr.size()-1, ans = -1; 5 | 6 | while(lower <= higher){ 7 | int mid = lower + (higher - lower)/2; 8 | 9 | if(arr[mid] == k) return mid; 10 | else if (arr[mid] < k) 11 | lower = mid+1; 12 | else{ 13 | if(arr[mid] < arr[0]) 14 | higher = mid-1; 15 | else 16 | lower = mid+1; 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | int searchFirstHalf(vector& arr, int k){ 23 | int lower = 0, higher = arr.size()-1, ans = -1; 24 | 25 | while(lower <= higher){ 26 | int mid = lower + (higher - lower)/2; 27 | 28 | if(arr[mid] == k) return mid; 29 | else if (arr[mid] < k){ 30 | if(arr[mid] < arr[0]) 31 | higher = mid-1; 32 | else 33 | lower = mid+1; 34 | } 35 | else 36 | higher = mid-1; 37 | } 38 | 39 | return -1; 40 | } 41 | 42 | 43 | int search(vector& arr, int n, int k) 44 | { 45 | int check = 0; 46 | if(k >= arr[0]) check = 1; 47 | 48 | if(check == 0) return searchSecondHalf(arr, k); 49 | else return searchFirstHalf(arr, k); 50 | } 51 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/7) Check If Linked List Is Palindrome.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/check-if-linked-list-is-palindrome_985248?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | *****************************************************************/ 30 | 31 | bool isPalindrome(Node *head) 32 | { 33 | Node *newListHead = NULL; 34 | Node *iter = head; 35 | 36 | while(iter != NULL){ 37 | Node* newNode = new Node(iter->data); 38 | newNode->next = newListHead; 39 | newListHead = newNode; 40 | iter = iter->next; 41 | } 42 | 43 | Node* iter1 = head, *iter2 = newListHead; 44 | 45 | while(iter1 != NULL){ 46 | if(iter1->data != iter2->data) return false; 47 | iter1 = iter1->next; 48 | iter2 = iter2->next; 49 | } 50 | 51 | return true; 52 | } -------------------------------------------------------------------------------- /Step 7 - Recursion [PatternWise]/Step 7.1 - Get a Strong Hold/1) Implement Atoi Function (Recursion).cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/implement-atoi-function_981270?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | void solve(string s, long long &ans, int index, bool &check){ 4 | if(index == s.length()) return; 5 | else if (s[index] == ' '){ 6 | solve(s, ans, index+1, check); 7 | return; 8 | } 9 | else if (ans == 0 && s[index] == '+'){ 10 | if(index != 0 && (s[index-1] == '+' || s[index-1] == '-')) 11 | return; 12 | check = false; 13 | solve(s, ans, index+1, check); 14 | return; 15 | } 16 | else if (ans == 0 && s[index] == '-'){ 17 | if(index != 0 && (s[index-1] == '+' || s[index-1] == '-')) 18 | return; 19 | check = true; 20 | solve(s, ans, index+1, check); 21 | return; 22 | } 23 | else if (!(48 <= s[index] && s[index] <= 57)) return; 24 | 25 | ans = ans*10 + (s[index] - 48); 26 | if(ans > INT_MAX){ 27 | ans = INT_MAX; 28 | ans += 5; 29 | return; 30 | } 31 | solve(s, ans, index+1, check); 32 | 33 | } 34 | 35 | int createAtoi(string s) { 36 | long long ans = 0; 37 | bool check = false; // false indicates positive. 38 | solve(s, ans, 0, check); 39 | 40 | if(check) 41 | ans = -ans; 42 | 43 | if(ans < INT_MIN) 44 | ans = INT_MIN; 45 | else if (ans > INT_MAX) 46 | ans = INT_MAX; 47 | 48 | return ans; 49 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/12) Number of Inversions.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/number-of-inversions_6840276?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | void countInversionHelper(vector&a, int start, int mid, int end, int &count){ 4 | int leftIndex = start, rightIndex = mid+1; 5 | vector temp(end-start+1); 6 | int tempIndex = 0; 7 | 8 | while(leftIndex <= mid && rightIndex <= end){ 9 | if(a[leftIndex] <= a[rightIndex]) 10 | temp[tempIndex++] = a[leftIndex++]; 11 | 12 | else if(a[leftIndex] > a[rightIndex]){ 13 | temp[tempIndex++] = a[rightIndex++]; 14 | count+=mid-leftIndex+1; 15 | } 16 | } 17 | 18 | while(leftIndex <= mid) 19 | temp[tempIndex++] = a[leftIndex++]; 20 | while(rightIndex <= end) 21 | temp[tempIndex++] = a[rightIndex++]; 22 | 23 | tempIndex = 0; 24 | for(int i = start; i<=end; i++) 25 | a[i] = temp[tempIndex++]; 26 | 27 | return; 28 | } 29 | 30 | void countInversion(vector&a, int start, int end, int &count){ 31 | if(start >= end) return; 32 | 33 | int mid = start + (end - start)/2; 34 | 35 | countInversion(a, start, mid, count); 36 | countInversion(a, mid+1, end, count); 37 | countInversionHelper(a, start, mid, end, count); 38 | return; 39 | } 40 | 41 | int numberOfInversions(vector&a, int n) { 42 | int count = 0; 43 | countInversion(a, 0, n-1, count); 44 | 45 | return count; 46 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.4 - Medium Problems of DLL/2) Find pairs with given sum in doubly linked list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/find-pairs-with-given-sum-in-doubly-linked-list_1164172?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of doubly linked list: 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *prev; 9 | * Node *next; 10 | * Node() { 11 | * this->data = 0; 12 | * this->prev = NULL; 13 | * this->next = NULL; 14 | * } 15 | * Node(int data) { 16 | * this->data = data; 17 | * this->prev = NULL; 18 | * this->next = NULL; 19 | * } 20 | * Node (int data, Node *next, Node *prev) { 21 | * this -> data = data; 22 | * this -> prev = prev; 23 | * this -> next = next; 24 | * } 25 | * }; 26 | * 27 | *************************************************************************/ 28 | 29 | vector> findPairs(Node* head, int k) 30 | { 31 | vector> ans; 32 | 33 | Node *tail = head; 34 | while(tail->next != NULL) 35 | tail = tail->next; 36 | 37 | while(head!=tail && head->data <= tail->data){ 38 | if(head->data + tail->data == k){ 39 | ans.push_back({head->data, tail->data}); 40 | head = head->next; 41 | tail = tail->prev; 42 | } 43 | else if(head->data + tail->data < k) 44 | head = head->next; 45 | else 46 | tail = tail->prev; 47 | } 48 | 49 | return ans; 50 | } 51 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/13) Team Contest.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/team-contest_6840309?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | // Strivers solution is better 4 | 5 | void countTeamHelper(vector&a, int start, int mid, int end, int &count){ 6 | 7 | int leftIndex = start, rightIndex = mid+1; 8 | vector temp(end-start+1); 9 | int tempIndex = 0; 10 | 11 | while(leftIndex <= mid && rightIndex <= end){ 12 | if(a[leftIndex] <= a[rightIndex]) 13 | temp[tempIndex++] = a[leftIndex++]; 14 | 15 | else if(a[leftIndex] > a[rightIndex]){ 16 | int index = lower_bound(a.begin()+leftIndex, a.begin()+mid+1, 2*a[rightIndex] + 1) - a.begin(); 17 | temp[tempIndex++] = a[rightIndex++]; 18 | count+=mid-index+1; 19 | } 20 | } 21 | 22 | while(leftIndex <= mid) 23 | temp[tempIndex++] = a[leftIndex++]; 24 | while(rightIndex <= end) 25 | temp[tempIndex++] = a[rightIndex++]; 26 | 27 | tempIndex = 0; 28 | for(int i = start; i<=end; i++) 29 | a[i] = temp[tempIndex++]; 30 | 31 | return; 32 | } 33 | 34 | void countTeam(vector&a, int start, int end, int &count){ 35 | if(start >= end) return; 36 | 37 | int mid = start + (end - start)/2; 38 | 39 | countTeam(a, start, mid, count); 40 | countTeam(a, mid+1, end, count); 41 | countTeamHelper(a, start, mid, end, count); 42 | return; 43 | } 44 | 45 | int team(vector & skill, int n) 46 | { 47 | int count = 0; 48 | countTeam(skill, 0, n-1, count); 49 | 50 | return count; 51 | } 52 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/12) Sort linked list of 0s 1s 2s.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sort-linked-list-of-0s-1s-2s_1071937?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | Following is the class structure of the Node class: 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | Node() 12 | { 13 | this->data = 0; 14 | next = NULL; 15 | } 16 | Node(int data) 17 | { 18 | this->data = data; 19 | this->next = NULL; 20 | } 21 | Node(int data, Node* next) 22 | { 23 | this->data = data; 24 | this->next = next; 25 | } 26 | }; 27 | */ 28 | 29 | Node* sortList(Node *head){ 30 | 31 | if(head->next == NULL) return head; 32 | 33 | Node *zeroHead = new Node(); 34 | Node *oneHead = new Node(); 35 | Node *twoHead = new Node(); 36 | 37 | Node *zeroItr = zeroHead; 38 | Node *oneItr = oneHead; 39 | Node *twoItr = twoHead; 40 | 41 | Node *itr = head; 42 | 43 | while(itr != NULL){ 44 | if(itr->data == 0){ 45 | zeroItr->next = itr; 46 | zeroItr = zeroItr->next; 47 | } 48 | else if(itr->data == 1){ 49 | oneItr->next = itr; 50 | oneItr = oneItr->next; 51 | } 52 | else{ 53 | twoItr->next = itr; 54 | twoItr = twoItr->next; 55 | } 56 | itr = itr->next; 57 | } 58 | 59 | twoItr->next = NULL; 60 | oneItr->next = twoHead->next; 61 | zeroItr->next = oneHead->next; 62 | 63 | return zeroHead->next; 64 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./9) Pattern 9.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/pattern/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_9 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printDiamond(int n) { 12 | 13 | // Top Pattern 14 | for(int i = 1; i<=n; i++){ 15 | 16 | // Extra Spaces 17 | for(int j = 0; j0; i--){ 32 | 33 | // Extra Spaces 34 | for(int j = 0; j> t; 55 | while (t--) { 56 | int n; 57 | cin >> n; 58 | 59 | Solution ob; 60 | ob.printDiamond(n); 61 | } 62 | return 0; 63 | } 64 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/5) Linked List Cycle II.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/linked-list-cycle-ii_1112628?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | 30 | *****************************************************************/ 31 | 32 | Node *firstNode(Node *head) 33 | { 34 | Node *slow = head, *fast = head; 35 | 36 | bool checkCycle = false; 37 | 38 | while(fast != NULL){ 39 | slow = slow->next; 40 | fast = fast->next; 41 | if(fast != NULL) 42 | fast = fast->next; 43 | else return NULL; 44 | 45 | if(slow == fast){ 46 | checkCycle = true; 47 | break; 48 | } 49 | } 50 | 51 | if(!checkCycle) 52 | return NULL; 53 | 54 | slow = head; 55 | 56 | while(slow != fast){ 57 | slow = slow->next; 58 | fast = fast->next; 59 | } 60 | 61 | return slow; 62 | } -------------------------------------------------------------------------------- /Step 4 - Binary Search [1D, 2D Arrays, Search Space]/Step 4.2 - BS on Answers/14) Median of two sorted arrays.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/median-of-two-sorted-arrays_985294?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | int kthElement(vector &arr1, vector& arr2, int n, int m, int k){ 4 | int flower = 0, fhigher = arr1.size()-1, fmid; 5 | int slower = 0, shigher = arr2.size(), smid; 6 | 7 | while(flower <= fhigher && slower <= shigher){ 8 | fmid = flower + (fhigher - flower)/2; 9 | smid = slower + (shigher - slower)/2; 10 | 11 | int numberCoveredTillNow = fmid + smid + 1; 12 | 13 | if(arr1[fmid] > arr2[smid]){ 14 | 15 | if(numberCoveredTillNow >= k) 16 | fhigher = fmid - 1; 17 | 18 | else if(numberCoveredTillNow < k) 19 | slower = smid + 1; 20 | 21 | } 22 | 23 | else{ 24 | 25 | if(numberCoveredTillNow >= k) 26 | shigher = smid - 1; 27 | 28 | else 29 | flower = fmid + 1; 30 | 31 | } 32 | } 33 | 34 | if(flower > fhigher) 35 | return arr2[slower+(k-flower-slower)-1]; 36 | 37 | else 38 | return arr1[flower+(k-flower-slower)-1]; 39 | } 40 | 41 | double median(vector& a, vector& b) { 42 | int totalSize = a.size() + b.size(); 43 | if(totalSize%2 == 0){ 44 | double ans = kthElement(a, b, a.size(), b.size(), totalSize/2) + kthElement(a, b, a.size(), b.size(), totalSize/2 + 1); 45 | ans = ans/2; 46 | return ans; 47 | } 48 | else 49 | return kthElement(a, b, a.size(), b.size(), totalSize/2 + 1); 50 | } -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.1 - Easy/9) Merge 2 Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sorted-array_6613259?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf 2 | 3 | vector < int > sortedArray(vector < int > a, vector < int > b) { 4 | vector unionArray; 5 | 6 | int indexA = 0, indexB = 0; 7 | 8 | while(indexA < a.size() && indexB < b.size()){ 9 | 10 | // current element of a is less 11 | if(a[indexA] < b[indexB]){ 12 | unionArray.push_back(a[indexA++]); 13 | while(indexA < a.size() && a[indexA] == unionArray[unionArray.size()-1]) indexA++; 14 | } 15 | 16 | // current element of b is less 17 | else if (a[indexA] > b[indexB]){ 18 | unionArray.push_back(b[indexB++]); 19 | while(indexB < b.size() && b[indexB] == unionArray[unionArray.size()-1]) indexB++; 20 | } 21 | 22 | // current element of a and b are equal 23 | else{ 24 | unionArray.push_back(a[indexA++]); 25 | while(indexA < a.size() && a[indexA] == unionArray[unionArray.size()-1]) indexA++; 26 | while(indexB < b.size() && b[indexB] == unionArray[unionArray.size()-1]) indexB++; 27 | } 28 | } 29 | 30 | // remaining elements in a 31 | while(indexA < a.size()){ 32 | unionArray.push_back(a[indexA++]); 33 | while(indexA < a.size() && a[indexA] == unionArray[unionArray.size()-1]) indexA++; 34 | } 35 | 36 | // remaining elements in b 37 | while(indexB < b.size()){ 38 | unionArray.push_back(b[indexB++]); 39 | while(indexB < b.size() && b[indexB] == unionArray[unionArray.size()-1]) indexB++; 40 | } 41 | 42 | 43 | return unionArray; 44 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./19) Pattern 19.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/double-triangle-pattern/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_19 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | // Top Pattern 14 | for(int i = 0; i> t; 57 | while (t--) { 58 | int n; 59 | cin >> n; 60 | 61 | Solution ob; 62 | ob.printTriangle(n); 63 | } 64 | return 0; 65 | } 66 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./20) Pattern 20.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/double-triangle-pattern-1662287416/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_20 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printTriangle(int n) { 12 | 13 | // Top Pattern 14 | for(int i = 0; i> t; 60 | while (t--) { 61 | int n; 62 | cin >> n; 63 | 64 | Solution ob; 65 | ob.printTriangle(n); 66 | } 67 | return 0; 68 | } 69 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.2 - Build-up Logical Thinking via Patterns./22) Pattern 22.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/square-pattern-1662666141/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=pattern_22 2 | 3 | //{ Driver Code Starts 4 | #include 5 | 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | class Solution { 10 | public: 11 | void printSquare(int n) { 12 | 13 | // Top Pattern 14 | for(int i = 0; i(n-i); j--) 19 | cout << j << " "; 20 | 21 | // Print Constant 22 | for(int k = 0; k<2*(n-i)-2; k++) 23 | cout << j << " "; 24 | 25 | // Print Increase 26 | for(; j<=n; j++) 27 | cout << j << " "; 28 | 29 | cout << endl; 30 | } 31 | 32 | // Bottom Pattern 33 | for(int i = n-2; i>=0; i--){ 34 | 35 | int j = n; 36 | // Print Decrease 37 | for(; j>(n-i); j--) 38 | cout << j << " "; 39 | 40 | // Print Constant 41 | for(int k = 0; k<2*(n-i)-2; k++) 42 | cout << j << " "; 43 | 44 | // Print Increase 45 | for(; j<=n; j++) 46 | cout << j << " "; 47 | 48 | cout << endl; 49 | } 50 | 51 | } 52 | }; 53 | 54 | //{ Driver Code Starts. 55 | 56 | int main() { 57 | int t; 58 | cin >> t; 59 | while (t--) { 60 | int n; 61 | cin >> n; 62 | 63 | Solution ob; 64 | ob.printSquare(n); 65 | } 66 | return 0; 67 | } 68 | // } Driver Code Ends -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.4 - Medium Problems of DLL/3) Remove duplicates from a sorted Doubly Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/remove-duplicates-from-a-sorted-doubly-linked-list_2420283?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of doubly linked list: 5 | * class Node { 6 | * public: 7 | * int data; 8 | * Node *prev; 9 | * Node *next; 10 | * Node() { 11 | * this->data = 0; 12 | * this->prev = NULL; 13 | * this->next = NULL; 14 | * } 15 | * Node(int data) { 16 | * this->data = data; 17 | * this->prev = NULL; 18 | * this->next = NULL; 19 | * } 20 | * Node (int data, Node *next, Node *prev) { 21 | * this->data = data; 22 | * this->prev = prev; 23 | * this->next = next; 24 | * } 25 | * }; 26 | * 27 | *************************************************************************/ 28 | 29 | Node * removeDuplicates(Node *head) 30 | { 31 | if(head->next == NULL) return head; 32 | 33 | Node *prev = head, *curr = head->next, *forward = curr->next; 34 | 35 | while(forward != NULL){ 36 | if(curr->data == prev->data){ 37 | prev->next = forward; 38 | forward->prev = prev; 39 | 40 | curr->next = NULL; 41 | curr->prev = NULL; 42 | 43 | curr = forward; 44 | forward = curr->next; 45 | } 46 | else{ 47 | prev = curr; 48 | curr = forward; 49 | forward = forward->next; 50 | } 51 | } 52 | if(curr->data == prev->data){ 53 | prev->next = NULL; 54 | curr->next = NULL; 55 | curr->prev = NULL; 56 | } 57 | 58 | return head; 59 | } 60 | -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/13) Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/-intersection-of-two-linked-lists_630457?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /**************************************************************** 4 | 5 | Following is the class structure of the Node class: 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | Node() 13 | { 14 | this->data = 0; 15 | next = NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | } 22 | Node(int data, Node* next) 23 | { 24 | this->data = data; 25 | this->next = next; 26 | } 27 | }; 28 | 29 | *****************************************************************/ 30 | 31 | 32 | int findLength(Node *head){ 33 | int length = 0; 34 | Node *temp = head; 35 | 36 | while(temp != NULL){ 37 | length++; 38 | temp = temp->next; 39 | } 40 | 41 | return length; 42 | } 43 | 44 | Node* findIntersection(Node *firstHead, Node *secondHead) 45 | { 46 | int len1 = findLength(firstHead); 47 | int len2 = findLength(secondHead); 48 | 49 | if(len1 < len2) return findIntersection(secondHead, firstHead); 50 | 51 | int diff = len1-len2; 52 | 53 | Node *firstItr = firstHead; 54 | Node *secondItr = secondHead; 55 | 56 | while(diff--) 57 | firstItr = firstItr->next; 58 | 59 | while(firstItr != NULL){ 60 | if(firstItr == secondItr) return firstItr; 61 | firstItr = firstItr->next; 62 | secondItr = secondItr->next; 63 | } 64 | 65 | return NULL; 66 | } 67 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/5) Queue in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> A queue is a linear list of elements in which deletions can take place only at one end called the front, and insertions can take place only at the end called the rear. 8 | -> The queue is a First In First Out type of data structure (FIFO), the terms FRONT and REAR are used in describing a linear list only when it is implemented as a queue. 9 | 10 | Syntax: queue variable_name; 11 | 12 | -> Most used functions : 13 | 14 | 1) push() – to insert an element in the queue. 15 | 16 | queue q 17 | q.push(110); 18 | q.push(220); 19 | 20 | 21 | 2) pop() – deletes the first element of the queue. 22 | 23 | q.pop(); 24 | 25 | 26 | 3) front() – returns a reference to the first element of the queue. 27 | 28 | q.front(); 29 | 30 | 31 | 4) back() – returns a reference to the last element of the queue. 32 | 33 | q.back(); 34 | 35 | 36 | 5) emplace() – to insert an element in the queue. 37 | 38 | queue q; 39 | q.emplace(1); 40 | q.emplace(2); 41 | 42 | 43 | 6) size() – returns the number of elements on the queue. 44 | 45 | q.size(); 46 | 47 | 48 | 7) empty() – to check if the queue is empty or not. 49 | 50 | q.empty(); 51 | 52 | */ 53 | 54 | void printqueue(queue q1) 55 | { 56 | queue q2=q1; 57 | while(!q2.empty()) 58 | { 59 | cout< q; 68 | for(int i=1;i<=5;i++) 69 | q.push(i); 70 | 71 | cout<<"The elements of the queue are:"< Medium -> Hard]/Step 3.3 - Hard/4) Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/majority-element_6915220?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=1 2 | 3 | // Optimal Approach (Extended Boyer Moore’s Voting Algorithm): 4 | vector majorityElement(vector v) { 5 | int n = v.size(); //size of the array 6 | 7 | int cnt1 = 0, cnt2 = 0; // counts 8 | int el1 = INT_MIN; // element 1 9 | int el2 = INT_MIN; // element 2 10 | 11 | // applying the Extended Boyer Moore's Voting Algorithm: 12 | for (int i = 0; i < n; i++) { 13 | if (cnt1 == 0 && el2 != v[i]) { 14 | cnt1 = 1; 15 | el1 = v[i]; 16 | } 17 | else if (cnt2 == 0 && el1 != v[i]) { 18 | cnt2 = 1; 19 | el2 = v[i]; 20 | } 21 | else if (v[i] == el1) cnt1++; 22 | else if (v[i] == el2) cnt2++; 23 | else { 24 | cnt1--, cnt2--; 25 | } 26 | } 27 | 28 | vector ls; // list of answers 29 | 30 | // Manually check if the stored elements in 31 | // el1 and el2 are the majority elements: 32 | cnt1 = 0, cnt2 = 0; 33 | for (int i = 0; i < n; i++) { 34 | if (v[i] == el1) cnt1++; 35 | if (v[i] == el2) cnt2++; 36 | } 37 | 38 | int mini = int(n / 3) + 1; 39 | if (cnt1 >= mini) ls.push_back(el1); 40 | if (cnt2 >= mini) ls.push_back(el2); 41 | 42 | // Uncomment the following line 43 | // if it is told to sort the answer array: 44 | // sort(ls.begin(), ls.end()); //TC --> O(2*log2) ~ O(1); 45 | 46 | return ls; 47 | } 48 | 49 | 50 | // My Approach : 51 | 52 | #include 53 | 54 | vector majorityElement(vector v) { 55 | map mp; 56 | for(auto ele : v) 57 | mp[ele]++; 58 | 59 | vector ans; 60 | for(auto it : mp) 61 | if(it.second > v.size()/3) ans.push_back(it.first); 62 | 63 | return ans; 64 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/14) Add one to a number represented as Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/add-one-to-a-number-represented-as-linked-list_920557?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /** 4 | * Definition of linked list: 5 | * 6 | * class Node { 7 | * public: 8 | * int data; 9 | * Node *next; 10 | * Node() { 11 | * this->data = 0; 12 | * this->next = NULL; 13 | * } 14 | * Node(int data) { 15 | * this->data = data; 16 | * this->next = NULL; 17 | * } 18 | * Node (int data, Node *next) { 19 | * this->data = data; 20 | * this->next = next; 21 | * } 22 | * }; 23 | * 24 | *************************************************************************/ 25 | 26 | Node * reverse(Node *head){ 27 | if(head == NULL || head->next == NULL) return head; 28 | 29 | Node *prev = NULL; 30 | Node *curr = head; 31 | Node *forward = head->next; 32 | 33 | while(forward != NULL){ 34 | curr->next = prev; 35 | prev = curr; 36 | curr = forward; 37 | forward = forward->next; 38 | } 39 | curr->next = prev; 40 | 41 | return curr; 42 | } 43 | 44 | Node *addOne(Node *head) 45 | { 46 | Node *reverseHead = reverse(head); 47 | 48 | Node* reverseItr = reverseHead; 49 | Node* reverseItrprev = reverseItr; 50 | 51 | int carry = 1; 52 | 53 | while(reverseItr != NULL){ 54 | reverseItr->data += carry; 55 | carry = reverseItr->data/10; 56 | reverseItr->data = reverseItr->data%10; 57 | 58 | if(carry == 0) 59 | break; 60 | 61 | reverseItrprev = reverseItr; 62 | reverseItr = reverseItr->next; 63 | } 64 | 65 | if(carry) 66 | reverseItrprev->next = new Node(1); 67 | 68 | return reverse(reverseHead); 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/4) Stack in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> A stack is a non-primitive linear data structure. 8 | -> It is an ordered list in which the addition of a new data item and deletion of the already existing data item is done from only one end known as the top of the stack (TOS) 9 | -> The element which is added in last will be first to be removed and the element which is inserted first will be removed in last. 10 | -> As all the deletion and insertion in a stack are done from the top of the stack, the last added element will be the first to be removed from the stack. That is the reason why stack is also called Last-in-First-out (LIFO). 11 | 12 | Syntax: stack variable_name; 13 | 14 | -> Most used functions : 15 | 16 | 1) push() – to insert an element in the stack. 17 | 18 | stack s; 19 | s.push(110); 20 | s.push(220); 21 | 22 | 23 | 2) pop() – deletes the last element of the stack. 24 | 25 | s.pop(); 26 | 27 | 28 | 3) top() – returns the element at the top of the stack. 29 | 30 | s.top(); 31 | 32 | 33 | 4) emplace() – to insert an element in the stack. 34 | 35 | stack s; 36 | s.emplace(1); 37 | s.emplace(2); 38 | 39 | 40 | 5) size() – returns the number of elements on the stack. 41 | 42 | s.size(); 43 | 44 | 45 | 6) empty() – to check if the stack is empty or not. 46 | 47 | s.empty(); 48 | 49 | */ 50 | 51 | void printstack(stack s1) 52 | { 53 | stack s2=s1; 54 | while(!s2.empty()) 55 | { 56 | cout< s; 65 | for(int i=1;i<=5;i++) 66 | s.push(i); 67 | 68 | cout<<"The elements of the stack are:"<data = 0; 12 | * this->next = NULL; 13 | * } 14 | * Node(int data) { 15 | * this->data = data; 16 | * this->next = NULL; 17 | * } 18 | * Node (int data, Node *next) { 19 | * this->data = data; 20 | * this->next = next; 21 | * } 22 | * }; 23 | * 24 | *************************************************************************/ 25 | 26 | Node *addTwoNumbers(Node *num1, Node *num2) 27 | { 28 | Node *ans = new Node(); 29 | Node *ansItr = ans; 30 | Node *num1Itr = num1; 31 | Node *num2Itr = num2; 32 | 33 | int sum = 0, carry = 0; 34 | 35 | while(num1Itr != NULL && num2Itr != NULL){ 36 | sum = num1Itr->data + num2Itr->data + carry; 37 | carry = sum/10; 38 | sum = sum%10; 39 | 40 | ansItr->next = new Node(sum); 41 | 42 | ansItr = ansItr->next; 43 | num1Itr = num1Itr->next; 44 | num2Itr = num2Itr->next; 45 | } 46 | 47 | while(num1Itr != NULL){ 48 | sum = num1Itr->data + carry; 49 | carry = sum/10; 50 | sum = sum%10; 51 | 52 | ansItr->next = new Node(sum); 53 | 54 | ansItr = ansItr->next; 55 | num1Itr = num1Itr->next; 56 | } 57 | 58 | while(num2Itr != NULL){ 59 | sum = num2Itr->data + carry; 60 | carry = sum/10; 61 | sum = sum%10; 62 | 63 | ansItr->next = new Node(sum); 64 | 65 | ansItr = ansItr->next; 66 | num2Itr = num2Itr->next; 67 | } 68 | 69 | if(carry) 70 | ansItr->next = new Node(1); 71 | 72 | return ans->next; 73 | } 74 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/7) Priority Queue in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> In the case of the max heap, priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains and the rest elements are in decreasing order. 8 | -> In the case of the min heap, priority queues are a type of container adaptors, specifically designed such that its first element is always the smallest of the elements it contains and the rest elements are in increasing order. 9 | 10 | Syntax for a max-heap priority queue : priority_queue variable_name; 11 | Syntax for a min-heap priority queue : priority_queue,greater> variable_name; 12 | 13 | -> Most used functions : 14 | 15 | 1) push() – to insert an element in the priority queue. 16 | 17 | pq.push(110); 18 | pq.push(220); 19 | 20 | 21 | 2) pop() – deletes the top element of the priority queue. 22 | 23 | pq.pop(); 24 | 25 | 26 | 3) top() – returns the element at the top of the priority queue. 27 | 28 | pq.top(); 29 | 30 | 31 | 4) emplace() – to insert an element in the priority. 32 | 33 | pq.emplace(1); 34 | pq.emplace(2); 35 | 36 | 37 | 5) size() – returns the number of elements in the priority queue. 38 | 39 | pq.size(); 40 | 41 | 42 | 6) empty() – to check if the priority queue is empty or not. 43 | 44 | pq.empty(); 45 | 46 | */ 47 | 48 | void printpriorityqueue(priority_queue pq) 49 | { 50 | priority_queue pq2=pq; 51 | while(!pq.empty()) 52 | { 53 | cout< pq; 62 | for(int i=1;i<=5;i++) 63 | pq.push(i); 64 | 65 | cout<<"The elements of the priority queue are:"<data = 0; 13 | * this->prev = NULL; 14 | * this->next = NULL; 15 | * } 16 | * Node(int data) { 17 | * this->data = data; 18 | * this->prev = NULL; 19 | * this->next = NULL; 20 | * } 21 | * Node (int data, Node *next, Node *prev) { 22 | * this->data = data; 23 | * this->prev = prev; 24 | * this->next = next; 25 | * } 26 | * }; 27 | * 28 | *************************************************************************/ 29 | 30 | Node * deleteAllOccurrences(Node* head, int k) { 31 | 32 | if(head == NULL || (head->next == NULL && head->data == k)) return NULL; 33 | 34 | Node *prev = NULL, *curr = head, *forward = head->next; 35 | 36 | while(forward != NULL){ 37 | if(curr->data == k){ 38 | if(prev != NULL) 39 | prev->next = forward; 40 | forward->prev = prev; 41 | 42 | if(curr == head) 43 | head = forward; 44 | 45 | curr->prev = NULL; 46 | curr->next = NULL; 47 | 48 | curr = forward; 49 | forward = forward->next; 50 | } 51 | else{ 52 | prev = curr; 53 | curr = forward; 54 | forward = forward->next; 55 | } 56 | } 57 | if(curr->data == k){ 58 | if(prev != NULL) 59 | prev->next = forward; 60 | 61 | if(curr == head) 62 | head = forward; 63 | 64 | curr->prev = NULL; 65 | curr->next = NULL; 66 | } 67 | 68 | return head; 69 | } 70 | -------------------------------------------------------------------------------- /Step 3 - Solve Problems on Arrays [Easy -> Medium -> Hard]/Step 3.3 - Hard/10) Merge Two Sorted Arrays Without Extra Space.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/merge-two-sorted-arrays-without-extra-space_6898839?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | #include 4 | 5 | void mergeTwoSortedArraysWithoutExtraSpace(vector &a, vector &b){ 6 | long long maxValue = 1e9+1; 7 | 8 | int ansIndexA = 0, ansIndexB = 0; 9 | 10 | int indexA = 0, indexB = 0; 11 | 12 | while(indexA < a.size() && indexB < b.size()){ 13 | long long valueA = a[indexA] > maxValue? a[indexA]/maxValue : a[indexA]; 14 | long long valueB = b[indexB] > maxValue? b[indexB]/maxValue : b[indexB]; 15 | 16 | if(valueA < valueB){ 17 | 18 | if(ansIndexA < a.size()){ 19 | a[ansIndexA] = a[ansIndexA]*maxValue + valueA; 20 | ansIndexA++; 21 | } 22 | else{ 23 | b[ansIndexB] = b[ansIndexB]*maxValue + valueA; 24 | ansIndexB++; 25 | } 26 | 27 | indexA++; 28 | } 29 | else{ 30 | 31 | if(ansIndexA < a.size()){ 32 | a[ansIndexA] = a[ansIndexA]*maxValue + valueB; 33 | ansIndexA++; 34 | } 35 | else{ 36 | b[ansIndexB] = b[ansIndexB]*maxValue + valueB; 37 | ansIndexB++; 38 | } 39 | 40 | indexB++; 41 | } 42 | } 43 | 44 | while(indexA < a.size()){ 45 | long long valueA = a[indexA] > maxValue? a[indexA]/maxValue : a[indexA]; 46 | if(ansIndexA < a.size()){ 47 | a[ansIndexA] = a[ansIndexA]*maxValue + valueA; 48 | ansIndexA++; 49 | } 50 | else{ 51 | b[ansIndexB] = b[ansIndexB]*maxValue + valueA; 52 | ansIndexB++; 53 | } 54 | 55 | indexA++; 56 | } 57 | 58 | while(indexB < b.size()){ 59 | long long valueB = b[indexB] > maxValue? b[indexB]/maxValue : b[indexB]; 60 | if(ansIndexA < a.size()){ 61 | a[ansIndexA] = a[ansIndexA]*maxValue + valueB; 62 | ansIndexA++; 63 | } 64 | else{ 65 | b[ansIndexB] = b[ansIndexB]*maxValue + valueB; 66 | ansIndexB++; 67 | } 68 | 69 | indexB++; 70 | } 71 | 72 | 73 | for(int i = 0; i 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> Double Ended Queue which is also called Deque is a type of queue data structure in which the insertion and deletion of elements can be either in front or rear. 8 | 9 | Syntax: deque variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) push_back() – to insert an element at the end of the deque. 14 | 15 | deque dq; 16 | dq.push_back(110); 17 | dq.push_back(220); 18 | 19 | 20 | 2) push_front() – to insert an element at the front of the deque. 21 | 22 | deque dq; 23 | dq.push_front(110); 24 | dq.push_front(220); 25 | 26 | 27 | 3) pop_back() – deletes the last element of the deque. 28 | 29 | dq.pop_back(); 30 | 31 | 32 | 4) pop_front() – deletes the front element of the deque. 33 | 34 | dq.pop_front(); 35 | 36 | 37 | 5) front() – it gives a reference to the first element of the deque. 38 | 39 | dq.front(); 40 | 41 | 42 | 6) back() – it gives a reference to the last element of the deque. 43 | 44 | dq.back(); 45 | 46 | 47 | 7) size() – returns the number of elements on the deque. 48 | 49 | dq.size(); 50 | 51 | 52 | 8) empty() – to check if the deque is empty or not. 53 | 54 | dq.empty(); 55 | 56 | */ 57 | 58 | void printdeque(deque dq) 59 | { 60 | deque::iterator it; 61 | for(it=dq.begin();it!=dq.end();it++) 62 | { 63 | cout<<*it<<" "; 64 | } 65 | cout< dq; 77 | dq.push_back(10); 78 | dq.push_back(20); 79 | dq.push_front(30); 80 | dq.push_front(40); 81 | dq.push_front(50); 82 | 83 | cout<<"The elements in the deque are: "; 84 | printdeque(dq); 85 | 86 | cout<<"The size of the deque is: "< 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> Map in STL are associative containers where each element consists of a key value and a mapped value. Two mapped values cannot have the same key value. 8 | 9 | Syntax: map variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the map. 14 | 15 | map mp; 16 | mp.insert({1,10}); 17 | mp.insert({2,20}); 18 | 19 | 2) begin() – return an iterator pointing to the first element in the map. 20 | 21 | mp.begin(); 22 | 23 | 24 | 3) end() – returns an iterator to the theoretical element after the last element. 25 | 26 | mp.end(); 27 | 28 | 29 | 4) clear() – deletes all the elements in the map. 30 | 31 | mp.clear(); 32 | 33 | 34 | 5) find() – to search for an element in the map. 35 | 36 | map mp; 37 | mp.insert({1,10}); 38 | mp.insert({2,20}); 39 | if(mp.find(2)!=mp.end()) 40 | cout<<"true"< mp; 64 | for (int i = 1; i <= 5; i++) { 65 | mp.insert({i , i * 10}); 66 | } 67 | 68 | cout << "Elements present in the map: " << endl; 69 | cout << "Key\tElement" << endl; 70 | for (auto it = mp.begin(); it != mp.end(); it++) { 71 | cout << it -> first << "\t" << it -> second << endl; 72 | } 73 | 74 | for (auto it : mp) { 75 | cout << it.first << "\t" << it.second << endl; 76 | } 77 | 78 | int n = 2; 79 | if (mp.find(2) != mp.end()) 80 | cout << n << " is present in map" << endl; 81 | 82 | mp.erase(mp.begin()); 83 | cout << "Elements after deleting the first element: " << endl; 84 | cout << "Key\tElement" << endl; 85 | for (auto it = mp.begin(); it != mp.end(); it++) { 86 | cout << it -> first << "\t" << it -> second << endl; 87 | } 88 | 89 | cout << "The size of the map is: " << mp.size() << endl; 90 | 91 | if (mp.empty() == false) 92 | cout << "The map is not empty " << endl; 93 | else 94 | cout << "The map is empty" << endl; 95 | 96 | mp.clear(); 97 | cout << "Size of the map after clearing all the elements: " << mp.size(); 98 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/8) Set in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> A set in STL is a container that stores unique elements in a particular order. Every operation on a set takes O(1) complexity in the average case and takes O(n) in the worst case. 8 | 9 | Syntax: set variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the set. 14 | 15 | set s; 16 | s.insert(1); 17 | s.insert(2); 18 | 19 | 20 | 2) begin() – return an iterator pointing to the first element in the set. 21 | 22 | s.begin(); 23 | 24 | 25 | 3) end() – returns an iterator to the theoretical element after the last element. 26 | 27 | s.end(); 28 | 29 | 30 | 4) count() – returns true or false based on whether the element is present in the set or not. 31 | 32 | set s; 33 | s.insert(1); 34 | s.insert(2); 35 | s.count(2); //returns true 36 | 37 | 38 | 5) clear() – deletes all the elements in the set. 39 | 40 | s.clear(); 41 | 42 | 43 | 6) find() – to search an element in the set. 44 | 45 | set s; 46 | s.insert(1); 47 | s.insert(2); 48 | if(s.find(2)!=s.end()) 49 | cout<<"true"< s; 71 | for (int i = 1; i <= 10; i++) { 72 | s.insert(i); 73 | } 74 | 75 | cout << "Elements present in the set: "; 76 | for (auto it = s.begin(); it != s.end(); it++) { 77 | cout << * it << " "; 78 | } 79 | cout << endl; 80 | 81 | for (auto it : s) 82 | cout << it << " "; 83 | cout << endl; 84 | 85 | int n = 2; 86 | if (s.find(2) != s.end()) 87 | cout << n << " is present in set" << endl; 88 | 89 | s.erase(s.begin()); 90 | cout << "Elements after deleting the first element: "; 91 | for (auto it = s.begin(); it != s.end(); it++) { 92 | cout << * it << " "; 93 | } 94 | cout << endl; 95 | 96 | cout << "The size of the set is: " << s.size() << endl; 97 | 98 | if (s.empty() == false) 99 | cout << "The set is not empty " << endl; 100 | else 101 | cout << "The set is empty" << endl; 102 | 103 | s.clear(); 104 | cout << "Size of the set after clearing all the elements: " << s.size(); 105 | } -------------------------------------------------------------------------------- /Step 6 - Learn LinkedList [Single LL, Double LL, Medium, Hard Problems]/Step 6.3 - Medium Problems of LL/11) Sort Linked List.cpp: -------------------------------------------------------------------------------- 1 | // https://www.codingninjas.com/studio/problems/sort-linked-list_625193?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTab=0 2 | 3 | /* 4 | Following is the class structure of the Node class: 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | Node() 12 | { 13 | this->data = 0; 14 | next = NULL; 15 | } 16 | Node(int data) 17 | { 18 | this->data = data; 19 | this->next = NULL; 20 | } 21 | Node(int data, Node* next) 22 | { 23 | this->data = data; 24 | this->next = next; 25 | } 26 | }; 27 | */ 28 | 29 | Node* reverseLL(Node *head){ 30 | if(head == NULL || head->next == NULL) return head; 31 | 32 | Node *prev = NULL, *curr = head, *forward = head->next; 33 | 34 | while(forward != NULL){ 35 | curr->next = prev; 36 | prev = curr; 37 | curr = forward; 38 | forward = forward->next; 39 | } 40 | 41 | curr->next = prev; 42 | 43 | return curr; 44 | 45 | } 46 | 47 | Node* sortList(Node* head){ 48 | if(head->next == NULL) return head; 49 | 50 | Node *evenHead = new Node(); 51 | Node *oddHead = new Node(); 52 | 53 | Node *evenItr = evenHead; 54 | Node *oddItr = oddHead; 55 | Node *itr = head; 56 | 57 | bool turn = true; 58 | 59 | while(itr != NULL){ 60 | if(turn){ 61 | evenItr->next = itr; 62 | evenItr = evenItr->next; 63 | } 64 | else{ 65 | oddItr->next = itr; 66 | oddItr = oddItr->next; 67 | } 68 | itr = itr->next; 69 | turn = !turn; 70 | } 71 | evenItr->next = NULL; 72 | oddItr->next = NULL; 73 | 74 | evenHead = evenHead->next; 75 | oddHead = reverseLL(oddHead->next); 76 | 77 | Node *sortHead = new Node(); 78 | Node *sortItr = sortHead; 79 | evenItr = evenHead; 80 | oddItr = oddHead; 81 | 82 | while(evenItr != NULL && oddItr != NULL){ 83 | if(evenItr->data < oddItr->data){ 84 | sortItr->next = evenItr; 85 | evenItr = evenItr->next; 86 | } 87 | else{ 88 | sortItr->next = oddItr; 89 | oddItr = oddItr->next; 90 | } 91 | sortItr = sortItr->next; 92 | } 93 | 94 | if(evenItr != NULL) 95 | sortItr->next = evenItr; 96 | else 97 | sortItr->next = oddItr; 98 | 99 | return sortHead->next; 100 | } 101 | -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/10) Multiset in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> A multiset in STL is an associative container just like a set the only difference is it can store duplicate elements in it. 8 | 9 | Syntax: multiset variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the multiset. 14 | 15 | multiset s; 16 | s.insert(1); 17 | s.insert(2); 18 | 19 | 20 | 2) begin() – return an iterator pointing to the first element in the multiset. 21 | 22 | s.begin(); 23 | 24 | 25 | 3) end() – returns an iterator to the theoretical element after the last element. 26 | 27 | s.end(); 28 | 29 | 30 | 4) count() – gives the count of a particular element in the multiset. 31 | 32 | multiset s; 33 | s.insert(1); 34 | s.insert(2); 35 | s.count(2); //returns 1 36 | 37 | 38 | 5) clear() – deletes all the elements in the multiset. 39 | 40 | s.clear(); 41 | 42 | 43 | 6) find() – to search an element in the multiset. 44 | 45 | multiset s; 46 | s.insert(1); 47 | s.insert(2); 48 | if(s.find(2)!=s.end()) 49 | cout<<"true"< s; 71 | for (int i = 1; i <= 10; i++) { 72 | s.insert(i); 73 | } 74 | 75 | s.insert(5); 76 | cout << "Elements present in the multiset: "; 77 | for (auto it = s.begin(); it != s.end(); it++) { 78 | cout << * it << " "; 79 | } 80 | cout << endl; 81 | 82 | for (auto it : s) { 83 | cout << it << " "; 84 | } 85 | cout << endl; 86 | 87 | int n = 2; 88 | if (s.find(2) != s.end()) 89 | cout << n << " is present in multiset" << endl; 90 | 91 | s.erase(s.begin()); 92 | cout << "Elements after deleting the first element: "; 93 | for (auto it = s.begin(); it != s.end(); it++) { 94 | cout << * it << " "; 95 | } 96 | cout << endl; 97 | 98 | cout << "The size of the multiset is: " << s.size() << endl; 99 | 100 | if (s.empty() == false) 101 | cout << "The multiset is not empty " << endl; 102 | else 103 | cout << "The multiset is empty" << endl; 104 | 105 | s.clear(); 106 | cout << "Size of the multiset after clearing all the elements: " << s.size(); 107 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/3) List in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> A list in STL is a contiguous container that allows the inserting and erasing of elements in constant time and iterating in both directions. 8 | -> It is implemented as linear doubly LinkedList in memory. 9 | 10 | Syntax: list variable_name; 11 | 12 | -> Most used functions : 13 | 14 | 1) push_back() – to insert an element at the end of the list. 15 | 16 | list li; 17 | li.push_back(110); 18 | li.push_back(220); 19 | 20 | 21 | 2) push_front() – to insert an element at the front of the list. 22 | 23 | list li; 24 | li.push_front(110); 25 | li.push_front(220); 26 | 27 | 28 | 3) pop_back() – deletes the last element of the list. 29 | 30 | li.pop_back(); 31 | 32 | 33 | 4) pop_front() – deletes the front element of the list. 34 | 35 | li.pop_front(); 36 | 37 | 38 | 5) front() – it gives a reference to the first element of the list. 39 | 40 | li.front(); 41 | 42 | 43 | 6) back() – it gives a reference to the last element of the list. 44 | 45 | li.back(); 46 | 47 | 48 | 7) reverse() – reverse the list. 49 | 50 | li.reverse(); 51 | 52 | 53 | 8) sort() – sorts the list in ascending order. 54 | 55 | li.sort(); 56 | 57 | 58 | 9) size() – returns the number of elements on the list. 59 | 60 | li.size(); 61 | 62 | 63 | 10) empty() – to check if the list is empty or not. 64 | 65 | li.empty(); 66 | 67 | */ 68 | 69 | void printlist(list li) 70 | { 71 | list::iterator it; 72 | for(it=li.begin();it!=li.end();it++) 73 | { 74 | cout<<*it<<" "; 75 | } 76 | cout< li; 87 | li.push_back(10); 88 | li.push_back(20); 89 | li.push_front(30); 90 | li.push_front(40); 91 | li.push_front(50); 92 | 93 | cout<<"The elements in the list are: "; 94 | printlist(li); 95 | 96 | cout<<"Reversing the list: "; 97 | li.reverse(); 98 | printlist(li); 99 | 100 | cout<<"Sorting the list: "; 101 | li.sort(); 102 | printlist(li); 103 | 104 | cout<<"The size of the list is: "< 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> Unordered_map in STL are associative containers where each element consists of a key value and a mapped value. 8 | -> Two mapped values cannot have the same key value. The elements can be in any order. 9 | 10 | Syntax: unordered_map variable_name; 11 | 12 | -> Most used functions : 13 | 14 | 1) insert() – to insert an element in the map. 15 | 16 | unordered_map mp; 17 | mp.insert({1,10}); 18 | mp.insert({2,20}); 19 | 20 | 21 | 2) begin() – return an iterator pointing to the first element in the map. 22 | 23 | mp.begin(); 24 | 25 | 26 | 3) end() – returns an iterator to the theoretical element after the last element. 27 | 28 | mp.end(); 29 | 30 | 31 | 4) clear() – deletes all the elements in the map. 32 | 33 | mp.clear(); 34 | 35 | 36 | 5) find() – to search for an element in the map. 37 | 38 | unordered_map mp; 39 | mp.insert({1,10}); 40 | mp.insert({2,20}); 41 | if(mp.find(2)!=mp.end()) 42 | cout<<"true"< mp; 66 | for (int i = 1; i <= 5; i++) { 67 | mp.insert({ i , i * 10}); 68 | } 69 | 70 | cout << "Elements present in the map: " << endl; 71 | cout << "Key\tElement" << endl; 72 | for (auto it = mp.begin(); it != mp.end(); it++) { 73 | cout << it -> first << "\t" << it -> second << endl; 74 | } 75 | 76 | for (auto it : mp) { 77 | cout << it.first << "\t" << it.second << endl; 78 | } 79 | 80 | int n = 2; 81 | if (mp.find(2) != mp.end()) 82 | cout << n << " is present in map" << endl; 83 | 84 | mp.erase(mp.begin()); 85 | cout << "Elements after deleting the first element: " << endl; 86 | cout << "Key\tElement" << endl; 87 | for (auto it = mp.begin(); it != mp.end(); it++) { 88 | cout << it -> first << "\t" << it -> second << endl; 89 | } 90 | 91 | cout << "The size of the map is: " << mp.size() << endl; 92 | 93 | if (mp.empty() == false) 94 | cout << "The map is not empty " << endl; 95 | else 96 | cout << "The map is empty" << endl; 97 | 98 | mp.clear(); 99 | cout << "Size of the set after clearing all the elements: " << mp.size(); 100 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/9) Unordered-set in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> An unordered set in STL is a container that stores unique elements in no particular order. Every operation on an unordered set takes O(1) complexity in the average case and takes O(n) in the worst case. 8 | 9 | Syntax: unordered_set variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the unordered set. 14 | 15 | unordered_set s; 16 | s.insert(1); 17 | s.insert(2); 18 | 19 | 20 | 2) begin() – return an iterator pointing to the first element in the unordered set. 21 | 22 | s.begin(); 23 | 24 | 25 | 3) end() – returns an iterator to the theoretical element after the last element. 26 | 27 | s.end(); 28 | 29 | 30 | 4) count() – it returns 1 if the element is present in the container otherwise 0. 31 | 32 | unordered_set s; 33 | s.insert(1); 34 | s.insert(2); 35 | s.count(2); //returns true 36 | 37 | 38 | 5) clear() – deletes all the elements in unordered set. 39 | 40 | s.clear(); 41 | 42 | 43 | 6) find() – to search an element in the unordered set. 44 | 45 | unordered_set s; 46 | s.insert(1); 47 | s.insert(2); 48 | if(s.find(2)!=s.end()) 49 | cout<<"true"< s; 71 | for (int i = 1; i <= 10; i++) { 72 | s.insert(i); 73 | } 74 | 75 | cout << "Elements present in the unordered set: "; 76 | for (auto it = s.begin(); it != s.end(); it++) { 77 | cout << * it << " "; 78 | } 79 | cout << endl; 80 | 81 | for (auto it : s) { 82 | cout << it << " "; 83 | } 84 | cout << endl; 85 | 86 | 87 | int n = 2; 88 | if (s.find(2) != s.end()) 89 | cout << n << " is present in unordered set" << endl; 90 | 91 | s.erase(s.begin()); 92 | cout << "Elements after deleting the first element: "; 93 | for (auto it = s.begin(); it != s.end(); it++) { 94 | cout << * it << " "; 95 | } 96 | cout << endl; 97 | 98 | cout << "The size of the unordered set is: " << s.size() << endl; 99 | 100 | if (s.empty() == false) 101 | cout << "The unordered set is not empty " << endl; 102 | else 103 | cout << "The unordered set is empty" << endl; 104 | 105 | s.clear(); 106 | cout << "Size of the unordered set after clearing all the elements: " << s.size(); 107 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/14) Multimap in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> Multimap in STL are associative containers like maps where each element consists of a key value and a mapped value, the only difference is multimaps can store duplicate elements (same key) 8 | 9 | Syntax: multimap variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the multimap. 14 | 15 | multimap mp; 16 | mp.insert({1,10}); 17 | mp.insert({2,20}); 18 | 19 | 20 | 2) begin() – return an iterator pointing to the first element in the multimap. 21 | 22 | mp.begin(); 23 | 24 | 25 | 3) end() – returns an iterator to the theoretical element after the last element. 26 | 27 | mp.end(); 28 | 29 | 30 | 4) clear() – deletes all the elements in the multimap. 31 | 32 | mp.clear(); 33 | 34 | 35 | 5) find() – to search for an element in the map. 36 | 37 | multimap mp; 38 | mp.insert({1,10}); 39 | mp.insert({2,20}); 40 | if(mp.find(2)!=mp.end()) 41 | cout<<"true"< mp; 65 | for (int i = 1; i <= 5; i++) { 66 | mp.insert({i , i * 10}); 67 | } 68 | mp.insert({4,40}); 69 | 70 | cout << "Elements present in the multimap: " << endl; 71 | cout << "Key\tElement" << endl; 72 | for (auto it = mp.begin(); it != mp.end(); it++) { 73 | cout << it -> first << "\t" << it -> second << endl; 74 | } 75 | 76 | for (auto it : mp) { 77 | cout << it.first << "\t" << it.second << endl; 78 | } 79 | 80 | int n = 2; 81 | if (mp.find(2) != mp.end()) 82 | cout << n << " is present in multimap" << endl; 83 | 84 | mp.erase(mp.begin()); 85 | cout << "Elements after deleting the first element: " << endl; 86 | cout << "Key\tElement" << endl; 87 | for (auto it = mp.begin(); it != mp.end(); it++) { 88 | cout << it -> first << "\t" << it -> second << endl; 89 | } 90 | 91 | cout << "The size of the multimap is: " << mp.size() << endl; 92 | 93 | if (mp.empty() == false) 94 | cout << "The multimap is not empty " << endl; 95 | else 96 | cout << "The multimap is empty" << endl; 97 | 98 | mp.clear(); 99 | cout << "Size of the multimap after clearing all the elements: " << mp.size(); 100 | } -------------------------------------------------------------------------------- /Step 1 - Learn the basics/Step 1.3 - Learn STL/11) Unordered-multiset in C++ STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | /* 6 | 7 | -> An unordered_multiset in STL is an associative container just like an unordered set the only difference is it can store duplicate elements in it. 8 | 9 | Syntax: unordered_multiset variable_name; 10 | 11 | -> Most used functions : 12 | 13 | 1) insert() – to insert an element in the unordered multiset. 14 | 15 | unordered_multiset s; 16 | s.insert(1); 17 | s.insert(2); 18 | 19 | 20 | 2) begin() – return an iterator pointing to the first element in the unordered multiset. 21 | 22 | s.begin(); 23 | 24 | 25 | 3) end() – returns an iterator to the theoretical element after the last element. 26 | 27 | s.end(); 28 | 29 | 30 | 4) count() – gives the count of a particular element in the unordered multiset. 31 | 32 | unordered_multiset s; 33 | s.insert(1); 34 | s.insert(2); 35 | s.count(2); //returns 1 36 | 37 | 38 | 5) clear() – deletes all the elements in the unordered multiset. 39 | 40 | s.clear(); 41 | 42 | 43 | 6) find() – to search an element in the unordered multiset. 44 | 45 | unordered_multiset s; 46 | s.insert(1); 47 | s.insert(2); 48 | if(s.find(2)!=s.end()) 49 | cout<<"true"< s; 71 | for (int i = 1; i <= 10; i++) { 72 | s.insert(i); 73 | } 74 | 75 | s.insert(5); 76 | cout << "Elements present in the unordered multiset: "; 77 | for (auto it = s.begin(); it != s.end(); it++) { 78 | cout << * it << " "; 79 | } 80 | cout << endl; 81 | 82 | for (auto it : s) { 83 | cout << it << " "; 84 | } 85 | cout << endl; 86 | 87 | int n = 2; 88 | if (s.find(2) != s.end()) 89 | cout << n << " is present in unorderd multiset" << endl; 90 | 91 | s.erase(s.begin()); 92 | cout << "Elements after deleting the first element: "; 93 | for (auto it = s.begin(); it != s.end(); it++) { 94 | cout << * it << " "; 95 | } 96 | cout << endl; 97 | 98 | cout << "The size of the unordered multiset is: " << s.size() << endl; 99 | 100 | if (s.empty() == false) 101 | cout << "The unordered multiset is not empty " << endl; 102 | else 103 | cout << "The unordered multiset is empty" << endl; 104 | 105 | s.clear(); 106 | cout<<"Size of the unordered multiset after clearing all the elements: "<