├── README.md ├── Lecture 3: Recursion 1 ├── Sum of digits (recursive).cpp ├── Number of Digits.cpp ├── Print Numbers.cpp ├── Geometric Sum.cpp ├── Multiplication (Recursive).cpp ├── Power.cpp ├── Sum of Array.cpp ├── Count Zeros.cpp ├── Check Palindrome (recursive).cpp ├── Check Number.cpp ├── All Indices of Number.cpp ├── First Index of Number.cpp └── Last Index of Number.cpp ├── Operators and For Loop ├── Binary to decimal.cpp ├── Terms of AP.cpp ├── Square Root (Integral).cpp ├── Reverse of a number.cpp ├── All Prime Numbers.cpp ├── Decimal to Binary.cpp ├── Sum or Product.cpp ├── Nth Fibonacci Number.cpp └── Count Characters.cpp ├── Conditionals and Loops ├── Number Pattern 1.cpp ├── Find power of a number.cpp ├── Check Case.cpp ├── Number Pattern 2.cpp ├── Start Pattern.cpp ├── Sum of Even Numbers till N.cpp ├── Sum of even & odd.cpp ├── Fahrenheit to Celsius Table.cpp └── Total Salary.cpp ├── Lecture 10: Character Arrays and 2D Arrays ├── Replace Character.cpp ├── Trim Spaces.cpp ├── Column Wise Sum.cpp ├── Remove Consecutive Duplicates.cpp ├── Remove character.cpp ├── Highest Occuring Character.cpp ├── Check Palindrome.cpp ├── Reverse Each Word.cpp ├── Reverse Word Wise.cpp ├── Print All Substrings.cpp └── Wave Print.cpp ├── Assignment: Recursion 1b ├── Remove X.cpp ├── Pair Star.cpp ├── String to Integer.cpp ├── Replace pi (recursive).cpp └── Tower of Hanoi.cpp ├── Lecture 8: Arrays ├── Array Sum.cpp ├── Swap Alternate.cpp ├── Find Duplicate.cpp ├── Pair Sum.cpp ├── Find Unique.cpp ├── Arrange Numbers in Array.cpp ├── Triplet Sum.cpp ├── Linear Search.cpp └── Sort 0 1.cpp ├── Test 1 (Milestone 1) ├── Number Star Pattern.cpp ├── Pyramid Number Pattern.cpp └── Second Largest.cpp ├── Patterns 1 ├── Code : Triangle Number Pattern.cpp ├── Code : Reverse Number Pattern.cpp ├── Code : Character Pattern.cpp ├── Code : Alpha Pattern.cpp ├── Code : Triangular Star Pattern.cpp ├── Code : Square Pattern.cpp └── Code : Interesting Alphabets.cpp ├── Patterns 2 ├── Code : Inverted Number Pattern.cpp ├── Code : Mirror Number Pattern.cpp ├── Code : Star Pattern.cpp ├── Code : Triangle of Numbers.cpp └── Code : Diamond of stars.cpp ├── Test 2 (Milestone 2) ├── Print 2D Array.cpp ├── Leaders in array.cpp └── Minimum Length Word.cpp ├── Lecture 4: Recursion 2 ├── Staircase.cpp ├── Replace Character Recursively.cpp ├── Print Subset Sum to K.cpp ├── Remove Duplicates Recursively.cpp ├── Print Subsets of Array.cpp ├── Binary Search (Recursive).cpp ├── Print Keypad Combinations Code.cpp ├── Print Permutations.cpp ├── Print all Codes - String.cpp ├── Check AB.cpp ├── Return Permutations - String.cpp ├── Return all codes - String.cpp ├── Merge Sort Code.cpp ├── Return subset of an array.cpp └── Return subsets sum to K.cpp ├── Lecture 7: Functions ├── Fibonacci Number.cpp └── Fahrenheit to Celsius Table.cpp ├── Lecture 15 : Hashmaps ├── Extract Unique characters.cpp ├── Longest subarray zero sum.cpp ├── Code : Pair Sum to 0.cpp ├── Code : Maximum Frequency Number.cpp └── Pairs with difference K.cpp ├── Lecture 18 : DP - 1 ├── Code : Staircase using Dp.cpp ├── Code : No. of balanced BTs using DP.cpp ├── Code : No. of balanced BTs.cpp ├── Code : Min Steps to 1.cpp └── Code : Minimum Count.cpp ├── Test 1(Milestone 3) ├── Does s contain t.cpp ├── Maximum Profit on App.cpp └── Split Array.cpp ├── Lecture 9: Searching and Sorting ├── Code Binary Search.cpp ├── Check Array Rotation.cpp ├── Code Bubble Sort.cpp ├── Code Insertion Sort.cpp ├── Sort 0 1 2.cpp └── Second Largest in array.cpp ├── Test 3 (Milestone 4) └── Longest Leaf to root path.cpp ├── Lecture 12 : Binary Trees ├── Preorder Binary Tree.cpp ├── Postorder Binary Tree.cpp ├── Code : Mirror.cpp └── Code : Height of Binary Tree.cpp ├── Lecture 14 : Priority Queues ├── Kth largest element.cpp ├── Check Max-Heap.cpp ├── Code : K largest elements.cpp ├── Code : K smallest Elements.cpp └── Merge K sorted arrays.cpp ├── Lecture 19 : DP - 2 ├── Loot Houses.cpp ├── Coin Tower.cpp ├── Longest Increasing Subsequence.cpp ├── Code : Edit Distance.cpp ├── All possible ways.cpp └── Code : Knapsack.cpp ├── Lecture 11 : Trees ├── Code : Find height.cpp ├── Code : Find sum of nodes.cpp ├── Code : PostOrder Traversal.cpp ├── Code : Count leaf nodes.cpp ├── Replace with depth.cpp ├── Code : Max data node.cpp ├── Count nodes.cpp └── Contains x.cpp ├── Lecture 5 : Time and Space Complexity Analysis ├── Find the Unique Element.cpp ├── Duplicate in array.cpp ├── Check Array Rotation.cpp └── Rotate array.cpp ├── Lecture 8 : Linked List 1 ├── Length of LL (recursive).cpp └── Length of LL.cpp ├── Test 2 (Milestone 3) ├── Next Number.cpp └── Delete Alternate Nodes.cpp ├── Lecture 10 : Stacks & Queues └── Reverse Queue.cpp ├── Lecture 13 : BST ├── Code: Construct BST from a Sorted Array.cpp ├── Check if a Binary Tree is BST.cpp ├── Code: Print Elements in Range.cpp └── Code: Search in BST.cpp ├── Lecture 20 : Graphs 1 └── 3 Cycle.cpp └── Lecture 17 : Backtracking └── Subset Sum.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Ninjas-Solutions-CPP 2 | Solutions of the Practice problems, Assignment problems and Test problems in DSA course in C++ of Coding Ninjas 3 | - written by @rajeevrpandey 4 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Sum of digits (recursive).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a recursive function that returns the sum of the digits of a given integer. 3 | Input format : 4 | Integer N 5 | Output format : 6 | Sum of digits of N 7 | Constraints : 8 | 0 <= N <= 10^9 9 | Sample Input 1 : 10 | 12345 11 | Sample Output 1 : 12 | 15 13 | Sample Input 2 : 14 | 9 15 | Sample Output 2 : 16 | 9 17 | */ 18 | 19 | int sumOfDigits(int n) { 20 | if(n==0) 21 | return 0; 22 | else 23 | return (n%10) + sumOfDigits(n/10); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Number of Digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the number 'n', find out and return the number of digits present in a number . 3 | Input Format : 4 | Integer n 5 | Output Format : 6 | Count of digits 7 | Constraints : 8 | 1 <= n <= 10^6 9 | Sample Input 1 : 10 | 156 11 | Sample Output 1 : 12 | 3 13 | Sample Input 2 : 14 | 7 15 | Sample Output 2 : 16 | 1 17 | */ 18 | 19 | int count(int n){ 20 | //write your code here 21 | if(n==0){ 22 | return 0; 23 | } 24 | else 25 | return 1 + count(n/10); 26 | } 27 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Print Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given the number 'n', write a code to print numbers from 1 to n in increasing order recursively. 3 | Input Format : 4 | Integer n 5 | Output Format : 6 | Numbers from 1 to n (separated by space) 7 | Constraints : 8 | 1 <= n <= 10000 9 | Sample Input 1 : 10 | 6 11 | Sample Output 1 : 12 | 1 2 3 4 5 6 13 | Sample Input 2 : 14 | 4 15 | Sample Output 2 : 16 | 1 2 3 4 17 | */ 18 | 19 | void print(int n){ 20 | //write your code here 21 | if(n == 0) 22 | return; 23 | print(n-1); 24 | cout << n << " "; 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Geometric Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given k, find the geometric sum i.e. 3 | 1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k) 4 | using recursion. 5 | Input format : 6 | Integer k 7 | Output format : 8 | Geometric sum (upto 5 decimal places) 9 | Constraints : 10 | 0 <= k <= 1000 11 | Sample Input 1 : 12 | 3 13 | Sample Output 1 : 14 | 1.87500 15 | Sample Input 2 : 16 | 4 17 | Sample Output 2 : 18 | 1.93750 19 | Explanation for Sample Input 1: 20 | 1+ 1/(2^1) + 1/(2^2) + 1/(2^3) = 1.87500 21 | */ 22 | 23 | #include 24 | double geometricSum(int k) { 25 | if(k==0) 26 | return 1; 27 | else 28 | return pow(2, -k) + geometricSum(k-1); 29 | } 30 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Multiplication (Recursive).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction and addition for your calculation. No other operators are allowed. 3 | Input format : 4 | Line 1 : Integer M 5 | Line 2 : Integer N 6 | Output format : 7 | M x N 8 | Constraints : 9 | 0 <= M <= 1000 10 | 0 <= N <= 1000 11 | Sample Input 1 : 12 | 3 13 | 5 14 | Sample Output 1 : 15 | 15 16 | Sample Input 2 : 17 | 4 18 | 0 19 | Sample Output 2 : 20 | 0 21 | */ 22 | 23 | int multiplyNumbers(int m, int n) { 24 | if(m==0 || n==0){ 25 | return 0; 26 | } 27 | else 28 | return n + multiplyNumbers(m-1, n); 29 | } 30 | -------------------------------------------------------------------------------- /Operators and For Loop/Binary to decimal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary number as an integer N, convert it into decimal and print. 3 | Input format : 4 | An integer N in the Binary Format 5 | Output format : 6 | Corresponding Decimal number (as integer) 7 | Constraints : 8 | 0 <= N <= 10^9 9 | Sample Input 1 : 10 | 1100 11 | Sample Output 1 : 12 | 12 13 | Sample Input 2 : 14 | 111 15 | Sample Output 2 : 16 | 7 17 | */ 18 | #include 19 | using namespace std; 20 | 21 | int main() { 22 | // Write your code here 23 | int x,dig,rev=0,pv=1; 24 | cin >> x; 25 | while(x>0){ 26 | dig = x%10; 27 | rev = rev + dig*pv; 28 | x = x/10; 29 | pv*=2; 30 | } 31 | cout << rev; 32 | } 33 | -------------------------------------------------------------------------------- /Operators and For Loop/Terms of AP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print first x terms of the series 3N + 2 which are not multiples of 4. 3 | Input format : 4 | Integer x 5 | Output format : 6 | Terms of series (separated by space) 7 | Constraints : 8 | 1 <= x <= 1,000 9 | Sample Input 1 : 10 | 10 11 | Sample Output 1 : 12 | 5 11 14 17 23 26 29 35 38 41 13 | Sample Input 2 : 14 | 4 15 | Sample Output 2 : 16 | 5 11 14 17 17 | */ 18 | #include 19 | using namespace std; 20 | 21 | int main() { 22 | // Write your code here 23 | int x; 24 | cin >> x; 25 | for(int i=1;i<=x;i++){ 26 | if((3*i+2)%4==0){ 27 | x++; 28 | continue; 29 | } 30 | cout << 3*i+2 << ' '; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Operators and For Loop/Square Root (Integral).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number N, find its square root. You need to find and print only the integral part of square root of N. 3 | For eg. if number given is 18, answer is 4. 4 | Input format : 5 | Integer N 6 | Output Format : 7 | Square root of N (integer part only) 8 | Constraints : 9 | 0 <= N <= 10^8 10 | Sample Input 1 : 11 | 10 12 | Sample Output 1 : 13 | 3 14 | Sample Input 2 : 15 | 4 16 | Sample Output 2 : 17 | 2 18 | */ 19 | #include 20 | using namespace std; 21 | 22 | int main() { 23 | // Write your code here 24 | int n,x,sq; 25 | cin >> n; 26 | for(x=0;x<=n/2;x++){ 27 | sq = x*x; 28 | if(sq>n) 29 | break; 30 | } 31 | cout << x-1 ; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Conditionals and Loops/Number Pattern 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern 3 | Pattern for N = 4 4 | 1 5 | 23 6 | 345 7 | 4567 8 | Input Format : 9 | N (Total no. of rows) 10 | Output Format : 11 | Pattern in N lines 12 | Sample Input 1 : 13 | 3 14 | Sample Output 1 : 15 | 1 16 | 23 17 | 345 18 | */ 19 | #include 20 | using namespace std; 21 | 22 | 23 | int main(){ 24 | 25 | /* Read input as specified in the question. 26 | * Print output as specified in the question. 27 | */int n; 28 | cin >> n; 29 | 30 | int i = 1; 31 | int val = 1; 32 | while (i <= n) { 33 | int j = 1; 34 | val = i; 35 | while (j <= i) { 36 | cout << val; 37 | j = j + 1; 38 | val = val + 1; 39 | } 40 | cout << endl; 41 | i = i + 1;} 42 | 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Replace Character.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an input string S and two characters c1 and c2, you need to replace every occurrence of character c1 with character c2 in the given string. 3 | Input Format : 4 | Line 1 : Input String S 5 | Line 2 : Character c1 and c2 (separated by space) 6 | Output Format : 7 | Updated string 8 | Note : 9 | You don't need to output anything. Just implement the given function. 10 | Constraints : 11 | 1 <= Length of String S <= 10^6 12 | Sample Input : 13 | abacd 14 | a x 15 | Sample Output : 16 | xbxcd 17 | */ 18 | void replaceCharacter(char input[], char c1, char c2) { 19 | // Write your code here 20 | int i = 0; 21 | while (input[i]){ 22 | if(input[i] == c1) 23 | input[i] = c2; 24 | i++; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Conditionals and Loops/Find power of a number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer. 3 | Note : For this question, you can assume that 0 raised to the power of 0 is 1 4 | 5 | 6 | Input format : 7 | Two integers x and n (separated by space) 8 | Output Format : 9 | x^n (i.e. x raise to the power n) 10 | Constraints: 11 | 0 <= x <= 8 12 | 0 <= n <= 9 13 | Sample Input 1 : 14 | 3 4 15 | Sample Output 1 : 16 | 81 17 | Sample Input 2 : 18 | 2 5 19 | Sample Output 2 : 20 | 32 21 | */ 22 | 23 | #include 24 | using namespace std; 25 | 26 | int main() { 27 | // Write your code here 28 | int pow=1,x,n; 29 | cin >> x >> n; 30 | while(n>0){ 31 | pow *= x; 32 | n--; 33 | } 34 | cout << pow; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to return the answer. 3 | Do this recursively. 4 | Input format : 5 | Two integers x and n (separated by space) 6 | Output Format : 7 | x^n (i.e. x raise to the power n) 8 | Constraints : 9 | 0 <= x <= 30 10 | 0 <= n <= 30 11 | Sample Input 1 : 12 | 3 4 13 | Sample Output 1 : 14 | 81 15 | Sample Input 2 : 16 | 2 5 17 | Sample Output 2 : 18 | 32 19 | */ 20 | 21 | int power(int x, int n) { 22 | /* Don't write main(). 23 | Don't read input, it is passed as function argument. 24 | Return output and don't print it. 25 | Taking input and printing output is handled automatically. 26 | */ 27 | if(n == 0){ 28 | return 1; 29 | } 30 | return x * power(x, (n-1)); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Operators and For Loop/Reverse of a number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to generate the reverse of a given number N. Print the corresponding reverse number. 3 | Note : If a number has trailing zeros, then its reverse will not include them. For e.g., reverse of 10400 will be 401 instead of 00401. 4 | 5 | 6 | Input format : 7 | Integer N 8 | Output format : 9 | Corresponding reverse number 10 | Constraints: 11 | 0 <= N < 10^8 12 | Sample Input 1 : 13 | 1234 14 | Sample Output 1 : 15 | 4321 16 | Sample Input 2 : 17 | 1980 18 | Sample Output 2 : 19 | 891 20 | */ 21 | #include 22 | using namespace std; 23 | 24 | int main() { 25 | // Write your code here 26 | int x,dig,rev=0; 27 | cin >> x; 28 | while(x>0){ 29 | dig = x%10; 30 | rev = rev*10+dig; 31 | x = x/10; 32 | } 33 | cout << rev; 34 | } 35 | -------------------------------------------------------------------------------- /Assignment: Recursion 1b/Remove X.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, compute recursively a new string where all 'x' chars have been removed. 3 | Input format : 4 | String S 5 | Output format : 6 | Modified String 7 | Constraints : 8 | 1 <= |S| <= 10^3 9 | where |S| represents the length of string S. 10 | Sample Input 1 : 11 | xaxb 12 | Sample Output 1: 13 | ab 14 | Sample Input 2 : 15 | abc 16 | Sample Output 2: 17 | abc 18 | Sample Input 3 : 19 | xx 20 | Sample Output 3: 21 | 22 | */ 23 | 24 | // Change in the given string itself. So no need to return or print anything 25 | 26 | void removeX(char input[]) { 27 | int len = 0; 28 | while (input[len]) 29 | len++; 30 | if(len < 1) 31 | return; 32 | removeX(input+1); 33 | if(input[0]=='x'){ 34 | for(int i=1; i<=len; i++) 35 | input[i-1]=input[i]; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Array Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of length N, you need to find and print the sum of all elements of the array. 3 | Input Format : 4 | Line 1 : An Integer N i.e. size of array 5 | Line 2 : N integers which are elements of the array, separated by spaces 6 | Output Format : 7 | Sum 8 | Constraints : 9 | 1 <= N <= 10^6 10 | Sample Input : 11 | 3 12 | 9 8 9 13 | Sample Output : 14 | 26 15 | */ 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | int main(){ 21 | /* Read input as specified in the question. 22 | * Print output as specified in the question. 23 | */long int n, sum = 0; 24 | cin >> n; 25 | int a[1000000]; 26 | for(int i = 0;i> a[i]; 28 | } 29 | for(int i = 0;i 17 | using namespace std; 18 | 19 | 20 | int main(){ 21 | 22 | // Write your code here 23 | int n; 24 | cin >> n; 25 | for(int i = 1;i<=n;i++){ 26 | for(int j = 1;j<=n;j++){ 27 | if(j<=n-i+1) 28 | cout << j; 29 | else 30 | cout << '*'; 31 | } 32 | for(int j = n;j>=1;j--){ 33 | if(j<=n-i+1) 34 | cout << j; 35 | else 36 | cout << '*'; 37 | } 38 | cout << endl; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Sum of Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of length N, you need to find and return the sum of all elements of the array. 3 | Do this recursively. 4 | Input Format : 5 | Line 1 : An Integer N i.e. size of array 6 | Line 2 : N integers which are elements of the array, separated by spaces 7 | Output Format : 8 | Sum 9 | Constraints : 10 | 1 <= N <= 10^3 11 | Sample Input 1 : 12 | 3 13 | 9 8 9 14 | Sample Output 1 : 15 | 26 16 | Sample Input 2 : 17 | 3 18 | 4 2 1 19 | Sample Output 2 : 20 | 7 21 | */ 22 | 23 | int sum(int input[], int n) { 24 | /* Don't write main(). 25 | Don't read input, it is passed as function argument. 26 | Return output and don't print it. 27 | Taking input and printing output is handled automatically. 28 | */ 29 | if(n<0) 30 | return 0; 31 | return input[n-1] + sum(input, n-1); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Patterns 1/Code : Triangle Number Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 22 6 | 333 7 | 4444 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Constraints 13 | 0 <= N <= 50 14 | Sample Input 1: 15 | 5 16 | Sample Output 1: 17 | 1 18 | 22 19 | 333 20 | 4444 21 | 55555 22 | Sample Input 2: 23 | 6 24 | Sample Output 2: 25 | 1 26 | 22 27 | 333 28 | 4444 29 | 55555 30 | 666666 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main(){ 37 | 38 | /* Read input as specified in the question. 39 | * Print output as specified in the question. 40 | */int n; 41 | cin >> n; 42 | for(int i = 1; i<=n;i++){ 43 | for(int j = 1; j<=i;j++) 44 | cout << i; 45 | cout << endl; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Patterns 1/Code : Reverse Number Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 21 6 | 321 7 | 4321 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Constraints 13 | 0 <= N <= 50 14 | Sample Input 1: 15 | 5 16 | Sample Output 1: 17 | 1 18 | 21 19 | 321 20 | 4321 21 | 54321 22 | Sample Input 2: 23 | 6 24 | Sample Output 2: 25 | 1 26 | 21 27 | 321 28 | 4321 29 | 54321 30 | 654321 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main(){ 37 | 38 | /* Read input as specified in the question. 39 | * Print output as specified in the question. 40 | */int n; 41 | cin >> n; 42 | for(int i = 1; i<=n;i++){ 43 | for(int j = 1; j<=i;j++) 44 | cout << i-j+1; 45 | cout << endl; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Patterns 1/Code : Character Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | A 5 | BC 6 | CDE 7 | DEFG 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Constraints 13 | 0 <= N <= 13 14 | Sample Input 1: 15 | 5 16 | Sample Output 1: 17 | A 18 | BC 19 | CDE 20 | DEFG 21 | EFGHI 22 | Sample Input 2: 23 | 6 24 | Sample Output 2: 25 | A 26 | BC 27 | CDE 28 | DEFG 29 | EFGHI 30 | FGHIJK 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main(){ 37 | 38 | /* Read input as specified in the question. 39 | * Print output as specified in the question. 40 | */int n; 41 | cin >> n; 42 | for(int i = 1; i<=n;i++){ 43 | for(int j = 1; j<=i;j++) 44 | cout << char(i+j+'A'-2); 45 | cout << endl; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Patterns 2/Code : Inverted Number Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 4444 5 | 333 6 | 22 7 | 1 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Constraints : 13 | 0 <= N <= 50 14 | Sample Input 1: 15 | 5 16 | Sample Output 1: 17 | 55555 18 | 4444 19 | 333 20 | 22 21 | 1 22 | Sample Input 2: 23 | 6 24 | Sample Output 2: 25 | 666666 26 | 55555 27 | 4444 28 | 333 29 | 22 30 | 1 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main(){ 37 | 38 | /* Read input as specified in the question. 39 | * Print output as specified in the question. 40 | */int n; 41 | cin >> n; 42 | for(int i = 1; i<=n;i++){ 43 | for(int k = 1; k<=n-i+1;k++) 44 | cout << n-i+1; 45 | cout << endl; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Test 1 (Milestone 1)/Pyramid Number Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given number of rows. 3 | Pattern for N = 4 4 | 1 5 | 212 6 | 32123 7 | 4321234 8 | Input format : N (Total no. of rows) 9 | 10 | Output format : Pattern in N lines 11 | 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 1 16 | 212 17 | 32123 18 | 4321234 19 | 543212345 20 | */ 21 | #include 22 | using namespace std; 23 | 24 | int main(){ 25 | 26 | // Write your code here 27 | int n; 28 | cin >> n; 29 | for(int i = 1;i<=n;i++){ 30 | for(int j = 1;j<=n-i;j++) 31 | cout << ' '; 32 | for(int k = i;k>=1;k--) 33 | cout << k; 34 | for(int l = 1;l<=i-1;l++){ 35 | if(i<2) 36 | continue; 37 | cout << l+1; 38 | } 39 | cout << endl; 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Conditionals and Loops/Check Case.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes a character as input and prints either 1, 0 or -1 according to the following rules. 3 | 1, if the character is an uppercase alphabet (A - Z) 4 | 0, if the character is a lowercase alphabet (a - z) 5 | -1, if the character is not an alphabet 6 | Input format : 7 | Single Character 8 | Output format : 9 | 1 or 0 or -1 10 | Constraints : 11 | Input can be any character 12 | Sample Input 1 : 13 | v 14 | Sample Output 1 : 15 | 0 16 | Sample Input 2 : 17 | V 18 | Sample Output 2 : 19 | 1 20 | Sample Input 3 : 21 | # 22 | Sample Output 3 : 23 | -1 24 | */ 25 | #include 26 | using namespace std; 27 | 28 | int main() { 29 | char a ; 30 | cin >> a ; 31 | if (a>='A' && a<='Z'){ 32 | cout << 1 ; 33 | }else if (a>='a' && a<='z'){ 34 | cout << 0 ; 35 | }else { 36 | cout << -1 ; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Trim Spaces.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an input string S that contains multiple words, you need to remove all the spaces present in the input string. 3 | There can be multiple spaces present after any word. 4 | Input Format : 5 | String S 6 | Output Format : 7 | Updated string 8 | Constraints : 9 | 1 <= Length of string S <= 10^6 10 | Sample Input : 11 | abc def g hi 12 | Sample Output : 13 | abcdefghi 14 | */ 15 | void trimSpaces(char input[]) { 16 | // Write your code here 17 | int i = 0,j=0; 18 | while(input[i]){ 19 | if(input[i] != ' '){ 20 | // for(j=i;input[j];j++){ 21 | // input[j]=input[j+1]; 22 | // } it didnt work in case of multiple consecutive spaces 23 | input[j]=input[i]; 24 | j++; 25 | } 26 | i++; 27 | 28 | } 29 | input[j]='\0'; 30 | } 31 | -------------------------------------------------------------------------------- /Patterns 1/Code : Alpha Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 3 4 | A 5 | BB 6 | CCC 7 | Input format : 8 | Integer N (Total no. of rows) 9 | Output format : 10 | Pattern in N lines 11 | Constraints 12 | 0 <= N <= 26 13 | Sample Input 1: 14 | 7 15 | Sample Output 1: 16 | A 17 | BB 18 | CCC 19 | DDDD 20 | EEEEE 21 | FFFFFF 22 | GGGGGGG 23 | Sample Input 2: 24 | 6 25 | Sample Output 2: 26 | A 27 | BB 28 | CCC 29 | DDDD 30 | EEEEE 31 | FFFFFF 32 | */ 33 | #include 34 | using namespace std; 35 | 36 | 37 | int main(){ 38 | 39 | /* Read input as specified in the question. 40 | * Print output as specified in the question. 41 | */int n; 42 | cin >> n; 43 | for(int i = 1; i<=n;i++){ 44 | for(int j = 1; j<=i;j++) 45 | cout << char(i+'A'-1); 46 | cout << endl; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Count Zeros.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer N, count and return the number of zeros that are present in the given integer using recursion. 3 | Input Format : 4 | Integer N 5 | Output Format : 6 | Number of zeros in N 7 | Constraints : 8 | 0 <= N <= 10^9 9 | Sample Input 1 : 10 | 0 11 | Sample Output 1 : 12 | 1 13 | Sample Input 2 : 14 | 00010204 15 | Sample Output 2 : 16 | 2 17 | Explanation for Sample Output 2 : 18 | Even though "00010204" has 5 zeros, the output would still be 2 because when you convert it to an integer, it becomes 10204. 19 | Sample Input 3 : 20 | 708000 21 | Sample Output 3 : 22 | 4 23 | */ 24 | 25 | int countZeros(int n) { 26 | if(n>0 && n<=9) 27 | return 0; 28 | else if(n==0) 29 | return 1; 30 | else{ 31 | if(n%10) 32 | return countZeros(n/10); 33 | else 34 | return 1 + countZeros(n/10); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Assignment: Recursion 1b/Pair Star.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*". 3 | Input format : 4 | String S 5 | Output format : 6 | Modified string 7 | Constraints : 8 | 0 <= |S| <= 1000 9 | where |S| represents length of string S. 10 | Sample Input 1 : 11 | hello 12 | Sample Output 1: 13 | hel*lo 14 | Sample Input 2 : 15 | aaaa 16 | Sample Output 2 : 17 | a*a*a*a 18 | */ 19 | 20 | // Change in the given string itself. So no need to return or print the changed string. 21 | 22 | void pairStar(char input[]) { 23 | int len = 0; 24 | while (input[len]) 25 | len++; 26 | if(len <= 1) 27 | return; 28 | pairStar(input+1); 29 | if(input[0]==input[1]){ 30 | for(int i=2*len; i>=1; i--) 31 | input[i+1]=input[i]; 32 | input[1]='*'; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Patterns 2/Code : Mirror Number Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 12 6 | 123 7 | 1234 8 | 9 | Input format : 10 | Integer N (Total no. of rows) 11 | Output format : 12 | Pattern in N lines 13 | Constraints 14 | 0 <= N <= 50 15 | Sample Input 1: 16 | 3 17 | Sample Output 1: 18 | 1 19 | 12 20 | 123 21 | Sample Input 2: 22 | 4 23 | Sample Output 2: 24 | 1 25 | 12 26 | 123 27 | 1234 28 | */ 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main(){ 34 | 35 | /* Read input as specified in the question. 36 | * Print output as specified in the question. 37 | */int n; 38 | cin >> n; 39 | for(int i = 1; i<=n;i++){ 40 | for(int j = 1; j<=n-i;j++) 41 | cout << ' '; 42 | for(int k = 1; k<=i;k++) 43 | cout << k; 44 | cout << endl; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Test 2 (Milestone 2)/Print 2D Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a 2D integer array with n rows and m columns. Print the 0th row from input n times, 1st row n-1 times…..(n-1)th row will be printed 1 time. 3 | Input format : 4 | Line 1 : No of rows (n) and no of columns (m) (separated by single space) 5 | Line 2 : Row 1 elements (separated by space) 6 | Line 3 : Row 2 elements (separated by space) 7 | Line 4 : and so on 8 | Sample Input 1: 9 | 3 3 10 | 1 2 3 11 | 4 5 6 12 | 7 8 9 13 | Sample Output 1 : 14 | 1 2 3 15 | 1 2 3 16 | 1 2 3 17 | 4 5 6 18 | 4 5 6 19 | 7 8 9 20 | */ 21 | 22 | #include 23 | using namespace std; 24 | 25 | void print2DArray(int **input, int row, int col) { 26 | int i,j,k; 27 | for(i=0;i 34 | using namespace std; 35 | 36 | 37 | int main(){ 38 | 39 | /* Read input as specified in the question. 40 | * Print output as specified in the question. 41 | */int n; 42 | cin >> n; 43 | for(int i = 1; i<=n;i++){ 44 | for(int j = 1; j<=i;j++) 45 | cout << '*'; 46 | cout << endl; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Patterns 2/Code : Star Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern 3 | Pattern for N = 4 4 | 5 | * 6 | *** 7 | ***** 8 | ******* 9 | 10 | Input Format : 11 | N (Total no. of rows) 12 | Output Format : 13 | Pattern in N lines 14 | Constraints : 15 | 0 <= N <= 50 16 | Sample Input 1 : 17 | 3 18 | Sample Output 1 : 19 | * 20 | *** 21 | ***** 22 | Sample Input 2 : 23 | 4 24 | Sample Output 2 : 25 | * 26 | *** 27 | ***** 28 | ******* 29 | */ 30 | #include 31 | using namespace std; 32 | 33 | 34 | int main(){ 35 | 36 | /* Read input as specified in the question. 37 | * Print output as specified in the question. 38 | */int n,k=0; 39 | cin >> n; 40 | for(int i = 1; i<=n;i++){ 41 | for(int j = 1; j<=n-i;j++) 42 | cout << ' '; 43 | for(k = 1; k<=2*i-1;k++) 44 | cout << '*'; 45 | 46 | cout << endl; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Patterns 1/Code : Square Pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 4444 5 | 4444 6 | 4444 7 | 4444 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Constraints 13 | 0 <= N <= 50 14 | Sample Input 1: 15 | 7 16 | Sample Output 1: 17 | 7777777 18 | 7777777 19 | 7777777 20 | 7777777 21 | 7777777 22 | 7777777 23 | 7777777 24 | Sample Input 1: 25 | 6 26 | Sample Output 1: 27 | 666666 28 | 666666 29 | 666666 30 | 666666 31 | 666666 32 | 666666 33 | */ 34 | 35 | #include 36 | using namespace std; 37 | 38 | 39 | int main(){ 40 | 41 | /* Read input as specified in the question. 42 | * Print output as specified in the question. 43 | */ 44 | int n,i,j; 45 | cin >> n; 46 | for(i = 1; i<=n;i++){ 47 | for(j = 1; j<=n;j++) 48 | cout << n; 49 | cout << endl; 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Patterns 1/Code : Interesting Alphabets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given number of rows. 3 | Pattern for N = 5 4 | E 5 | DE 6 | CDE 7 | BCDE 8 | ABCDE 9 | Input format : 10 | N (Total no. of rows) 11 | Output format : 12 | Pattern in N lines 13 | Constraints 14 | 0 <= N <= 26 15 | Sample Input 1: 16 | 8 17 | Sample Output 1: 18 | H 19 | GH 20 | FGH 21 | EFGH 22 | DEFGH 23 | CDEFGH 24 | BCDEFGH 25 | ABCDEFGH 26 | Sample Input 2: 27 | 7 28 | Sample Output 2: 29 | G 30 | FG 31 | EFG 32 | DEFG 33 | CDEFG 34 | BCDEFG 35 | ABCDEFG 36 | */ 37 | #include 38 | using namespace std; 39 | 40 | 41 | int main() { 42 | 43 | /* Read input as specified in the question. 44 | * Print output as specified in the question. 45 | */int n; 46 | cin >> n; 47 | for(int i = 1; i<=n;i++){ 48 | for(int j = 1; j<=i;j++) 49 | cout << char(n-i+j+'A'-1); 50 | cout << endl; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Staircase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A child is running up a staircase with N steps, and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run up to the stairs. You need to return number of possible ways W. 3 | Input format : 4 | Integer N 5 | Output Format : 6 | Integer W 7 | Constraints : 8 | 1 <= N <= 30 9 | Sample Input 1 : 10 | 4 11 | Sample Output 1 : 12 | 7 13 | Sample Input 2 : 14 | 5 15 | Sample Output 2 : 16 | 13 17 | */ 18 | 19 | int staircase(int n){ 20 | /* Don't write main(). 21 | * Don't read input, it is passed as function argument. 22 | * Return output and don't print it. 23 | * Taking input and printing output is handled automatically. 24 | */ 25 | if(n==1 || n==0) 26 | return 1; 27 | if(n==2) 28 | return 2; 29 | 30 | int ans=staircase(n-1)+staircase(n-2)+staircase(n-3); 31 | return ans; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/Check Palindrome (recursive).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether a given String S is a palindrome using recursion. Return true or false. 3 | Input Format : 4 | String S 5 | Output Format : 6 | 'true' or 'false' 7 | Constraints : 8 | 0 <= |S| <= 1000 9 | where |S| represents length of string S. 10 | Sample Input 1 : 11 | racecar 12 | Sample Output 1: 13 | true 14 | Sample Input 2 : 15 | ninja 16 | Sample Output 2: 17 | false 18 | */ 19 | 20 | bool checkPalindrome(char input[]) { 21 | int i = 0; 22 | while (input[i]) 23 | i++; 24 | if(i<=1) 25 | return true; 26 | else if(i==2){ 27 | if(input[0]==input[1]) 28 | return true; 29 | else 30 | return false; 31 | } 32 | else{ 33 | if(input[0]==input[i-1]){ 34 | input[i-1] = '\0'; 35 | return true && checkPalindrome(input+1); 36 | } 37 | else 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Conditionals and Loops/Number Pattern 2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern 3 | Pattern for N = 4 4 | 1 5 | 23 6 | 345 7 | 4567 8 | 9 | 10 | 11 | Input Format : 12 | N (Total no. of rows) 13 | Output Format : 14 | Pattern in N lines 15 | Sample Input : 16 | 5 17 | Sample Output : 18 | 19 | 1 20 | 23 21 | 345 22 | 4567 23 | 56789 24 | 25 | */ 26 | #include 27 | using namespace std; 28 | 29 | 30 | int main(){ 31 | 32 | /* Read input as specified in the question. 33 | * Print output as specified in the question. 34 | */int n; 35 | //cout << "Enter n" << endl; 36 | cin >> n; 37 | 38 | int i = 1; 39 | int val = 1; 40 | while (i <= n) { 41 | int k = 1; 42 | while (k <= n - i) { 43 | cout << " "; 44 | k = k + 1; 45 | } 46 | val = i; 47 | int j = 1; 48 | while (j <= i) { 49 | cout << val; 50 | j = j + 1; 51 | val = val + 1; 52 | } 53 | cout << endl; 54 | i = i + 1; 55 | } 56 | 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Assignment: Recursion 1b/String to Integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a recursive function to convert a given string into the number it represents. That is input will be a numeric string that contains only numbers, you need to convert the string into corresponding integer and return the answer. 3 | Input format : 4 | Numeric string S (string, Eg. "1234") 5 | Output format : 6 | Corresponding integer N (int, Eg. 1234) 7 | Constraints : 8 | 0 < |S| <= 9 9 | where |S| represents length of string S. 10 | Sample Input 1 : 11 | 00001231 12 | Sample Output 1 : 13 | 1231 14 | Sample Input 2 : 15 | 12567 16 | Sample Output 2 : 17 | 12567 18 | */ 19 | 20 | #include 21 | int stringToNumber(char input[]) { 22 | int len = 0; 23 | while (input[len]) 24 | len++; 25 | if(len < 1) 26 | return 0; 27 | else if(len == 1){ 28 | return input[0] - 48; 29 | } 30 | int ans = stringToNumber(input+1); 31 | return (input[0] - 48) * pow(10, len-1) + ans; 32 | } 33 | -------------------------------------------------------------------------------- /Assignment: Recursion 1b/Replace pi (recursive).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14". 3 | Constraints : 4 | 1 <= |S| <= 50 5 | where |S| represents the length of string S. 6 | Sample Input 1 : 7 | xpix 8 | Sample Output : 9 | x3.14x 10 | Sample Input 2 : 11 | pipi 12 | Sample Output : 13 | 3.143.14 14 | Sample Input 3 : 15 | pip 16 | Sample Output : 17 | 3.14p 18 | Constraints:- 19 | 1<=|S|<=50 20 | */ 21 | 22 | // Change in the given string itself. So no need to return or print anything 23 | 24 | void replacePi(char input[]) { 25 | int len = 0; 26 | while (input[len]) 27 | len++; 28 | if(len <= 1) 29 | return; 30 | replacePi(input+1); 31 | if(input[0]=='p' && input[1]=='i'){ 32 | for(int i=2*len; i>=2; i--) 33 | input[i+2]=input[i]; 34 | input[0]='3'; 35 | input[1]='.'; 36 | input[2]='1'; 37 | input[3]='4'; 38 | } 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Column Wise Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a 2D integer array of size M*N, find and print the sum of ith column elements separated by space. 3 | Input Format : 4 | First and only line of input contains M and N, followed by M * N space separated integers representing the elements in the 2D array. 5 | Output Format : 6 | Sum of every ith column elements (separated by space) 7 | Constraints : 8 | 1 <= M, N <= 10^3 9 | Sample Input : 10 | 4 2 11 | 1 2 12 | 3 4 13 | 5 6 14 | 7 8 15 | Sample Output : 16 | 16 20 17 | */ 18 | #include 19 | using namespace std; 20 | 21 | 22 | int main(){ 23 | 24 | int m,n,i,j,sum=0; 25 | cin >> m >> n; 26 | int arr[m][n]; 27 | for(i=0;i> arr[i][j]; 30 | } 31 | } 32 | for(i=0;i 30 | using namespace std; 31 | 32 | int main(){ 33 | 34 | /* Read input as specified in the question. 35 | * Print output as specified in the question. 36 | */ 37 | int n; 38 | cin >> n; 39 | cout << 2 << endl; 40 | for(int i=3;i<=n;i++){ 41 | 42 | bool divided = false; 43 | for(int j=2;j<=i-1;j++){ 44 | if(i%j==0){ 45 | divided = true; 46 | break; 47 | } 48 | } 49 | if(!divided) 50 | cout << i << endl; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Patterns 2/Code : Triangle of Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given number of rows. 3 | Pattern for N = 4 4 | 5 | 1 6 | 232 7 | 34543 8 | 4567654 9 | 10 | Input format : 11 | Integer N (Total no. of rows) 12 | Output format : 13 | Pattern in N lines 14 | Constraints : 15 | 0 <= N <= 50 16 | Sample Input 1: 17 | 5 18 | Sample Output 1: 19 | 1 20 | 232 21 | 34543 22 | 4567654 23 | 567898765 24 | Sample Input 2: 25 | 4 26 | Sample Output 2: 27 | 1 28 | 232 29 | 34543 30 | 4567654 31 | */ 32 | #include 33 | using namespace std; 34 | 35 | int main() { 36 | int n,k; 37 | cin >> n; 38 | for(int i = 1; i<=n;i++){ 39 | for(int j = 1; j<=n-i;j++) 40 | cout << ' '; 41 | for(k = i; k<=2*i-1;k++){ 42 | cout << k; 43 | 44 | } 45 | 46 | k=2*i-2; 47 | while(k>=i){ 48 | cout << k; 49 | k--; 50 | } 51 | cout << endl; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Print Subset Sum to K.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A and an integer K, print all subsets of A which sum to K. 3 | Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array. 4 | Note : The order of subsets are not important. Just print them in different lines. 5 | Input format : 6 | Line 1 : Size of input array 7 | Line 2 : Array elements separated by space 8 | Line 3 : K 9 | Sample Input: 10 | 9 11 | 5 12 3 17 1 18 15 3 17 12 | 6 13 | Sample Output: 14 | 3 3 15 | 5 1 16 | */ 17 | 18 | // void printSubsetSumToK(int input[], int size, int k) { 19 | // // Write your code here 20 | // } 21 | void printSubsetSumToK(int input[], int size, int k,string output="") { 22 | // Write your code here 23 | if(size==0) 24 | { 25 | if(k==0) 26 | { 27 | cout< 37 | using namespace std; 38 | 39 | 40 | int main(){ 41 | 42 | /* Read input as specified in the question. 43 | * Print output as specified in the question. 44 | */int n; 45 | //cout << "Enter n" << endl; 46 | cin >> n; 47 | 48 | int i = 1; 49 | int val = 1; 50 | while (i <= n) { 51 | int k = 1; 52 | while (k <= n - i) { 53 | cout << " "; 54 | k = k + 1; 55 | } 56 | 57 | int j = 1; 58 | while (j <= 2*i-1) { 59 | cout << '*'; 60 | j = j + 1; 61 | val = val + 1; 62 | } 63 | cout << endl; 64 | i = i + 1; 65 | } 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /Lecture 7: Functions/Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false. 3 | Fibonacci Series is defined by the recurrence 4 | F(n) = F(n-1) + F(n-2) 5 | where F(0) = 0 and F(1) = 1 6 | 7 | 8 | Input Format : 9 | Integer N 10 | Output Format : 11 | true or false 12 | Constraints : 13 | 0 <= n <= 10^4 14 | Sample Input 1 : 15 | 5 16 | Sample Output 1 : 17 | true 18 | Sample Input 2 : 19 | 14 20 | Sample Output 2 : 21 | false 22 | */ 23 | bool checkMember(int n){ 24 | 25 | /* Don't write main(). 26 | * Don't read input, it is passed as function argument. 27 | * Return output and don't print it. 28 | * Taking input and printing output is handled automatically. 29 | */int n1,n2,n3,i; 30 | n1=0,n2=1; 31 | if(n == n1||n == n2) 32 | return true; 33 | for(i=3;n3<=n;i++) 34 | { 35 | 36 | n3 = n1 + n2; 37 | n1 = n2; 38 | n2 = n3; 39 | if(n3 == n) 40 | return true; 41 | } 42 | return false; 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Operators and For Loop/Decimal to Binary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a decimal number (integer N), convert it into binary and print. 3 | Note: The given input number could be large, so the corresponding binary number can exceed the integer range. So you may want to take the answer as long for CPP and Java. 4 | Note for C++ coders: Do not use the inbuilt implementation of "pow" function. The implementation of that function is done for 'double's and it may fail when used for 'int's, 'long's, or 'long long's. Implement your own "pow" function to work for non-float data types. 5 | 6 | 7 | Input format : 8 | Integer N 9 | Output format : 10 | Corresponding Binary number (long) 11 | Constraints : 12 | 0 <= N <= 10^5 13 | Sample Input 1 : 14 | 12 15 | Sample Output 1 : 16 | 1100 17 | Sample Input 2 : 18 | 7 19 | Sample Output 2 : 20 | 111 21 | */ 22 | #include 23 | using namespace std; 24 | 25 | int main() { 26 | // Write your code here 27 | long x,dig,rev=0,pv=1; 28 | cin >> x; 29 | while(x>0){ 30 | dig = x%2; 31 | rev = rev+dig*pv; 32 | x = x/2; 33 | pv*=10; 34 | } 35 | cout << rev; 36 | } 37 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Remove Consecutive Duplicates.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given string(str), remove all the consecutive duplicate characters. 3 | Example: 4 | Input String: "aaaa" 5 | Expected Output: "a" 6 | 7 | Input String: "aabbbcc" 8 | Expected Output: "abc" 9 | Input Format: 10 | The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. 11 | Output Format: 12 | The only line of output prints the updated string. 13 | Note: 14 | You are not required to print anything. It has already been taken care of. 15 | Constraints: 16 | 0 <= N <= 10^6 17 | Where N is the length of the input string. 18 | 19 | Time Limit: 1 second 20 | Sample Input 1: 21 | aabccbaa 22 | Sample Output 1: 23 | abcba 24 | Sample Input 2: 25 | xxyyzxx 26 | Sample Output 2: 27 | xyzx 28 | */ 29 | 30 | void removeConsecutiveDuplicates(char input[]) { 31 | int i=0,j=0; 32 | while(input[j]){ 33 | if(input[j+1]!=input[j]){ 34 | input[i] = input[j]; 35 | i++; 36 | } 37 | j++; 38 | } 39 | input[i] = '\0'; 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Remove Duplicates Recursively.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S, remove consecutive duplicates from it recursively. 3 | Input Format : 4 | String S 5 | Output Format : 6 | Output string 7 | Constraints : 8 | 1 <= |S| <= 10^3 9 | where |S| represents the length of string 10 | Sample Input 1 : 11 | aabccba 12 | Sample Output 1 : 13 | abcba 14 | Sample Input 2 : 15 | xxxyyyzwwzzz 16 | Sample Output 2 : 17 | xyzwz 18 | */ 19 | 20 | void removeConsecutiveDuplicates(char *input) { 21 | /* Don't write main(). 22 | * Don't read input, it is passed as function argument. 23 | * Change in the given string itself. 24 | * No need to return or print anything 25 | * Taking input and printing output is handled automatically. 26 | */ 27 | if(input[0]=='\0' || input[1]=='\0') 28 | return; 29 | removeConsecutiveDuplicates(input+1); 30 | if(input[0]==input[1]){ 31 | int i=1; 32 | while(input[i]==input[0]) 33 | i++; 34 | int j = 1; 35 | while(input[i]){ 36 | input[j]=input[i]; 37 | i++; 38 | j++; 39 | } 40 | input[j]=input[i]; 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Print Subsets of Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array (of length n), find and print all the subsets of input array. 3 | Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array. 4 | Note : The order of subsets are not important. Just print the subsets in different lines. 5 | Input format : 6 | Line 1 : Integer n, Size of array 7 | Line 2 : Array elements (separated by space) 8 | Constraints : 9 | 1 <= n <= 15 10 | Sample Input: 11 | 3 12 | 15 20 12 13 | Sample Output: 14 | [] (this just represents an empty array, don't worry about the square brackets) 15 | 12 16 | 20 17 | 20 12 18 | 15 19 | 15 12 20 | 15 20 21 | 15 20 12 22 | */ 23 | 24 | // void printSubsetsOfArray(int input[], int size) { 25 | // // Write your code here 26 | 27 | // } 28 | void printSubsetsOfArray(int input[], int size,string output="") { 29 | // Write your code here 30 | if(size==0) 31 | { 32 | cout< 22 | using namespace std; 23 | 24 | 25 | int main(){ 26 | 27 | /* 28 | Read input as specified in the question. 29 | Print output as specified in the question. 30 | */int n, sum = 0, i = 2; 31 | cin >> n; 32 | if(n 36 | using namespace std; 37 | 38 | 39 | int main() { 40 | 41 | /* Read input as specified in the question. 42 | * Print output as specified in the question. 43 | */int n,k,i,j; 44 | cin >> n; 45 | for(i = 1; i<=(n+1)/2;i++){ 46 | for(j = 1; j<=((n+1)/2)-i;j++) 47 | cout << ' '; 48 | for(k = 1; k<=2*i-1;k++) 49 | cout << '*'; 50 | cout << endl; 51 | } 52 | for(i = (n-1)/2; i>=1;i--){ 53 | for(j = 1; j<=((n-1)/2)-i+1;j++) 54 | cout << ' '; 55 | for(k = 1; k<=2*i-1;k++) 56 | cout << '*'; 57 | cout << endl; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Operators and For Loop/Sum or Product.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that asks the user for a number N and a choice C. And then give them the possibility to choose between computing the sum and computing the product of all integers in the range 1 to N (both inclusive). 3 | If C is equal to - 4 | 1, then print the sum 5 | 2, then print the product 6 | Any other number, then print '-1' (without the quotes) 7 | Input format : 8 | Line 1 : Integer N 9 | Line 2 : Choice C 10 | Output Format : 11 | Sum or product according to user's choice 12 | Constraints : 13 | 1 <= N <= 12 14 | Sample Input 1 : 15 | 10 16 | 1 17 | Sample Output 1 : 18 | 55 19 | Sample Input 2 : 20 | 10 21 | 2 22 | Sample Output 2 : 23 | 3628800 24 | Sample Input 3 : 25 | 10 26 | 4 27 | Sample Output 3 : 28 | -1 29 | */ 30 | #include 31 | using namespace std; 32 | 33 | int main() { 34 | // Write your code here 35 | int n,c; 36 | cin >> n >> c; 37 | if(c==1){ 38 | int sum = 0; 39 | for(int i=1;i<=n;i++) 40 | sum += i; 41 | cout << sum; 42 | } 43 | else if(c==2){ 44 | int sum = 1; 45 | for(int i=1;i<=n;i++) 46 | sum *= i; 47 | cout << sum; 48 | } 49 | else 50 | cout << -1; 51 | } 52 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/All Indices of Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of length N and an integer x, you need to find all the indexes where x is present in the input array. Save all the indexes in the output array (in increasing order). 3 | Do this recursively. Indexing in the array starts from 0. 4 | Input Format : 5 | Line 1 : An Integer N i.e. size of array 6 | Line 2 : N integers which are elements of the array, separated by spaces 7 | Line 3 : Integer x 8 | Output Format : 9 | Return the size of the output array 10 | Constraints : 11 | 1 <= N <= 10^3 12 | Sample Input : 13 | 5 14 | 9 8 10 8 8 15 | 8 16 | Sample Output : 17 | 1 3 4 18 | */ 19 | 20 | int allIndexes(int input[], int size, int x, int output[]) { 21 | /* Don't write main(). 22 | Don't read input, it is passed as function argument. 23 | Save all the indexes in the output array passed and return the size of output array. 24 | Taking input and printing output is handled automatically. 25 | */ 26 | if(size<=0){ 27 | return 0; 28 | } 29 | int ans; 30 | ans = allIndexes(input, size-1, x, output); 31 | if(input[size-1] == x){ 32 | output[ans] = size-1; 33 | return ans+1; 34 | } 35 | else{ 36 | return ans; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Test 1 (Milestone 1)/Second Largest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Take input a stream of n integer elements, find the second largest element present. 3 | The given elements can contain duplicate elements as well. If only 0 or 1 element is given, the second largest should be INT_MIN ( - 2^31 ). 4 | Input format : 5 | 6 | Line 1 : Total number of elements (n) 7 | 8 | Line 2 : N elements (separated by space) 9 | 10 | Sample Input 1: 11 | 4 12 | 3 9 0 9 13 | Sample Output 1: 14 | 3 15 | Sample Input 2 : 16 | 2 17 | 4 4 18 | Sample Output 2: 19 | -2147483648 20 | Sample Output Explanation: 21 | Since both the elements are equal here, hence second largest element is INT_MIN i.e. ( -2^31 ) 22 | */ 23 | #include 24 | using namespace std; 25 | #include 26 | 27 | 28 | int main(){ 29 | 30 | // Write your code here 31 | int n; 32 | cin>>n; 33 | int max=INT_MIN,secondMax=INT_MIN; 34 | int num; 35 | int count=1; 36 | 37 | while(count<=n){ 38 | cin>>num; 39 | if (num>max){ 40 | secondMax=max; 41 | max=num; 42 | } 43 | else if(num>secondMax&&num!=max){ 44 | secondMax=num; 45 | } 46 | count++; 47 | 48 | 49 | } 50 | cout< 22 | #include 23 | using namespace std; 24 | 25 | #include "solution.h" 26 | 27 | int main() { 28 | string str; 29 | cin >> str; 30 | cout << uniqueChar(str); 31 | } 32 | */ 33 | 34 | #include 35 | string uniqueChar(string str) 36 | { 37 | // Write your code here 38 | string s; 39 | unordered_map m; 40 | for (int i = 0; i < str.length(); i++) 41 | { 42 | if (m.count(str[i]) == 0) 43 | { 44 | m[str[i]]++; 45 | s += str[i]; 46 | } 47 | } 48 | return s; 49 | } 50 | -------------------------------------------------------------------------------- /Lecture 18 : DP - 1/Code : Staircase using Dp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | // write your code here 7 | return 0; 8 | } 9 | 10 | */ 11 | 12 | /* 13 | A child runs up a staircase with 'n' steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count and return all possible ways in which the child can run-up to the stairs. 14 | Input format : 15 | The first and the only line of input contains an integer value, 'n', denoting the total number of steps. 16 | Output format : 17 | Print the total possible number of ways. 18 | Constraints : 19 | 0 <= n <= 10 ^ 4 20 | 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 4 24 | Sample Output 1: 25 | 7 26 | Sample Input 2: 27 | 10 28 | Sample Output 2: 29 | 274 30 | */ 31 | 32 | /* 33 | #include 34 | using namespace std; 35 | 36 | #include "solution.h" 37 | 38 | int main() 39 | { 40 | int n; 41 | cin >> n; 42 | cout << staircase(n); 43 | } 44 | */ 45 | 46 | long staircase(int n) 47 | { 48 | // Write your code here 49 | long *ans = new long[n + 1]; 50 | ans[0] = 1; 51 | ans[1] = 1; 52 | ans[2] = 2; 53 | for (int i = 3; i <= n; i++) 54 | ans[i] = ans[i - 1] + ans[i - 2] + ans[i - 3]; 55 | return ans[n]; 56 | } 57 | -------------------------------------------------------------------------------- /Assignment: Recursion 1b/Tower of Hanoi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using third rod (say auxiliary). The rules are : 3 | 1) Only one disk can be moved at a time. 4 | 2) A disk can be moved only if it is on the top of a rod. 5 | 3) No disk can be placed on the top of a smaller disk. 6 | Print the steps required to move n disks from source rod to destination rod. 7 | Source Rod is named as 'a', auxiliary rod as 'b' and destination rod as 'c'. 8 | Input Format : 9 | Integer n 10 | Output Format : 11 | Steps in different lines (in one line print source and destination rod name separated by space) 12 | Constraints : 13 | 0 <= n <= 20 14 | Sample Input 1 : 15 | 2 16 | Sample Output 1 : 17 | a b 18 | a c 19 | b c 20 | Sample Input 2 : 21 | 3 22 | Sample Output 2 : 23 | a c 24 | a b 25 | c b 26 | a c 27 | b a 28 | b c 29 | a c 30 | */ 31 | 32 | void towerOfHanoi(int n, char source, char auxiliary, char destination) { 33 | if(n<1) 34 | return; 35 | else{ 36 | towerOfHanoi(n-1, source, destination, auxiliary); 37 | cout << source << ' ' << destination << endl; 38 | towerOfHanoi(n-1, auxiliary, source, destination); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 3: Recursion 1/First Index of Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of length N and an integer x, you need to find and return the first index of integer x present in the array. Return -1 if it is not present in the array. 3 | First index means, the index of first occurrence of x in the input array. 4 | Do this recursively. Indexing in the array starts from 0. 5 | Input Format : 6 | Line 1 : An Integer N i.e. size of array 7 | Line 2 : N integers which are elements of the array, separated by spaces 8 | Line 3 : Integer x 9 | Output Format : 10 | first index or -1 11 | Constraints : 12 | 1 <= N <= 10^3 13 | Sample Input : 14 | 4 15 | 9 8 10 8 16 | 8 17 | Sample Output : 18 | 1 19 | */ 20 | 21 | int firstIndex(int input[], int size, int x) { 22 | /* Don't write main(). 23 | Don't read input, it is passed as function argument. 24 | Return output and don't print it. 25 | Taking input and printing output is handled automatically. 26 | */ 27 | if(size<=0){ 28 | return -1; 29 | } 30 | if(input[0] == x) 31 | return 0; 32 | else{ 33 | int ans = firstIndex(input+1, size-1, x); 34 | if(ans==-1) 35 | return -1; 36 | else 37 | return ans+1; 38 | } 39 | // return firstIndex(input+1, size-1, x); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Test 2 (Milestone 2)/Leaders in array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array A of size n. Find and print all the leaders present in the input array. An array element A[i] is called Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i]. 3 | Print all the leader elements separated by space and in the same order they are present in the input array. 4 | Input Format : 5 | Line 1 : Integer n, size of array 6 | Line 2 : Array A elements (separated by space) 7 | Output Format : 8 | leaders of array (separated by space) 9 | Constraints : 10 | 1 <= n <= 10^6 11 | Sample Input 1 : 12 | 6 13 | 3 12 34 2 0 -1 14 | Sample Output 1 : 15 | 34 2 0 -1 16 | Sample Input 2 : 17 | 5 18 | 13 17 5 4 6 19 | Sample Output 2 : 20 | 17 6 21 | */ 22 | 23 | void Leaders(int* arr,int len) 24 | { 25 | /* Don't write main(). 26 | * Don't read input, it is passed as function argument. 27 | * Print your output exactly in the same format as shown. 28 | * Don't print any extra line. 29 | */ 30 | for (int i = 0; i < len; i++) 31 | { 32 | int j; 33 | for (j = i+1; j < len; j++) 34 | { 35 | if (arr[i] 24 | 25 | bool isSubSequence(char *str1, char *str2, int n, int m){ 26 | 27 | // the entire m is present in the string 28 | if (m == 0) 29 | return true; 30 | 31 | // the string is completed 32 | if (n == 0) 33 | return false; 34 | 35 | 36 | if (str1[n - 1] == str2[m - 1]) 37 | return isSubSequence(str1, str2, n - 1, m - 1); 38 | 39 | return isSubSequence(str1, str2, n - 1, m); 40 | } 41 | 42 | bool checksequenece(char large[] , char*small) { 43 | int n = strlen(large); 44 | int m = strlen(small); 45 | bool ans = isSubSequence(large, small, n, m); 46 | return ans; 47 | } 48 | -------------------------------------------------------------------------------- /Operators and For Loop/Nth Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula - 3 | F(n) = F(n-1) + F(n-2), 4 | Where, F(1) = 1, 5 | F(2) = 1 6 | Provided N you have to find out the Nth Fibonacci Number. 7 | Input Format : 8 | The first line of each test case contains a real number ‘N’. 9 | Output Format : 10 | For each test case, return its equivalent Fibonacci number. 11 | Constraints: 12 | 1 <= N <= 10000 13 | Where ‘N’ represents the number for which we have to find its equivalent Fibonacci number. 14 | 15 | Time Limit: 1 second 16 | Sample Input 1: 17 | 6 18 | Sample Output 1: 19 | 8 20 | Explanation 21 | Now the number is ‘6’ so we have to find the “6th” Fibonacci number 22 | So by using the property of the Fibonacci series i.e 23 | 24 | [ 1, 1, 2, 3, 5, 8, 13, 21] 25 | So the “6th” element is “8” hence we get the output. 26 | */ 27 | #include 28 | using namespace std; 29 | 30 | 31 | int main(){ 32 | //Write your code here. 33 | int n,n1,n2,n3,i; 34 | cin >> n; 35 | if(n==1||n==2) 36 | cout << 1; 37 | else{ 38 | for(n1=1,n2=1,i=3;i<=n;i++) 39 | { 40 | n3 = n1 + n2; 41 | n1 = n2; 42 | n2 = n3; 43 | } 44 | cout << n3; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Conditionals and Loops/Sum of even & odd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately. 3 | Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5. 4 | Input format : 5 | Integer N 6 | Output format : 7 | Sum_of_Even_Digits Sum_of_Odd_Digits 8 | (Print first even sum and then odd sum separated by space) 9 | Constraints 10 | 0 <= N <= 10^8 11 | Sample Input 1: 12 | 1234 13 | Sample Output 1: 14 | 6 4 15 | Sample Input 2: 16 | 552245 17 | Sample Output 2: 18 | 8 15 19 | Explanation for Input 2: 20 | For the given input, the even digits are 2, 2 and 4 and if we take the sum of these digits it will come out to be 8(2 + 2 + 4) and similarly, if we look at the odd digits, they are, 5, 5 and 5 which makes a sum of 15(5 + 5 + 5). Hence the answer would be, 8(evenSum) 15(oddSum) 21 | */ 22 | #include 23 | using namespace std; 24 | 25 | int main() { 26 | // Write your code here 27 | int even = 0,odd = 0,digit; 28 | long n; 29 | cin >> n; 30 | while(n>0){ 31 | digit = n % 10; 32 | if (digit % 2 == 0) 33 | even += digit; 34 | else 35 | odd += digit; 36 | n = n / 10; 37 | } 38 | cout << even << ' ' << odd; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Binary Search (Recursive).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer sorted array (sorted in increasing order) and an element x, find the x in given array using binary search. Return the index of x. 3 | Return -1 if x is not present in the given array. 4 | Note : If given array size is even, take first mid. 5 | Input format : 6 | 7 | Line 1 : Array size 8 | 9 | Line 2 : Array elements (separated by space) 10 | 11 | Line 3 : x (element to be searched) 12 | 13 | Sample Input : 14 | 6 15 | 2 3 4 5 6 8 16 | 5 17 | Sample Output: 18 | 3 19 | */ 20 | 21 | // input - input array 22 | // size - length of input array 23 | // element - value to be searched 24 | // int binarySearch(int input[], int size, int element) { 25 | 26 | 27 | // } 28 | int binarySearch(int input[], int size, int element,int start=0) 29 | { 30 | int mid=(start+size)/2; 31 | if(start>size) 32 | { 33 | return -1; 34 | } 35 | else 36 | { 37 | if(input[mid]==element) 38 | { 39 | return mid; 40 | } 41 | else if(input[mid]element) 47 | { 48 | size=mid-1; 49 | return binarySearch(input,size,element,start); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Swap Alternate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list. 3 | You don't need to print or return anything, just change in the input array itself. 4 | Input Format : 5 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 6 | 7 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 8 | 9 | Second line contains 'N' single space separated integers representing the elements in the array/list. 10 | Output Format : 11 | For each test case, print the elements of the resulting array in a single row separated by a single space. 12 | 13 | Output for every test case will be printed in a separate line. 14 | Constraints : 15 | 1 <= t <= 10^2 16 | 0 <= N <= 10^5 17 | Time Limit: 1sec 18 | Sample Input 1: 19 | 1 20 | 6 21 | 9 3 6 12 4 32 22 | Sample Output 1 : 23 | 3 9 12 6 32 4 24 | Sample Input 2: 25 | 2 26 | 9 27 | 9 3 6 12 4 32 5 11 19 28 | 4 29 | 1 2 3 4 30 | Sample Output 2 : 31 | 3 9 12 6 32 4 11 5 19 32 | 2 1 4 3 33 | */ 34 | void swapAlternate(int *arr, int size) 35 | { 36 | //Write your code here 37 | int temp; 38 | for(int i=0; i 24 | using namespace std; 25 | 26 | #include "solution.h" 27 | 28 | int main() { 29 | int n; 30 | cin >> n; 31 | cout << balancedBTs(n); 32 | } 33 | */ 34 | 35 | #define mod 1000000007 36 | int balancedBTs(int n) 37 | { 38 | // Write your code here 39 | int *ans = new int[n + 1]; 40 | ans[0] = 1; 41 | ans[1] = 1; 42 | for (int i = 2; i <= n; i++) 43 | { 44 | int val1 = (int)(((long)ans[i - 1] * ans[i - 1]) % mod); 45 | int val2 = (int)((2 * (long)ans[i - 1] * ans[i - 2]) % mod); 46 | ans[i] = (val1 + val2) % mod; 47 | } 48 | return ans[n]; 49 | } 50 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Print Keypad Combinations Code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer n, using phone keypad find out and print all the possible strings that can be made using digits of input n. 3 | Note : The order of strings are not important. Just print different strings in new lines. 4 | Input Format : 5 | Integer n 6 | Output Format : 7 | All possible strings in different lines 8 | Constraints : 9 | 1 <= n <= 10^6 10 | Sample Input: 11 | 23 12 | Sample Output: 13 | ad 14 | ae 15 | af 16 | bd 17 | be 18 | bf 19 | cd 20 | ce 21 | cf 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | string dial(int key){ 30 | unordered_mapm; 31 | m[0]=""; 32 | m[1]=""; 33 | m[2]="abc"; 34 | m[3]="def"; 35 | m[4]="ghi"; 36 | m[5]="jkl"; 37 | m[6]="mno"; 38 | m[7]="pqrs"; 39 | m[8]="tuv"; 40 | m[9]="wxyz"; 41 | return m[key]; 42 | } 43 | void pK(int num,string out){ 44 | 45 | if(num==0){ 46 | cout< 25 | int lengthOfLongestSubsetWithZeroSum(int *arr, int n) 26 | { 27 | // Write your code here 28 | unordered_map m; 29 | int length = 0; 30 | int sum = 0; 31 | for (int i = 0; i < n; i++) 32 | { 33 | sum += arr[i]; 34 | if (arr[i] == 0 && length == 0) 35 | length = 1; 36 | if (sum == 0) 37 | length = i + 1; 38 | if (m.count(sum) > 0) 39 | length = max(length, i - m[sum]); 40 | else 41 | m[sum] = i; 42 | } 43 | return length; 44 | } 45 | -------------------------------------------------------------------------------- /Operators and For Loop/Count Characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to count and print the total number of characters (lowercase english alphabets only), digits (0 to 9) and white spaces (single space, tab i.e. '\t' and newline i.e. '\n') entered till '$'. 3 | That is, input will be a stream of characters and you need to consider all the characters which are entered till '$'. 4 | Print count of characters, count of digits and count of white spaces respectively (separated by space). 5 | Input Format : 6 | A stream of characters terminated by '$' 7 | Output Format : 8 | 3 integers i.e. count_of_characters count_of_digits count_of_whitespaces (separated by space) 9 | Sample Input : 10 | abc def4 5$ 11 | Sample Output : 12 | 6 2 2 13 | Sample Output Explanation : 14 | Number of characters : 6 (a, b, c, d, e, f) 15 | Number of digits : 2 (4, 5) 16 | Number of white spaces : 2 (one space after abc and one newline after 4) 17 | */ 18 | #include 19 | using namespace std; 20 | 21 | int main(){ 22 | 23 | /* Read input as specified in the question. 24 | * Print output as specified in the question. 25 | */char c; 26 | c = cin.get(); 27 | int ch = 0, dig = 0, wh = 0; 28 | while (c != '$') { 29 | if(c>='a'&&c<='z') 30 | ch++; 31 | else if(c>='0'&&c<='9') 32 | dig++; 33 | else if(c==' '||c=='\t'||c=='\n') 34 | wh++; 35 | c = cin.get(); 36 | } 37 | cout << ch << ' ' << dig << ' ' << wh; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Print Permutations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an input string (STR), print all possible permutations of the input string. 3 | Note: 4 | The input string may contain the same characters, so there will also be the same permutations. 5 | The order of permutations doesn’t matter. 6 | Input Format: 7 | The only input line contains a string (STR) of alphabets in lower case 8 | Output Format: 9 | Print each permutations in a new line 10 | Constraint: 11 | 1<=length of STR<=8 12 | Time Limit: 1sec 13 | Sample Input 1: 14 | cba 15 | Sample Output 1: 16 | abc 17 | acb 18 | bac 19 | bca 20 | cab 21 | cba 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | // void printPermutations(string input){ 30 | 31 | // /* Don't write main() function. 32 | // * Don't read input, it is passed as function argument. 33 | // * Print output as specified in the question 34 | // */ 35 | // } 36 | void printPermutations(string input,string output=""){ 37 | 38 | /* Don't write main() function. 39 | * Don't read input, it is passed as function argument. 40 | * Print output as specified in the question 41 | */ 42 | if(input=="") 43 | { 44 | cout<input[i-1] && i 21 | using namespace std; 22 | 23 | // void printAllPossibleCodes(string input) { 24 | // /* 25 | // Given the input as a string, print all its possible combinations. You do not need to return anything. 26 | // */ 27 | // } 28 | map keyMap; 29 | 30 | void printAllPossibleCodes(string input,string output="") { 31 | /* 32 | Given the input as a string, print all its possible combinations. You do not need to return anything. 33 | */ 34 | char ch='a'; 35 | for(int i=1;i<=26;i++,ch++) 36 | keyMap[to_string(i)]=ch; 37 | 38 | if(input=="") 39 | { 40 | cout<1 && stoi(input.substr(0,2))<=26) 46 | printAllPossibleCodes(input.substr(2),output+keyMap[input.substr(0,2)]); 47 | } 48 | -------------------------------------------------------------------------------- /Lecture 9: Searching and Sorting/Code Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Bubble Sort'. 3 | Note: 4 | Change in the input array/list itself. You don't need to return or print the elements. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | Output format : 12 | For each test case, print the elements of the array/list in sorted order separated by a single space. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^3 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 21 | 7 22 | 2 13 4 1 3 6 28 23 | Sample Output 1: 24 | 1 2 3 4 6 13 28 25 | Sample Input 2: 26 | 2 27 | 5 28 | 9 3 6 2 0 29 | 4 30 | 4 3 2 1 31 | Sample Output 2: 32 | 0 2 3 6 9 33 | 1 2 3 4 34 | */ 35 | void bubbleSort(int *input, int size) 36 | { 37 | //Write your code here 38 | int temp; 39 | for(int i=0;iinput[j+1]){ 42 | temp = input[j]; 43 | input[j] = input[j+1]; 44 | input[j+1] = temp; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Highest Occuring Character.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given a string(str), find and return the highest occurring character. 3 | Example: 4 | Input String: "abcdeapapqarr" 5 | Expected Output: 'a' 6 | Since 'a' has appeared four times in the string which happens to be the highest frequency character, the answer would be 'a'. 7 | If there are two characters in the input string with the same frequency, return the character which comes first. 8 | Consider: 9 | Assume all the characters in the given string to be in lowercase always. 10 | Input Format: 11 | The first and only line of input contains a string without any leading and trailing spaces. 12 | Output Format: 13 | The only line of output prints the updated string. 14 | Note: 15 | You are not required to print anything explicitly. It has already been taken care of. 16 | Constraints: 17 | 0 <= N <= 10^6 18 | Where N is the length of the input string. 19 | 20 | Time Limit: 1 second 21 | Sample Input 1: 22 | abdefgbabfba 23 | Sample Output 1: 24 | b 25 | Sample Input 2: 26 | xy 27 | Sample Output 2: 28 | x 29 | */ 30 | 31 | char highestOccurringChar(char input[]) { 32 | int charfreq[256] = {0}; 33 | int maxindex=0, max=0; 34 | int i = 0, j; 35 | while(input[i]){ 36 | j = int(input[i]); 37 | charfreq[j]++; 38 | i++; 39 | } 40 | for(i=0;i<256;i++){ 41 | if(charfreq[i]>max){ 42 | max = charfreq[i]; 43 | maxindex = i; 44 | } 45 | } 46 | return char(maxindex); 47 | } 48 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Check AB.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks if the string was generated using the following rules: 3 | a. The string begins with an 'a' 4 | b. Each 'a' is followed by nothing or an 'a' or "bb" 5 | c. Each "bb" is followed by nothing or an 'a' 6 | If all the rules are followed by the given string, return true otherwise return false. 7 | Input format : 8 | String S 9 | Output format : 10 | 'true' or 'false' 11 | Constraints : 12 | 1 <= |S| <= 1000 13 | where |S| represents length of string S. 14 | Sample Input 1 : 15 | abb 16 | Sample Output 1 : 17 | true 18 | Sample Input 2 : 19 | abababa 20 | Sample Output 2 : 21 | false 22 | Explanation for Sample Input 2 23 | In the above example, a is not followed by either "a" or "bb", instead it's followed by "b" which results in false to be returned. 24 | */ 25 | 26 | 27 | // bool checkAB(char input[]) { 28 | // // Write your code here 29 | 30 | // } 31 | 32 | bool checkAB(char input[],char prev='\0') { 33 | // Write your code here 34 | if(input[0]=='\0') 35 | return true; 36 | else if(input[0]=='b' && prev=='\0') 37 | return false; 38 | else if(input[0]=='b' && prev=='b' && input[1]=='b') 39 | return false; 40 | else if(input[0]=='b' && prev!='b' && input[1]=='\0') 41 | return false; 42 | else if(input[0]=='b' && prev=='a' && input[1]!='b') 43 | return false; 44 | else 45 | return checkAB(input+1,input[0]); 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Return Permutations - String.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S, find and return all the possible permutations of the input string. 3 | Note 1 : The order of permutations is not important. 4 | Note 2 : If original string contains duplicate characters, permutations will also be duplicates. 5 | Input Format : 6 | String S 7 | Output Format : 8 | All permutations (in different lines) 9 | Sample Input : 10 | abc 11 | Sample Output : 12 | abc 13 | acb 14 | bac 15 | bca 16 | cab 17 | cba 18 | */ 19 | 20 | #include 21 | using namespace std; 22 | 23 | // int returnPermutations(string input, string output[]){ 24 | // /* Don't write main() function. 25 | // * Don't read input, it is passed as function argument. 26 | // * Print output as specified in the question 27 | // */ 28 | // } 29 | int returnPermutations(string input, string output[]) 30 | { 31 | /* Don't write main() function. 32 | * Don't read input, it is passed as function argument. 33 | * Print output as specified in the question 34 | */ 35 | 36 | if(input == "") 37 | { 38 | output[0] = ""; 39 | return 1; 40 | } 41 | 42 | int smallOutput = returnPermutations(input.substr(1),output); 43 | string temp; 44 | for(int i=0;i *left; 26 | BinaryTreeNode *right; 27 | 28 | BinaryTreeNode(T data) { 29 | this -> data = data; 30 | left = NULL; 31 | right = NULL; 32 | } 33 | }; 34 | ***************/ 35 | #include 36 | vector* longestPath(BinaryTreeNode* root) { 37 | // Write your code here 38 | if(root == NULL) { 39 | vector *temp = new vector(); 40 | return temp; 41 | } 42 | 43 | vector *left = longestPath(root -> left); 44 | vector *right = longestPath(root -> right); 45 | 46 | if(left -> size() > right -> size()) { 47 | left -> push_back(root -> data); 48 | return left; 49 | } else { 50 | right -> push_back(root -> data); 51 | return right; 52 | } 53 | } 54 | 55 | // Time Complexity : O(n) 56 | // Auxillary Space : O(n) 57 | -------------------------------------------------------------------------------- /Lecture 9: Searching and Sorting/Code Insertion Sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Insertion Sort'. 3 | Note: 4 | Change in the input array/list itself. You don't need to return or print the elements. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | Output Format : 12 | For each test case, print the elements of the array/list in sorted order separated by a single space. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^3 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 21 | 7 22 | 2 13 4 1 3 6 28 23 | Sample Output 1: 24 | 1 2 3 4 6 13 28 25 | Sample Input 2: 26 | 2 27 | 5 28 | 9 3 6 2 0 29 | 4 30 | 4 3 2 1 31 | Sample Output 2: 32 | 0 2 3 6 9 33 | 1 2 3 4 34 | */ 35 | void insertionSort(int *input, int size) 36 | { 37 | //Write your code here 38 | int i, key, j; 39 | for (i = 1; i < size; i++) 40 | { 41 | key = input[i]; 42 | j = i - 1; 43 | 44 | 45 | while (j >= 0 && input[j] > key) 46 | { 47 | input[j + 1] = input[j]; 48 | j = j - 1; 49 | } 50 | input[j + 1] = key; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 12 : Binary Trees/Preorder Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given the root node of a binary tree.Print its preorder traversal. 3 | Input Format: 4 | The first and the only line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data. 5 | Output Format: 6 | The only line of output prints the preorder traversal of the given binary tree. 7 | Constraints: 8 | 1 <= N <= 10^6 9 | Where N is the total number of nodes in the binary tree. 10 | 11 | Time Limit: 1 sec 12 | Sample Input 1: 13 | 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1 14 | Sample Output 1: 15 | 1 2 4 5 3 6 7 16 | Sample Input 2: 17 | 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 18 | Sample Output 1: 19 | 5 6 2 3 9 10 20 | */ 21 | 22 | /********************************************************** 23 | Following is the Binary Tree Node class structure 24 | 25 | template 26 | class BinaryTreeNode { 27 | public : 28 | T data; 29 | BinaryTreeNode *left; 30 | BinaryTreeNode *right; 31 | 32 | BinaryTreeNode(T data) { 33 | this -> data = data; 34 | left = NULL; 35 | right = NULL; 36 | } 37 | }; 38 | 39 | ***********************************************************/ 40 | 41 | void preOrder(BinaryTreeNode *root) { 42 | // Write your code here 43 | if(root == NULL) { 44 | return; 45 | } 46 | 47 | cout << root -> data << " "; 48 | preOrder(root -> left); 49 | preOrder(root -> right); 50 | } 51 | -------------------------------------------------------------------------------- /Lecture 14 : Priority Queues/Kth largest element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A of random integers and an integer k, find and return the kth largest element in the array. 3 | Note: Try to do this question in less than O(N * logN) time. 4 | Input Format : 5 | The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. 6 | The following line contains N space separated integers, that denote the value of the elements of the array. 7 | The following contains an integer, that denotes the value of k. 8 | Output Format : 9 | The first and only line of output contains the kth largest element 10 | Constraints : 11 | 1 <= N, Ai, k <= 10^5 12 | Time Limit: 1 sec 13 | Sample Input 1 : 14 | 6 15 | 9 4 8 7 11 3 16 | 2 17 | Sample Output 1 : 18 | 9 19 | Sample Input 2 : 20 | 8 21 | 2 6 10 11 13 4 1 20 22 | 4 23 | Sample Output 2 : 24 | 10 25 | */ 26 | 27 | /* 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | #include "solution.h" 33 | 34 | int main() { 35 | int n; 36 | cin >> n; 37 | 38 | int* arr = new int[n]; 39 | 40 | for (int i = 0; i < n; i++) { 41 | cin >> arr[i]; 42 | } 43 | 44 | int k; 45 | cin >> k; 46 | 47 | cout << kthLargest(arr, n, k); 48 | 49 | delete[] arr; 50 | } 51 | */ 52 | 53 | #include 54 | int kthLargest(int *arr, int n, int k) 55 | { 56 | // Write your code here 57 | priority_queue pq; 58 | for (int i = 0; i < n; i++) 59 | pq.push(arr[i]); 60 | while (--k) 61 | pq.pop(); 62 | return pq.top(); 63 | } 64 | -------------------------------------------------------------------------------- /Lecture 12 : Binary Trees/Postorder Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given Binary Tree of integers, print the post-order traversal. 3 | Input Format: 4 | The first and the only line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data. 5 | Output Format: 6 | The only line of output prints the post-order traversal of the given binary tree. 7 | Constraints: 8 | 1 <= N <= 10^6 9 | Where N is the total number of nodes in the binary tree. 10 | 11 | Time Limit: 1 sec 12 | Sample Input 1: 13 | 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1 14 | Sample Output 1: 15 | 4 5 2 6 7 3 1 16 | Sample Input 2: 17 | 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1 18 | Sample Output 1: 19 | 2 9 3 6 10 5 20 | */ 21 | 22 | /********************************************************** 23 | Following is the Binary Tree Node class structure 24 | 25 | template 26 | class BinaryTreeNode { 27 | public : 28 | T data; 29 | BinaryTreeNode *left; 30 | BinaryTreeNode *right; 31 | 32 | BinaryTreeNode(T data) { 33 | this -> data = data; 34 | left = NULL; 35 | right = NULL; 36 | } 37 | }; 38 | 39 | ***********************************************************/ 40 | 41 | void postOrder(BinaryTreeNode *root) { 42 | // Write your code here 43 | if(root == NULL) { 44 | return; 45 | } 46 | 47 | postOrder(root -> left); 48 | postOrder(root -> right); 49 | cout << root -> data << " "; 50 | 51 | return; 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/Loot Houses.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A thief wants to loot houses. He knows the amount of money in each house. He cannot loot two consecutive houses. Find the maximum amount of money he can loot. 3 | Input format : 4 | The first line of input contains an integer value of 'n'. It is the total number of houses. 5 | 6 | The second line of input contains 'n' integer values separated by a single space denoting the amount of money each house has. 7 | Output format : 8 | Print the the maximum money that can be looted. 9 | Constraints : 10 | 0 <= n <= 10 ^ 4 11 | 12 | Time Limit: 1 sec 13 | Sample Input 1 : 14 | 6 15 | 5 5 10 100 10 5 16 | Sample Output 1 : 17 | 110 18 | Sample Input 2 : 19 | 6 20 | 10 2 30 20 3 50 21 | Sample Output 2 : 22 | 90 23 | Explanation of Sample Output 2 : 24 | Looting first, third, and the last houses([10 + 30 + 50]) will result in the maximum loot, and all the other possible combinations would result in less than 90. 25 | */ 26 | 27 | /* 28 | #include 29 | using namespace std; 30 | 31 | #include "solution.h" 32 | 33 | int main() 34 | { 35 | int n; 36 | cin >> n; 37 | int *arr = new int[n]; 38 | for (int i = 0; i < n; i++) 39 | { 40 | cin >> arr[i]; 41 | } 42 | 43 | cout << maxMoneyLooted(arr, n); 44 | 45 | delete[] arr; 46 | } 47 | */ 48 | 49 | int maxMoneyLooted(int *arr, int n) 50 | { 51 | int *output = new int[n]; 52 | output[0] = arr[0]; 53 | output[1] = max(arr[0], arr[1]); 54 | for (int i = 2; i < n; i++) 55 | output[i] = max(output[i - 1], output[i - 2] + arr[i]); 56 | return output[n - 1]; 57 | } 58 | -------------------------------------------------------------------------------- /Lecture 18 : DP - 1/Code : No. of balanced BTs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer h, find the possible number of balanced binary trees of height h. You just need to return the count of possible binary trees which are balanced. 3 | This number can be huge, so, return output modulus 10^9 + 7. 4 | Write a simple recursive solution. 5 | Input Format : 6 | The first and only line of input contains an integer, that denotes the value of h. Here, h is the height of the tree. 7 | Output Format : 8 | The first and only line of output contains the count of balanced binary trees modulus 10^9 + 7. 9 | Constraints : 10 | 1 <= h <= 24 11 | Time Limit: 1 sec 12 | Sample Input 1: 13 | 3 14 | Sample Output 1: 15 | 15 16 | Sample Input 2: 17 | 4 18 | Sample Output 2: 19 | 315 20 | */ 21 | 22 | /* 23 | #include 24 | using namespace std; 25 | 26 | #include "solution.h" 27 | 28 | int main() { 29 | int n; 30 | cin >> n; 31 | cout << balancedBTs(n); 32 | } 33 | */ 34 | 35 | #define mod 1000000007 36 | int balancedBTs(int n, int *ans) 37 | { 38 | // Write your code here 39 | if (n == 1 || n == 0) 40 | return 1; 41 | if (ans[n] != -1) 42 | return ans[n]; 43 | int x = balancedBTs(n - 1, ans); 44 | int y = balancedBTs(n - 2, ans); 45 | int val1 = (int)(((long)x * x) % mod); 46 | int val2 = (int)((2 * (long)x * y) % mod); 47 | ans[n] = (val1 + val2) % mod; 48 | return ans[n]; 49 | } 50 | 51 | int balancedBTs(int n) 52 | { 53 | // Write your code here 54 | int *ans = new int[n + 1]; 55 | for (int i = 0; i <= n; i++) 56 | ans[i] = -1; 57 | return balancedBTs(n, ans); 58 | } 59 | -------------------------------------------------------------------------------- /Lecture 14 : Priority Queues/Check Max-Heap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers, check whether it represents max-heap or not. Return true if the given array represents max-heap, else return false. 3 | Input Format: 4 | The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. 5 | The following line contains N space separated integers, that denote the value of the elements of the array. 6 | Output Format : 7 | The first and only line of output contains true if it represents max-heap and false if it is not a max-heap. 8 | Constraints: 9 | 1 <= N <= 10^5 10 | 1 <= Ai <= 10^5 11 | Time Limit: 1 sec 12 | Sample Input 1: 13 | 8 14 | 42 20 18 6 14 11 9 4 15 | Sample Output 1: 16 | true 17 | */ 18 | 19 | /* 20 | #include 21 | using namespace std; 22 | 23 | #include "solution.h" 24 | 25 | int main() { 26 | int n; 27 | cin >> n; 28 | int *arr = new int[n]; 29 | 30 | for (int i = 0; i < n; i++) { 31 | cin >> arr[i]; 32 | } 33 | 34 | cout << (isMaxHeap(arr, n) ? "true\n" : "false\n"); 35 | 36 | delete[] arr; 37 | } 38 | */ 39 | 40 | bool isMaxHeap(int arr[], int n) 41 | { 42 | // Write your code here 43 | for (int i = 0; i < n; i++) 44 | { 45 | int leftChildIndex = 2 * i + 1; 46 | int rightChildIndex = 2 * i + 2; 47 | if (leftChildIndex < n && rightChildIndex < n && (!(arr[leftChildIndex] < arr[i] && arr[rightChildIndex] < arr[i]))) 48 | return false; 49 | else if (leftChildIndex < n && !(arr[leftChildIndex] < arr[i])) 50 | return false; 51 | } 52 | return true; 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Code : Find height.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a generic tree, find and return the height of given tree. 3 | Input Format: 4 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 5 | Output Format : 6 | The first and only line of output prints the height of the given generic tree. 7 | Constraints: 8 | Time Limit: 1 sec 9 | Sample Input 1: 10 | 10 3 20 30 40 2 40 50 0 0 0 0 11 | Sample Output 1: 12 | 3 13 | */ 14 | 15 | /************************************************************ 16 | 17 | Following is the structure for the TreeNode class 18 | 19 | template 20 | class TreeNode { 21 | public: 22 | T data; 23 | vector*> children; 24 | 25 | TreeNode(T data) { 26 | this->data = data; 27 | } 28 | 29 | ~TreeNode() { 30 | for (int i = 0; i < children.size(); i++) { 31 | delete children[i]; 32 | } 33 | } 34 | }; 35 | 36 | ************************************************************/ 37 | #include 38 | 39 | int getHeight(TreeNode* root) { 40 | // Write your code here 41 | //corner case 42 | if(root == NULL) { 43 | return 0; 44 | } 45 | int count = 0; 46 | 47 | for(int i = 0; i < root ->children.size(); i++) { 48 | int temp = getHeight(root -> children[i]); 49 | count = max(temp,count); 50 | } 51 | return count + 1; 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 15 : Hashmaps/Code : Pair Sum to 0.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a random integer array A of size N. Find and print the count of pair of elements in the array which sum up to 0. 3 | Note: Array A can contain duplicate elements as well. 4 | Input format: 5 | The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. 6 | The following line contains N space separated integers, that denote the value of the elements of the array. 7 | Output format : 8 | The first and only line of output contains the count of pair of elements in the array which sum up to 0. 9 | Constraints : 10 | 0 <= N <= 10^4 11 | Time Limit: 1 sec 12 | Sample Input 1: 13 | 5 14 | 2 1 -2 2 3 15 | Sample Output 1: 16 | 2 17 | */ 18 | 19 | /* 20 | #include 21 | using namespace std; 22 | 23 | #include "solution.h" 24 | 25 | int main() { 26 | int n; 27 | cin >> n; 28 | 29 | int* arr = new int[n]; 30 | 31 | for (int i = 0; i < n; ++i) { 32 | cin >> arr[i]; 33 | } 34 | 35 | cout << pairSum(arr, n); 36 | 37 | delete[] arr; 38 | } 39 | */ 40 | 41 | #include 42 | int pairSum(int *input, int n) 43 | { 44 | // Write your code here 45 | unordered_map m; 46 | int count = 0; 47 | for (int i = 0; i < n; i++) 48 | { 49 | if (m.count(input[i]) == 0) 50 | m[input[i]] = 1; 51 | else 52 | m[input[i]] += 1; 53 | if (m.count(-input[i])) 54 | { 55 | count = count + m[-input[i]]; 56 | if (input[i] == 0) 57 | count--; 58 | } 59 | } 60 | return count; 61 | } 62 | -------------------------------------------------------------------------------- /Lecture 5 : Time and Space Complexity Analysis/Find the Unique Element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. 3 | Now, in the given array/list, 'M' numbers are present twice and one number is present only once. 4 | You need to find and return that number which is unique in the array/list. 5 | Note: 6 | Unique element is always present in the array/list according to the given condition. 7 | Input format : 8 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 9 | 10 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 11 | 12 | Second line contains 'N' single space separated integers representing the elements in the array/list. 13 | Output Format : 14 | For each test case, print the unique element present in the array. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 10^2 19 | 0 <= N <= 10^6 20 | 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 1 24 | 7 25 | 2 3 1 6 3 6 2 26 | Sample Output 1: 27 | 1 28 | Sample Input 2: 29 | 2 30 | 5 31 | 2 4 7 2 7 32 | 9 33 | 1 3 1 3 6 6 7 10 7 34 | Sample Output 2: 35 | 4 36 | 10 37 | */ 38 | 39 | #include 40 | // int findUnique(int *arr, int n) { 41 | // // Write your code here 42 | // } 43 | int findUnique(int *arr, int n) { 44 | // Write your code here 45 | int res = 0; 46 | 47 | for(int i = 0; i < n; i++) { 48 | res ^= arr[i]; 49 | } 50 | 51 | return res; 52 | } 53 | 54 | // Time Complexity : O(n) 55 | // Space Complexity : O(1) 56 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Code : Find sum of nodes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a generic tree, find and return the sum of all nodes present in the given tree. 3 | Input format : 4 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 5 | Output Format : 6 | The first and only line of output prints the sum of all nodes of the given generic tree. 7 | Constraints: 8 | Time Limit: 1 sec 9 | Sample Input 1: 10 | 10 3 20 30 40 2 40 50 0 0 0 0 11 | Sample Output 1: 12 | 190 13 | */ 14 | 15 | /************************************************************ 16 | 17 | Following is the structure for the TreeNode class 18 | 19 | template 20 | class TreeNode { 21 | public: 22 | T data; 23 | vector*> children; 24 | 25 | TreeNode(T data) { 26 | this->data = data; 27 | } 28 | 29 | ~TreeNode() { 30 | for (int i = 0; i < children.size(); i++) { 31 | delete children[i]; 32 | } 33 | } 34 | }; 35 | 36 | ************************************************************/ 37 | #include 38 | int sumOfNodes(TreeNode* root) { 39 | // Write your code here 40 | //corner case 41 | if(root == NULL) { 42 | return 0; 43 | } 44 | int sum = root -> data; 45 | 46 | for(int i = 0; i < root -> children.size(); i++) { 47 | sum += sumOfNodes(root -> children[i]); 48 | } 49 | return sum; 50 | } 51 | -------------------------------------------------------------------------------- /Lecture 18 : DP - 1/Code : Min Steps to 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a positive integer 'n', find and return the minimum number of steps that 'n' has to take to get reduced to 1. You can perform any one of the following 3 steps: 3 | 1.) Subtract 1 from it. (n = n - ­1) , 4 | 2.) If its divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) , 5 | 3.) If its divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ). 6 | Write brute-force recursive solution for this. 7 | Input format : 8 | The first and the only line of input contains an integer value, 'n'. 9 | Output format : 10 | Print the minimum number of steps. 11 | Constraints : 12 | 1 <= n <= 200 13 | 14 | Time Limit: 1 sec 15 | Sample Input 1 : 16 | 4 17 | Sample Output 1 : 18 | 2 19 | Explanation of Sample Output 1 : 20 | For n = 4 21 | Step 1 : n = 4 / 2 = 2 22 | Step 2 : n = 2 / 2 = 1 23 | Sample Input 2 : 24 | 7 25 | Sample Output 2 : 26 | 3 27 | Explanation of Sample Output 2 : 28 | For n = 7 29 | Step 1 : n = 7 ­- 1 = 6 30 | Step 2 : n = 6 / 3 = 2 31 | Step 3 : n = 2 / 2 = 1 32 | */ 33 | 34 | /* 35 | #include 36 | using namespace std; 37 | #include "solution.h" 38 | 39 | int main() 40 | { 41 | int n; 42 | cin >> n; 43 | cout << countMinStepsToOne(n); 44 | } 45 | */ 46 | 47 | int countMinStepsToOne(int n) 48 | { 49 | // Write your code here 50 | if (n == 1 || n == 0) 51 | return 0; 52 | int a = INT_MAX, b = INT_MAX, c = INT_MAX; 53 | if (n % 3 == 0) 54 | a = countMinStepsToOne(n / 3); 55 | if (n % 2 == 0) 56 | b = countMinStepsToOne(n / 2); 57 | c = countMinStepsToOne(n - 1); 58 | int ans = min(a, min(b, c)) + 1; 59 | return ans; 60 | } 61 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Check Palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, determine if it is a palindrome, considering only alphanumeric characters. 3 | Palindrome 4 | A palindrome is a word, number, phrase, or other sequences of characters which read the same backwards and forwards. 5 | Example: 6 | If the input string happens to be, "malayalam" then as we see that this word can be read the same as forward and backwards, it is said to be a valid palindrome. 7 | 8 | The expected output for this example will print, 'true'. 9 | From that being said, you are required to return a boolean value from the function that has been asked to implement. 10 | Input Format: 11 | The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. 12 | Output Format: 13 | The only line of output prints either 'true' or 'false'. 14 | Note: 15 | You are not required to print anything. It has already been taken care of. 16 | Constraints: 17 | 0 <= N <= 10^6 18 | Where N is the length of the input string. 19 | 20 | Time Limit: 1 second 21 | Sample Input 1 : 22 | abcdcba 23 | Sample Output 1 : 24 | true 25 | Sample Input 2: 26 | coding 27 | Sample Output 2: 28 | false 29 | */ 30 | bool checkPalindrome(char str[]) { 31 | // Write your code here 32 | int i = 0; 33 | // int j = str.length() - 1; 34 | int j = 0; 35 | while (str[j]) 36 | j++; 37 | j--; 38 | // cout << endl << i << endl << j; 39 | while(i<=j){ 40 | if(str[i]==str[j]){ 41 | i++; 42 | j--; 43 | } 44 | else 45 | return false; 46 | } 47 | return true; 48 | } 49 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/Coin Tower.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Whis and Beerus are playing a new game today. They form a tower of N coins and make a move in alternate turns. Beerus plays first. In one step, the player can remove either 1, X, or Y coins from the tower. The person to make the last move wins the game. Can you find out who wins the game? 3 | Input format : 4 | The first and the only line of input contains three integer values separated by a single space. They denote the value of N, X and Y, respectively. 5 | Output format : 6 | Prints the name of the winner, either 'Whis' or 'Beerus' (Without the quotes). 7 | Constraints : 8 | 1 <= N <= 10 ^ 6 9 | 2 <= X <= Y<= 50 10 | 11 | Time Limit: 1 sec 12 | Sample Input 1 : 13 | 4 2 3 14 | Sample Output 1 : 15 | Whis 16 | Sample Input 2 : 17 | 10 2 4 18 | Sample Output 2 : 19 | Beerus 20 | */ 21 | 22 | /* 23 | #include 24 | #include 25 | using namespace std; 26 | 27 | #include "solution.h" 28 | 29 | int main() 30 | { 31 | int n, x, y; 32 | cin >> n >> x >> y; 33 | cout << findWinner(n, x, y); 34 | } 35 | */ 36 | 37 | string findWinner(int n, int x, int y) 38 | { 39 | // Write your code here 40 | int *dp = new int[n + 1]; 41 | 42 | dp[0] = false; 43 | dp[1] = true; 44 | 45 | for (int i = 2; i <= n; i++) 46 | { 47 | if (i - 1 >= 0 and !dp[i - 1]) 48 | dp[i] = true; 49 | else if (i - x >= 0 and !dp[i - x]) 50 | dp[i] = true; 51 | else if (i - y >= 0 and !dp[i - y]) 52 | dp[i] = true; 53 | else 54 | dp[i] = false; 55 | } 56 | 57 | if (dp[n]) 58 | return "Beerus"; 59 | else 60 | return "Whis"; 61 | } 62 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Find Duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array. 3 | Note : 4 | Duplicate number is always present in the given array/list. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | Output Format : 12 | For each test case, print the duplicate element in the array/list. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^3 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 21 | 9 22 | 0 7 2 5 4 7 1 3 6 23 | Sample Output 1: 24 | 7 25 | Sample Input 2: 26 | 2 27 | 5 28 | 0 2 1 3 1 29 | 7 30 | 0 3 1 5 4 3 2 31 | Sample Output 2: 32 | 1 33 | 3 34 | */ 35 | int duplicateNumber(int *arr, int size) 36 | { 37 | //Write your code here 38 | int j; 39 | for(int i=0; idata = data; 36 | this->next = NULL; 37 | } 38 | }; 39 | 40 | *****************************************************************/ 41 | 42 | // int length(Node *head) { 43 | // // Write your code here 44 | // } 45 | int length(Node *head) { 46 | // Write your code here 47 | //corner case 48 | if(head == NULL) { 49 | return 0; 50 | } 51 | return length(head -> next) + 1; 52 | } 53 | // Time Complexity : O(n) 54 | // Auxillary Space : O(n) 55 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array with N elements, you need to find the length of the longest subsequence in the given array such that all elements of the subsequence are sorted in strictly increasing order. 3 | Input Format 4 | The first line of input contains an integer N. The following line contains N space separated integers, that denote the value of elements of array. 5 | Output Format 6 | The first and only line of output contains the length of longest subsequence. 7 | Constraints 8 | 1 <= N <= 10^3 9 | Time Limit: 1 second 10 | Sample Input 1 : 11 | 6 12 | 5 4 11 1 16 8 13 | Sample Output 1 : 14 | 3 15 | Sample Output Explanation 16 | Length of longest subsequence is 3 i.e. (5,11,16) or (4,11,16). 17 | Sample Input 2 : 18 | 3 19 | 1 2 2 20 | Sample Output 2 : 21 | 2 22 | */ 23 | 24 | /* 25 | #include 26 | using namespace std; 27 | 28 | #include "solution.h" 29 | 30 | int main() { 31 | int n; 32 | cin >> n; 33 | int* arr = new int[n]; 34 | 35 | for (int i = 0; i < n; i++) { 36 | cin >> arr[i]; 37 | } 38 | 39 | cout << longestIncreasingSubsequence(arr, n); 40 | } 41 | */ 42 | 43 | int longestIncreasingSubsequence(int *arr, int n) 44 | { 45 | // Write your code here 46 | int *output = new int[n]; 47 | output[0] = 1; 48 | for (int i = 1; i < n; i++) 49 | { 50 | int ans = 0; 51 | for (int j = i - 1; j >= 0; j--) 52 | { 53 | if (arr[j] < arr[i]) 54 | ans = max(output[j], ans); 55 | } 56 | output[i] = ans + 1; 57 | } 58 | int res = 0; 59 | for (int i = 0; i < n; i++) 60 | res = max(output[i], res); 61 | return res; 62 | } 63 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/Code : Edit Distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two strings s and t of lengths m and n respectively, find the edit distance between the strings. 3 | Edit Distance 4 | Edit Distance of two strings is minimum number of operations required to make one string equal to other. In order to do so you can perform any of the following three operations only : 5 | 1. Delete a character 6 | 2. Replace a character with another one 7 | 3. Insert a character 8 | Note 9 | Strings don't contain spaces 10 | Input Format : 11 | The first line of input contains a string, that denotes the value of s. The following line contains a string, that denotes the value of t. 12 | Output Format : 13 | The first and only line of output contains the edit distance value between the given strings. 14 | Constraints : 15 | 1<= m,n <= 10 16 | Time Limit: 1 second 17 | Sample Input 1 : 18 | abc 19 | dc 20 | Sample Output 1 : 21 | 2 22 | */ 23 | 24 | /* 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | #include "solution.h" 30 | 31 | int main() { 32 | string s1; 33 | string s2; 34 | 35 | cin >> s1; 36 | cin >> s2; 37 | 38 | cout << editDistance(s1, s2); 39 | } 40 | */ 41 | 42 | int editDistance(string s1, string s2) 43 | { 44 | // Write your code here 45 | if (s1.size() == 0 || s2.size() == 0) 46 | return max(s1.size(), s2.size()); 47 | 48 | if (s1[0] == s2[0]) 49 | return editDistance(s1.substr(1), s2.substr(1)); 50 | else 51 | { 52 | int x = editDistance(s1.substr(1), s2) + 1; 53 | int y = editDistance(s1, s2.substr(1)) + 1; 54 | int z = editDistance(s1.substr(1), s2.substr(1)) + 1; 55 | return min(x, min(y, z)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Reverse Each Word.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Aadil has been provided with a sentence in the form of a string as a function parameter. The task is to implement a function so as to print the sentence such that each word in the sentence is reversed. 3 | Example: 4 | Input Sentence: "Hello, I am Aadil!" 5 | The expected output will print, ",olleH I ma !lidaA". 6 | Input Format: 7 | The first and only line of input contains a string without any leading and trailing spaces. The input string represents the sentence given to Aadil. 8 | Output Format: 9 | The only line of output prints the sentence(string) such that each word in the sentence is reversed. 10 | Constraints: 11 | 0 <= N <= 10^6 12 | Where N is the length of the input string. 13 | 14 | Time Limit: 1 second 15 | Sample Input 1: 16 | Welcome to Coding Ninjas 17 | Sample Output 1: 18 | emocleW ot gnidoC sajniN 19 | Sample Input 2: 20 | Always indent your code 21 | Sample Output 2: 22 | syawlA tnedni ruoy edoc 23 | */ 24 | 25 | #include 26 | void reverseEachWord(char input[]) { 27 | int len = 0; 28 | while(input[len] != '\0'){ 29 | len++; 30 | } 31 | int start, end, i, j=0; 32 | for(int i=0;i= len) 46 | break; 47 | i=j; 48 | j++; 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Test 1(Milestone 3)/Maximum Profit on App.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have made a smartphone app and want to set its subscription price such that the profit earned is maximised. There are certain users who will subscribe to your app only if their budget is greater than or equal to your price. 3 | You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn. 4 | Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you can earn is : 5 | m * x 6 | where m is total number of subscribers whose budget is greater than or equal to x. 7 | Input format : 8 | Line 1 : N (No. of subscribers) 9 | Line 2 : Budget of subscribers (separated by space) 10 | Output Format : 11 | Maximum profit 12 | Constraints : 13 | 1 <= N <= 10^6 14 | 1 <=budget[i]<=9999 15 | Sample Input 1 : 16 | 4 17 | 30 20 53 14 18 | Sample Output 1 : 19 | 60 20 | Sample Output 1 Explanation : 21 | Price of your app should be Rs. 20 or Rs. 30. For both prices, you can get the profit Rs. 60. 22 | Sample Input 2 : 23 | 5 24 | 34 78 90 15 67 25 | Sample Output 2 : 26 | 201 27 | Sample Output 2 Explanation : 28 | Price of your app should be Rs. 67. You can get the profit Rs. 201 (i.e. 3 * 67). 29 | */ 30 | 31 | #include 32 | 33 | int maximumProfit(int budget[], int n) { 34 | // Write your code here 35 | sort(budget, budget + n); 36 | 37 | int *arr = new int[n]; 38 | 39 | for(int i = 0; i < n; i++) { 40 | arr[i] = budget[i] * (n - i); 41 | } 42 | 43 | int max_profit = 0; 44 | 45 | for(int i = 0; i < n; i++) { 46 | max_profit = max(arr[i], max_profit); 47 | } 48 | 49 | return max_profit; 50 | } 51 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Return all codes - String.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric string S. Write a program to return the list of all possible codes that can be generated from the given string. 3 | Note : The order of codes are not important. And input string does not contain 0s. 4 | Input format : 5 | A numeric string 6 | Constraints : 7 | 1 <= Length of String S <= 10 8 | Sample Input: 9 | 1123 10 | Sample Output: 11 | aabc 12 | kbc 13 | alc 14 | aaw 15 | kw 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | 21 | void help(string input,string out,vector &str){ 22 | if(input.size()==0){ 23 | str.push_back(out); 24 | return; 25 | } 26 | char c1=(input[0]-48)+96; 27 | 28 | //Ignore the output coming from zero in a string 29 | if(input[0]=='0') 30 | return; 31 | 32 | help(input.substr(1),out+c1,str); 33 | 34 | if(input.size()>1){ 35 | int d=(input[0]-48)*10+(input[1]-48); 36 | if(d>26) 37 | return; 38 | char c2=96+d; 39 | help(input.substr(2),out+c2,str); 40 | } 41 | 42 | } 43 | 44 | 45 | int getCodes(string input, string output[10000]) { 46 | /* 47 | You are given the input text and output string array. Find all possible codes and store in the output string array. Don’t print the codes. 48 | Also, return the number of codes return to the output string. You do not need to print anything. 49 | */ 50 | vector str; 51 | string out=""; 52 | help(input,out,str); 53 | 54 | for(int i=0;i 48 | int pairSum(int *input, int size, int x) 49 | { 50 | //Write your code here 51 | int pairs = 0; 52 | int j; 53 | for(int i=0; i 21 | class TreeNode { 22 | public: 23 | T data; 24 | vector*> children; 25 | 26 | TreeNode(T data) { 27 | this->data = data; 28 | } 29 | 30 | ~TreeNode() { 31 | for (int i = 0; i < children.size(); i++) { 32 | delete children[i]; 33 | } 34 | } 35 | }; 36 | 37 | ************************************************************/ 38 | 39 | 40 | void printPostOrder(TreeNode* root) { 41 | // Write your code here 42 | //corner case 43 | if(root == NULL) { 44 | return; 45 | } 46 | 47 | for(int i = 0; i < root -> children.size() ; i++) { 48 | printPostOrder(root -> children[i]); 49 | } 50 | 51 | cout << root -> data << " "; 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Find Unique.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. 3 | Now, in the given array/list, 'M' numbers are present twice and one number is present only once. 4 | You need to find and return that number which is unique in the array/list. 5 | Note: 6 | Unique element is always present in the array/list according to the given condition. 7 | Input format : 8 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 9 | 10 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 11 | 12 | Second line contains 'N' single space separated integers representing the elements in the array/list. 13 | Output Format : 14 | For each test case, print the unique element present in the array. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 10^2 19 | 0 <= N <= 10^3 20 | Time Limit: 1 sec 21 | Sample Input 1: 22 | 1 23 | 7 24 | 2 3 1 6 3 6 2 25 | Sample Output 1: 26 | 1 27 | Sample Input 2: 28 | 2 29 | 5 30 | 2 4 7 2 7 31 | 9 32 | 1 3 1 3 6 6 7 10 7 33 | Sample Output 2: 34 | 4 35 | 10 36 | */ 37 | int findUnique(int *arr, int size) 38 | { 39 | //Write your code here 40 | int j; 41 | for(int i=0; i= len) 45 | break; 46 | } 47 | end=i-1; 48 | while(start= len) 58 | break; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Code : Count leaf nodes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a generic tree, count and return the number of leaf nodes present in the given tree. 3 | Input format : 4 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 5 | Output Format : 6 | The first and only line of output prints the count of leaf nodes present in the given tree. 7 | Constraints: 8 | Time Limit: 1 sec 9 | Sample Input 1 : 10 | 10 3 20 30 40 2 40 50 0 0 0 0 11 | Sample Output 1 : 12 | 4 13 | */ 14 | 15 | /************************************************************ 16 | 17 | Following is the structure for the TreeNode class 18 | 19 | template 20 | class TreeNode { 21 | public: 22 | T data; 23 | vector*> children; 24 | 25 | TreeNode(T data) { 26 | this->data = data; 27 | } 28 | 29 | ~TreeNode() { 30 | for (int i = 0; i < children.size(); i++) { 31 | delete children[i]; 32 | } 33 | } 34 | }; 35 | 36 | ************************************************************/ 37 | #include 38 | 39 | int getLeafNodeCount(TreeNode* root) { 40 | // Write your code here 41 | //corner case 42 | if(root == NULL) { 43 | return 0; 44 | } 45 | 46 | //base case 47 | if(root -> children.size() == 0) { 48 | return 1; 49 | } 50 | 51 | int count = 0 ; 52 | for(int i = 0; i < root -> children.size() ; i++) { 53 | count += getLeafNodeCount(root -> children[i]); 54 | } 55 | 56 | return count; 57 | } 58 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Arrange Numbers in Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an empty array(ARR) and its size N. The only input taken from the user will be N and you need not worry about the array. 3 | Your task is to populate the array using the integer values in the range 1 to N(both inclusive) in the order - 1,3,5,.......,6,4,2. 4 | Note: 5 | You need not print the array. You only need to populate it. 6 | Input Format : 7 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 8 | 9 | The first and the only line of each test case or query contains an integer 'N'. 10 | Output Format : 11 | For each test case, print the elements of the array/list separated by a single space. 12 | 13 | Output for every test case will be printed in a separate line. 14 | Constraints : 15 | 1 <= t <= 10^2 16 | 0 <= N <= 10^4 17 | 18 | Time Limit: 1sec 19 | Sample Input 1 : 20 | 1 21 | 6 22 | Sample Output 1 : 23 | 1 3 5 6 4 2 24 | Explanation of Sample Input 1 : 25 | Since the value of N is 6, the number will be stored in the array in such a fashion that 1 will appear at 0th index, then 2 at the last index, in a similar fashion 3 is stored at index 1. Hence the array becomes 1 3 5 6 4 2. 26 | Sample Input 2 : 27 | 2 28 | 9 29 | 3 30 | Sample Output 2 : 31 | 1 3 5 7 9 8 6 4 2 32 | 1 3 2 33 | */ 34 | void arrange(int *arr, int n) 35 | { 36 | //Write your code here 37 | if(n%2==0){ 38 | for(int i=0;i 22 | class TreeNode { 23 | public: 24 | T data; 25 | vector*> children; 26 | 27 | TreeNode(T data) { 28 | this->data = data; 29 | } 30 | 31 | ~TreeNode() { 32 | for (int i = 0; i < children.size(); i++) { 33 | delete children[i]; 34 | } 35 | } 36 | }; 37 | 38 | ************************************************************/ 39 | 40 | void replaceWithDepthValue(TreeNode* root, int level = 0) { 41 | // Write your code here 42 | //corner case 43 | if(root == NULL) { 44 | return; 45 | } 46 | 47 | root -> data = level; 48 | 49 | for(int i = 0; i < root -> children.size(); i++) { 50 | replaceWithDepthValue(root -> children[i], level + 1); 51 | } 52 | return; 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/All possible ways.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two integers a and b. You need to find and return the count of possible ways in which we can represent the number a as the sum of unique integers raise to the power b. 3 | For example: if a = 10 and b = 2, only way to represent 10 as sum of unique integers raised to power 2 is- 4 | 10 = 1^2 + 3^2 5 | Hence, answer is 1. 6 | Note : x^y represents x raise to the power y 7 | Input Format: 8 | The first line of input contains two space separated integers, that denote the value of a and b. 9 | Output Format: 10 | The first and only line of output contains count of ways in which a can be represented as sum of unique integers raised to power b. 11 | Constraints : 12 | 1 <= a <= 10^4 13 | 1 <= b <= 20 14 | Time Limit: 1 second 15 | Sample Input 1 : 16 | 10 2 17 | Sample Output 1 : 18 | 1 19 | Sample Input 2 : 20 | 100 2 21 | Sample Output 2 : 22 | 3 23 | Explanation: 24 | Following are the three ways: 25 | 1. 100 = 10^2 26 | 2. 100 = 8^2 + 6^2 27 | 3. 100 = 7^2+5^2+4^2+3^2+1^2 28 | */ 29 | 30 | /* 31 | #include 32 | using namespace std; 33 | #include "solution.h" 34 | 35 | int main() 36 | { 37 | int a, b; 38 | cin >> a >> b; 39 | cout << getAllWays(a, b); 40 | } 41 | */ 42 | 43 | #include 44 | int getAllWays(int a, int b, int base, int *output) 45 | { 46 | if (a < pow(base, b)) 47 | return 0; 48 | if (a == pow(base, b)) 49 | return 1; 50 | 51 | output[a] = getAllWays(a - pow(base, b), b, base + 1, output) + getAllWays(a, b, base + 1, output); 52 | 53 | return output[a]; 54 | } 55 | 56 | int getAllWays(int a, int b) 57 | { 58 | // Write your code here 59 | int *output = new int[10000]; 60 | for (int i = 0; i < 10000; i++) 61 | output[i] = -1; 62 | return getAllWays(a, b, 1, output); 63 | } 64 | -------------------------------------------------------------------------------- /Lecture 7: Functions/Fahrenheit to Celsius Table.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), you need to convert all Fahrenheit values from Start to End at the gap of W, into their corresponding Celsius values and print the table. 3 | Input Format : 4 | 3 integers - S, E and W respectively 5 | Output Format : 6 | Fahrenheit to Celsius conversion table. One line for every Fahrenheit and Celsius Fahrenheit value. Fahrenheit value and its corresponding Celsius value should be separate by tab ("\t") 7 | Constraints : 8 | 0 <= S <= 1000 9 | 0 <= E <= 1000 10 | 0 <= W <= 1000 11 | Sample Input 1: 12 | 0 13 | 100 14 | 20 15 | Sample Output 1: 16 | 0 -17 17 | 20 -6 18 | 40 4 19 | 60 15 20 | 80 26 21 | 100 37 22 | Sample Input 2: 23 | 120 24 | 200 25 | 40 26 | Sample Output 2: 27 | 120 48 28 | 160 71 29 | 200 93 30 | Explanation for Sample Output 2 : 31 | Start value is 120, end value is 200 and step size is 40. Therefore, the values we need to convert are 120, 120 + 40 = 160, and 160 + 40 = 200. 32 | The formula for converting Fahrenheit to Celsius is: 33 | Celsius Value = (5/9)*(Fahrenheit Value - 32) 34 | Plugging 120 into the formula, the celsius value will be (5 / 9)*(120 - 32) => (5 / 9) * 88 => (5 * 88) / 9 => 440 / 9 => 48.88 35 | But we'll only print 48 because we are only interested in the integral part of the value. 36 | */ 37 | void printTable(int start, int end, int step) { 38 | /* Don't write main(). 39 | * Don't read input, it is passed as function argument. 40 | * Print output and don't return it. 41 | * Taking input is handled automatically. 42 | */ 43 | int F=start, C; 44 | while(F<=end){ 45 | C = 5.0/9 * (F-32); 46 | cout << F << "\t" << C << endl; 47 | F += step; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Merge Sort Code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Sort an array A using Merge Sort. 3 | Change in the input array itself. So no need to return or print anything. 4 | Input format : 5 | Line 1 : Integer n i.e. Array size 6 | Line 2 : Array elements (separated by space) 7 | Output format : 8 | Array elements in increasing order (separated by space) 9 | Constraints : 10 | 1 <= n <= 10^3 11 | Sample Input 1 : 12 | 6 13 | 2 6 8 5 4 3 14 | Sample Output 1 : 15 | 2 3 4 5 6 8 16 | Sample Input 2 : 17 | 5 18 | 2 1 5 2 3 19 | Sample Output 2 : 20 | 1 2 2 3 5 21 | */ 22 | 23 | #include 24 | 25 | 26 | void merge(int a[] , int si ,int ei) 27 | { 28 | int size = ei-si+1; 29 | int mid=(si+ei)/2; 30 | 31 | int* out = new int[size]; 32 | int i = si; 33 | int j = mid+1; 34 | int k = 0; 35 | while(i<=mid && j<=ei){ 36 | if(a[i]=ei){ 72 | return; 73 | } 74 | 75 | int mid = (si+ei) /2; 76 | mergeSort2(a,si,mid); 77 | mergeSort2(a,mid+1,ei); 78 | merge(a,si,ei); 79 | 80 | } 81 | void mergeSort(int a[], int n){ 82 | 83 | int si = 0; 84 | int ei= n-1; 85 | 86 | mergeSort2(a , si , ei); 87 | 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /Lecture 8 : Linked List 1/Length of LL.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given singly linked list of integers, find and return its length. Do it using an iterative method. 3 | Input format : 4 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 5 | 6 | First and the only line of each test case or query contains elements of the singly linked list separated by a single space. 7 | Remember/Consider : 8 | While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. 9 | Output format : 10 | For each test case, print the length of the linked list. 11 | 12 | Output for every test case will be printed in a separate line. 13 | Constraints : 14 | 1 <= t <= 10^2 15 | 0 <= N <= 10^5 16 | Time Limit: 1 sec 17 | Sample Input 1 : 18 | 1 19 | 3 4 5 2 6 1 9 -1 20 | Sample Output 1 : 21 | 7 22 | Sample Input 2 : 23 | 2 24 | 10 76 39 -3 2 9 -23 9 -1 25 | -1 26 | Sample Output 2 : 27 | 8 28 | 0 29 | */ 30 | 31 | /**************************************************************** 32 | 33 | Following is the class structure of the Node class: 34 | 35 | class Node 36 | { 37 | public: 38 | int data; 39 | Node *next; 40 | Node(int data) 41 | { 42 | this->data = data; 43 | this->next = NULL; 44 | } 45 | }; 46 | 47 | *****************************************************************/ 48 | 49 | // int length(Node *head) 50 | // { 51 | // //Write your code here 52 | // } 53 | int length(Node *head) { 54 | int count = 0; 55 | 56 | while(head) { 57 | count++; 58 | head = head -> next; 59 | } 60 | 61 | return count; 62 | } 63 | 64 | // Time Complexity : O(n) 65 | // Auxillary Space : O(1) 66 | -------------------------------------------------------------------------------- /Test 2 (Milestone 3)/Next Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a large number represented in the form of a linked list. Write code to increment the number by 1 in-place(i.e. without using extra space). 3 | Note: You don't need to print the elements, just update the elements and return the head of updated LL. 4 | Input Constraints: 5 | 1 <= Length of Linked List <=10^6. 6 | Input format : 7 | Line 1 : Linked list elements (separated by space and terminated by -1) 8 | Output Format : 9 | Line 1: Updated linked list elements 10 | Sample Input 1 : 11 | 3 9 2 5 -1 12 | Sample Output 1 : 13 | 3 9 2 6 14 | Sample Input 2 : 15 | 9 9 9 -1 16 | Sample Output 1 : 17 | 1 0 0 0 18 | */ 19 | 20 | /********** 21 | * Following is the Node class that is already written. 22 | 23 | class Node{ 24 | public: 25 | int data; 26 | Node *next; 27 | Node(int data){ 28 | this -> data = data; 29 | this -> next = NULL; 30 | } 31 | }; 32 | 33 | *********/ 34 | 35 | Node* helper(Node *head) { 36 | 37 | //base case -> increment the last number by one 38 | if(head -> next == NULL){ 39 | head -> data++; 40 | return head; 41 | } 42 | 43 | head -> next = helper(head -> next); 44 | 45 | if(head -> next -> data > 9) { 46 | head -> next -> data = 0; 47 | head -> data += 1; 48 | } 49 | 50 | return head; 51 | } 52 | 53 | Node* NextLargeNumber(Node *head) { 54 | // recursion will incerement the number accordingly 55 | head = helper(head); 56 | 57 | //if the first element is > 9 then we set its value to 0 and add a new node with value 1 to it 58 | if(head -> data > 9) { 59 | Node *newNode = new Node(1); 60 | head -> data = 0; 61 | newNode -> next = head; 62 | head = newNode; 63 | } 64 | return head; 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Print All Substrings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given input string(str), write a function to print all the possible substrings. 3 | Substring 4 | A substring is a contiguous sequence of characters within a string. 5 | Example: "cod" is a substring of "coding". Whereas, "cdng" is not as the characters taken are not contiguous 6 | Input Format: 7 | The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. 8 | Output Format: 9 | Print the total number of substrings possible, where every substring is printed on a single line and hence the total number of output lines will be equal to the total number of substrings. 10 | Note: 11 | The order in which the substrings are printed, does not matter. 12 | Constraints: 13 | 0 <= N <= 10^6 14 | Where N is the length of the input string. 15 | 16 | Time Limit: 1 second 17 | Sample Input 1: 18 | abc 19 | Sample Output 1: 20 | a 21 | ab 22 | abc 23 | b 24 | bc 25 | c 26 | Sample Input 2: 27 | co 28 | Sample Output 2: 29 | c 30 | co 31 | o 32 | */ 33 | void printSubstrings(char input[]) { 34 | // outermost for loop 35 | // this is for the selection 36 | // of starting point 37 | for (int i = 0; input[i]; i++) { 38 | 39 | // 2nd for loop is for selection 40 | // of ending point 41 | for (int j = i; input[j]; j++) { 42 | 43 | // 3rd loop is for printing from 44 | // starting point to ending point 45 | for (int k = i; k <= j; k++) { 46 | cout << input[k]; 47 | } 48 | 49 | // changing the line after printing 50 | // from starting point to ending point 51 | cout << endl; 52 | } 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Triplet Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) and a number X. Find and return the number of triplets in the array/list which sum to X. 3 | Note : 4 | Given array/list can contain duplicate elements. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the first array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | 12 | Third line contains an integer 'X'. 13 | Output format : 14 | For each test case, print the total number of triplets present in the array/list. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 50 19 | 0 <= N <= 10^2 20 | 0 <= X <= 10^9 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 1 24 | 7 25 | 1 2 3 4 5 6 7 26 | 12 27 | Sample Output 1: 28 | 5 29 | Sample Input 2: 30 | 2 31 | 7 32 | 1 2 3 4 5 6 7 33 | 19 34 | 9 35 | 2 -5 8 -6 0 5 10 11 -3 36 | 10 37 | Sample Output 2: 38 | 0 39 | 5 40 | 41 | 42 | Explanation for Input 2: 43 | Since there doesn't exist any triplet with sum equal to 19 for the first query, we print 0. 44 | 45 | For the second query, we have 5 triplets in total that sum up to 10. They are, (2, 8, 0), (2, 11, -3), (-5, 5, 10), (8, 5, -3) and (-6, 5, 11) 46 | */ 47 | int tripletSum(int *input, int size, int x) 48 | { 49 | //Write your code here 50 | int triplet = 0; 51 | for(int i=0; i on each line. Now add 13 to Fahrenheit Value at each step until you 40 | reach 119 in this case. You may or may not exactly land on the end value depending on the steps you are taking. 41 | */ 42 | #include 43 | using namespace std; 44 | 45 | 46 | int main(){ 47 | 48 | /* Read input as specified in the question. 49 | * Print output as specified in the question. 50 | */int s,e,w,C,F; 51 | cin >> s >> e >> w; 52 | F=s; 53 | while(F<=e){ 54 | C = 5.0/9 * (F-32); 55 | cout << F << " " << C << endl; 56 | F += w; 57 | } 58 | 59 | 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /Lecture 5 : Time and Space Complexity Analysis/Duplicate in array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3, and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array. 3 | Note : 4 | Duplicate number is always present in the given array/list. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | Output Format : 12 | For each test case, print the duplicate element in the array/list. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^6 18 | 19 | Time Limit: 1 sec 20 | Sample Input 1: 21 | 1 22 | 9 23 | 0 7 2 5 4 7 1 3 6 24 | Sample Output 1: 25 | 7 26 | Sample Input 2: 27 | 2 28 | 5 29 | 0 2 1 3 1 30 | 7 31 | 0 3 1 5 4 3 2 32 | Sample Output 2: 33 | 1 34 | 3 35 | */ 36 | 37 | // int findDuplicate(int *arr, int n) 38 | // { 39 | // //Write your code here 40 | // } 41 | int findDuplicate(int *arr, int n) { 42 | //Write your code here 43 | //we know range of intergers is from 0 to n- 2 44 | int xor1 = 0; 45 | for(int i = 0 ; i <= n - 2; i++) { 46 | xor1 ^= i; 47 | } 48 | 49 | int xor2 = 0; 50 | for(int i = 0 ; i < n; i++) { 51 | xor2 ^= arr[i]; 52 | } 53 | 54 | return xor1 ^ xor2; 55 | } 56 | 57 | // Time Complexiity : O(n) 58 | // Space COmplexity : O(1) 59 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Linear Search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N, and an integer X. You need to search for the integer X in the given array/list using 'Linear Search'. 3 | You have been required to return the index at which X is present in the array/list. If X has multiple occurrences in the array/list, then you need to return the index at which the first occurrence of X would be encountered. In case X is not present in the array/list, then return -1. 4 | 'Linear search' is a method for finding an element within an array/list. It sequentially checks each element of the array/list until a match is found or the whole array/list has been searched. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | 12 | Third line contains the value of X(integer to be searched in the given array/list) 13 | Output format : 14 | For each test case, print the index at which X is present or -1, otherwise. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 10^2 19 | 0 <= N <= 10^5 20 | -2 ^ 31 <= X <= (2 ^ 31) - 1 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 1 24 | 7 25 | 2 13 4 1 3 6 28 26 | 3 27 | Sample Output 1: 28 | 4 29 | Sample Input 2: 30 | 2 31 | 7 32 | 2 13 4 1 3 6 28 33 | 9 34 | 5 35 | 7 8 5 9 5 36 | 5 37 | Sample Output 2: 38 | -1 39 | 2 40 | */ 41 | int linearSearch(int *arr, int n, int x) 42 | { 43 | //Write your code here 44 | for(int i=0;i 26 | using namespace std; 27 | 28 | #include "solution.h" 29 | 30 | int main() { 31 | int n; 32 | cin >> n; 33 | 34 | int* arr = new int[n]; 35 | 36 | for (int i = 0; i < n; ++i) { 37 | cin >> arr[i]; 38 | } 39 | 40 | cout << highestFrequency(arr, n); 41 | 42 | delete[] arr; 43 | } 44 | */ 45 | 46 | #include 47 | int highestFrequency(int arr[], int n) 48 | { 49 | // Write your code here 50 | unordered_map m; 51 | int index = 0, max = 0; 52 | for (int i = n - 1; i >= 0; i--) 53 | { 54 | if (m.count(arr[i]) == 0) 55 | m[arr[i]] = 1; 56 | else 57 | { 58 | m[arr[i]] += 1; 59 | if (max <= m[arr[i]]) 60 | { 61 | max = m[arr[i]]; 62 | index = i; 63 | } 64 | } 65 | } 66 | return arr[index]; 67 | } 68 | -------------------------------------------------------------------------------- /Lecture 8: Arrays/Sort 0 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list. 3 | Note: 4 | You need to change in the given array/list itself. Hence, no need to return or print anything. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers(all 0s and 1s) representing the elements in the array/list. 11 | Output format : 12 | For each test case, print the sorted array/list elements in a row separated by a single space. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^5 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 21 | 7 22 | 0 1 1 0 1 0 1 23 | Sample Output 1: 24 | 0 0 0 1 1 1 1 25 | Sample Input 2: 26 | 2 27 | 8 28 | 1 0 1 1 0 1 0 1 29 | 5 30 | 0 1 0 1 0 31 | Sample Output 2: 32 | 0 0 0 1 1 1 1 1 33 | 0 0 0 1 1 34 | */ 35 | void sortZeroesAndOne(int *input, int size) 36 | { 37 | //Write your code here 38 | int i = 0, temp; 39 | int j = size - 1; 40 | 41 | while(i < j) // pointer i moves left to right and j moves right to left 42 | { 43 | if(input[i] == 1) // i found a '1' 44 | { 45 | temp = input[j]; 46 | input[j] = input[i]; 47 | input[i] = temp; // the '1' got pushed to the right 48 | j--; // j collects all the '1's to the right 49 | } 50 | else // i = 0 51 | i++; // move forward and find '1' 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 10 : Stacks & Queues/Reverse Queue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse Queue 3 | You have been given a queue that can store integers as the data. You are required to write a function that reverses the queue. 4 | 5 | Input Format: 6 | The first list of input contains an integer 't' denoting the number of test cases/queries to be run. 7 | Then the test cases follow. 8 | The first line input for each test case/query contains an integer N, denoting the total number of elements in the queue. 9 | The second line of input contains N integers separated by a single space, representing the order in which the elements are enqueued into the queue. 10 | 11 | Output Format: 12 | For each test case/query, the only line of output prints the order in which the queue elements are dequeued, all of them separated by a single space. 13 | Output for every test case/query will be printed on a new line. 14 | 15 | Note: You are not required to print the expected output explicitly, it has already been taken care of. Just make the changes in the input queue itself. 16 | 17 | Constraints: 18 | 1 <= t <= 100 19 | 1 <= N <= 10^4 20 | -2^31 <= data <= 2^31 - 1 21 | 22 | Time Limit: 1sec 23 | 24 | Sample Input 1: 25 | 1 26 | 6 27 | 1 2 3 4 5 10 28 | 29 | Note: Here, 1 is at the front and 10 is at the rear of the queue. 30 | 31 | Sample Output 1: 32 | 10 5 4 3 2 1 33 | 34 | 35 | Sample Input 2: 36 | 2 37 | 5 38 | 2 8 15 1 10 39 | 3 40 | 10 20 30 41 | 42 | Sample Output 2: 43 | 10 1 15 8 2 44 | 30 20 10 45 | */ 46 | 47 | #include 48 | #include 49 | 50 | void reverseQueue(queue &input) { 51 | // Write your code here 52 | stack helper; 53 | 54 | while(!input.empty()) { 55 | helper.push(input.front()); 56 | input.pop(); 57 | } 58 | while(!helper.empty()) { 59 | input.push(helper.top()); 60 | helper.pop(); 61 | } 62 | return; 63 | } 64 | -------------------------------------------------------------------------------- /Lecture 12 : Binary Trees/Code : Mirror.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given Binary Tree of type integer, update it with its corresponding mirror image. 3 | 4 | Input Format: 5 | The first and the only line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data. 6 | Output Format: 7 | The only line of output prints the mirrored tree in a level-wise order. 8 | Each level will be printed on a new line. Elements printed at each level will be separated by a single line. 9 | Note: 10 | You are not required to print anything explicitly. It has already been taken care of. 11 | Constraints: 12 | 1 <= N <= 10^5 13 | Where N is the total number of nodes in the binary tree. 14 | 15 | Time Limit: 1 sec 16 | Sample Input 1: 17 | 1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1 18 | Sample Output 1: 19 | 1 20 | 3 2 21 | 7 6 5 4 22 | Sample Input 2: 23 | 5 10 6 2 3 -1 -1 -1 -1 -1 9 -1 -1 24 | Sample Output 2: 25 | 5 26 | 6 10 27 | 3 2 28 | 9 29 | */ 30 | 31 | /********************************************************** 32 | Following is the Binary Tree Node class structure 33 | 34 | template 35 | class BinaryTreeNode { 36 | public : 37 | T data; 38 | BinaryTreeNode *left; 39 | BinaryTreeNode *right; 40 | 41 | BinaryTreeNode(T data) { 42 | this -> data = data; 43 | left = NULL; 44 | right = NULL; 45 | } 46 | }; 47 | 48 | ***********************************************************/ 49 | 50 | void mirrorBinaryTree(BinaryTreeNode* root) { 51 | // Write your code here 52 | //corner case 53 | if(root == NULL) { 54 | return; 55 | } 56 | 57 | mirrorBinaryTree(root -> left); 58 | mirrorBinaryTree(root -> right); 59 | 60 | swap(root -> left, root -> right); 61 | 62 | return; 63 | } 64 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Code : Max data node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a generic tree, find and return the node with maximum data. You need to return the node which is having maximum data. 3 | Return null if tree is empty. 4 | Input format : 5 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 6 | Output Format : 7 | The first and only line of output contains the data of the node with largest data in the given tree. 8 | Constraints: 9 | Time Limit: 1 sec 10 | Sample Input 1: 11 | 10 3 20 30 40 2 40 50 0 0 0 0 12 | Sample Output 1: 13 | 50 14 | */ 15 | 16 | /************************************************************ 17 | 18 | Following is the structure for the TreeNode class 19 | 20 | template 21 | class TreeNode { 22 | public: 23 | T data; 24 | vector*> children; 25 | 26 | TreeNode(T data) { 27 | this->data = data; 28 | } 29 | 30 | ~TreeNode() { 31 | for (int i = 0; i < children.size(); i++) { 32 | delete children[i]; 33 | } 34 | } 35 | }; 36 | 37 | ************************************************************/ 38 | #include 39 | #include 40 | 41 | TreeNode* maxDataNode(TreeNode* root) { 42 | // Write your code here 43 | //corner case 44 | if(root == NULL){ 45 | return root; 46 | } 47 | 48 | TreeNode *maximum = root; 49 | 50 | for(int i = 0; i < root -> children.size() ;i++) { 51 | TreeNode *temp = maxDataNode(root -> children[i]); 52 | if(temp -> data > maximum -> data) { 53 | maximum = temp; 54 | } 55 | } 56 | return maximum; 57 | } 58 | -------------------------------------------------------------------------------- /Lecture 18 : DP - 1/Code : Minimum Count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer N, find and return the count of minimum numbers required to represent N as a sum of squares. 3 | That is, if N is 4, then we can represent it as : {1^2 + 1^2 + 1^2 + 1^2} and {2^2}. The output will be 1, as 1 is the minimum count of numbers required to represent N as sum of squares. 4 | Input format : 5 | The first and the only line of input contains an integer value, 'N'. 6 | Output format : 7 | Print the minimum count of numbers required. 8 | Constraints : 9 | 0 <= n <= 10 ^ 4 10 | 11 | Time Limit: 1 sec 12 | Sample Input 1 : 13 | 12 14 | Sample Output 1 : 15 | 3 16 | Explanation of Sample Output 1 : 17 | 12 can be represented as : 18 | A) (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) 19 | 20 | B) (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (1^2) + (2 ^ 2) 21 | 22 | C) (1^2) + (1^2) + (1^2) + (1^2) + (2 ^ 2) + (2 ^ 2) 23 | 24 | D) (2 ^ 2) + (2 ^ 2) + (2 ^ 2) 25 | 26 | As we can see, the output should be 3. 27 | Sample Input 2 : 28 | 9 29 | Sample Output 2 : 30 | 1 31 | */ 32 | 33 | /* 34 | #include 35 | using namespace std; 36 | 37 | #include "solution.h" 38 | 39 | int main() 40 | { 41 | int n; 42 | cin >> n; 43 | cout << minCount(n); 44 | } 45 | */ 46 | 47 | int minCount(int n, int *ans) 48 | { 49 | if (sqrt(n) - floor(sqrt(n)) == 0) 50 | return 1; 51 | if (n == 0 || n == 1 || n == 2 || n == 3) 52 | return n; 53 | if (ans[n] != -1) 54 | return ans[n]; 55 | int val = n; 56 | for (int i = 1; (i * i) <= n; i++) 57 | ans[n] = val = min(val, 1 + minCount(n - i * i, ans)); 58 | return ans[n]; 59 | } 60 | 61 | int minCount(int n) 62 | { 63 | // Write your code here 64 | int *ans = new int[n + 1]; 65 | for (int i = 0; i <= n; i++) 66 | ans[i] = -1; 67 | return minCount(n, ans); 68 | } 69 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Count nodes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a tree and an integer x, find and return the number of nodes which contains data greater than x. 3 | Input format: 4 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 5 | The following line contains an integer, that denotes the value of x. 6 | Output Format : 7 | The first and only line of output prints the count of nodes greater than x. 8 | Constraints: 9 | Time Limit: 1 sec 10 | Sample Input 1 : 11 | 10 3 20 30 40 2 40 50 0 0 0 0 12 | 35 13 | Sample Output 1 : 14 | 3 15 | Sample Input 2 : 16 | 10 3 20 30 40 2 40 50 0 0 0 0 17 | 10 18 | Sample Output 2: 19 | 5 20 | */ 21 | 22 | /************************************************************ 23 | 24 | Following is the structure for the TreeNode class 25 | 26 | template 27 | class TreeNode { 28 | public: 29 | T data; 30 | vector*> children; 31 | 32 | TreeNode(T data) { 33 | this->data = data; 34 | } 35 | 36 | ~TreeNode() { 37 | for (int i = 0; i < children.size(); i++) { 38 | delete children[i]; 39 | } 40 | } 41 | }; 42 | 43 | ************************************************************/ 44 | 45 | int getLargeNodeCount(TreeNode* root, int x) { 46 | // Write your code here 47 | //corner case 48 | if(root == NULL) { 49 | return 0; 50 | } 51 | int count = 0; 52 | 53 | for(int i = 0; i < root -> children.size(); i++) { 54 | count += getLargeNodeCount(root -> children[i], x); 55 | } 56 | 57 | if(root -> data > x) { 58 | return count + 1; 59 | } else { 60 | return count; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Lecture 13 : BST/Code: Construct BST from a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Construct BST from a Sorted Array 3 | Given a sorted integer array A of size n, which contains all unique elements. You need to construct a balanced BST from this input array. 4 | Return the root of constructed BST. 5 | 6 | Note: If array size is even, take first mid as root. 7 | 8 | Input format: 9 | The first line of input contains an integer, which denotes the value of n. The following line contains n space separated integers, 10 | that denote the values of array. 11 | 12 | Output Format: 13 | The first and only line of output contains values of BST nodes, printed in pre order traversal. 14 | 15 | Constraints: 16 | Time Limit: 1 second 17 | 18 | Sample Input 1: 19 | 7 20 | 1 2 3 4 5 6 7 21 | 22 | Sample Output 1: 23 | 4 2 1 3 6 5 7 24 | */ 25 | 26 | /********************************************************** 27 | 28 | Following is the Binary Tree Node class structure 29 | 30 | template 31 | class BinaryTreeNode { 32 | public : 33 | T data; 34 | BinaryTreeNode *left; 35 | BinaryTreeNode *right; 36 | 37 | BinaryTreeNode(T data) { 38 | this -> data = data; 39 | left = NULL; 40 | right = NULL; 41 | } 42 | }; 43 | 44 | ***********************************************************/ 45 | BinaryTreeNode* helper(int *input, int startIndex, int endIndex) { 46 | //base case 47 | if(startIndex > endIndex) { 48 | return NULL; 49 | } 50 | 51 | int mid = startIndex + (endIndex - startIndex) / 2; 52 | 53 | BinaryTreeNode *root = new BinaryTreeNode(input[mid]); 54 | root -> left = helper(input, startIndex, mid - 1); 55 | root -> right = helper(input,mid + 1, endIndex); 56 | 57 | return root; 58 | } 59 | 60 | BinaryTreeNode* constructTree(int *input, int n) { 61 | // Write your code here 62 | return helper(input, 0, n - 1); 63 | } 64 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Return subset of an array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array (of length n), find and return all the subsets of input array. 3 | Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array. 4 | 5 | 6 | Note : The order of subsets are not important. 7 | 8 | 9 | Input format : 10 | 11 | Line 1 : Size of array 12 | 13 | Line 2 : Array elements (separated by space) 14 | 15 | Sample Input: 16 | 3 17 | 15 20 12 18 | Sample Output: 19 | [] (this just represents an empty array, don't worry about the square brackets) 20 | 12 21 | 20 22 | 20 12 23 | 15 24 | 15 12 25 | 15 20 26 | 15 20 12 27 | */ 28 | 29 | 30 | /*** 31 | You need to save all the subsets in the given 2D output array. And return the number of subsets(i.e. number of rows filled in output) from the given function. 32 | 33 | In ith row of output array, 1st column contains length of the ith subset. And from 1st column actual subset follows. 34 | For eg. Input : {1, 2}, then output should contain 35 | {{0}, // Length of this subset is 0 36 | {1, 2}, // Length of this subset is 1 37 | {1, 1}, // Length of this subset is also 1 38 | {2, 1, 2}} // Length of this subset is 2 39 | 40 | Don’t print the subsets, just save them in output. 41 | ***/ 42 | 43 | // int subset(int input[], int n, int output[][20]) { 44 | // // Write your code here 45 | 46 | // } 47 | int subset(int input[], int n, int output[][20]) { 48 | // Write your code here 49 | if(n==0) 50 | { 51 | output[0][0]=0; 52 | return 1; 53 | } 54 | else 55 | { 56 | int smallOutput=subset(input+1,n-1,output); 57 | int i,j; 58 | 59 | for(i=0;i 38 | class BinaryTreeNode { 39 | public : 40 | T data; 41 | BinaryTreeNode *left; 42 | BinaryTreeNode *right; 43 | 44 | BinaryTreeNode(T data) { 45 | this -> data = data; 46 | left = NULL; 47 | right = NULL; 48 | } 49 | }; 50 | 51 | ***********************************************************/ 52 | 53 | int height(BinaryTreeNode* root) { 54 | // Write our code here 55 | //corner case 56 | if(root == NULL) { 57 | return 0; 58 | } 59 | 60 | return max(height(root -> left), height(root -> right)) + 1; 61 | } 62 | -------------------------------------------------------------------------------- /Test 1(Milestone 3)/Split Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array A of size N, check if the input array can be splitted in two parts such that - 3 | - Sum of both parts is equal 4 | - All elements in the input, which are divisible by 5 should be in same group. 5 | - All elements in the input, which are divisible by 3 (but not divisible by 5) should be in other group. 6 | - Elements which are neither divisible by 5 nor by 3, can be put in any group. 7 | Groups can be made with any set of elements, i.e. elements need not to be continuous. And you need to consider each and every element of input array in some group. 8 | Return true, if array can be split according to the above rules, else return false. 9 | Note : You will get marks only if all the test cases are passed. 10 | Input Format : 11 | Line 1 : Integer N (size of array) 12 | Line 2 : Array A elements (separated by space) 13 | Output Format : 14 | true or false 15 | Constraints : 16 | 1 <= N <= 50 17 | Sample Input 1 : 18 | 2 19 | 1 2 20 | Sample Output 1 : 21 | false 22 | Sample Input 2 : 23 | 3 24 | 1 4 3 25 | Sample Output 2 : 26 | true 27 | */ 28 | 29 | bool helper(int *arr, int n, int idx, int sum1, int sum2) { 30 | if(idx == n) { 31 | return sum1 == sum2; 32 | } 33 | 34 | if(arr[idx] % 5 == 0) { 35 | sum1 += arr[idx]; 36 | } 37 | 38 | else if(arr[idx] % 3 == 0) { 39 | sum2 += arr[idx]; 40 | } 41 | 42 | // special case 43 | else{ 44 | return helper(arr, n, idx + 1, sum1 + arr[idx], sum2) or helper(arr, n, idx + 1, sum1, sum2 + arr[idx]); 45 | } 46 | 47 | return helper(arr, n, idx + 1, sum1, sum2); 48 | 49 | } 50 | 51 | 52 | bool splitArray(int *input, int size) { 53 | /* Don't write main(). 54 | * Don't read input, it is passed as function argument. 55 | * Return output and don't print it. 56 | * Taking input and printing output is handled automatically. 57 | */ 58 | helper(input, size, 0 , 0, 0); 59 | } 60 | -------------------------------------------------------------------------------- /Lecture 10: Character Arrays and 2D Arrays/Wave Print.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, i.e, print the first column top to bottom, next column bottom to top and so on. 3 | Input format : 4 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 5 | 6 | First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list. 7 | 8 | Second line onwards, the next 'N' lines or rows represent the ith row values. 9 | 10 | Each of the ith row constitutes 'M' column values separated by a single space. 11 | Output format : 12 | For each test case, print the elements of the two-dimensional array/list in the sine wave order in a single line, separated by a single space. 13 | 14 | Output for every test case will be printed in a seperate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^3 18 | 0 <= M <= 10^3 19 | Time Limit: 1sec 20 | Sample Input 1: 21 | 1 22 | 3 4 23 | 1 2 3 4 24 | 5 6 7 8 25 | 9 10 11 12 26 | Sample Output 1: 27 | 1 5 9 10 6 2 3 7 11 12 8 4 28 | Sample Input 2: 29 | 2 30 | 5 3 31 | 1 2 3 32 | 4 5 6 33 | 7 8 9 34 | 10 11 12 35 | 13 14 15 36 | 3 3 37 | 10 20 30 38 | 40 50 60 39 | 70 80 90 40 | Sample Output 2: 41 | 1 4 7 10 13 14 11 8 5 2 3 6 9 12 15 42 | 10 40 70 80 50 20 30 60 90 43 | 44 | */ 45 | 46 | void wavePrint(int **input, int nRows, int mCols) 47 | { 48 | int i,j=0; 49 | for(i=0;i=0;j--){ 58 | cout << input[j][i] <<' '; 59 | } 60 | j=0; 61 | } 62 | // cout << endl; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Lecture 14 : Priority Queues/Code : K largest elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k largest numbers from given array. You need to save them in an array and return it. 3 | Time complexity should be O(nlogk) and space complexity should be not more than O(k). 4 | Order of elements in the output is not important. 5 | Input Format : 6 | Line 1 : Size of array (n) 7 | Line 2 : Array elements (separated by space) 8 | Line 3 : Integer k 9 | Output Format : 10 | k largest elements 11 | Sample Input : 12 | 13 13 | 2 12 9 16 10 5 3 20 25 11 1 8 6 14 | 4 15 | Sample Output : 16 | 12 17 | 16 18 | 20 19 | 25 20 | */ 21 | 22 | /* 23 | #include 24 | using namespace std; 25 | #include "Solution.h" 26 | 27 | int main() { 28 | 29 | int size; 30 | cin >> size; 31 | int *input = new int[1 + size]; 32 | 33 | for(int i = 0; i < size; i++) 34 | cin >> input[i]; 35 | 36 | int k; 37 | cin >> k; 38 | 39 | vector output = kLargest(input, size, k); 40 | for(int i = 0; i < output.size(); i++) 41 | cout << output[i] << endl; 42 | 43 | return 0; 44 | } 45 | */ 46 | 47 | #include 48 | #include 49 | vector kLargest(int arr[], int n, int k) 50 | { 51 | /* Don't write main(). 52 | * Don't read input, it is passed as function argument. 53 | * Return output and don't print it. 54 | * Taking input and printing output is handled automatically. 55 | */ 56 | priority_queue, greater> pq; 57 | for (int i = 0; i < k; i++) 58 | pq.push(arr[i]); 59 | for (int i = k; i < n; i++) 60 | { 61 | if (pq.top() < arr[i]) 62 | { 63 | pq.pop(); 64 | pq.push(arr[i]); 65 | } 66 | } 67 | vector v; 68 | while (!pq.empty()) 69 | { 70 | v.push_back(pq.top()); 71 | pq.pop(); 72 | } 73 | return v; 74 | } 75 | -------------------------------------------------------------------------------- /Lecture 9: Searching and Sorting/Sort 0 1 2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'. 3 | 'Single Scan' refers to iterating over the array/list just once or to put it in other words, you will be visiting each element in the array/list just once. 4 | Note: 5 | You need to change in the given array/list itself. Hence, no need to return or print anything. 6 | Input format : 7 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 8 | 9 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 10 | 11 | Second line contains 'N' single space separated integers(all 0s, 1s and 2s) representing the elements in the array/list. 12 | Output Format : 13 | For each test case, print the sorted array/list elements in a row separated by a single space. 14 | 15 | Output for every test case will be printed in a separate line. 16 | Constraints : 17 | 1 <= t <= 10^2 18 | 0 <= N <= 10^5 19 | Time Limit: 1 sec 20 | Sample Input 1: 21 | 1 22 | 7 23 | 0 1 2 0 2 0 1 24 | Sample Output 1: 25 | 0 0 0 1 1 2 2 26 | Sample Input 2: 27 | 2 28 | 5 29 | 2 2 0 1 1 30 | 7 31 | 0 1 2 0 1 2 0 32 | Sample Output 2: 33 | 0 1 1 2 2 34 | 0 0 0 1 1 2 2 35 | */ 36 | void sort012(int *arr, int n) 37 | { 38 | //Write your code here 39 | int i, temp, next0 = 0, next2 = n-1; 40 | for(i=0;i<=next2;){ 41 | 42 | if(arr[i] == 2){ 43 | temp = arr[i]; 44 | arr[i] = arr[next2]; 45 | arr[next2] = temp; 46 | next2--; 47 | } 48 | 49 | else if(arr[i] == 0){ 50 | temp = arr[i]; 51 | arr[i] = arr[next0]; 52 | arr[next0] = temp; 53 | next0++; 54 | i++; 55 | } 56 | 57 | else{ // arr[i] == 1 58 | i++; 59 | } 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Conditionals and Loops/Total Salary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to calculate the total salary of a person. The user has to enter the basic salary (an integer) and the grade (an uppercase character), 3 | and depending upon which the total salary is calculated as - 4 | totalSalary = basic + hra + da + allow – pf 5 | where : 6 | hra = 20% of basic 7 | da = 50% of basic 8 | allow = 1700 if grade = ‘A’ 9 | allow = 1500 if grade = ‘B’ 10 | allow = 1300 if grade = ‘C' or any other character 11 | pf = 11% of basic. 12 | Round off the total salary and then print the integral part only. 13 | Note: Try finding out a function on the internet to do so 14 | Input format : 15 | Basic salary & Grade (separated by space) 16 | Output Format : 17 | Total Salary 18 | Constraints : 19 | 0 <= Basic Salary <= 7,500,000 20 | Sample Input 1 : 21 | 10000 A 22 | Sample Output 1 : 23 | 17600 24 | Sample Input 2 : 25 | 4567 B 26 | Sample Output 2 : 27 | 8762 28 | Explanation of Input 2: 29 | We have been given the basic salary as Rs. 4567. We need to calculate the hra, da and pf. 30 | Now when we calculate each of the, it turns out to be: 31 | hra = 20% of Rs. 4567 = Rs. 913.4 32 | da = 50% od Rs. 4567 = Rs. 2283.5 33 | pf = 11% of Rs. 4567 = Rs. 502.37 34 | 35 | Since, the grade is 'B', we take allowance as Rs. 1500. 36 | On substituting these values to the formula of totalSalary, we get Rs. 8761.53 and now rounding it off will result in Rs. 8762 and hence the Answer. 37 | */ 38 | #include 39 | #include 40 | using namespace std; 41 | 42 | int main() { 43 | // Write your code here 44 | int basic; 45 | char grade; 46 | cin >> basic >> grade; 47 | double hra,da,allow,pf,totalSalary; 48 | hra = 0.20 * basic; 49 | da = 0.50 * basic; 50 | if (grade == 'A') 51 | allow = 1700; 52 | else if (grade == 'B') 53 | allow = 1500; 54 | else 55 | allow = 1300; 56 | pf = 0.11 * basic; 57 | totalSalary = basic + hra + da + allow - pf; 58 | cout << llround(totalSalary); 59 | } 60 | -------------------------------------------------------------------------------- /Lecture 9: Searching and Sorting/Second Largest in array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N. You are required to find and return the second largest element present in the array/list. 3 | If N <= 1 or all the elements are same in the array/list then return -2147483648 or -2 ^ 31(It is the smallest value for the range of Integer) 4 | Input format : 5 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 6 | 7 | The first line of each test case or query contains an integer 'N' representing the size of the array/list. 8 | 9 | The second line contains 'N' single space separated integers representing the elements in the array/list. 10 | Output Format : 11 | For each test case, print the second largest in the array/list if exists, -2147483648 otherwise. 12 | 13 | Output for every test case will be printed in a separate line. 14 | Constraints : 15 | 1 <= t <= 10^2 16 | 0 <= N <= 10^5 17 | 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 21 | 7 22 | 2 13 4 1 3 6 28 23 | Sample Output 1: 24 | 13 25 | Sample Input 2: 26 | 1 27 | 5 28 | 9 3 6 2 9 29 | Sample Output 2: 30 | 6 31 | Sample Input 3: 32 | 2 33 | 2 34 | 6 6 35 | 4 36 | 90 8 90 5 37 | Sample Output 3: 38 | -2147483648 39 | 8 40 | */ 41 | #include 42 | int findSecondLargest(int *input, int n) 43 | { 44 | //Write your code here 45 | if(n<=1) 46 | return INT_MIN; 47 | // bool isSame = true; 48 | // for(int i=0;ifirst){ 58 | second = first; 59 | first = input[i]; 60 | } 61 | else if(input[i]==first) 62 | continue; 63 | else if(input[i]>second) 64 | second = input[i]; 65 | } 66 | return second; 67 | } 68 | -------------------------------------------------------------------------------- /Lecture 5 : Time and Space Complexity Analysis/Check Array Rotation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N. It has been sorted(in increasing order) and then rotated by some number 'K' (K is greater than 0) in the right hand direction. 3 | Your task is to write a function that returns the value of 'K', that means, the index from which the array/list has been rotated. 4 | Input format : 5 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 6 | 7 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 8 | 9 | Second line contains 'N' single space separated integers representing the elements in the array/list. 10 | Output Format : 11 | For each test case, print the value of 'K' or the index from which which the array/list has been rotated. 12 | 13 | Output for every test case will be printed in a separate line. 14 | Constraints : 15 | 1 <= t <= 10^2 16 | 2 <= N <= 10^5 17 | Time Limit: 1 sec 18 | Sample Input 1: 19 | 1 20 | 6 21 | 5 6 1 2 3 4 22 | Sample Output 1: 23 | 2 24 | Sample Input 2: 25 | 2 26 | 5 27 | 3 6 8 9 10 28 | 4 29 | 10 20 30 1 30 | Sample Output 2: 31 | 0 32 | 3 33 | */ 34 | 35 | // int arrayRotateCheck(int *input, int size) 36 | // { 37 | // //Write your code here 38 | // } 39 | int arrayRotateCheck(int *arr, int size) { 40 | //Write your code here 41 | // we will use binary search to solve this question 42 | 43 | int low = 0; 44 | int high = size - 1; 45 | 46 | while(low < high) { 47 | int mid = low + ((high - low ) / 2); 48 | 49 | if(arr[mid] < arr[0]) { 50 | high = mid; 51 | } 52 | else if (arr[mid] >= arr[0]) { 53 | low = mid + 1; 54 | } 55 | 56 | } 57 | 58 | // sanity check 59 | if(arr[low] < arr[0]){ 60 | return low; 61 | } else { 62 | return 0; 63 | } 64 | } 65 | 66 | // Time Complexity : O(logn) 67 | // Space Complexity : O(1) 68 | -------------------------------------------------------------------------------- /Lecture 11 : Trees/Contains x.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a generic tree and an integer x, check if x is present in the given tree or not. Return true if x is present, return false otherwise. 3 | Input format : 4 | The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. 5 | The following line contains an integer, that denotes the value of x. 6 | Output format : 7 | The first and only line of output contains true, if x is present and false, otherwise. 8 | Constraints: 9 | Time Limit: 1 sec 10 | Sample Input 1 : 11 | 10 3 20 30 40 2 40 50 0 0 0 0 12 | 40 13 | Sample Output 1 : 14 | true 15 | Sample Input 2 : 16 | 10 3 20 30 40 2 40 50 0 0 0 0 17 | 4 18 | Sample Output 2: 19 | false 20 | */ 21 | 22 | /************************************************************ 23 | 24 | Following is the structure for the TreeNode class 25 | 26 | template 27 | class TreeNode { 28 | public: 29 | T data; 30 | vector*> children; 31 | 32 | TreeNode(T data) { 33 | this->data = data; 34 | } 35 | 36 | ~TreeNode() { 37 | for (int i = 0; i < children.size(); i++) { 38 | delete children[i]; 39 | } 40 | } 41 | }; 42 | 43 | ************************************************************/ 44 | #include 45 | 46 | bool isPresent(TreeNode* root, int x) { 47 | // Write your code here 48 | //corner case 49 | if(root == NULL) { 50 | return false; 51 | } 52 | //base case 53 | if(root -> data == x) { 54 | return true; 55 | } 56 | bool flag = false; 57 | 58 | for(int i = 0; i < root -> children.size() ; i++) { 59 | flag = isPresent(root -> children[i], x); 60 | if(flag) { 61 | return flag; 62 | } 63 | } 64 | 65 | return flag; 66 | } 67 | -------------------------------------------------------------------------------- /Lecture 4: Recursion 2/Return subsets sum to K.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A of size n and an integer K, return all subsets of A which sum to K. 3 | Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array. 4 | 5 | 6 | Note : The order of subsets are not important. 7 | 8 | 9 | Input format : 10 | Line 1 : Integer n, Size of input array 11 | Line 2 : Array elements separated by space 12 | Line 3 : K 13 | Constraints : 14 | 1 <= n <= 20 15 | Sample Input : 16 | 9 17 | 5 12 3 17 1 18 15 3 17 18 | 6 19 | Sample Output : 20 | 3 3 21 | 5 1 22 | */ 23 | 24 | /*** 25 | You need to save all the subsets in the given 2D output array. And return the number of subsets(i.e. number of rows filled in output) from the given function. 26 | 27 | In ith row of output array, 1st column contains length of the ith subset. And from 1st column actual subset follows. 28 | For eg. Input : {1, 3, 4, 2} and K = 5, then output array should contain 29 | {{2, 1, 4}, // Length of this subset is 2 30 | {2, 3, 2}} // Length of this subset is 2 31 | 32 | Don’t print the subsets, just save them in output. 33 | ***/ 34 | 35 | // int subsetSumToK(int input[], int n, int output[][50], int k) { 36 | // // Write your code here 37 | 38 | // } 39 | int subsetSumToK(int input[], int n, int output[][50], int k) { 40 | // Write your code here 41 | if(n==0) 42 | { 43 | if(k==0) 44 | { 45 | output[0][0]=0; 46 | return 1; 47 | } 48 | else 49 | return 0; 50 | } 51 | 52 | int smallOutput1=subsetSumToK(input+1,n-1,output,k); 53 | int smallOutput2=subsetSumToK(input+1,n-1,output+smallOutput1,k-input[0]); 54 | 55 | int smallOutputSize=smallOutput1+smallOutput2; 56 | for(int i=smallOutput1;i0;j--) 59 | { 60 | output[i][j+1]=output[i][j]; 61 | } 62 | output[i][1]=input[0]; 63 | output[i][0]++; 64 | } 65 | return smallOutputSize; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Test 2 (Milestone 2)/Minimum Length Word.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string S (that can contain multiple words), you need to find the word which has minimum length. 3 | Note : If multiple words are of same length, then answer will be first minimum length word in the string. 4 | Words are seperated by single space only. 5 | Input Format : 6 | String S 7 | Output Format : 8 | Minimum length word 9 | Constraints : 10 | 1 <= Length of String S <= 10^5 11 | Sample Input 1 : 12 | this is test string 13 | Sample Output 1 : 14 | is 15 | Sample Input 2 : 16 | abc de ghihjk a uvw h j 17 | Sample Output 2 : 18 | a 19 | */ 20 | 21 | /* input - Input String 22 | * output - Save the result in the output array (passed as argument). You don’t have to 23 | * print or return the result 24 | */ 25 | #include 26 | void minLengthWord(char input[], char output[]){ 27 | // int n=input.size(); 28 | // int n = 0; 29 | // while(input[n] != '\0'){ 30 | // n++; 31 | // } 32 | // int minlen=0,j; 33 | // for(int i=0;iminlen){ 37 | // minlen = j-i; 38 | // for(int k=i;k= n) 43 | // break; 44 | // i=j+1; 45 | // j++; 46 | // } 47 | int len = strlen(input); 48 | int si = 0, ei = 0; 49 | int min_length = len, min_start_index = 0; 50 | while (ei <= len) 51 | { 52 | if (ei < len && input[ei] != ' ') 53 | ei++; 54 | 55 | else 56 | { 57 | 58 | int curr_length = ei - si; 59 | 60 | if (curr_length < min_length) 61 | { 62 | min_length = curr_length; 63 | min_start_index = si; 64 | } 65 | 66 | ei++; 67 | si=ei; 68 | } 69 | 70 | 71 | } 72 | for(int i=0;i 20 -> 30 -> 40 -> 50 -> 60 -> null 5 | Alternate nodes will be: 20, 40, and 60. 6 | 7 | Hence after deleting, the list will be: 8 | Output: 10 -> 30 -> 50 -> null 9 | Note : 10 | The head of the list will remain the same. Don't need to print or return anything. 11 | Input format : 12 | The first and the only line of input will contain the elements of the Singly Linked List separated by a single space and terminated by -1. 13 | Output Format : 14 | The only line of output will contain the updated list elements. 15 | Input Constraints: 16 | 1 <= N <= 10 ^ 6. 17 | Where N is the size of the Singly Linked List 18 | 19 | Time Limit: 1 sec 20 | Sample Input 1: 21 | 1 2 3 4 5 -1 22 | Sample Output 1: 23 | 1 3 5 24 | Explanation of Sample Input 1: 25 | 2, 4 are alternate nodes so we need to delete them 26 | Sample Input 2: 27 | 10 20 30 40 50 60 70 -1 28 | Sample Output 2: 29 | 10 30 50 70 30 | */ 31 | 32 | 33 | 34 | /********************************************************* 35 | 36 | // Following is the node structure 37 | 38 | class Node { 39 | public: 40 | int data; 41 | Node * next; 42 | Node(int data){ 43 | this -> data = data; 44 | this -> next = NULL; 45 | } 46 | 47 | ~Node() { 48 | if(next) { 49 | delete next; 50 | } 51 | } 52 | }; 53 | 54 | *********************************************************/ 55 | void deleteAlternateNodes(Node *head) { 56 | if(head == NULL) { 57 | return; 58 | } 59 | 60 | Node *prev = head; 61 | Node *next = head -> next; 62 | 63 | while(prev and next) { 64 | prev -> next = next -> next; 65 | prev = next -> next; 66 | Node *temp = next; 67 | if(prev != NULL) { 68 | next = prev -> next; // if prev is null and we access its next, it will give segmentation fault 69 | } 70 | free(temp); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Lecture 13 : BST/Check if a Binary Tree is BST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Check if a Binary Tree is BST 3 | Given a binary tree with N number of nodes, check if that input tree is BST (Binary Search Tree). If yes, return true, return false otherwise. 4 | 5 | Note: Duplicate elements should be kept in the right subtree. 6 | 7 | Input format : 8 | The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. 9 | If any node does not have a left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, 10 | therefore, it will not be a part of the data of any node. 11 | 12 | Output format : 13 | The first and only line of output contains either true or false. 14 | 15 | Constraints : 16 | Time Limit: 1 second 17 | 18 | Sample Input 1 : 19 | 3 1 5 -1 2 -1 -1 -1 -1 20 | 21 | Sample Output 1 : 22 | true 23 | 24 | 25 | Sample Input 2 : 26 | 5 2 10 0 1 -1 15 -1 -1 -1 -1 -1 -1 27 | 28 | Sample Output 2 : 29 | false 30 | */ 31 | 32 | /********************************************************** 33 | Following is the Binary Tree Node class structure 34 | 35 | template 36 | class BinaryTreeNode { 37 | public : 38 | T data; 39 | BinaryTreeNode *left; 40 | BinaryTreeNode *right; 41 | 42 | BinaryTreeNode(T data) { 43 | this -> data = data; 44 | left = NULL; 45 | right = NULL; 46 | } 47 | }; 48 | 49 | ***********************************************************/ 50 | #include 51 | bool helper(BinaryTreeNode *root, int min, int max){ 52 | //corner case 53 | if(root == NULL){ 54 | return true; 55 | } 56 | 57 | if(root -> data < min or root -> data > max){ 58 | return false; 59 | } 60 | 61 | bool left = helper(root -> left, min, root -> data - 1); // left subtree 62 | bool right = helper(root -> right, root -> data, max); // right subtree 63 | 64 | return left and right; 65 | } 66 | 67 | bool isBST(BinaryTreeNode *root){ 68 | return helper(root, INT_MIN, INT_MAX); 69 | } 70 | -------------------------------------------------------------------------------- /Lecture 13 : BST/Code: Print Elements in Range.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print Elements in Range 3 | Given a Binary Search Tree and two integers k1 and k2, find and print the elements which are in range k1 and k2 (both inclusive). 4 | Print the elements in increasing order. 5 | 6 | Input format: 7 | The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. 8 | If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. 9 | The following line contains two integers, that denote the value of k1 and k2. 10 | 11 | Output Format: 12 | The first and only line of output prints the elements which are in range k1 and k2 (both inclusive). Print the elements in increasing order. 13 | 14 | Constraints: 15 | Time Limit: 1 second 16 | 17 | Sample Input 1: 18 | 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 19 | 6 10 20 | 21 | Sample Output 1: 22 | 6 7 8 10 23 | */ 24 | 25 | /********************************************************** 26 | 27 | Following is the Binary Tree Node class structure 28 | 29 | template 30 | class BinaryTreeNode { 31 | public : 32 | T data; 33 | BinaryTreeNode *left; 34 | BinaryTreeNode *right; 35 | 36 | BinaryTreeNode(T data) { 37 | this -> data = data; 38 | left = NULL; 39 | right = NULL; 40 | } 41 | }; 42 | 43 | ***********************************************************/ 44 | 45 | void elementsInRangeK1K2(BinaryTreeNode* root, int k1, int k2) { 46 | // Write your code here 47 | //corner case 48 | if(root == NULL) { 49 | return; 50 | } 51 | 52 | // since we need to print in ascending order 53 | if(k1 < root -> data) { 54 | elementsInRangeK1K2(root -> left, k1, k2); 55 | } 56 | 57 | if(root -> data >= k1 and root -> data <= k2) { 58 | cout << root -> data << " "; 59 | } 60 | 61 | if (root -> data < k2) { 62 | elementsInRangeK1K2(root -> right, k1, k2); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Lecture 13 : BST/Code: Search in BST.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | Search in BST 5 | Given a BST and an integer k. Find if the integer k is present in given BST or not. You have to return true, if node with data k is present, return false otherwise. 6 | 7 | Note: Assume that BST contains all unique elements. 8 | 9 | Input Format: 10 | The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. 11 | If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. 12 | The following line of input contains an integer, that denotes the value of k. 13 | 14 | Output Format: 15 | The first and only line of output contains a boolean value. Print true, if node with data k is present, print false otherwise. 16 | 17 | Constraints: 18 | Time Limit: 1 second 19 | 20 | Sample Input 1 : 21 | 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 22 | 2 23 | 24 | Sample Output 1 : 25 | true 26 | 27 | 28 | Sample Input 2 : 29 | 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 30 | 12 31 | 32 | Sample Output 2 : 33 | false 34 | */ 35 | 36 | /********************************************************** 37 | 38 | Following is the Binary Tree Node class structure 39 | 40 | template 41 | class BinaryTreeNode { 42 | public : 43 | T data; 44 | BinaryTreeNode *left; 45 | BinaryTreeNode *right; 46 | 47 | BinaryTreeNode(T data) { 48 | this -> data = data; 49 | left = NULL; 50 | right = NULL; 51 | } 52 | }; 53 | 54 | ***********************************************************/ 55 | 56 | bool searchInBST(BinaryTreeNode *root , int k) { 57 | // corner case 58 | if(root == NULL) { 59 | return false; 60 | } 61 | bool flag = false; 62 | if(k < root -> data) { 63 | flag = searchInBST(root -> left, k); 64 | } else if(k > root -> data) { 65 | flag = searchInBST(root -> right, k); 66 | } else { 67 | return true; 68 | } 69 | 70 | return flag; 71 | } 72 | -------------------------------------------------------------------------------- /Lecture 15 : Hashmaps/Pairs with difference K.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given with an array of integers and an integer K. You have to find and print the count of all such pairs which have difference K. 3 | Note: Take absolute difference between the elements of the array. 4 | Input Format: 5 | The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol n. 6 | The following line contains n space separated integers, that denote the value of the elements of the array. 7 | The following line contains an integer, that denotes the value of K. 8 | Output format : 9 | The first and only line of output contains count of all such pairs which have an absolute difference of K. 10 | Constraints : 11 | 0 <= n <= 10^4 12 | Time Limit: 1 sec 13 | Sample Input 1 : 14 | 4 15 | 5 1 2 4 16 | 3 17 | Sample Output 1 : 18 | 2 19 | Sample Input 2 : 20 | 4 21 | 4 4 4 4 22 | 0 23 | Sample Output 2 : 24 | 6 25 | */ 26 | 27 | /* 28 | #include 29 | using namespace std; 30 | 31 | #include "solution.h" 32 | 33 | int main() { 34 | int n; 35 | cin >> n; 36 | 37 | int *input = new int[n]; 38 | 39 | for (int i = 0; i < n; i++) { 40 | cin >> input[i]; 41 | } 42 | 43 | int k; 44 | cin >> k; 45 | 46 | cout << getPairsWithDifferenceK(input, n, k); 47 | 48 | delete[] input; 49 | } 50 | */ 51 | 52 | #include 53 | int getPairsWithDifferenceK(int *input, int n, int k) 54 | { 55 | // Write your code here 56 | unordered_map m; 57 | int count = 0; 58 | for (int i = 0; i < n; i++) 59 | m[input[i]]++; 60 | if (k != 0) 61 | { 62 | for (int i = 0; i < n; i++) 63 | { 64 | if (m.count(input[i] - k) > 0) 65 | count = count + m[input[i] - k]; 66 | } 67 | } 68 | else if (k == 0) 69 | { 70 | for (int i = 0; i < n; i++) 71 | { 72 | if (m.count(input[i]) > 0) 73 | { 74 | count = count + m[input[i]]; 75 | m[input[i]]--; 76 | } 77 | count--; 78 | } 79 | } 80 | return count; 81 | } 82 | -------------------------------------------------------------------------------- /Lecture 5 : Time and Space Complexity Analysis/Rotate array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left). 3 | Note: 4 | Change in the input array/list itself. You don't need to return or print the elements. 5 | Input format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | 12 | Third line contains the value of 'D' by which the array/list needs to be rotated. 13 | Output Format : 14 | For each test case, print the rotated array/list in a row separated by a single space. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 10^4 19 | 0 <= N <= 10^6 20 | 0 <= D <= N 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 1 24 | 7 25 | 1 2 3 4 5 6 7 26 | 2 27 | Sample Output 1: 28 | 3 4 5 6 7 1 2 29 | Sample Input 2: 30 | 2 31 | 7 32 | 1 2 3 4 5 6 7 33 | 0 34 | 4 35 | 1 2 3 4 36 | 2 37 | Sample Output 2: 38 | 1 2 3 4 5 6 7 39 | 3 4 1 2 40 | */ 41 | 42 | // void rotate(int *input, int d, int n) 43 | // { 44 | // //Write your code here 45 | // } 46 | int gcd(int a, int b) { 47 | if(b == 0){ 48 | return a; 49 | } 50 | gcd(b, a % b); 51 | } 52 | 53 | void rotate(int *input, int d, int n) { 54 | //Write your code here 55 | //using juggling sort we can solve this question 56 | int idx = gcd(n, d); 57 | 58 | for(int i = 0; i < idx; i++) { 59 | int temp = input[i]; 60 | int j = i; 61 | 62 | for(;true;) { 63 | int k = j + d; 64 | if(k >= n) { 65 | k -= n; 66 | } 67 | if(k == i) 68 | break; 69 | 70 | input[j] = input[k]; 71 | j = k; 72 | } 73 | input[j] = temp; 74 | } 75 | } 76 | 77 | // Time Complexity : O(n) 78 | // Space Complexity : O(1) 79 | -------------------------------------------------------------------------------- /Lecture 19 : DP - 2/Code : Knapsack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A thief robbing a store can carry a maximal weight of W into his knapsack. There are N items, and i-th item weigh 'Wi' and the value being 'Vi.' What would be the maximum value V, that the thief can steal? 3 | Input Format : 4 | The first line of the input contains an integer value N, which denotes the total number of items. 5 | 6 | The second line of input contains the N number of weights separated by a single space. 7 | 8 | The third line of input contains the N number of values separated by a single space. 9 | 10 | The fourth line of the input contains an integer value W, which denotes the maximum weight the thief can steal. 11 | Output Format : 12 | Print the maximum value of V that the thief can steal. 13 | Constraints : 14 | 1 <= N <= 20 15 | 1<= Wi <= 100 16 | 1 <= Vi <= 100 17 | 18 | Time Limit: 1 sec 19 | Sample Input 1 : 20 | 4 21 | 1 2 4 5 22 | 5 4 8 6 23 | 5 24 | Sample Output 1 : 25 | 13 26 | Sample Input 2 : 27 | 5 28 | 12 7 11 8 9 29 | 24 13 23 15 16 30 | 26 31 | Sample Output 2 : 32 | 51 33 | */ 34 | 35 | /* 36 | #include 37 | using namespace std; 38 | 39 | #include "solution.h" 40 | 41 | int main() 42 | { 43 | int n; 44 | cin >> n; 45 | 46 | int *weights = new int[n]; 47 | int *values = new int[n]; 48 | 49 | for (int i = 0; i < n; i++) 50 | { 51 | cin >> weights[i]; 52 | } 53 | 54 | for (int i = 0; i < n; i++) 55 | { 56 | cin >> values[i]; 57 | } 58 | 59 | int maxWeight; 60 | cin >> maxWeight; 61 | 62 | cout << knapsack(weights, values, n, maxWeight) << endl; 63 | 64 | delete[] weights; 65 | delete[] values; 66 | } 67 | */ 68 | 69 | int knapsack(int *weights, int *values, int n, int maxWeight) 70 | { 71 | // write your code here 72 | if (maxWeight == 0 || n == 0) 73 | return 0; 74 | 75 | if (maxWeight < weights[0]) 76 | return knapsack(weights + 1, values + 1, n - 1, maxWeight); 77 | else 78 | { 79 | int a = knapsack(weights + 1, values + 1, n - 1, maxWeight - weights[0]) + values[0]; 80 | int b = knapsack(weights + 1, values + 1, n - 1, maxWeight); 81 | return max(a, b); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Lecture 20 : Graphs 1/3 Cycle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a graph with N vertices (numbered from 0 to N-1) and M undirected edges, then count the distinct 3-cycles in the graph. A 3-cycle PQR is a cycle in which (P,Q), (Q,R) and (R,P) are connected by an edge. 3 | Input Format : 4 | The first line of input contains two space separated integers, that denotes the value of N and M. 5 | Each of the following M lines contain two integers, that denote the vertices which have an undirected edge between them. Let us denote the two vertices with the symbol u and v. 6 | Output Format : 7 | Print the count the number of 3-cycles in the given graph 8 | Constraints : 9 | 0 <= N <= 100 10 | 0 <= M <= (N*(N-1))/2 11 | 0 <= u <= N - 1 12 | 0 <= v <= N - 1 13 | Time Limit: 1 sec 14 | Sample Input 1: 15 | 3 3 16 | 0 1 17 | 1 2 18 | 2 0 19 | Sample Output 1: 20 | 1 21 | */ 22 | 23 | #include 24 | using namespace std; 25 | 26 | int countCycles(bool **arr, long long n) 27 | { 28 | int count = 0; 29 | for (int i = 0; i < n; i++) 30 | { 31 | for (int j = 0; j < n; j++) 32 | { 33 | if (i != j && arr[i][j] == 1) 34 | { 35 | for (int k = 0; k < n; k++) 36 | { 37 | if (j != k && k != i && arr[j][k] == 1) 38 | { 39 | if (arr[i][k] == 1) 40 | { 41 | count += 1; 42 | } 43 | } 44 | } 45 | } 46 | } 47 | } 48 | return count; 49 | } 50 | 51 | int main() 52 | { 53 | // Write your code here 54 | long long v, e; 55 | cin >> v >> e; 56 | bool **edges = new bool *[v]; 57 | for (long long i = 0; i < v; i++) 58 | { 59 | edges[i] = new bool[v]; 60 | for (long long j = 0; j < v; j++) 61 | { 62 | edges[i][j] = false; 63 | } 64 | } 65 | for (long long i = 0; i < e; i++) 66 | { 67 | long long start, end; 68 | cin >> start >> end; 69 | edges[start][end] = true; 70 | edges[end][start] = true; 71 | } 72 | cout << countCycles(edges, v) / 6; 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Lecture 14 : Priority Queues/Code : K smallest Elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k smallest numbers from given array. You need to save them in an array and return it. 3 | Time complexity should be O(n * logk) and space complexity should not be more than O(k). 4 | Note: Order of elements in the output is not important. 5 | Input Format : 6 | The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. 7 | The following line contains N space separated integers, that denote the value of the elements of the array. 8 | The following line contains an integer, that denotes the value of k. 9 | Output Format : 10 | The first and only line of output print k smallest elements. The elements in the output are separated by a single space. 11 | Constraints: 12 | Time Limit: 1 sec 13 | Sample Input 1 : 14 | 13 15 | 2 12 9 16 10 5 3 20 25 11 1 8 6 16 | 4 17 | Sample Output 1 : 18 | 1 2 3 5 19 | */ 20 | 21 | /* 22 | #include 23 | #include 24 | #include 25 | using namespace std; 26 | 27 | #include "solution.h" 28 | 29 | int main() { 30 | int size; 31 | cin >> size; 32 | 33 | int *input = new int[size]; 34 | 35 | for (int i = 0; i < size; i++) { 36 | cin >> input[i]; 37 | } 38 | 39 | int k; 40 | cin >> k; 41 | 42 | vector output = kSmallest(input, size, k); 43 | sort(output.begin(), output.end()); 44 | 45 | for (int i = 0; i < output.size(); i++) { 46 | cout << output[i] << " "; 47 | } 48 | 49 | delete[] input; 50 | } 51 | */ 52 | 53 | #include 54 | vector kSmallest(int arr[], int n, int k) 55 | { 56 | // Write your code here 57 | priority_queue pq; 58 | for (int i = 0; i < k; i++) 59 | pq.push(arr[i]); 60 | for (int i = k; i < n; i++) 61 | { 62 | if (pq.top() > arr[i]) 63 | { 64 | pq.pop(); 65 | pq.push(arr[i]); 66 | } 67 | } 68 | vector v; 69 | while (!pq.empty()) 70 | { 71 | v.push_back(pq.top()); 72 | pq.pop(); 73 | } 74 | return v; 75 | } 76 | -------------------------------------------------------------------------------- /Lecture 14 : Priority Queues/Merge K sorted arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given k different arrays, which are sorted individually (in ascending order). You need to merge all the given arrays such that output array should be sorted (in ascending order). 3 | Hint : Use Heaps. 4 | Input Format: 5 | The first line of input contains an integer, that denotes value of k. 6 | The following lines of input represent k sorted arrays. Each sorted array uses two lines of input. The first line contains an integer, that denotes the size of the array. The following line contains elements of the array. 7 | Output Format: 8 | The first and only line of output contains space separated elements of the merged and sorted array, as described in the task. 9 | Constraints: 10 | 0 <= k <= 1000 11 | 0 <= n1, n2 <= 10000 12 | Time Limit: 1 second 13 | Sample Input 1: 14 | 4 15 | 3 16 | 1 5 9 17 | 2 18 | 45 90 19 | 5 20 | 2 6 78 100 234 21 | 1 22 | 0 23 | Sample Output 1: 24 | 0 1 2 5 6 9 45 78 90 100 234 25 | */ 26 | 27 | /* 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | #include "solution.h" 33 | 34 | int main() { 35 | int k; 36 | cin >> k; 37 | 38 | vector *> input; 39 | 40 | for (int j = 1; j <= k; j++) { 41 | int size; 42 | cin >> size; 43 | vector *current = new vector; 44 | 45 | for (int i = 0; i < size; i++) { 46 | int a; 47 | cin >> a; 48 | current->push_back(a); 49 | } 50 | 51 | input.push_back(current); 52 | } 53 | 54 | vector output = mergeKSortedArrays(input); 55 | 56 | for (int i = 0; i < output.size(); i++) { 57 | cout << output[i] << " "; 58 | } 59 | 60 | return 0; 61 | } 62 | */ 63 | 64 | #include 65 | vector mergeKSortedArrays(vector *> input) 66 | { 67 | // Write your code here 68 | priority_queue, greater> pq; 69 | for (int i = 0; i < input.size(); i++) 70 | for (int j = 0; j < input[i]->size(); j++) 71 | pq.push(input[i]->at(j)); 72 | vector v; 73 | while (!pq.empty()) 74 | { 75 | v.push_back(pq.top()); 76 | pq.pop(); 77 | } 78 | return v; 79 | } 80 | -------------------------------------------------------------------------------- /Lecture 17 : Backtracking/Subset Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an array of integers and a target K. You have to find the count of subsets of the given array that sum up to K. 3 | Note: 4 | 1. Subset can have duplicate values. 5 | 2. Empty subset is a valid subset and has sum equal to zero. 6 | Input Format: 7 | The first line of input will contain an integer T, that denotes the value of number of test cases. 8 | Each test case contains two lines. The first line of each test case will contain two space-separated integers N and K, where N is the size of the given array and K is the target value. 9 | The second line of each test case will contain N space separated integers denoting the elements of the input array. 10 | Output Format ; 11 | For each test case, print the number of subsets that sum upto K in a separate line. 12 | Constraints 13 | 1 <= T <= 50 14 | 2 <= N <= 15 15 | 0 <= array[i] <= 10^9 16 | 0 <= K <= 10^9 17 | Time Limit: 1 second 18 | Sample Input 1: 19 | 1 20 | 5 6 21 | 2 4 4 3 1 22 | Sample Output 1: 23 | 3 24 | Explanation: 25 | Following are the three subsets, that sum upto K=6: 26 | 1. [2, 4], Element 4 in this subset is the one at index 1 27 | 2. [2, 4], Element 4 in this subset is the one at index 2 28 | 3. [2, 3, 1] 29 | Sample Input 2: 30 | 2 31 | 8 8 32 | 1 4 1 3 1 1 2 3 33 | 8 2 34 | 4 1 4 4 2 4 1 1 35 | Sample Output 2: 36 | 30 37 | 4 38 | */ 39 | 40 | #include 41 | using namespace std; 42 | #define ll long long 43 | 44 | ll subsetSum(ll arr[], ll n, ll i, ll sum, ll count) 45 | { 46 | if (i == n) 47 | { 48 | if (sum == 0) 49 | count++; 50 | return count; 51 | } 52 | count = subsetSum(arr, n, i + 1, sum - arr[i], count); 53 | count = subsetSum(arr, n, i + 1, sum, count); 54 | return count; 55 | } 56 | 57 | // Driver code 58 | int main() 59 | { 60 | int t; 61 | cin >> t; 62 | while (t--) 63 | { 64 | ll n, k; 65 | cin >> n >> k; 66 | ll arr[n]; 67 | for (ll i = 0; i < n; i++) 68 | cin >> arr[i]; 69 | if (n == 0) 70 | cout << "" 71 | << "\n"; 72 | else 73 | cout << subsetSum(arr, n, 0, k, 0) << "\n"; 74 | } 75 | return 0; 76 | } 77 | --------------------------------------------------------------------------------