├── Chapter - 1 - Arrays and Strings ├── 1. Is Unique │ ├── Is Uniqe.java │ ├── is_unique.cpp │ └── is_unique.py ├── 2. Check Permutation │ ├── Check Permutation.java │ └── check_permutation.py ├── 3. URLify │ ├── URLify.java │ └── URLify.py ├── 4. Palindrome Permutation │ ├── Palindrome Permuation.java │ └── Palindrome Permuation.py ├── 5. One Away │ └── One Away.java ├── 6. String Compression │ └── String compression.java ├── 8. Zero Matrix │ └── Zero Matrix.java └── 9. String Rotation │ └── String Rotation.java ├── Chapter - 2 - Linked List ├── 1. Remove Dups │ └── RemoveDups.cpp ├── 2. Return Kth to last │ └── ReturnKthToLast.cpp ├── 3. Delete Middle Node │ └── DeleteMiddleNode.cpp ├── 4. Partition │ └── Partition.cpp ├── 5. Sum Lists │ └── SumLists.cpp ├── 6. Palindrome │ └── Palindrome.cpp ├── 7. Intersection │ └── Intersection.cpp └── 8. Detect Loop │ └── DetectLoop.cpp ├── Chapter - 3 - Stacks and Queues ├── 2. StackMin │ └── StackMin.cpp ├── 3. Set Of Stacks │ └── SetOfStacks.cpp └── 4. MyQueue │ └── MyQueue.cpp ├── Chapter - 4 - Trees and Graphs ├── 1. Route between nodes │ └── RoutesBetweenNodes.cpp ├── 2. Minimal Tree │ └── MinimalTree.cpp ├── 3. List Of Depths │ └── ListOfDepths.cpp ├── 4. Check Balanced │ └── CheckBalanced.cpp └── 5. Validate BST │ └── ValidateBST.cpp ├── Chapter - 8 - Recusrion and Dynamic Programming └── 1. Triple Step │ └── TripleStep.cpp └── README.md /Chapter - 1 - Arrays and Strings/1. Is Unique/Is Uniqe.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void isUnique(String str){ 6 | if(str.length() > 128){ 7 | System.out.println("There are repeating characters in this String"); 8 | return; 9 | }else{ 10 | boolean[] present = new boolean[128]; 11 | for(char ch : str.toCharArray()){ 12 | if(present[ch]){ 13 | System.out.println("There are repeating characters in this String"); 14 | return; 15 | } 16 | present[ch] = true; 17 | } 18 | System.out.println("There are no repeating characters in this String"); 19 | } 20 | } 21 | 22 | public static void main(String[] args) { // unit testing (optional) 23 | 24 | Scanner scanner = new Scanner(System.in); 25 | String str = scanner.nextLine(); 26 | 27 | isUnique(str); 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/1. Is Unique/is_unique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool is_unique(string str) { 6 | if (str.length()>128) return false; 7 | bool char_set[128]; 8 | for(int i=0;i>str; 19 | cout<128: 3 | return False 4 | char_set=[None]*128 5 | for i in range(0,len(str)): 6 | ascii_char=ord(str[i]) 7 | if char_set[ascii_char]: 8 | return False 9 | char_set[ascii_char]=True 10 | return True 11 | 12 | str_user = input("Enter the string you want to check: ") 13 | print(is_unique(str_user)) 14 | -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/2. Check Permutation/Check Permutation.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.HashMap; 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void checkPermutationBySorting(String str1, String str2){ 8 | 9 | if(str1.length() != str2.length()){ 10 | System.out.println("Both are not permutations of each other"); 11 | return; 12 | } 13 | 14 | //sort array 1 15 | char tempArray[] = str1.toCharArray(); 16 | Arrays.sort(tempArray); 17 | str1 = new String(tempArray); 18 | 19 | tempArray= str2.toCharArray(); 20 | Arrays.sort(tempArray); 21 | str2 = new String(tempArray); 22 | 23 | if(str1.equals(str2)){ 24 | System.out.println("Both are permutations of each other"); 25 | }else{ 26 | System.out.println("Both are not permutations of each other"); 27 | } 28 | } 29 | 30 | public static void checkPermutationByCount(String str1, String str2){ 31 | 32 | if(str1.length() != str2.length()){ 33 | System.out.println("Both are not permutations of each other"); 34 | return; 35 | } 36 | 37 | int[] length = new int[128]; 38 | for(char ch : str1.toCharArray()){ 39 | length[ch]++; 40 | } 41 | for(char ch : str2.toCharArray()){ 42 | length[ch]--; 43 | } 44 | for(int i = 0 ; i < str1.length() ; i++){ 45 | if(length[i] != 0){ 46 | System.out.println("Both are not permutations of each other"); 47 | return; 48 | } 49 | } 50 | System.out.println("Both are permutations of each other"); 51 | } 52 | 53 | public static void main(String[] args) { // unit testing (optional) 54 | 55 | Scanner scanner = new Scanner(System.in); 56 | String str1 = scanner.nextLine(); 57 | String str2 = scanner.nextLine(); 58 | 59 | checkPermutationBySorting(str1, str2); 60 | checkPermutationByCount(str1, str2); 61 | 62 | } 63 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/2. Check Permutation/check_permutation.py: -------------------------------------------------------------------------------- 1 | def listify(str): 2 | lis=[] 3 | for i in str: 4 | lis.append(i) 5 | return lis 6 | 7 | def check_permutation_sort(str1,str2): 8 | if len(str1)!=len(str2): 9 | return False 10 | 11 | if (listify(str1)).sort()==(listify(str2)).sort(): 12 | return True 13 | else: 14 | return False 15 | 16 | def check_permutation_count(str1,str2): 17 | if len(str1)!=len(str2): 18 | return False 19 | letters=[0]*128 20 | new_char=listify(str1) 21 | for i in new_char: 22 | letters[ord(i)]=1 23 | for i in range(0,len(str2)): 24 | val=ord(str2[i]) 25 | letters[val]=letters[val]-1 26 | if letters[val]<0: 27 | return False 28 | return True 29 | 30 | str1 = input("Enter First String: ") 31 | str2 = input("Enter Second String: ") 32 | print(check_permutation_sort(str1,str2)) 33 | print(check_permutation_count(str1,str2)) 34 | -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/3. URLify/URLify.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void URLify(String str, int size){ 6 | StringBuilder builder = new StringBuilder(); 7 | for(char ch : str.toCharArray()){ 8 | if(size > 0) { 9 | if (ch != ' ') { 10 | builder.append(ch); 11 | } else { 12 | builder.append("%20"); 13 | } 14 | size--; 15 | } 16 | } 17 | System.out.println("The modified string is " + builder.toString()); 18 | } 19 | 20 | public static void main(String[] args) { // unit testing (optional) 21 | 22 | Scanner scanner = new Scanner(System.in); 23 | String str = scanner.nextLine(); 24 | int size = scanner.nextInt(); 25 | 26 | URLify(str, size); 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/3. URLify/URLify.py: -------------------------------------------------------------------------------- 1 | def listify(str): 2 | lis=[] 3 | for i in str: 4 | lis.append(i) 5 | return lis 6 | 7 | def replace_Spaces(str): 8 | new_str=listify(str) 9 | new_lis=[] 10 | for i in new_str: 11 | if i==' ': 12 | i='%20' 13 | new_lis.append(i) 14 | return ''.join(new_lis) 15 | 16 | 17 | user_string=input("Enter string to URLify") 18 | print(replace_Spaces(user_string)) 19 | -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/4. Palindrome Permutation/Palindrome Permuation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void palindromePermutation(String str){ 6 | //removing all spaces 7 | str = str.replaceAll("\\s", ""); 8 | 9 | int[] length = new int[128]; 10 | for(char ch : str.toCharArray()){ 11 | length[ch]++; 12 | } 13 | 14 | boolean isPalindrome = false; 15 | 16 | for(int i = 0 ; i < 128 ; i++){ 17 | if(length[i]%2 == 0){ 18 | 19 | }else if(length[i]%2 == 1){ 20 | if(isPalindrome){ 21 | isPalindrome = false; 22 | System.out.println("It is not a palindrome of any permutation of given string"); 23 | return; 24 | }else { 25 | isPalindrome = true; 26 | } 27 | } 28 | } 29 | System.out.println("It is a palindrome of any permutation of given string"); 30 | } 31 | 32 | public static void main(String[] args) { // unit testing (optional) 33 | 34 | Scanner scanner = new Scanner(System.in); 35 | String str = scanner.nextLine(); 36 | 37 | palindromePermutation(str); 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/4. Palindrome Permutation/Palindrome Permuation.py: -------------------------------------------------------------------------------- 1 | def check_palindrome_permutation(str): 2 | freq_char={} 3 | count=0 4 | str=str.replace(' ','') 5 | 6 | for i in str: 7 | if i in freq_char: 8 | freq_char[i] += 1 9 | else: 10 | freq_char[i] =1 11 | for i in freq_char: 12 | if freq_char[i]%2==1: 13 | count=count+1 14 | if count>2: 15 | return False 16 | return True 17 | 18 | 19 | str1=input("Enter the string") 20 | print(check_palindrome_permutation(str1)) 21 | -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/5. One Away/One Away.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void oneReplace(String str1, String str2){ 6 | boolean changed = false; 7 | for(int i = 0 ; i < str1.length() ; i++){ 8 | if(str1.charAt(i) != str2.charAt(i)){ 9 | if(changed){ 10 | System.out.println("Not One away"); 11 | return; 12 | } 13 | changed = true; 14 | } 15 | } 16 | System.out.println("One away"); 17 | } 18 | 19 | public static void oneInsert(String str1, String str2){ 20 | int index1 = 0, index2 = 0; 21 | 22 | while(index1 < str1.length() && index2 < str2.length()){ 23 | if(str1.charAt(index1) != str2.charAt(index2)){ 24 | if(index1 != index2){ 25 | System.out.println("Not One away"); 26 | return; 27 | } 28 | index2++; 29 | }else{ 30 | index1++; 31 | index2++; 32 | } 33 | } 34 | System.out.println("One away"); 35 | } 36 | 37 | public static void oneAway(String str1, String str2){ 38 | switch(str1.length() - str2.length()){ 39 | 40 | case 0 : 41 | oneReplace(str1, str2); 42 | break; 43 | case 1 : 44 | oneInsert(str1, str2); 45 | break; 46 | case -1 : 47 | oneInsert(str2, str1); 48 | default : 49 | System.out.println("Not One away"); 50 | break; 51 | } 52 | } 53 | 54 | public static void main(String[] args) { // unit testing (optional) 55 | 56 | Scanner scanner = new Scanner(System.in); 57 | String str1 = scanner.nextLine(); 58 | String str2 = scanner.nextLine(); 59 | 60 | oneAway(str1, str2); 61 | 62 | 63 | } 64 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/6. String Compression/String compression.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void stringCompression(String str){ 6 | int i = 0; 7 | StringBuilder builder = new StringBuilder(); 8 | int count = 1; 9 | while(i < str.length() - 1){ 10 | if(str.charAt(i) == str.charAt(i + 1)){ 11 | count++; 12 | }else{ 13 | builder.append(str.charAt(i)).append(count); 14 | count = 1; 15 | } 16 | i++; 17 | } 18 | builder.append(str.charAt(i)).append(count); 19 | if(str.length() < builder.toString().length()){ 20 | System.out.println(str); 21 | }else{ 22 | System.out.println(builder.toString()); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { // unit testing (optional) 27 | 28 | Scanner scanner = new Scanner(System.in); 29 | String str = scanner.nextLine(); 30 | 31 | stringCompression(str); 32 | 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/8. Zero Matrix/Zero Matrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void zeroMatrix(int[][] arr, int m, int n){ 6 | int i, j, x = 0, y = 0; 7 | for(i = 0 ; i < m ; i++){ 8 | for(j = 0 ; j < n ; j++){ 9 | if(arr[i][j] == 0){ 10 | x = i; 11 | y = j; 12 | } 13 | } 14 | 15 | } 16 | int k; 17 | for(k = 0 ; k < n ; k++){ 18 | arr[k][y] = 0; 19 | } 20 | for(k = 0 ; k < m ; k++){ 21 | arr[x][k] = 0; 22 | } 23 | for(i = 0 ; i < m ; i++){ 24 | for(j = 0 ; j < n ; j++){ 25 | System.out.print(arr[i][j] + " "); 26 | } 27 | System.out.println(""); 28 | } 29 | } 30 | 31 | public static void main(String[] args) { // unit testing (optional) 32 | 33 | Scanner scanner = new Scanner(System.in); 34 | int m = scanner.nextInt(); 35 | int n = scanner.nextInt(); 36 | int[][] mat = new int[m][n]; 37 | 38 | for(int i = 0 ; i < m ; i++){ 39 | for(int j = 0 ; j < n ; j++){ 40 | mat[i][j] = scanner.nextInt(); 41 | } 42 | } 43 | 44 | zeroMatrix(mat, m, n); 45 | 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Chapter - 1 - Arrays and Strings/9. String Rotation/String Rotation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static boolean isSubstring(String str1, String str2){ 6 | return str2.contains(str1); 7 | } 8 | 9 | public static void stringRotation(String str1, String str2){ 10 | str2 = str2 + str2; 11 | if(isSubstring(str1, str2)){ 12 | System.out.println("It is a rotation"); 13 | }else{ 14 | System.out.println("It is not a rotation"); 15 | } 16 | } 17 | 18 | public static void main(String[] args) { // unit testing (optional) 19 | 20 | Scanner scanner = new Scanner(System.in); 21 | String str1 = scanner.nextLine(); 22 | String str2 = scanner.nextLine(); 23 | 24 | stringRotation(str1, str2); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/1. Remove Dups/RemoveDups.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp) count++; 40 | return count; 41 | } 42 | 43 | void printList(Node* head){ 44 | Node* temp = head; 45 | while(temp){ 46 | cout << temp->data << " "; 47 | temp = temp->next; 48 | } 49 | } 50 | 51 | void RemoveDups(Node *head){ 52 | bool vis[10000]; 53 | for(int i = 0 ; i < 10000 ; i++) vis[i] = false; 54 | 55 | Node* temp = head; 56 | Node* ans = NULL; 57 | while(temp){ 58 | if(vis[temp->data] == true){ 59 | 60 | }else{ 61 | vis[temp->data] = true; 62 | ans = pushToLast(ans, temp->data); 63 | } 64 | temp = temp->next; 65 | } 66 | cout << "New list after removal of duplicates is : "; 67 | printList(ans); 68 | } 69 | 70 | int main() { 71 | //Number of nodes in the linked list 72 | int n; 73 | cin >> n; 74 | Node* head = NULL; 75 | for(int i = 0 ; i < n ; i++){ 76 | int x; 77 | cin >> x; 78 | head = pushToLast(head, x); 79 | } 80 | cout << "Old list was : "; 81 | printList(head); 82 | cout << endl; 83 | 84 | RemoveDups(head); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/2. Return Kth to last/ReturnKthToLast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void kthToLast(Node *head){ 55 | int k; 56 | cin >> k; 57 | int n = getLength(head); 58 | if(k > n){ 59 | cout << "k is greater than n !!!!"; 60 | }else{ 61 | Node* slow = head; 62 | Node* fast = head; 63 | while(k--) fast = fast->next; 64 | while(fast){ 65 | slow = slow->next; 66 | fast = fast->next; 67 | } 68 | cout << "kth from last is : " << slow->data; 69 | } 70 | } 71 | 72 | int main() { 73 | //Number of nodes in the linked list 74 | int n; 75 | cin >> n; 76 | Node* head = NULL; 77 | for(int i = 0 ; i < n ; i++){ 78 | int x; 79 | cin >> x; 80 | head = pushToLast(head, x); 81 | } 82 | cout << "Old list was : "; 83 | printList(head); 84 | cout << endl; 85 | 86 | kthToLast(head); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/3. Delete Middle Node/DeleteMiddleNode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void deleteMiddleNode(Node* node){ 55 | if(node == NULL || node->next == NULL){ 56 | cout << "Not possible"; 57 | }else { 58 | node->data = node->next->data; 59 | node->next = node->next->next; 60 | cout << "Deleted the node" << endl; 61 | } 62 | } 63 | 64 | int main() { 65 | int n; 66 | cin >> n; 67 | Node* head = NULL; 68 | for(int i = 0 ; i < n ; i++){ 69 | int x; 70 | cin >> x; 71 | head = pushToLast(head, x); 72 | } 73 | cout << "Old list was : "; 74 | printList(head); 75 | cout << endl; 76 | //Some random node to be deleted 77 | Node* temp = head->next->next->next->next; 78 | deleteMiddleNode(temp); 79 | cout << "new Updated list is : "; 80 | printList(head); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/4. Partition/Partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void deleteMiddleNode(Node* node){ 55 | if(node == NULL || node->next == NULL){ 56 | cout << "Not possible"; 57 | }else { 58 | node->data = node->next->data; 59 | node->next = node->next->next; 60 | cout << "Deleted the node" << endl; 61 | } 62 | } 63 | 64 | void partition(Node* head, int x){ 65 | Node* less = NULL; 66 | Node* more = NULL; 67 | Node* equal = NULL; 68 | Node* temp = head; 69 | while(temp){ 70 | if(temp->data < x){ 71 | less = pushToLast(less, temp->data); 72 | }else if(temp->data > x){ 73 | more = pushToLast(more, temp->data); 74 | }else if(temp->data == x){ 75 | equal = pushToLast(equal, temp->data); 76 | } 77 | temp = temp->next; 78 | } 79 | Node* ans = less; 80 | temp = ans; 81 | while(temp->next) temp = temp->next; 82 | if(equal != NULL) temp->next = equal; 83 | while(temp->next) temp = temp->next; 84 | temp->next = more; 85 | cout << "New list is : "; 86 | printList(ans); 87 | } 88 | 89 | int main() { 90 | int n; 91 | cin >> n; 92 | Node* head = NULL; 93 | for(int i = 0 ; i < n ; i++){ 94 | int x; 95 | cin >> x; 96 | head = pushToLast(head, x); 97 | } 98 | cout << "Old list was : "; 99 | printList(head); 100 | cout << endl; 101 | 102 | int x; 103 | cin >> x; 104 | partition(head, x); 105 | 106 | return 0; 107 | #include 108 | using namespace std; 109 | 110 | struct Node{ 111 | int data; 112 | Node* next; 113 | Node(int x){ 114 | data = x; 115 | next = NULL; 116 | } 117 | }; 118 | 119 | Node* pushToLast(Node* head, int data){ 120 | Node* node = new Node(data); 121 | Node* temp = head; 122 | if(head == NULL){ 123 | head = node; 124 | }else{ 125 | while(temp && temp->next) temp = temp->next; 126 | temp->next = node; 127 | } 128 | return head; 129 | } 130 | 131 | Node* pushToFront(Node* head, int data){ 132 | Node* node = new Node(data); 133 | Node* temp = head; 134 | if(temp == NULL) head = node; 135 | else{ 136 | node->next = head; 137 | head = node; 138 | } 139 | return head; 140 | } 141 | 142 | int getLength(Node* head){ 143 | Node* temp = head; 144 | int count = 0; 145 | while(temp){ 146 | count++; 147 | temp = temp->next; 148 | } 149 | return count; 150 | } 151 | 152 | void printList(Node* head){ 153 | Node* temp = head; 154 | while(temp){ 155 | cout << temp->data << " "; 156 | temp = temp->next; 157 | } 158 | } 159 | 160 | void deleteMiddleNode(Node* node){ 161 | if(node == NULL || node->next == NULL){ 162 | cout << "Not possible"; 163 | }else { 164 | node->data = node->next->data; 165 | node->next = node->next->next; 166 | cout << "Deleted the node" << endl; 167 | } 168 | } 169 | 170 | void partition(Node* head, int x){ 171 | Node* less = NULL; 172 | Node* more = NULL; 173 | Node* equal = NULL; 174 | Node* temp = head; 175 | while(temp){ 176 | if(temp->data < x){ 177 | less = pushToLast(less, temp->data); 178 | }else if(temp->data > x){ 179 | more = pushToLast(more, temp->data); 180 | }else if(temp->data == x){ 181 | equal = pushToLast(equal, temp->data); 182 | } 183 | temp = temp->next; 184 | } 185 | Node* ans = less; 186 | temp = ans; 187 | while(temp->next) temp = temp->next; 188 | if(equal != NULL) temp->next = equal; 189 | while(temp->next) temp = temp->next; 190 | temp->next = more; 191 | cout << "New list is : "; 192 | printList(ans); 193 | } 194 | 195 | int main() { 196 | int n; 197 | cin >> n; 198 | Node* head = NULL; 199 | for(int i = 0 ; i < n ; i++){ 200 | int x; 201 | cin >> x; 202 | head = pushToLast(head, x); 203 | } 204 | cout << "Old list was : "; 205 | printList(head); 206 | cout << endl; 207 | 208 | int x; 209 | cin >> x; 210 | partition(head, x); 211 | 212 | return 0; 213 | } 214 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/5. Sum Lists/SumLists.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void sumLists(Node* head1, Node* head2){ 55 | Node* temp1 = head1; 56 | Node* temp2 = head2; 57 | Node* ans = NULL; 58 | int carry = 0; 59 | while(temp1 && temp2){ 60 | int sum = temp1->data + temp2->data + carry; 61 | ans = pushToLast(ans, sum%10); 62 | carry = sum/10; 63 | temp1 = temp1->next; 64 | temp2 = temp2->next; 65 | } 66 | while(temp1){ 67 | int sum = temp1->data + carry; 68 | ans = pushToLast(ans, sum%10); 69 | carry = sum/10; 70 | temp1 = temp1->next; 71 | } 72 | while(temp2){ 73 | int sum = temp2->data + carry; 74 | ans = pushToLast(ans, sum%10); 75 | carry = sum/10; 76 | temp2 = temp2->next; 77 | } 78 | if(carry != 0){ 79 | ans = pushToLast(ans, carry); 80 | } 81 | cout << "the sum list is : "; 82 | printList(ans); 83 | } 84 | 85 | int main() { 86 | int n1; 87 | cin >> n1; 88 | int n2; 89 | cin >> n2; 90 | Node* head1 = NULL; 91 | for(int i = 0 ; i < n1 ; i++){ 92 | int x; 93 | cin >> x; 94 | head1 = pushToLast(head1, x); 95 | } 96 | Node* head2 = NULL; 97 | for(int i = 0 ; i < n2 ; i++){ 98 | int x; 99 | cin >> x; 100 | head2 = pushToLast(head2, x); 101 | } 102 | // cout << "Old list was : "; 103 | // printList(head); 104 | // cout << endl; 105 | 106 | sumLists(head1, head2); 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/6. Palindrome/Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void palindrome(Node *head){ 55 | Node* ans = NULL; 56 | Node* temp = head; 57 | while(temp){ 58 | ans = pushToFront(ans, temp->data); 59 | temp = temp->next; 60 | } 61 | temp = head; 62 | while(temp && ans){ 63 | if(temp->data != ans->data){ 64 | cout << "Not a Plaindrome"; 65 | return; 66 | } 67 | temp = temp->next; 68 | ans = ans->next; 69 | } 70 | cout << "Palindrome !!!"; 71 | } 72 | 73 | int main() { 74 | int n; 75 | cin >> n; 76 | Node* head = NULL; 77 | for(int i = 0 ; i < n ; i++){ 78 | int x; 79 | cin >> x; 80 | head = pushToLast(head, x); 81 | } 82 | 83 | palindrome(head); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/7. Intersection/Intersection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void intersection(Node* head1, Node* head2){ 55 | int n1 = getLength(head1); 56 | int n2 = getLength(head2); 57 | int diff = abs(n1 - n2); 58 | if(n1 > n2){ 59 | while(diff--) head1 = head1->next; 60 | }else if(n2 > n1){ 61 | while(diff--) head2 = head2->next; 62 | } 63 | while(head1 && head2 && head1 != head2){ 64 | head1 = head1->next; 65 | head2 = head2->next; 66 | } 67 | if(head1 && head2 && head1 == head2) cout << "Found intersection !!!"; 68 | else cout << "Not intersecting !!!"; 69 | } 70 | 71 | int main() { 72 | int n1; 73 | cin >> n1; 74 | int n2; 75 | cin >> n2; 76 | Node* head1 = NULL; 77 | for(int i = 0 ; i < n1 ; i++){ 78 | int x; 79 | cin >> x; 80 | head1 = pushToLast(head1, x); 81 | } 82 | Node* head2 = NULL; 83 | for(int i = 0 ; i < n2 ; i++){ 84 | int x; 85 | cin >> x; 86 | head2 = pushToLast(head2, x); 87 | } 88 | 89 | intersection(head1, head2); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /Chapter - 2 - Linked List/8. Detect Loop/DetectLoop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | 13 | Node* pushToLast(Node* head, int data){ 14 | Node* node = new Node(data); 15 | Node* temp = head; 16 | if(head == NULL){ 17 | head = node; 18 | }else{ 19 | while(temp && temp->next) temp = temp->next; 20 | temp->next = node; 21 | } 22 | return head; 23 | } 24 | 25 | Node* pushToFront(Node* head, int data){ 26 | Node* node = new Node(data); 27 | Node* temp = head; 28 | if(temp == NULL) head = node; 29 | else{ 30 | node->next = head; 31 | head = node; 32 | } 33 | return head; 34 | } 35 | 36 | int getLength(Node* head){ 37 | Node* temp = head; 38 | int count = 0; 39 | while(temp){ 40 | count++; 41 | temp = temp->next; 42 | } 43 | return count; 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* temp = head; 48 | while(temp){ 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | } 53 | 54 | void detectLoop(Node* head){ 55 | Node* slow = head; 56 | Node* fast = head; 57 | slow = slow->next; 58 | fast = fast->next->next; 59 | while(fast && fast->next){ 60 | if(slow == fast) break; 61 | slow = slow->next; 62 | fast = fast->next->next; 63 | } 64 | if(slow == fast){ 65 | slow = head; 66 | while(slow != fast){ 67 | slow = slow->next; 68 | fast = fast->next; 69 | } 70 | cout << "Loop detected and starting node of the loop is " << slow->data; 71 | }else{ 72 | cout << "No loop found"; 73 | } 74 | } 75 | 76 | int main() { 77 | int n; 78 | cin >> n; 79 | Node* head = NULL; 80 | for(int i = 0 ; i < n ; i++){ 81 | int x; 82 | cin >> x; 83 | head = pushToLast(head, x); 84 | } 85 | 86 | detectLoop(head); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Chapter - 3 - Stacks and Queues/2. StackMin/StackMin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data = x; 9 | next = NULL; 10 | } 11 | }; 12 | struct MinStack{ 13 | Node* top = new Node(10000); 14 | void push(int x){ 15 | Node* node = new Node(x); 16 | if(top == NULL){ 17 | top = node; 18 | }else{ 19 | node->next = top; 20 | top = node; 21 | } 22 | } 23 | int min(){ 24 | return top->data; 25 | } 26 | void pop(){ 27 | int x = top->data; 28 | Node* temp = top; 29 | top = top->next; 30 | free(temp); 31 | } 32 | }; 33 | struct Stack{ 34 | 35 | Node* top; 36 | MinStack ms; 37 | 38 | void push(int x){ 39 | if(x < ms.min()) ms.push(x); 40 | Node* node = new Node(x); 41 | if(top == NULL){ 42 | top = node; 43 | }else{ 44 | node->next = top; 45 | top = node; 46 | } 47 | } 48 | 49 | int getMin(){ 50 | return ms.min(); 51 | } 52 | 53 | void pop(){ 54 | if(top == NULL) cout << "Stack is empty, cannot pop more items !!" << endl; 55 | else{ 56 | int x = top->data; 57 | if(x == ms.min()){ 58 | ms.pop(); 59 | } 60 | Node* temp = top; 61 | top = top->next; 62 | free(temp); 63 | } 64 | } 65 | 66 | }; 67 | 68 | int main() { 69 | Stack s; 70 | s.push(4); 71 | s.push(7); 72 | s.push(6); 73 | s.push(10); 74 | s.push(9); 75 | s.push(2); 76 | s.push(5); 77 | 78 | cout << s.getMin() << " "; 79 | s.pop(); 80 | cout << s.getMin() << " "; 81 | s.pop(); 82 | cout << s.getMin() << " "; 83 | s.pop(); 84 | cout << s.getMin() << " "; 85 | s.pop(); 86 | cout << s.getMin() << " "; 87 | s.pop(); 88 | cout << s.getMin() << " "; 89 | s.pop(); 90 | cout << s.getMin() << " "; 91 | s.pop(); 92 | cout << s.getMin() << " "; 93 | 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Chapter - 3 - Stacks and Queues/3. Set Of Stacks/SetOfStacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define topval 4 5 | #define capval 2 6 | 7 | struct Node{ 8 | int data; 9 | Node* next; 10 | Node(int x){ 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | struct Stack{ 17 | Node* top; 18 | int cap = 0; 19 | void push(int x){ 20 | cap++; 21 | Node* node = new Node(x); 22 | if(top == NULL){ 23 | top = node; 24 | }else{ 25 | node->next = top; 26 | top = node; 27 | } 28 | } 29 | void pop(){ 30 | if(top == NULL) cout << "Stack is empty, cannot pop more items !!" << endl; 31 | else{ 32 | cap--; 33 | Node* temp = top; 34 | top = top->next; 35 | free(temp); 36 | } 37 | } 38 | }; 39 | 40 | struct SetOfStacks{ 41 | Stack s[10]; //total to sets of stacks 42 | int top = 0; //stack number for the current top 43 | 44 | int getTop(){ 45 | if(top == topval) return s[top-1].top->data; 46 | if(s[top].cap == 0){ 47 | top--; 48 | cout << "Reducing top !!" << endl; 49 | } 50 | return s[top].top->data; 51 | } 52 | void push(int x){ 53 | if(top == topval){ 54 | cout << "No more stacks remaining !!!" << endl; 55 | }else{ 56 | if(s[top].cap == capval){ 57 | cout << "Increasing top !!" << endl; 58 | top++; 59 | } 60 | if(top != topval){ 61 | cout << "Inserting : " << x << endl; 62 | s[top].push(x); 63 | }else { 64 | cout << "No more stacks remaining !!!" << endl; 65 | } 66 | } 67 | } 68 | void pop(){ 69 | if(top == topval) top--; 70 | if(s[top].cap == 0){ 71 | top--; 72 | cout << "Reducing top !!" << endl; 73 | } 74 | if(top == -1){ 75 | cout << "All stacks are empty !!!" << endl; 76 | }else{ 77 | cout << "Popped : " << s[top].top->data << endl; 78 | s[top].pop(); 79 | } 80 | } 81 | }; 82 | 83 | int main() { 84 | SetOfStacks s; 85 | s.push(4); 86 | s.push(7); 87 | s.push(6); 88 | s.push(10); 89 | s.push(9); 90 | s.push(2); 91 | s.push(5); 92 | cout << "Top is : " << s.getTop() << endl; 93 | s.pop(); 94 | cout << "Top is : " << s.getTop() << endl; 95 | s.pop(); 96 | cout << "Top is : " << s.getTop() << endl; 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /Chapter - 3 - Stacks and Queues/4. MyQueue/MyQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define topval 4 5 | #define capval 2 6 | 7 | struct Node{ 8 | int data; 9 | Node* next; 10 | Node(int x){ 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | struct Stack{ 17 | Node* top; 18 | void push(int x){ 19 | Node* node = new Node(x); 20 | if(top == NULL){ 21 | top = node; 22 | }else{ 23 | node->next = top; 24 | top = node; 25 | } 26 | } 27 | void pop(){ 28 | if(top == NULL) cout << "Stack is empty, cannot pop more items !!" << endl; 29 | else{ 30 | Node* temp = top; 31 | top = top->next; 32 | free(temp); 33 | } 34 | } 35 | }; 36 | 37 | struct MyQueue{ 38 | Stack s; 39 | void enqueue(int x){ 40 | if(s.top == NULL){ 41 | s.push(x); 42 | }else{ 43 | Stack temp; 44 | while(s.top){ 45 | int x = s.top->data; 46 | s.pop(); 47 | temp.push(x); 48 | } 49 | temp.push(x); 50 | while(temp.top){ 51 | int x = temp.top->data; 52 | temp.pop(); 53 | s.push(x); 54 | } 55 | } 56 | } 57 | void dequeue(){ 58 | if(s.top == NULL) cout << "Queue is empty"; 59 | s.pop(); 60 | } 61 | int getTop(){ 62 | if(s.top == NULL) return -1; 63 | return s.top->data; 64 | } 65 | }; 66 | 67 | int main() { 68 | MyQueue s; 69 | s.enqueue(4); 70 | s.enqueue(7); 71 | // s.enqueue(6); 72 | // s.enqueue(10); 73 | // s.enqueue(9); 74 | // s.enqueue(2); 75 | // s.enqueue(5); 76 | // cout << "Top is : " << s.getTop() << endl; 77 | // s.dequeue(); 78 | // cout << "Top is : " << s.getTop() << endl; 79 | // s.dequeue(); 80 | // cout << "Top is : " << s.getTop() << endl; 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Chapter - 4 - Trees and Graphs/1. Route between nodes/RoutesBetweenNodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define n 10 5 | 6 | struct Graph{ 7 | vector adj[n]; 8 | void pushEdge(int from, int to){ 9 | adj[from].push_back(to); 10 | } 11 | }; 12 | 13 | void printPaths(int u, int d, vector adj[], vector visited, int path[], int &path_index){ 14 | visited[u] = true; 15 | path[path_index] = u; 16 | path_index++; 17 | 18 | if (u == d) { 19 | for (int i = 0; i::iterator i; 23 | for (i = adj[u].begin(); i != adj[u].end(); ++i) 24 | if (!visited[*i]) 25 | printPaths(*i, d, adj, visited, path, path_index); 26 | } 27 | path_index--; 28 | visited[u] = false; 29 | } 30 | 31 | void findPath(int from, int to, vector adj[]){ 32 | queue q; 33 | q.push(from); 34 | vector vis(n, false); 35 | vis[from] = true; 36 | bool found = false; 37 | while(!q.empty()){ 38 | int num = q.front(); 39 | q.pop(); 40 | for(auto it : adj[num]){ 41 | if(!vis[it]){ 42 | if(it == to){ 43 | found = true; 44 | break; 45 | } 46 | q.push(it); 47 | vis[it] = true; 48 | } 49 | } 50 | } 51 | if(!found){ 52 | cout << "No Path found !!!" << endl; 53 | }else{ 54 | cout << "Path found !!" << endl; 55 | for(int i = 0 ; i < n ; i++) vis[i] = false; 56 | int path_index = 0; 57 | int path[n]; 58 | printPaths(from, to, adj, vis, path, path_index); 59 | } 60 | } 61 | 62 | int main() { 63 | Graph g; 64 | g.pushEdge(1, 2); 65 | g.pushEdge(2, 4); 66 | g.pushEdge(4, 7); 67 | g.pushEdge(2, 3); 68 | g.pushEdge(3, 5); 69 | g.pushEdge(1, 5); 70 | g.pushEdge(5, 6); 71 | g.pushEdge(6, 7); 72 | 73 | findPath(1, 6, g.adj); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Chapter - 4 - Trees and Graphs/2. Minimal Tree/MinimalTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* left; 7 | Node* right; 8 | Node(int x){ 9 | data = x; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | void inorder(Node* head){ 15 | if(head == NULL) return; 16 | inorder(head->left); 17 | cout << head->data << " "; 18 | inorder(head->right); 19 | } 20 | 21 | Node* MinimalTree(int arr[], int low, int high){ 22 | if(low > high) return NULL; 23 | int mid = (low + high)/2; 24 | Node* node = new Node(arr[mid]); 25 | node->left = MinimalTree(arr, low, mid-1); 26 | node->right = MinimalTree(arr, mid+1, high); 27 | return node; 28 | } 29 | 30 | int main() { 31 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 32 | 33 | Node* head = NULL; 34 | head = MinimalTree(arr, 0, 10 - 1); 35 | inorder(head); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter - 4 - Trees and Graphs/3. List Of Depths/ListOfDepths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int value; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int x){ 9 | value = x; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | Node* buildBinaryTree(Node *root, vector nums, int i, int n){ 15 | if(i < n){ 16 | Node* node = new Node(nums[i]); 17 | root = node; 18 | root->left = buildBinaryTree(root->left, nums, 2*i + 1, n); 19 | root->right = buildBinaryTree(root->right, nums, 2*i + 2, n); 20 | } 21 | return root; 22 | } 23 | 24 | int findDepth(Node* root){ 25 | int count = 0; 26 | while(root){ 27 | root = root->left; 28 | count++; 29 | } 30 | return count; 31 | } 32 | 33 | void levelOrderTraversal(Node *root, vector> &list, int nodes){ 34 | if (root == NULL) return; 35 | 36 | queue q; 37 | q.push(root); 38 | int j = 0; 39 | int found = 1; 40 | while (!q.empty()) { 41 | Node *curr = q.front(); 42 | q.pop(); 43 | if(found == 0){ 44 | j = j+1; 45 | found = pow(2, j); 46 | } 47 | if(curr->left) q.push(curr->left); 48 | if(curr->right) q.push(curr->right); 49 | list[j].push_back(curr->value); 50 | found--; 51 | } 52 | } 53 | 54 | int main() { 55 | // your code goes here 56 | Node* root = NULL; 57 | int n; 58 | cin >> n; 59 | vector nums(n); 60 | for(int i = 0 ; i < n ; i++) cin >> nums[i]; 61 | 62 | root = buildBinaryTree(root, nums, 0, n); 63 | int depth = findDepth(root); 64 | vector> list(depth); 65 | 66 | levelOrderTraversal(root, list, n); 67 | for(int i = 0 ; i < list.size() ; i++){ 68 | for(int j = 0 ; j < list[i].size() ; j++){ 69 | cout << list[i][j] << " "; 70 | } 71 | cout << endl; 72 | } 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Chapter - 4 - Trees and Graphs/4. Check Balanced/CheckBalanced.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* left; 7 | Node* right; 8 | Node(int x){ 9 | data = x; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | Node* insertIntoTree(Node* head, int x){ 15 | //This insertion works for a BST insertion but would work well for this case 16 | if(head == NULL) return new Node(x); 17 | 18 | if(x < head->data){ 19 | head->left = insertIntoTree(head->left, x); 20 | }else if(x > head->data){ 21 | head->right = insertIntoTree(head->right, x); 22 | } 23 | 24 | return head; 25 | } 26 | 27 | int getDepth(Node* head){ 28 | if(head == NULL) return 0; 29 | return (max(getDepth(head->left), getDepth(head->right)) + 1); 30 | } 31 | 32 | bool checkBalanced(Node* head){ 33 | if(head == NULL) return true; 34 | int leftDepth = getDepth(head->left); 35 | int rightDepth = getDepth(head->right); 36 | if(abs(leftDepth - rightDepth) > 1){ 37 | return false; 38 | }else{ 39 | return (checkBalanced(head->left) && checkBalanced(head->right)); 40 | } 41 | } 42 | 43 | int main() { 44 | 45 | Node* head = NULL; 46 | head = insertIntoTree(head, 6); 47 | head = insertIntoTree(head, 3); 48 | head = insertIntoTree(head, 8); 49 | head = insertIntoTree(head, 1); 50 | head = insertIntoTree(head, 7); 51 | head = insertIntoTree(head, 9); 52 | head = insertIntoTree(head, 10); 53 | head = insertIntoTree(head, 2); 54 | head = insertIntoTree(head, 4); 55 | head = insertIntoTree(head, 5); 56 | 57 | bool check = checkBalanced(head); 58 | if(check) cout << "Balanced !!!!"; 59 | else cout << "Not Balanced !!!!"; 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter - 4 - Trees and Graphs/5. Validate BST/ValidateBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode{ 5 | int data; 6 | TreeNode* left; 7 | TreeNode* right; 8 | TreeNode(int x){ 9 | data = x; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | void validateBST(TreeNode* head){ 15 | vector order; 16 | inorder(head, order); 17 | for(int i = 1 ; i < order.size() ; i++){ 18 | if(order[i] < order[i-1]){ 19 | cout << "Not a BST !!!"; 20 | return; 21 | } 22 | } 23 | cout << "It is a BST !!!"; 24 | } 25 | 26 | void inorder(TreeNode* head, vector &order){ 27 | if(head == NULL) return; 28 | inorder(head->left); 29 | order.push_back(head->data); 30 | inorder(head->right); 31 | } 32 | 33 | int main() { 34 | 35 | //Build a binary tree and then pass it into the validateBST function 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter - 8 - Recusrion and Dynamic Programming/1. Triple Step/TripleStep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countSteps(int n){ 5 | vector vec(n+1); 6 | vec[1] = 1; 7 | vec[2] = 2; 8 | vec[3] = 4; 9 | 10 | for(int i = 4 ; i <= n ; i++){ 11 | vec[i] = vec[i-1] + vec[i-2] + vec[i-3]; 12 | } 13 | return vec[n]; 14 | } 15 | 16 | int main(){ 17 | 18 | int n; 19 | cin >> n; 20 | cout << countSteps(n); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cracking-The-Coding-Interview 2 | 3 | This is a repo containing all solutions to the problems in the book Cracking the Coding Interview 6th Edition 4 | --------------------------------------------------------------------------------