├── .DS_Store ├── 0_JavaProgrammingBasics ├── .DS_Store └── Patterns │ ├── Patterns.class │ └── Patterns.java ├── 10_Backtracking ├── BacktrackOnArrays.java ├── BacktrackonStrings.java ├── Classroom.java ├── FindPermutations.java ├── FindSubsets.java ├── GridWays.java ├── NQueens.java └── Sudoku.java ├── 11_ArrayLists ├── ArraylistBasics.java ├── Classroom.java ├── ContainerWithMostWater.java ├── Multidimenional.java ├── PairSum.java ├── PrintMaximum.java ├── PrintReverse.java ├── Sorting.java └── Swap.java ├── 12_LinkedList.java ├── Classroom.java ├── DLL.java ├── DoubleLL.java ├── LL.java ├── LinkedList.java ├── MergeSort.java └── RemoveNth.java ├── 13_Stacks ├── .DS_Store ├── AreaInHistogram.java ├── Classroom.java ├── DuplicateParanthesis.java ├── NextGreater.java ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── StackAL.java ├── StackJCF.java ├── StackLL.java └── ValidParentheses.java ├── 14_Queues ├── Classroom.java ├── DequeInJava.java ├── FirstNonRepeating.java ├── Interleave.java ├── Reversal.java └── SlidingWindowMax.java ├── 15_Greedy ├── ActivitySelection.java ├── Chocola.java ├── Classroom.java ├── FractionalKnapsack.java ├── IndianCoins.java ├── JobSequencing.java ├── MaxChainLength.java └── MinAbsoluteDiff.java ├── 16_BinaryTrees ├── Classroom.java ├── Diameter.java ├── Height.java ├── KAncestor.java ├── KLevel.java ├── LowestCommonAncestor.java ├── MinDistanceBtwNodes.java ├── SubtreeOfAnother.java ├── SumTree.java └── TopView.java ├── 17_Binary Search Trees (1) ├── .DS_Store ├── BST$Node.class ├── BST.class ├── BST.java ├── PrintInRange.java ├── RootToLeaf.java ├── SearchInBST.java └── validateBST.java ├── 17_Binary Search Trees (2) ├── ArrayToBST.java ├── BST.java ├── BSTToBalancedBST.java ├── Classroom.java ├── MergeBSTs.java ├── MirrorBST.java └── largestBSTinBT.java ├── 1_Arrays ├── .DS_Store ├── ArraysCC.java ├── ArraysIntro.class ├── ArraysIntro.java ├── BinarySearch.class ├── BinarySearch.java ├── BuyAndSellStock.class ├── BuyAndSellStock.java ├── LargestNumber.java ├── LargestSubarraySum.class ├── MaxSubarraySum.java ├── PairsinArray.java ├── PrintSubarrays.java ├── ReverseArray.java └── TrappingRainWater.java ├── 2_Basic Sorting ├── .cph │ ├── .BubbleSort.java_41eeaefae5b0b8f6a93948e09c26ddb5.prob │ ├── .CountingSort.java_fc872988cc0feecf565fd1d27ecdccc6.prob │ ├── .InsertionSort.java_4d676f1bf2956dfcb99be310f5de9ed4.prob │ └── .SelectionSort.java_98c44a99488c6a2f2bf88ad05f3317eb.prob ├── BubbleSort.class ├── BubbleSort.java ├── CountingSort.class ├── CountingSort.java ├── InsertionSort.class ├── InsertionSort.java ├── SelectionSort.class └── SelectionSort.java ├── 3_Strings ├── .cph │ ├── .CompareStrings.java_f957cf8eeaefaf2c4efcc497680d5489.prob │ ├── .ConvertToUppercase.java_dbc2b0356514420a452f623e25cc992e.prob │ ├── .FindLargest.java_629b9d19a1faadc5e077c24128405166.prob │ ├── .Palindrome.java_b828988604cd95856efef56006a2f688.prob │ ├── .ShortestPath.java_af266e5bc20f2716ce638dec592f0411.prob │ └── .StringCompression.java_ffbe50ca0f95f60d04af80da36e7c434.prob ├── CompareStrings.class ├── CompareStrings.java ├── ConvertToUppercase.class ├── ConvertToUppercase.java ├── FindLargest.class ├── FindLargest.java ├── Palindrome.class ├── Palindrome.java ├── ShortestPath.class ├── ShortestPath.java ├── StringBasics.java ├── StringCompression.class └── StringCompression.java ├── 4_2DArrays ├── Basics.class ├── Basics.java ├── DiagonalSum.java ├── SearchInSorted.java └── SpiralMatrix.java ├── 5_Bit Manipulation ├── .cph │ ├── .BitOperations.java_009e45164a346fb6b50d90305f5ba190.prob │ └── .FastExponentiation.java_623e029c1cac2b3d5943120746712bc1.prob ├── BitOperations.class ├── BitOperations.java ├── BitwiseOperators.java ├── FastExponentiation.class ├── FastExponentiation.java └── OddEven.java ├── 7_RecursionBasics ├── .DS_Store └── RecursionBasics.java ├── 8_DivideAndConquer ├── .DS_Store ├── DividenConquer.java ├── MergeSort.java ├── QuickSort.java └── RotatedSortedArray.java └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/.DS_Store -------------------------------------------------------------------------------- /0_JavaProgrammingBasics/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/0_JavaProgrammingBasics/.DS_Store -------------------------------------------------------------------------------- /0_JavaProgrammingBasics/Patterns/Patterns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/0_JavaProgrammingBasics/Patterns/Patterns.class -------------------------------------------------------------------------------- /0_JavaProgrammingBasics/Patterns/Patterns.java: -------------------------------------------------------------------------------- 1 | public class Patterns { 2 | //Hollow Rectangle 3 | public static void hollowRectangle(int n, int m) { 4 | for(int i=1; i<=n; i++) { 5 | for(int j=1; j<=m; j++) { 6 | if(i == 1 || j == 1 || i == n || j == m) { 7 | System.out.print("*"); 8 | } else { 9 | System.out.print(" "); 10 | } 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | 16 | public static void inverted_rotated_halfPyramid(int n) { 17 | for(int i=1; i<=n; i++) { 18 | //spaces 19 | for(int j=1; j<=n-i; j++) { 20 | System.out.print(" "); 21 | } 22 | //stars 23 | for(int j=1; j<=i; j++) { 24 | System.out.print("*"); 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | 30 | public static void inverted_halfPyramid_withNumbers(int n) { 31 | for(int i=1; i<=n; i++) { 32 | //numbers 33 | for(int j=1; j<=n-i+1; j++) { 34 | System.out.print(j); 35 | } 36 | //spaces... 37 | System.out.println(); 38 | } 39 | } 40 | 41 | public static void floyds_triangle(int n) { 42 | int number = 1; 43 | for(int i=1; i<=n; i++) { 44 | //numbers 45 | for(int j=1; j<=i; j++) { 46 | System.out.print(number +" "); 47 | number++; 48 | } 49 | //spaces ... 50 | System.out.println(); 51 | } 52 | } 53 | 54 | public static void zero_one_triangle(int n) { 55 | for(int i=1; i<=n; i++) { 56 | //binary bits 57 | for(int j=1; j<=i; j++) { 58 | if((i+j) % 2 == 0) { //even 59 | System.out.print("1"); 60 | } else { //odd 61 | System.out.print("0"); 62 | } 63 | } 64 | System.out.println(); 65 | } 66 | } 67 | 68 | public static void butterfly(int n) { 69 | //1st half 70 | for(int i=1; i<=n; i++) { 71 | //stars 72 | for(int j=1; j<=i; j++) { 73 | System.out.print("*"); 74 | } 75 | 76 | //spaces 77 | for(int j=1; j<=2*(n-i); j++) { 78 | System.out.print(" "); 79 | } 80 | 81 | //stars 82 | for(int j=1; j<=i; j++) { 83 | System.out.print("*"); 84 | } 85 | 86 | System.out.println(); 87 | } 88 | 89 | //2nd half 90 | for(int i=n; i>=1; i--) { 91 | //stars 92 | for(int j=1; j<=i; j++) { 93 | System.out.print("*"); 94 | } 95 | 96 | //spaces 97 | for(int j=1; j<=2*(n-i); j++) { 98 | System.out.print(" "); 99 | } 100 | 101 | //stars 102 | for(int j=1; j<=i; j++) { 103 | System.out.print("*"); 104 | } 105 | 106 | System.out.println(); 107 | } 108 | } 109 | 110 | public static void solid_rhombus(int n) { 111 | for(int i=1; i<=n; i++) { 112 | //spaces 113 | for(int j=1; j<=n-i; j++) { 114 | System.out.print(" "); 115 | } 116 | 117 | //stars 118 | for(int j=1; j<=n; j++) { 119 | System.out.print("*"); 120 | } 121 | 122 | System.out.println(); 123 | } 124 | } 125 | 126 | public static void hollow_rhombus(int n) { 127 | for(int i=1; i<=n; i++) { 128 | //spaces 129 | for(int j=1; j<=n-i; j++) { 130 | System.out.print(" "); 131 | } 132 | 133 | //stars 134 | for(int j=1; j<=n; j++) { 135 | if(i == 1 || j == 1 || i == n || j == n) { 136 | System.out.print("*"); 137 | } else { 138 | System.out.print(" "); 139 | } 140 | } 141 | 142 | System.out.println(); 143 | } 144 | } 145 | 146 | public static void diamond(int n) { 147 | //1st half 148 | for(int i=1; i<=n; i++) { 149 | //spaces 150 | for(int j=1; j<=n-i; j++) { 151 | System.out.print(" "); 152 | } 153 | 154 | //stars 155 | for(int j=1; j<=(2*i)-1; j++) { 156 | System.out.print("*"); 157 | } 158 | System.out.println(); 159 | } 160 | 161 | //2nd half 162 | for(int i=n; i>=0; i--) { 163 | //spaces 164 | for(int j=1; j<=n-i; j++) { 165 | System.out.print(" "); 166 | } 167 | 168 | //stars 169 | for(int j=1; j<=(2*i)-1; j++) { 170 | System.out.print("*"); 171 | } 172 | System.out.println(); 173 | } 174 | } 175 | 176 | public static void number_pyramid(int n) { 177 | for(int i=1; i<=n; i++) { 178 | //spaces 179 | for(int j=1; j<=n-i; j++) { 180 | System.out.print(" "); 181 | } 182 | 183 | //numbers 184 | for(int j=1; j<=i; j++) { 185 | System.out.print(i+" "); 186 | } 187 | 188 | System.out.println(); 189 | } 190 | } 191 | 192 | public static void palindromic_pattern_withNumbers(int n) { 193 | for(int i=1; i<=n; i++) { 194 | //spaces 195 | for(int j=1; j<=n-i; j++) { 196 | System.out.print(" "); 197 | } 198 | 199 | //descending 200 | for(int j=i; j>=1; j--) { 201 | System.out.print(j); 202 | } 203 | 204 | //ascending 205 | for(int j=2; j<=i; j++) { 206 | System.out.print(j); 207 | } 208 | 209 | System.out.println(); 210 | } 211 | 212 | } 213 | 214 | public static void main(String args[]) { 215 | hollowRectangle(4, 5); 216 | inverted_rotated_halfPyramid(4); 217 | inverted_halfPyramid_withNumbers(5); 218 | floyds_triangle(5); 219 | zero_one_triangle(5); 220 | butterfly(4); 221 | solid_rhombus(5); 222 | hollow_rhombus(5); 223 | diamond(4); 224 | number_pyramid(5); 225 | palindromic_pattern_withNumbers(5); 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /10_Backtracking/BacktrackOnArrays.java: -------------------------------------------------------------------------------- 1 | public class BacktrackOnArrays { 2 | public static void printArr(int arr[]) { 3 | for(int i=0; i n || j > m) { 6 | return 0; 7 | } 8 | 9 | int ways = countGridWays(i+1, j, n, m) + countGridWays(i, j+1, n, m); 10 | return ways; 11 | } 12 | public static void main(String args[]) { 13 | int n = 3, m = 3; 14 | System.out.println(countGridWays(0, 0, n-1, m-1)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /10_Backtracking/NQueens.java: -------------------------------------------------------------------------------- 1 | public class NQueens { 2 | public static boolean isSafe(char board[][], int row, int col) { 3 | //up 4 | for(int i=row-1; i>=0; i--) { 5 | if(board[i][col] == 'Q') { 6 | return false; 7 | } 8 | } 9 | 10 | //up left diag 11 | for(int i=row-1, j=col-1; i>=0 && j>=0; i--,j--) { 12 | if(board[i][j] == 'Q') { 13 | return false; 14 | } 15 | } 16 | 17 | //up right diag 18 | for(int i=row-1, j=col+1; i>=0 && j list = new ArrayList<>(); 5 | ArrayList list2 = new ArrayList<>(); 6 | ArrayList list3 = new ArrayList<>(); 7 | 8 | //Add Operation 9 | list.add(1); 10 | list.add(2); 11 | list.add(3); 12 | list.add(3); 13 | System.out.println(list); 14 | 15 | //Get Operation 16 | System.out.println(list.get(0)); 17 | 18 | //Delete Operation 19 | list.remove(1); // 1 is index 20 | System.out.println(list); 21 | 22 | //Modify Operation 23 | list.set(0, 2); 24 | System.out.println(list); 25 | 26 | //Contains Operation 27 | System.out.println(list.contains(3)); 28 | System.out.println(list.contains(4)); 29 | 30 | //Size of list 31 | System.out.println(list.size()); 32 | 33 | //Iteration 34 | for(int i=0; i list, int target) { 6 | 7 | // for(int i=0; i list, int target) { 20 | int lp = 0; 21 | int rp = list.size()-1; 22 | 23 | while(lp != rp) { 24 | //case 1 25 | if(list.get(lp)+list.get(rp) == target) { 26 | return true; 27 | } 28 | 29 | //case 2 30 | if(list.get(lp)+list.get(rp) < target) { 31 | lp++; 32 | } else { 33 | //case 3 34 | rp--; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | 41 | //O(n) 42 | public static boolean pairSum2(ArrayList list, int target) { 43 | int bp = -1; 44 | int n = list.size(); 45 | for(int i=0; i list.get(i+1)) { //breaking point 47 | bp = i; 48 | break; 49 | } 50 | } 51 | 52 | int lp = bp+1; //smallest 53 | int rp = bp; //largest 54 | 55 | while(lp != rp) { 56 | //case1 57 | if(list.get(lp) + list.get(rp) == target) { 58 | return true; 59 | } 60 | 61 | //case 2 62 | if(list.get(lp) + list.get(rp) < target) { 63 | lp = (lp+1) % n; 64 | } else { 65 | //case 3 66 | rp = (n+rp-1) % n; 67 | } 68 | } 69 | 70 | return false; 71 | } 72 | 73 | public static void main(String args[]) { 74 | ArrayList list = new ArrayList<>(); 75 | //11, 15, 6, 8, 9, 10 - Sorted & Rotated 76 | list.add(11); 77 | list.add(15); 78 | list.add(6); 79 | list.add(8); 80 | list.add(9); 81 | list.add(10); 82 | int target = 100; 83 | System.out.println(pairSum2(list, target)); 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /11_ArrayLists/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class ContainerWithMostWater { 3 | //Brute Force - O(n^2) 4 | public static int FMWC(ArrayList height) { 5 | int maxWater = 0; 6 | for(int i=0; i height) { 20 | int leftPtr = 0, rightPtr = height.size()-1; 21 | int maxWater = 0; 22 | while(leftPtr < rightPtr) { 23 | int ht = Math.min(height.get(leftPtr), height.get(rightPtr)); 24 | int width = rightPtr-leftPtr; 25 | int currWater = ht * width; 26 | maxWater = Math.max(currWater, maxWater); 27 | 28 | //move ptrs 29 | if(height.get(leftPtr) < height.get(rightPtr)) { 30 | leftPtr++; 31 | } else { 32 | rightPtr--; 33 | } 34 | } 35 | 36 | return maxWater; 37 | } 38 | public static void main(String args[]) { 39 | ArrayList height = new ArrayList<>(); 40 | height.add(1); 41 | height.add(8); 42 | height.add(6); 43 | height.add(2); 44 | height.add(5); 45 | height.add(4); 46 | height.add(8); 47 | height.add(3); 48 | height.add(7); 49 | 50 | System.out.println(FMWC(height)); 51 | System.out.println(findMaxWaterContainer(height)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /11_ArrayLists/Multidimenional.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class Multidimenional { 3 | public static void main(String args[]) { 4 | ArrayList> mainList = new ArrayList<>(); 5 | ArrayList list1 = new ArrayList<>(); 6 | ArrayList list2 = new ArrayList<>(); 7 | ArrayList list3 = new ArrayList<>(); 8 | 9 | mainList.add(list1); 10 | mainList.add(list2); 11 | mainList.add(list3); 12 | 13 | for(int i=1; i<=5; i++) { 14 | list1.add(i*1); 15 | list2.add(i*2); 16 | list3.add(i*3); 17 | } 18 | 19 | //print all numbers 20 | for(int i=0; i currList = mainList.get(i); 22 | for(int j=0; j list, int target) { 5 | int lp = 0, rp = list.size()-1; 6 | while(lp < rp) { 7 | if(lp+rp == target) { 8 | return true; 9 | } 10 | if(lp+rp < target) { 11 | lp++; 12 | } else { 13 | rp--; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | 20 | public static boolean pairSum2(ArrayList list, int target) { 21 | int bp = -1; 22 | int n = list.size(); 23 | for(int i=0; i list.get(i+1)) { //breaking point 25 | bp = i; 26 | break; 27 | } 28 | } 29 | 30 | int lp = bp+1; //smallest 31 | int rp = bp; //largest 32 | 33 | while(lp != rp) { 34 | //case1 35 | if(list.get(lp) + list.get(rp) == target) { 36 | return true; 37 | } 38 | 39 | //case 2 40 | if(list.get(lp) + list.get(rp) < target) { 41 | lp = (lp+1) % n; 42 | } else { 43 | //case 3 44 | rp = (n+rp-1) % n; 45 | } 46 | } 47 | 48 | return false; 49 | } 50 | 51 | public static void main(String args[]) { 52 | ArrayList list = new ArrayList<>(); 53 | list.add(1); 54 | list.add(2); 55 | list.add(3); 56 | list.add(4); 57 | list.add(5); 58 | int target = 5; 59 | 60 | System.out.println(findSum1(list, target)); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /11_ArrayLists/PrintMaximum.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class PrintMaximum { 4 | public static void printMax(ArrayList list) { 5 | int max = Integer.MIN_VALUE; 6 | for(int i=0; i list = new ArrayList<>(); 11 | list.add(1); 12 | list.add(2); 13 | list.add(3); 14 | list.add(4); 15 | list.add(5); 16 | 17 | reverse(list); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /11_ArrayLists/Sorting.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class Sorting { 5 | 6 | public static void main(String args[]) { 7 | ArrayList list = new ArrayList<>(); 8 | list.add(5); 9 | list.add(3); 10 | list.add(6); 11 | list.add(7); 12 | list.add(1); 13 | 14 | //ascending order 15 | Collections.sort(list); 16 | System.out.println(list); 17 | 18 | //descending order 19 | Collections.sort(list, Collections.reverseOrder()); 20 | System.out.println(list); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /11_ArrayLists/Swap.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class Swap { 3 | public static void swap(ArrayList list, int idx1, int idx2) { 4 | int temp = list.get(idx1); 5 | list.set(idx1, list.get(idx2)); 6 | list.set(idx2, temp); 7 | } 8 | public static void main(String args[]) { 9 | ArrayList list = new ArrayList<>(); 10 | list.add(2); 11 | list.add(5); 12 | list.add(9); 13 | list.add(3); 14 | list.add(6); 15 | 16 | System.out.println(list); 17 | swap(list, 1, 3); 18 | System.out.println(list); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /12_LinkedList.java/Classroom.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | public class Classroom { 4 | public static void main(String args[]) { 5 | //create - 6 | LinkedList ll = new LinkedList<>(); 7 | 8 | //add 9 | ll.addLast(1); 10 | ll.addLast(2); 11 | ll.addFirst(0); 12 | //0->1->2 13 | System.out.println(ll); 14 | //remove 15 | ll.removeLast(); 16 | ll.removeFirst(); 17 | System.out.println(ll); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /12_LinkedList.java/DLL.java: -------------------------------------------------------------------------------- 1 | public class DLL { 2 | public static class Node { 3 | int data; 4 | Node next; 5 | Node prev; 6 | 7 | public Node(int data) { 8 | this.data = data; 9 | this.next = null; 10 | this.prev = null; 11 | } 12 | } 13 | 14 | public static Node head; 15 | public static Node tail; 16 | public static int size; 17 | 18 | public void addFirst(int data) { 19 | size++; 20 | Node newNode = new Node(data); 21 | if(head == null) { 22 | head = tail = newNode; 23 | return; 24 | } 25 | 26 | head.prev = newNode; 27 | newNode.next = head; 28 | head = newNode; 29 | } 30 | 31 | public void print() { 32 | Node temp = head; 33 | while(temp != null) { 34 | System.out.print(temp.data+"<->"); 35 | temp = temp.next; 36 | } 37 | System.out.println("null"); 38 | } 39 | 40 | public int removeFirst() { 41 | if(head == null) { 42 | return Integer.MIN_VALUE; 43 | } 44 | size--; 45 | if(head == tail) { 46 | int val = head.data; 47 | head = tail = null; 48 | return val; 49 | } 50 | int val = head.data; 51 | head = head.next; 52 | head.prev = null; 53 | return val; 54 | } 55 | 56 | public void reverse() { 57 | Node curr = head; 58 | Node prev = null; 59 | Node next; 60 | while(curr != null) { 61 | next = curr.next; 62 | curr.next = prev; 63 | curr.prev = next; 64 | prev = curr; 65 | curr = next; 66 | } 67 | head = prev; 68 | } 69 | public static void main(String args[]) { 70 | DLL dll = new DLL(); 71 | dll.addFirst(3); 72 | dll.addFirst(2); 73 | dll.addFirst(1); 74 | dll.print(); 75 | 76 | dll.removeFirst(); 77 | dll.print(); 78 | dll.addFirst(1); 79 | dll.print(); 80 | 81 | dll.reverse(); 82 | dll.print(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /12_LinkedList.java/DoubleLL.java: -------------------------------------------------------------------------------- 1 | public class DoubleLL { 2 | public class Node { 3 | int data; 4 | Node next; 5 | Node prev; 6 | 7 | public Node(int data) { 8 | this.data = data; 9 | this.next = null; 10 | this.prev = null; 11 | } 12 | } 13 | public static Node head; 14 | public static Node tail; 15 | public static int size; 16 | 17 | //add 18 | public void addFirst(int data) { 19 | Node newNode = new Node(data); 20 | size++; 21 | if(head == null) { 22 | head = tail = newNode; 23 | return; 24 | } 25 | 26 | newNode.next = head; 27 | head.prev = newNode; 28 | head = newNode; 29 | } 30 | 31 | //print 32 | public void print() { 33 | Node temp = head; 34 | while(temp != null) { 35 | System.out.print(temp.data +"<->"); 36 | temp = temp.next; 37 | } 38 | System.out.println("null"); 39 | } 40 | 41 | //remove - removeLast 42 | public int removeFirst() { 43 | if(head == null) { 44 | System.out.println("DLL is empty"); 45 | return Integer.MIN_VALUE; 46 | } 47 | 48 | if(size == 1) { 49 | int val = head.data; 50 | head = tail = null; 51 | size--; 52 | return val; 53 | } 54 | int val = head.data; 55 | head = head.next; 56 | head.prev = null; 57 | size--; 58 | return val; 59 | } 60 | 61 | public void reverse() { 62 | Node curr = head; 63 | Node prev = null; 64 | Node next; 65 | 66 | while(curr != null) { 67 | next = curr.next; 68 | curr.next = prev; 69 | curr.prev = next; 70 | 71 | prev = curr; 72 | curr = next; 73 | } 74 | head = prev; 75 | } 76 | 77 | public static void main(String args[]) { 78 | DoubleLL dll = new DoubleLL(); 79 | dll.addFirst(3); 80 | dll.addFirst(2); 81 | dll.addFirst(1); 82 | 83 | dll.print(); 84 | dll.reverse(); 85 | dll.print(); 86 | 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /12_LinkedList.java/LL.java: -------------------------------------------------------------------------------- 1 | public class LL { 2 | public static class Node { 3 | int data; 4 | Node next; 5 | public Node(int data) { 6 | this.data = data; 7 | this.next = null; 8 | } 9 | } 10 | public static Node head; 11 | public static Node tail; 12 | public static int size; 13 | 14 | public void addFirst(int data) { 15 | size++; 16 | Node newNode = new Node(data); 17 | if(head == null) { 18 | head = tail = newNode; 19 | } else { 20 | newNode.next = head; 21 | head = newNode; 22 | } 23 | } 24 | 25 | public void addLast(int data) { 26 | size++; 27 | Node newNode = new Node(data); 28 | if(head == null) { 29 | head = tail = newNode; 30 | } else { 31 | tail.next = newNode; 32 | tail = newNode; 33 | } 34 | } 35 | 36 | public void print() { 37 | Node temp = head; 38 | while(temp != null) { 39 | System.out.print(temp.data+"->"); 40 | temp = temp.next; 41 | } 42 | System.out.println("null"); 43 | } 44 | 45 | public void add(int idx, int data) { 46 | if(idx == 0) { 47 | addFirst(data); 48 | return; 49 | } 50 | size++; 51 | Node temp = head; 52 | int i = 0; 53 | while(temp != null) { 54 | if(i == idx-1) {//add here 55 | Node newNode = new Node(data); 56 | //insertion at middle 57 | newNode.next = temp.next; 58 | temp.next = newNode; 59 | return; 60 | } 61 | temp = temp.next; 62 | i++; 63 | } 64 | } 65 | 66 | public int removeFirst() { 67 | if(size == 0) { 68 | System.out.println("empty LL"); 69 | return Integer.MIN_VALUE; 70 | } else if(size == 1) { 71 | int val = head.data; 72 | head = tail = null; 73 | size = 0; 74 | return val; 75 | } 76 | int val = head.data; 77 | head = head.next; 78 | size--; 79 | return val; 80 | } 81 | 82 | public int removeLast() { 83 | if(size == 0) { 84 | System.out.println("empty LL"); 85 | return Integer.MIN_VALUE; 86 | } else if(size == 1) { 87 | int val = head.data; 88 | head = tail = null; 89 | size = 0; 90 | return val; 91 | } 92 | 93 | Node temp = head; 94 | for(int i=0; i"); 45 | temp = temp.next; 46 | } 47 | System.out.println("null"); 48 | } 49 | 50 | public void add(int idx, int data) { 51 | if(idx == 0) { 52 | addFirst(data); 53 | return; 54 | } 55 | Node newNode = new Node(data); 56 | size++; 57 | Node temp = head; 58 | int i = 0; 59 | 60 | while(i < idx-1) { 61 | temp = temp.next; 62 | i++; 63 | } 64 | 65 | //i = idx-1; temp -> prev 66 | newNode.next = temp.next; 67 | temp.next = newNode; 68 | } 69 | 70 | public int removeFirst() { 71 | if(size == 0) { 72 | System.out.println("LL is empty"); 73 | return Integer.MIN_VALUE; 74 | } else if(size == 1) { 75 | int val = head.data; 76 | head = tail = null; 77 | size = 0; 78 | return val; 79 | } 80 | int val = head.data; 81 | head = head.next; 82 | size--; 83 | return val; 84 | } 85 | 86 | public int removeLast() { 87 | if(size == 0) { 88 | System.out.println("LL is empty"); 89 | return Integer.MIN_VALUE; 90 | } else if(size == 1) { 91 | int val = head.data; 92 | head = tail = null; 93 | size = 0; 94 | return val; 95 | } 96 | //prev : i = size-2 97 | Node prev = head; 98 | for(int i=0; i last.next = null 273 | prev.next = null; 274 | } 275 | 276 | private Node getMid(Node head) { 277 | Node slow = head; 278 | Node fast = head.next; 279 | 280 | while(fast != null && fast.next != null) { 281 | slow = slow.next; 282 | fast = fast.next.next; 283 | } 284 | return slow; //mid node 285 | } 286 | 287 | private Node merge(Node head1, Node head2) { 288 | Node mergedLL = new Node(-1); 289 | Node temp = mergedLL; 290 | 291 | while(head1 != null && head2 != null) { 292 | if(head1.data <= head2.data) { 293 | temp.next = head1; 294 | head1 = head1.next; 295 | temp = temp.next; 296 | } else { 297 | temp.next = head2; 298 | head2 = head2.next; 299 | temp = temp.next; 300 | } 301 | } 302 | 303 | while(head1 != null) { 304 | temp.next = head1; 305 | head1 = head1.next; 306 | temp = temp.next; 307 | } 308 | 309 | while(head2 != null) { 310 | temp.next = head2; 311 | head2 = head2.next; 312 | temp = temp.next; 313 | } 314 | 315 | return mergedLL.next; 316 | } 317 | 318 | public Node mergeSort(Node head) { 319 | if(head == null || head.next == null) { 320 | return head; 321 | } 322 | 323 | //find mid 324 | Node mid = getMid(head); 325 | //left & right MS 326 | Node rightHead = mid.next; 327 | mid.next = null; 328 | Node newLeft = mergeSort(head); 329 | Node newRight = mergeSort(rightHead); 330 | 331 | //merge 332 | return merge(newLeft, newRight); 333 | } 334 | 335 | public void zigZag() { 336 | //find mid 337 | Node slow = head; 338 | Node fast = head.next; 339 | while(fast != null && fast.next != null) { 340 | slow = slow.next; 341 | fast = fast.next.next; 342 | } 343 | Node mid = slow; 344 | 345 | //reverse 2nd half 346 | Node curr = mid.next; 347 | mid.next = null; 348 | Node prev = null; 349 | Node next; 350 | 351 | while(curr != null) { 352 | next = curr.next; 353 | curr.next = prev; 354 | prev = curr; 355 | curr = next; 356 | } 357 | 358 | Node left = head; 359 | Node right = prev; 360 | Node nextL, nextR; 361 | 362 | //alt merge - zig-zag merge 363 | while(left != null && right != null) { 364 | nextL = left.next; 365 | left.next = right; 366 | nextR = right.next; 367 | right.next = nextL; 368 | 369 | left = nextL; 370 | right = nextR; 371 | } 372 | } 373 | public static void main(String args[]) { 374 | LinkedList ll = new LinkedList(); 375 | ll.addLast(1); 376 | ll.addLast(2); 377 | ll.addLast(3); 378 | ll.addLast(4); 379 | ll.addLast(5); 380 | ll.addLast(6); 381 | //1->2->3->4->5 382 | 383 | ll.print(); 384 | ll.zigZag(); 385 | ll.print(); 386 | } 387 | } 388 | -------------------------------------------------------------------------------- /12_LinkedList.java/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | public class MergeSort { 3 | public static void mergeSort(LinkedList ll) { 4 | 5 | 6 | } 7 | public static void main(String args[]) { 8 | LinkedList ll = new LinkedList<>(); 9 | ll.addLast(4); 10 | ll.addLast(3); 11 | ll.addLast(2); 12 | ll.addLast(1); 13 | 14 | System.out.println(ll); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /12_LinkedList.java/RemoveNth.java: -------------------------------------------------------------------------------- 1 | public class RemoveNth { 2 | public static void main(String args[]) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /13_Stacks/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/13_Stacks/.DS_Store -------------------------------------------------------------------------------- /13_Stacks/AreaInHistogram.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AreaInHistogram { 4 | public static void print(int arr[]) { 5 | for(int i=0; i s = new Stack<>(); 15 | 16 | //Next Right Smallest 17 | for(int i=arr.length-1; i>=0; i--) { 18 | while(!s.isEmpty() && arr[s.peek()] >= arr[i]) { 19 | s.pop(); 20 | } 21 | if(s.isEmpty()) { 22 | nrs[i] = arr.length; 23 | } else { 24 | nrs[i] = s.peek(); 25 | } 26 | s.push(i); 27 | } 28 | 29 | //Next Left Smallest 30 | s = new Stack<>(); 31 | for(int i=0; i= arr[i]) { 33 | s.pop(); 34 | } 35 | if(s.isEmpty()) { 36 | nls[i] = -1; 37 | } else { 38 | nls[i] = s.peek(); 39 | } 40 | s.push(i); 41 | } 42 | 43 | //Calculate Rectangle 44 | int max = 0; 45 | for(int i=0; i s = new Stack<>(); 13 | int nxtGreat[] = new int[arr.length]; 14 | 15 | for(int i=arr.length-1; i>=0; i--) { 16 | while(!s.isEmpty() && arr[s.peek()] <= arr[i]) { 17 | s.pop(); 18 | } 19 | if(s.isEmpty()) { 20 | nxtGreat[i] = -1; 21 | } else { 22 | nxtGreat[i] = arr[s.peek()]; 23 | } 24 | s.push(i); 25 | } 26 | 27 | print(nxtGreat); 28 | } 29 | } -------------------------------------------------------------------------------- /13_Stacks/Question1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //To push an element at the bottom of a stack 3 | public class Question1 { 4 | public static void pushAtBottom(Stack s, int data) { 5 | if(s.isEmpty()) { 6 | s.push(data); 7 | return; 8 | } 9 | 10 | int temp = s.pop(); 11 | pushAtBottom(s, data); 12 | s.push(temp); 13 | } 14 | 15 | public static void main(String args[]) { 16 | Stack stack = new Stack<>(); 17 | stack.push(1); 18 | stack.push(2); 19 | stack.push(3); 20 | pushAtBottom(stack, 4); 21 | 22 | while(!stack.isEmpty()) { 23 | System.out.println(stack.pop()); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /13_Stacks/Question2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //Reverse a String using a Stack 3 | 4 | public class Question2 { 5 | public static void main(String args[]) { 6 | String str = "HelloWorld"; 7 | 8 | Stack s = new Stack<>(); 9 | 10 | int i=0; 11 | while(i s, int data) { 6 | if(s.isEmpty()) { 7 | s.push(data); 8 | return; 9 | } 10 | 11 | int temp = s.pop(); 12 | pushAtBottom(s, data); 13 | s.push(temp); 14 | } 15 | 16 | public static void reverse(Stack s) { 17 | if(s.isEmpty()) { 18 | return; 19 | } 20 | 21 | int top = s.pop(); 22 | reverse(s); 23 | pushAtBottom(s, top); 24 | } 25 | 26 | public static void main(String args[]) { 27 | Stack stack = new Stack<>(); 28 | stack.push(1); 29 | stack.push(2); 30 | stack.push(3); 31 | 32 | while(!stack.isEmpty()) { 33 | System.out.println(stack.pop()); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /13_Stacks/Question4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //Stock Span Problem 3 | 4 | public class Question4 { 5 | public static void stockSpan(int stocks[]) { 6 | Stack s = new Stack<>(); 7 | int span[] = new int[stocks.length]; 8 | span[0] = 1; 9 | s.push(0); 10 | 11 | for(int i=1; i= stocks[s.peek()]) { 14 | s.pop(); 15 | } 16 | if(s.isEmpty()) { 17 | span[i] = i+1; 18 | } else { 19 | int prevHigh = s.peek(); 20 | span[i] = i - prevHigh; 21 | } 22 | 23 | s.push(i); 24 | } 25 | 26 | for(int i=0; i list = new ArrayList<>(); 6 | 7 | public void push(int data) { 8 | list.add(data); 9 | } 10 | 11 | public boolean isEmpty() { 12 | return list.size() == 0; 13 | } 14 | 15 | public int pop() { 16 | if(isEmpty()) { 17 | return -1; 18 | } 19 | int top = list.remove(list.size()-1); 20 | return top; 21 | } 22 | 23 | public int peek() { 24 | if(isEmpty()) { 25 | return -1; 26 | } 27 | return list.get(list.size()-1); 28 | } 29 | } 30 | public static void main(String args[]) { 31 | Stack stack = new Stack(); 32 | stack.push(1); 33 | stack.push(2); 34 | stack.push(3); 35 | stack.push(4); 36 | 37 | while(!stack.isEmpty()) { 38 | System.out.println(stack.peek()); 39 | stack.pop(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /13_Stacks/StackJCF.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class StackJCF { 4 | public static void main(String args[]) { 5 | Stack stack = new Stack<>(); 6 | stack.push(1); 7 | stack.push(2); 8 | stack.push(3); 9 | stack.push(4); 10 | 11 | while(!stack.isEmpty()) { 12 | System.out.println(stack.peek()); 13 | stack.pop(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /13_Stacks/StackLL.java: -------------------------------------------------------------------------------- 1 | //stack using Linked List 2 | public class StackDS { 3 | private static class Node { 4 | int data; 5 | Node next; 6 | 7 | Node(int data) { 8 | this.data = data; 9 | next = null; 10 | } 11 | } 12 | 13 | static class Stack { 14 | public static Node head = null; 15 | public static void push(int data) { 16 | Node newNode = new Node(data); 17 | 18 | if(head == null) { 19 | head = newNode; 20 | return; 21 | } 22 | newNode.next = head; 23 | head = newNode; 24 | } 25 | 26 | public static boolean isEmpty() { 27 | return head == null; 28 | } 29 | 30 | public static int pop() { 31 | if(isEmpty()) { 32 | return -1; 33 | } 34 | Node top = head; 35 | head = head.next; 36 | return top.data; 37 | } 38 | 39 | public static int peek() { 40 | if(isEmpty()) { 41 | return -1; 42 | } 43 | Node top = head; 44 | return top.data; 45 | } 46 | } 47 | 48 | public static void main(String args[]) { 49 | Stack stack = new Stack(); 50 | stack.push(1); 51 | stack.push(2); 52 | stack.push(3); 53 | stack.push(4); 54 | 55 | while(!stack.isEmpty()) { 56 | System.out.println(stack.peek()); 57 | stack.pop(); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /13_Stacks/ValidParentheses.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ValidParentheses { 4 | public static boolean isValid(String str) { 5 | Stack s = new Stack<>(); 6 | int i=0; 7 | while(i deque = new LinkedList<>(); 6 | 7 | public void push(int data) { 8 | deque.addLast(data); 9 | } 10 | 11 | public int pop() { 12 | return deque.removeLast(); 13 | } 14 | 15 | public int peek() { 16 | return deque.getLast(); 17 | } 18 | } 19 | 20 | static class Queue { 21 | Deque deque = new LinkedList<>(); 22 | 23 | public void add(int data) { 24 | deque.addLast(data); 25 | } 26 | 27 | public int remove() { 28 | return deque.removeFirst(); 29 | } 30 | 31 | public int peek() { 32 | return deque.getFirst(); 33 | } 34 | } 35 | public static void main(String args[]) { 36 | Queue q = new Queue(); 37 | q.add(1); 38 | q.add(2); 39 | q.add(3); 40 | 41 | System.out.println("peek = " + q.peek()); 42 | System.out.println(q.remove()); 43 | System.out.println(q.remove()); 44 | System.out.println(q.remove()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /14_Queues/DequeInJava.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DequeInJava { 4 | public static void main(String args[]) { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /14_Queues/FirstNonRepeating.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FirstNonRepeating { 4 | public static void printNonRepeating(String str) { 5 | int freq[] = new int[26]; 6 | Queue q = new LinkedList<>(); 7 | 8 | for(int i=0; i 1) { 13 | q.remove(); 14 | } 15 | 16 | if(q.isEmpty()) { 17 | System.out.print(-1+" "); 18 | } else { 19 | System.out.print(q.peek()+" "); 20 | } 21 | } 22 | 23 | System.out.println(); 24 | } 25 | public static void main(String args[]) { 26 | String str = "aabccxb"; 27 | printNonRepeating(str); 28 | } 29 | } -------------------------------------------------------------------------------- /14_Queues/Interleave.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Interleave { 4 | public static void interleave(Queue q) { 5 | Queue first = new LinkedList<>(); 6 | int sz = q.size(); 7 | for(int i=0; i q = new LinkedList<>(); 18 | q.add(1); 19 | q.add(2); 20 | q.add(3); 21 | q.add(4); 22 | q.add(5); 23 | q.add(6); 24 | q.add(7); 25 | q.add(8); 26 | q.add(9); 27 | q.add(10); 28 | interleave(q); 29 | 30 | while(!q.isEmpty()) { 31 | System.out.print(q.remove()+" "); 32 | } 33 | System.out.println(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /14_Queues/Reversal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Reversal { 4 | public static void reverse(Queue q) { 5 | Stack s = new Stack<>(); 6 | 7 | while(!q.isEmpty()) { 8 | s.push(q.remove()); 9 | } 10 | 11 | while(!s.isEmpty()) { 12 | q.add(s.pop()); 13 | } 14 | } 15 | public static void main(String args[]) { 16 | Queue q = new LinkedList<>(); 17 | q.add(1); 18 | q.add(2); 19 | q.add(3); 20 | q.add(4); 21 | q.add(5); 22 | 23 | reverse(q); 24 | 25 | while(!q.isEmpty()) { 26 | System.out.print(q.remove()+" "); 27 | } 28 | System.out.println(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /14_Queues/SlidingWindowMax.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SlidingWindowMax { 4 | public static void slidingWindowMax(int nums[], int k) { 5 | Deque q = new LinkedList<>(); 6 | 7 | for(int i=0; i o[2])); 18 | 19 | ArrayList ans = new ArrayList<>(); 20 | ans.add(0); 21 | int prevEnd = end[0]; 22 | 23 | for(int i=1; i= prevEnd) { 25 | ans.add(i); 26 | prevEnd = end[i]; 27 | } 28 | } 29 | 30 | System.out.println(ans.size()); 31 | 32 | //print As 33 | for(int i=0; i o[1])); 9 | 10 | int maxLen = 1; 11 | int prevEnd = pairs[0][1]; 12 | for(int i=1; i prevEnd) { 14 | maxLen++; 15 | prevEnd = pairs[i][1]; 16 | } 17 | } 18 | 19 | System.out.println("max chain length = "+ maxLen); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /15_Greedy/MinAbsoluteDiff.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class MinAbsoluteDiff { 3 | public static void main(String args[]) { 4 | int A[] = {4, 1, 8, 7}; 5 | int B[] = {2, 3, 6, 5}; 6 | 7 | Arrays.sort(A); 8 | Arrays.sort(B); 9 | 10 | int sum = 0; 11 | for(int i=0; i path) { 175 | if(root == null) { 176 | return false; 177 | } 178 | 179 | path.add(root); 180 | 181 | if(root.data == n) { 182 | return true; 183 | } 184 | 185 | boolean foundLeft = getPath(root.left, n, path); 186 | boolean foundRight = getPath(root.right, n, path); 187 | 188 | if(foundLeft || foundRight) { 189 | return true; 190 | } 191 | 192 | path.remove(path.size()-1); 193 | return false; 194 | } 195 | 196 | public static Node lca(Node root, int n1, int n2) { //O(n) 197 | ArrayList path1 = new ArrayList<>(); 198 | ArrayList path2 = new ArrayList<>(); 199 | 200 | getPath(root, n1, path1); 201 | getPath(root, n2, path2); 202 | 203 | //last common ancestor 204 | int i = 0; 205 | for(; i i-1th 212 | Node lca = path1.get(i-1); 213 | return lca; 214 | } 215 | 216 | public static Node lca2(Node root, int n1, int n2) { 217 | if(root == null || root.data == n1 || root.data == n2) { 218 | return root; 219 | } 220 | 221 | Node leftLca = lca2(root.left, n1, n2); 222 | Node rightLca = lca2(root.right, n1, n2); 223 | 224 | //leftLCA=val rightLca = null 225 | if(rightLca == null) { 226 | return leftLca; 227 | } 228 | if(leftLca == null) { 229 | return rightLca; 230 | } 231 | 232 | return root; 233 | } 234 | 235 | public static int lcaDist(Node root, int n) { 236 | if(root == null) { 237 | return -1; 238 | } 239 | 240 | if(root.data == n) { 241 | return 0; 242 | } 243 | 244 | int leftDist = lcaDist(root.left, n); 245 | int rightDist = lcaDist(root.right, n); 246 | 247 | if(leftDist == -1 && rightDist == -1) { 248 | return -1; 249 | } else if(leftDist == -1) { 250 | return rightDist+1; 251 | } else { 252 | return leftDist+1; 253 | } 254 | } 255 | 256 | public static int minDist(Node root, int n1, int n2) { 257 | Node lca = lca2(root, n1, n2); 258 | int dist1 = lcaDist(lca, n1); 259 | int dist2 = lcaDist(lca, n2); 260 | 261 | return dist1 + dist2; 262 | } 263 | 264 | public static int KAncestor(Node root, int n, int k) { 265 | if(root == null) { 266 | return -1; 267 | } 268 | if(root.data == n) { 269 | return 0; 270 | } 271 | 272 | int leftDist = KAncestor(root.left, n, k); 273 | int rightDist = KAncestor(root.right, n, k); 274 | 275 | if(leftDist == -1 && rightDist == -1) { 276 | return -1; 277 | } 278 | 279 | int max = Math.max(leftDist, rightDist); 280 | if(max+1 == k) { 281 | System.out.println(root.data); 282 | } 283 | return max+1; 284 | } 285 | 286 | public static int transform(Node root) { 287 | if(root == null) { 288 | return 0; 289 | } 290 | int leftChild = transform(root.left); 291 | int rightChild = transform(root.right); 292 | 293 | int data = root.data; 294 | 295 | int newLeft = root.left == null ? 0 : root.left.data; 296 | int newRight = root.right == null ? 0 : root.right.data; 297 | 298 | root.data = newLeft + leftChild + newRight + rightChild; 299 | return data; 300 | } 301 | 302 | public static void preorder(Node root) { 303 | if(root == null) { 304 | return; 305 | } 306 | 307 | System.out.print(root.data+" "); 308 | preorder(root.left); 309 | preorder(root.right); 310 | } 311 | public static void main(String args[]) { 312 | /* 313 | 1 314 | / \ 315 | 2 3 316 | / \ / \ 317 | 4 5 6 7 318 | 319 | expected sum tree is : 320 | 321 | 27 322 | / \ 323 | 9 13 324 | / \ / \ 325 | 0 0 0 0 326 | */ 327 | Node root = new Node(1); 328 | root.left = new Node(2); 329 | root.right = new Node(3); 330 | root.left.left = new Node(4); 331 | root.left.right = new Node(5); 332 | root.right.left = new Node(6); 333 | root.right.right = new Node(7); 334 | 335 | transform(root); 336 | preorder(root); 337 | } 338 | } 339 | -------------------------------------------------------------------------------- /16_BinaryTrees/Diameter.java: -------------------------------------------------------------------------------- 1 | public class Diameter { 2 | static class Node { 3 | int data; 4 | Node left, right; 5 | 6 | public Node(int data) { 7 | this.data = data; 8 | this.left = null; 9 | this.right = null; 10 | } 11 | } 12 | 13 | public static int height(Node root) { 14 | if(root == null) { 15 | return 0; 16 | } 17 | 18 | return Math.max(height(root.left), height(root.right)) + 1; 19 | } 20 | 21 | //Approach-1 22 | public static int diameter1(Node root) { 23 | if(root == null) { 24 | return 0; 25 | } 26 | 27 | int diamRoot = height(root.left) + height(root.right) + 1; 28 | int diamLeft = diameter1(root.left); 29 | int diamRight = diameter1(root.right); 30 | 31 | return Math.max(diamRoot, Math.max(diamLeft, diamRight)); 32 | } 33 | 34 | static class Info { 35 | int ht; 36 | int diam; 37 | 38 | public Info(int ht, int diam) { 39 | this.ht = ht; 40 | this.diam = diam; 41 | } 42 | } 43 | 44 | public static Info diameter2(Node root) { 45 | if(root == null) { 46 | return new Info(0, 0); 47 | } 48 | 49 | Info left = diameter2(root.left); 50 | Info right = diameter2(root.right); 51 | 52 | int diamRoot = left.ht + right.ht + 1; 53 | 54 | int ht = Math.max(left.ht, right.ht) + 1; 55 | int diam = Math.max(diamRoot, Math.max(left.diam, right.diam)); 56 | 57 | return new Info(ht, diam); 58 | } 59 | public static void main(String args[]) { 60 | /* 61 | 1 62 | / \ 63 | 2 3 64 | / \ \ 65 | 4 5 6 66 | */ 67 | Node root = new Node(1); 68 | root.left = new Node(2); 69 | root.right = new Node(3); 70 | root.left.left = new Node(4); 71 | root.left.right = new Node(5); 72 | root.right.right = new Node(6); 73 | 74 | System.out.println(diameter1(root)); 75 | System.out.println(diameter2(root).diam); 76 | } 77 | } -------------------------------------------------------------------------------- /16_BinaryTrees/Height.java: -------------------------------------------------------------------------------- 1 | public class Height { 2 | static class Node { 3 | int data; 4 | Node left, right; 5 | 6 | public Node(int data) { 7 | this.data = data; 8 | this.left = null; 9 | this.right = null; 10 | } 11 | } 12 | 13 | public static int height(Node root) { 14 | if(root == null) { 15 | return 0; 16 | } 17 | 18 | return Math.max(height(root.left), height(root.right)) + 1; 19 | } 20 | public static void main(String args[]) { 21 | /* 22 | 1 23 | / \ 24 | 2 3 25 | / \ \ 26 | 4 5 6 27 | */ 28 | Node root = new Node(1); 29 | root.left = new Node(2); 30 | root.right = new Node(3); 31 | root.left.left = new Node(4); 32 | root.left.right = new Node(5); 33 | root.right.right = new Node(6); 34 | 35 | System.out.println(height(root)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /16_BinaryTrees/KAncestor.java: -------------------------------------------------------------------------------- 1 | public class KAncestor { 2 | static class Node { 3 | int data; 4 | Node left, right; 5 | 6 | public Node(int data) { 7 | this.data = data; 8 | this.left = null; 9 | this.right = null; 10 | } 11 | } 12 | 13 | public static int KthAncestor(Node root, int n, int k) { 14 | if(root == null) { 15 | return -1; 16 | } 17 | 18 | if(root.data == n) { 19 | return 0; 20 | } 21 | int leftDist = KthAncestor(root.left, n, k); 22 | int rightDist = KthAncestor(root.right, n, k); 23 | 24 | if(leftDist == -1 && rightDist == -1) { 25 | return -1; 26 | } 27 | 28 | int dist = Math.max(leftDist, rightDist); 29 | if(dist+1 == k) { 30 | System.out.println(root.data); 31 | } 32 | return dist + 1; 33 | } 34 | public static void main(String args[]) { 35 | /* 36 | 1 37 | / \ 38 | 2 3 39 | / \ / \ 40 | 4 5 6 7 41 | */ 42 | Node root = new Node(1); 43 | root.left = new Node(2); 44 | root.right = new Node(3); 45 | root.left.left = new Node(4); 46 | root.left.right = new Node(5); 47 | root.right.left = new Node(6); 48 | root.right.right = new Node(7); 49 | 50 | KthAncestor(root, 3, 1); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /16_BinaryTrees/KLevel.java: -------------------------------------------------------------------------------- 1 | public class KLevel { 2 | static class Node { 3 | int data; 4 | Node left, right; 5 | 6 | public Node(int data) { 7 | this.data = data; 8 | this.left = null; 9 | this.right = null; 10 | } 11 | } 12 | 13 | public static void KthLevel(Node root, int k, int level) { 14 | if(root == null) { 15 | return; 16 | } 17 | 18 | if(level == k) { 19 | System.out.print(root.data+" "); 20 | return; 21 | } 22 | KthLevel(root.left, k, level+1); 23 | KthLevel(root.right, k, level+1); 24 | } 25 | 26 | public static void main(String args[]) { 27 | /* 28 | 1 29 | / \ 30 | 2 3 31 | / \ / \ 32 | 4 5 6 7 33 | 34 | */ 35 | Node root = new Node(1); 36 | root.left = new Node(2); 37 | root.right = new Node(3); 38 | root.left.left = new Node(4); 39 | root.left.right = new Node(5); 40 | root.right.left = new Node(6); 41 | root.right.right = new Node(7); 42 | 43 | KthLevel(root, 3, 1); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /16_BinaryTrees/LowestCommonAncestor.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LowestCommonAncestor { 4 | static class Node { 5 | int data; 6 | Node left, right; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.left = null; 11 | this.right = null; 12 | } 13 | } 14 | 15 | public static boolean rootToNodePath(Node root, int node, ArrayList path) { 16 | if(root == null) { 17 | return false; 18 | } 19 | 20 | path.add(root); 21 | 22 | if(root.data == node) { 23 | return true; 24 | } 25 | 26 | if(root.left != null && rootToNodePath(root.left, node, path)) { 27 | return true; 28 | } 29 | if(root.right != null && rootToNodePath(root.right, node, path)) { 30 | return true; 31 | } 32 | 33 | path.remove(path.size()-1); 34 | return false; 35 | } 36 | 37 | public static Node lca(Node root, int n1, int n2) { 38 | ArrayList path1 = new ArrayList<>(); 39 | ArrayList path2 = new ArrayList<>(); 40 | rootToNodePath(root, n1, path1); 41 | rootToNodePath(root, n2, path2); 42 | 43 | int i=0; 44 | for(;i map = new HashMap<>(); 30 | Queue q = new LinkedList<>(); 31 | q.add(new QueueInfo(root, 0)); 32 | q.add(null); 33 | int min = 0, max = 0; 34 | 35 | while(!q.isEmpty()) { 36 | QueueInfo curr = q.remove(); 37 | if(curr == null) { 38 | if(q.isEmpty()) { 39 | break; 40 | } else { 41 | q.add(null); 42 | } 43 | } else { 44 | 45 | //check if HD is encountered for 1st time 46 | if(!map.containsKey(curr.hd)) { 47 | map.put(curr.hd, curr.node.data); 48 | } 49 | 50 | if(curr.node.left != null) { 51 | q.add(new QueueInfo(curr.node.left, curr.hd-1)); 52 | min = Math.min(min, curr.hd-1); 53 | } 54 | 55 | if(curr.node.right != null) { 56 | q.add(new QueueInfo(curr.node.right, curr.hd+1)); 57 | max = Math.max(max, curr.hd+1); 58 | } 59 | } 60 | } 61 | 62 | for(int i=min; i<=max; i++) { 63 | System.out.print(map.get(i)+" "); 64 | } 65 | System.out.println(); 66 | } 67 | public static void main(String args[]) { 68 | /* 69 | 1 70 | / \ 71 | 2 3 72 | / \ / \ 73 | 4 5 6 7 74 | 75 | expected output : 4 2 1 3 7 76 | */ 77 | Node root = new Node(1); 78 | root.left = new Node(2); 79 | root.right = new Node(3); 80 | root.left.left = new Node(4); 81 | root.left.right = new Node(5); 82 | root.right.left = new Node(6); 83 | root.right.right = new Node(7); 84 | 85 | /* 86 | 1 87 | / \ 88 | 2 3 89 | \ 90 | 4 91 | \ 92 | 5 93 | \ 94 | 6 95 | 96 | expected output : 2 1 3 6 97 | */ 98 | Node root2 = new Node(1); 99 | root2.left = new Node(2); 100 | root2.right = new Node(3); 101 | root2.left.right = new Node(4); 102 | root2.left.right.right = new Node(5); 103 | root2.left.right.right.right = new Node(6); 104 | 105 | 106 | topView(root); 107 | topView(root2); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /17_Binary Search Trees (1)/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/17_Binary Search Trees (1)/.DS_Store -------------------------------------------------------------------------------- /17_Binary Search Trees (1)/BST$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/17_Binary Search Trees (1)/BST$Node.class -------------------------------------------------------------------------------- /17_Binary Search Trees (1)/BST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/17_Binary Search Trees (1)/BST.class -------------------------------------------------------------------------------- /17_Binary Search Trees (1)/BST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BST { 4 | static class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | 9 | Node(int data) { 10 | this.data = data; 11 | this.left = null; 12 | this.right = null; 13 | } 14 | } 15 | 16 | public static Node insert(Node root, int val) { 17 | if(root == null) { 18 | root = new Node(val); 19 | return root; 20 | } 21 | 22 | if(root.data > val) { //go to left subtree 23 | root.left = insert(root.left, val); 24 | } else { // go to right subtree 25 | root.right = insert(root.right, val); 26 | } 27 | 28 | return root; 29 | } 30 | 31 | public static void inorder(Node root) { 32 | if(root == null) { 33 | return; 34 | } 35 | 36 | inorder(root.left); 37 | System.out.print(root.data+" "); 38 | inorder(root.right); 39 | } 40 | 41 | public static Node delete(Node root, int val) { 42 | if(root == null) { 43 | return null; 44 | } 45 | 46 | else if(root.data > val) { 47 | root.left = delete(root.left, val); 48 | } 49 | 50 | else if(root.data < val) { 51 | root.right = delete(root.right, val); 52 | } 53 | 54 | else { 55 | //case 1 56 | if(root.left == null && root.right == null) { 57 | return null; 58 | } 59 | 60 | //case 2 61 | else if(root.left == null) { 62 | return root.right; 63 | } 64 | else if(root.right == null) { 65 | return root.left; 66 | } 67 | 68 | //case 3 69 | else { 70 | Node IS = inorderSuccessor(root.right); 71 | root.data = IS.data; 72 | root.right = delete(root.right, IS.data); 73 | } 74 | } 75 | 76 | return root; 77 | } 78 | 79 | public static Node inorderSuccessor(Node root) { 80 | while(root.left!=null) { 81 | root = root.left; 82 | } 83 | 84 | return root; 85 | } 86 | public static void main(String args[]) { 87 | Node root = null; 88 | int values[] = {8, 5, 3, 1, 4, 6, 10, 11, 14}; 89 | for(int i=0; i val) { //go to left subtree 21 | root.left = insert(root.left, val); 22 | } else { // go to right subtree 23 | root.right = insert(root.right, val); 24 | } 25 | 26 | return root; 27 | } 28 | 29 | public static void printInRange(Node root, int k1, int k2) { 30 | if(root == null) { 31 | return; 32 | } 33 | 34 | if(root.data >= k1 && root.data <= k2) { 35 | printInRange(root.left, k1, k2); 36 | System.out.print(root.data+" "); 37 | printInRange(root.right, k1, k2); 38 | } 39 | 40 | else if(root.data > k2) { 41 | printInRange(root.left, k1, k2); 42 | } 43 | 44 | else { 45 | printInRange(root.right, k1, k2); 46 | } 47 | } 48 | 49 | public static void main(String args[]) { 50 | Node root = null; 51 | int values[] = {8, 5, 3, 1, 4, 6, 10, 11, 14}; 52 | for(int i=0; i val) { //go to left subtree 23 | root.left = insert(root.left, val); 24 | } else { // go to right subtree 25 | root.right = insert(root.right, val); 26 | } 27 | 28 | return root; 29 | } 30 | 31 | public static void printPath(ArrayList path) { 32 | for(int i=0; i path) { 39 | if(root == null) { 40 | return; 41 | } 42 | 43 | path.add(root.data); 44 | 45 | if(root.left == null && root.right == null) { 46 | printPath(path); 47 | } 48 | printRoot2Leaf(root.left, path); 49 | printRoot2Leaf(root.right, path); 50 | 51 | path.remove(path.size()-1); 52 | } 53 | 54 | public static void main(String args[]) { 55 | Node root = null; 56 | int values[] = {8, 5, 3, 6, 10, 11, 14}; 57 | for(int i=0; i()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /17_Binary Search Trees (1)/SearchInBST.java: -------------------------------------------------------------------------------- 1 | public class SearchInBST { 2 | static class Node { 3 | int data; 4 | Node left; 5 | Node right; 6 | 7 | Node(int data) { 8 | this.data = data; 9 | this.left = null; 10 | this.right = null; 11 | } 12 | } 13 | 14 | public static Node insert(Node root, int val) { 15 | if(root == null) { 16 | root = new Node(val); 17 | return root; 18 | } 19 | 20 | if(root.data > val) { //go to left subtree 21 | root.left = insert(root.left, val); 22 | } else { // go to right subtree 23 | root.right = insert(root.right, val); 24 | } 25 | 26 | return root; 27 | } 28 | 29 | public static boolean search(Node root, int key) { 30 | if(root == null) { 31 | return false; 32 | } 33 | 34 | if(root.data == key) { 35 | return true; 36 | } 37 | 38 | if(root.data > key) { 39 | return search(root.left, key); 40 | } 41 | 42 | return search(root.right, key); 43 | } 44 | 45 | public static void main(String args[]) { 46 | Node root = null; 47 | int values[] = {5, 1, 3, 4, 2, 7}; 48 | for(int i=0; i val) { //go to left subtree 21 | root.left = insert(root.left, val); 22 | } else { // go to right subtree 23 | root.right = insert(root.right, val); 24 | } 25 | 26 | return root; 27 | } 28 | 29 | public boolean isValidBST(Node root) { 30 | return isValidUtil(root, null, null); 31 | } 32 | 33 | public boolean isValidUtil(Node root, Node leftMax, Node rightMin) { 34 | if(root == null) { 35 | return true; 36 | } 37 | 38 | if(leftMax != null && root.data <= leftMax.data) { 39 | return false; 40 | } 41 | 42 | if(rightMin != null && root.data >= rightMin.data) { 43 | return false; 44 | } 45 | 46 | return isValidUtil(root.left, leftMax, root) 47 | && isValidUtil(root.right, root, rightMin); 48 | } 49 | public static void main(String args[]) { 50 | Node root = null; 51 | int values[] = {8, 5, 3, 6, 10, 11, 14}; 52 | for(int i=0; i()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /17_Binary Search Trees (2)/ArrayToBST.java: -------------------------------------------------------------------------------- 1 | public class ArrayToBST { 2 | static class Node { 3 | int data; 4 | Node left; 5 | Node right; 6 | 7 | public Node(int data) { 8 | this.data = data; 9 | this.left = this.right = null; 10 | } 11 | } 12 | 13 | public static void preorder(Node root) { 14 | if(root == null) { 15 | return; 16 | } 17 | 18 | System.out.print(root.data+" "); 19 | preorder(root.left); 20 | preorder(root.right); 21 | } 22 | 23 | public static Node convertToBST(int arr[], int st, int end) { 24 | if(st > end) { 25 | return null; 26 | } 27 | 28 | int mid = (st+end)/2; 29 | Node curr = new Node(arr[mid]); 30 | curr.left = convertToBST(arr, st, mid-1); 31 | curr.right = convertToBST(arr, mid+1, end); 32 | return curr; 33 | } 34 | public static void main(String args[]) { 35 | int arr[] = {3, 5, 6, 8, 10, 11, 12}; 36 | /* 37 | 8 38 | / \ 39 | 5 11 40 | / \ / \ 41 | 3 6 10 12 42 | expected BST 43 | */ 44 | 45 | Node root = convertToBST(arr, 0, arr.length-1); 46 | preorder(root); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /17_Binary Search Trees (2)/BST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BST { 4 | static class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | 9 | Node(int data) { 10 | this.data = data; 11 | } 12 | } 13 | 14 | public static Node insert(Node root, int val) { 15 | if(root == null) { 16 | root = new Node(val); 17 | return root; 18 | } 19 | 20 | if(root.data > val) { 21 | //left subtree 22 | root.left = insert(root.left, val); 23 | } else { 24 | //right subtree 25 | root.right = insert(root.right, val); 26 | } 27 | return root; 28 | } 29 | 30 | public static void inorder(Node root) { 31 | if(root == null) { 32 | return; 33 | } 34 | inorder(root.left); 35 | System.out.print(root.data+" "); 36 | inorder(root.right); 37 | } 38 | 39 | public static boolean search(Node root, int key) { // O(H) 40 | if(root == null) { 41 | return false; 42 | } 43 | 44 | if(root.data == key) { 45 | return true; 46 | } 47 | 48 | if(root.data > key) { 49 | return search(root.left, key); 50 | } 51 | 52 | else { 53 | return search(root.right, key); 54 | } 55 | } 56 | 57 | public static Node delete(Node root, int val) { 58 | if(root.data < val) { 59 | root.right = delete(root.right, val); 60 | } 61 | else if(root.data > val) { 62 | root.left = delete(root.left, val); 63 | } 64 | else {//voila 65 | //case 1 - leaf node 66 | if(root.left == null && root.right == null) { 67 | return null; 68 | } 69 | 70 | //case 2 - single child 71 | if(root.left == null) { 72 | return root.right; 73 | } 74 | else if(root.right == null) { 75 | return root.left; 76 | } 77 | 78 | //case 3 - both children 79 | Node IS = findInorderSuccessor(root.right); 80 | root.data = IS.data; 81 | root.right = delete(root.right, IS.data); 82 | } 83 | return root; 84 | } 85 | 86 | public static Node findInorderSuccessor(Node root) { 87 | while(root.left != null) { 88 | root = root.left; 89 | } 90 | return root; 91 | } 92 | 93 | public static void printInRange(Node root, int k1, int k2) { 94 | if(root == null) { 95 | return; 96 | } 97 | if(root.data >= k1 && root.data <= k2) { 98 | printInRange(root.left, k1, k2); 99 | System.out.print(root.data+" "); 100 | printInRange(root.right, k1, k2); 101 | } 102 | else if(root.data < k1) { 103 | printInRange(root.left, k1, k2); 104 | } 105 | else { 106 | printInRange(root.right, k1, k2); 107 | } 108 | } 109 | 110 | public static void printPath( ArrayList path) { 111 | for(int i=0; i"); 113 | } 114 | System.out.println("Null"); 115 | } 116 | 117 | public static void printRoot2Leaf(Node root, ArrayList path) { 118 | if(root == null) { 119 | return; 120 | } 121 | 122 | path.add(root.data); 123 | if(root.left == null && root.right == null) { 124 | printPath(path); 125 | } 126 | 127 | printRoot2Leaf(root.left, path); 128 | printRoot2Leaf(root.right, path); 129 | path.remove(path.size()-1); 130 | } 131 | 132 | public static boolean isValidBST(Node root, Node min, Node max) { 133 | if(root == null) { 134 | return true; 135 | } 136 | 137 | if(min != null && root.data <= min.data) { 138 | return false; 139 | } 140 | 141 | else if(max != null && root.data >= max.data) { 142 | return false; 143 | } 144 | 145 | return isValidBST(root.left, min, root) 146 | && isValidBST(root.right, root, max); 147 | } 148 | 149 | public static void main(String args[]) { 150 | int values[] = {1, 1, 1}; 151 | Node root = null; 152 | 153 | for(int i=0; i arr, int st, int end) { 26 | if(st > end) { 27 | return null; 28 | } 29 | 30 | int mid = (st+end)/2; 31 | Node curr = new Node(arr.get(mid)); 32 | curr.left = convertToBST(arr, st, mid-1); 33 | curr.right = convertToBST(arr, mid+1, end); 34 | return curr; 35 | } 36 | 37 | public static void getInorder(Node root, ArrayList arr) { 38 | if(root == null) { 39 | return; 40 | } 41 | getInorder(root.left, arr); 42 | arr.add(root.data); 43 | getInorder(root.right, arr); 44 | } 45 | 46 | public static Node balanceBST(Node root) { 47 | ArrayList arr = new ArrayList<>(); 48 | getInorder(root, arr); 49 | return convertToBST(arr, 0, arr.size()-1); 50 | } 51 | public static int height(Node root) { 52 | if(root == null) 53 | return 0; 54 | 55 | return Math.max(height(root.left), height(root.right)) + 1; 56 | } 57 | public static void main(String args[]) { 58 | /* 59 | 8 60 | / \ 61 | 6 10 62 | / \ 63 | 5 11 64 | / \ 65 | 3 12 66 | given BST 67 | */ 68 | Node root = new Node(8); 69 | root.left = new Node(6); 70 | root.left.left = new Node(5); 71 | root.left.left.left = new Node(3); 72 | 73 | root.right = new Node(10); 74 | root.right.right = new Node(11); 75 | root.right.right.right = new Node(12); 76 | 77 | /* 78 | 8 79 | / \ 80 | 5 11 81 | / \ / \ 82 | 3 6 10 12 83 | expected BST 84 | */ 85 | 86 | root = balanceBST(root); 87 | preorder(root); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /17_Binary Search Trees (2)/Classroom.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Classroom { 4 | static class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | 9 | public Node(int data) { 10 | this.data = data; 11 | this.left = this.right = null; 12 | } 13 | } 14 | 15 | public static void getInorder(Node root, ArrayList arr) { 16 | if(root == null) { 17 | return; 18 | } 19 | 20 | getInorder(root.left, arr); 21 | arr.add(root.data); 22 | getInorder(root.right, arr); 23 | } 24 | 25 | public static Node createBST(ArrayList arr, int st, int end) { 26 | if(st > end) { 27 | return null; 28 | } 29 | int mid = (st+end)/2; 30 | Node root = new Node(arr.get(mid)); 31 | root.left = createBST(arr, st, mid-1); 32 | root.right = createBST(arr, mid+1, end); 33 | return root; 34 | } 35 | public static Node mergeBSTs(Node root1, Node root2) { 36 | //step1 37 | ArrayList arr1 = new ArrayList<>(); 38 | getInorder(root1, arr1); 39 | 40 | //step2 41 | ArrayList arr2 = new ArrayList<>(); 42 | getInorder(root2, arr2); 43 | 44 | //merge 45 | int i=0, j=0; 46 | ArrayList finalArr = new ArrayList<>(); 47 | while(ibalanced BST 68 | return createBST(finalArr, 0, finalArr.size()-1); 69 | } 70 | 71 | public static void preorder(Node root) { 72 | if(root == null) { 73 | return; 74 | } 75 | 76 | System.out.print(root.data+" "); 77 | preorder(root.left); 78 | preorder(root.right); 79 | } 80 | public static void main(String args[]) { 81 | /* 82 | 2 83 | / \ 84 | 1 4 85 | BST1 86 | */ 87 | Node root1 = new Node(2); 88 | root1.left = new Node(1); 89 | root1.right = new Node(4); 90 | 91 | /* 92 | 9 93 | / \ 94 | 3 12 95 | BST2 96 | */ 97 | Node root2 = new Node(9); 98 | root2.left = new Node(3); 99 | root2.right = new Node(12); 100 | //O(n+m) -> linear 101 | 102 | /* 103 | 3 104 | / \ 105 | 1 9 106 | \ / \ 107 | 2 4 12 108 | final ans : BST 109 | */ 110 | Node root = mergeBSTs(root1, root2); 111 | preorder(root); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /17_Binary Search Trees (2)/MergeBSTs.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class MergeBSTs { 4 | static class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | 9 | public Node(int data) { 10 | this.data = data; 11 | this.left = this.right = null; 12 | } 13 | } 14 | 15 | public static void preorder(Node root) { 16 | if(root == null) { 17 | return; 18 | } 19 | 20 | System.out.print(root.data+" "); 21 | preorder(root.left); 22 | preorder(root.right); 23 | } 24 | 25 | public static void getInorder(Node root, ArrayList arr) { 26 | if(root == null) 27 | return; 28 | 29 | getInorder(root.left, arr); 30 | arr.add(root.data); 31 | getInorder(root.right, arr); 32 | } 33 | 34 | public static Node constructBST(ArrayList arr, int st, int end) { 35 | if(st > end) { 36 | return null; 37 | } 38 | 39 | int mid = (st+end)/2; 40 | Node curr = new Node(arr.get(mid)); 41 | curr.left = constructBST(arr, st, mid-1); 42 | curr.right = constructBST(arr, mid+1, end); 43 | 44 | return curr; 45 | } 46 | 47 | public static Node merge(Node root1, Node root2) { 48 | ArrayList arr1 = new ArrayList<>(); 49 | ArrayList arr2 = new ArrayList<>(); 50 | getInorder(root1, arr1); 51 | getInorder(root2, arr2); 52 | 53 | ArrayList finalArr = new ArrayList<>(); 54 | int i=0, j=0; 55 | while(i < arr1.size() && j < arr2.size()) { 56 | if(arr1.get(i) <= arr2.get(j)) { 57 | finalArr.add(arr1.get(i++)); 58 | } else { 59 | finalArr.add(arr2.get(j++)); 60 | } 61 | } 62 | 63 | while(i= rightInfo.min) { 50 | return new Info(false, sz, max, min); 51 | } 52 | 53 | if(leftInfo.isBST && rightInfo.isBST) { 54 | maxBST = Math.max(maxBST, sz); 55 | return new Info(true, sz, max, min); 56 | } 57 | 58 | return new Info(false, sz, max, min); 59 | } 60 | public static void main(String args[]) { 61 | /* 62 | 50 63 | / \ 64 | 30 60 65 | / \ / \ 66 | 5 20 45 70 67 | / \ 68 | 65 80 69 | 70 | given BT 71 | */ 72 | Node root = new Node(50); 73 | root.left = new Node(30); 74 | root.left.left = new Node(5); 75 | root.left.right = new Node(20); 76 | 77 | root.right = new Node(60); 78 | root.right.left = new Node(45); 79 | root.right.right = new Node(70); 80 | root.right.right.left = new Node(65); 81 | root.right.right.right = new Node(80); 82 | 83 | /* 84 | 60 85 | / \ 86 | 45 70 87 | / \ 88 | 65 80 89 | expected BST 90 | */ 91 | 92 | largestBST(root); 93 | System.out.println(maxBST); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /1_Arrays/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/1_Arrays/.DS_Store -------------------------------------------------------------------------------- /1_Arrays/ArraysCC.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ArraysCC { 4 | 5 | public static int buyAndSellStocks(int prices[]) { 6 | int buyPrice = Integer.MAX_VALUE; 7 | int maxProfit = 0; 8 | 9 | for(int i=0; i=0; i--) { 20 | rightMax[i] = Math.max(rightMax[i+1], height[i]); 21 | } 22 | 23 | int water = 0; 24 | for(int i=0; i arr[j+1]) { 10 | //swap 11 | int temp = arr[j]; 12 | arr[j] = arr[j+1]; 13 | arr[j+1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | 19 | public static void modifiedBubbleSort(int arr[]) { 20 | for(int turn=0; turn arr[j+1]) { 24 | //swap 25 | int temp = arr[j]; 26 | arr[j] = arr[j+1]; 27 | arr[j+1] = temp; 28 | swapped = true; 29 | } 30 | } 31 | if(swapped == false) { 32 | break; 33 | } 34 | } 35 | } 36 | 37 | public static void bubbleSortDescending(int arr[]) { 38 | for(int turn=0; turn 0) { 19 | arr[j] = i; 20 | j++; 21 | count[i]--; 22 | } 23 | } 24 | } 25 | 26 | public static void countingSortDescending(int arr[]) { 27 | int largest = Integer.MIN_VALUE; 28 | for(int i=0; i=0; i--) { 38 | while(count[i] > 0) { 39 | arr[j] = i; 40 | j++; 41 | count[i]--; 42 | } 43 | } 44 | } 45 | 46 | public static void printArr(int arr[]) { 47 | for(int i=0; i= 0 && arr[prev] > curr) { 12 | arr[prev+1] = arr[prev]; 13 | prev--; 14 | } 15 | arr[prev+1] = curr; 16 | } 17 | } 18 | 19 | public static void insertionSortDescending(int arr[]) { 20 | for(int i=1; i= 0 && arr[prev] < curr) { 25 | arr[prev+1] = arr[prev]; 26 | prev--; 27 | } 28 | arr[prev+1] = curr; 29 | } 30 | } 31 | 32 | public static void printArr(int arr[]) { 33 | for(int i=0; i arr[j]) { 11 | minPos = j; 12 | } 13 | } 14 | 15 | //swap 16 | int temp = arr[turn]; 17 | arr[turn] = arr[minPos]; 18 | arr[minPos] = temp; 19 | } 20 | } 21 | 22 | public static void selectionSortDescending(int arr[]) { 23 | for(int turn=0; turn 0) { 12 | largest = fruits[i]; 13 | } 14 | } 15 | 16 | System.out.println(largest); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3_Strings/Palindrome.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/3_Strings/Palindrome.class -------------------------------------------------------------------------------- /3_Strings/Palindrome.java: -------------------------------------------------------------------------------- 1 | //Problem : Check if a string is a palindrome 2 | public class Palindrome { 3 | public static boolean isPalindrome(String str) { 4 | for(int i=0; i 1 ? newStr + count.toString() : newStr; 18 | } 19 | 20 | return newStr; 21 | } 22 | public static void main(String args[]) { 23 | String str = "aaabbcccdd"; 24 | System.out.println(compressString(str)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /4_2DArrays/Basics.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/4_2DArrays/Basics.class -------------------------------------------------------------------------------- /4_2DArrays/Basics.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Basics { 4 | public static void main(String args[]) { 5 | int n = 5; //rows 6 | int m = 3; //cols 7 | int marks[][] = new int [n][m]; 8 | 9 | //Taking Input 10 | Scanner sc = new Scanner(System.in); 11 | 12 | for(int i=0; i=0; j--,i++) { 15 | if(i == j) { 16 | continue; 17 | } 18 | sum += matrix[i][j]; 19 | } 20 | 21 | return sum; 22 | } 23 | public static void main(String args[]) { 24 | int matrix[][] = {{1, 2, 3, 4}, 25 | {5, 6, 7, 8}, 26 | {9, 10, 11, 12}, 27 | {13, 14, 15, 16}}; 28 | calcSum(matrix); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /4_2DArrays/SearchInSorted.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | //Problem : Search for key in sorted matrix 4 | 5 | public class SearchInSorted { 6 | public static boolean staircaseSearch(int matrix[][], int key) { 7 | int n = matrix.length, m = matrix[0].length; 8 | int i = 0, j = n-1; 9 | 10 | while(j>=0 && i key) { 21 | j--; 22 | } 23 | 24 | } 25 | 26 | System.out.println("NOT Found"); 27 | return false; 28 | } 29 | public static void main(String args[]) { 30 | int matrix[][] = {{10, 20, 30, 40}, 31 | {15, 25, 35, 45}, 32 | {27, 29, 37, 48}, 33 | {32, 33, 39, 50}}; 34 | int key = 33; 35 | staircaseSearch(matrix, key); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /4_2DArrays/SpiralMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | //Problem : Print a Matrix in Spiral Fashion 4 | 5 | public class SpiralMatrix { 6 | public static void printSpiral(int matrix[][]) { 7 | int startRow = 0; 8 | int startCol = 0; 9 | int endRow = matrix.length-1; 10 | int endCol = matrix[0].length-1; 11 | 12 | //print boundaries 13 | while(startRow <= endCol && startCol <= endCol) { 14 | for(int j=startCol; j<=endCol; j++) { 15 | System.out.print(matrix[startRow][j]+" "); 16 | } 17 | 18 | for(int i=startRow+1; i<=endRow; i++) { 19 | System.out.print(matrix[i][endCol]+" "); 20 | } 21 | 22 | for(int j=endCol-1; j>=startCol; j--) { 23 | if(startRow == endRow) { 24 | break; 25 | } 26 | System.out.print(matrix[endRow][j]+" "); 27 | } 28 | 29 | for(int i=endRow-1; i>startRow; i--) { 30 | if(startCol == endCol) { 31 | break; 32 | } 33 | System.out.print(matrix[i][startCol]+" "); 34 | } 35 | 36 | startCol++; startRow++; 37 | endCol--; endRow--; 38 | } 39 | 40 | System.out.println(); 41 | } 42 | 43 | public static void main(String args[]) { 44 | int matrix[][] = {{1, 2, 3, 4}, 45 | {5, 6, 7, 8}, 46 | {9, 10, 11, 12}, 47 | {13, 14, 15, 16}}; 48 | printSpiral(matrix); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /5_Bit Manipulation/.cph/.BitOperations.java_009e45164a346fb6b50d90305f5ba190.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: BitOperations","url":"/Users/shradhakhapra/Desktop/Java-B/Bit Manipulation/BitOperations.java","tests":[{"id":1651254287515,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/shradhakhapra/Desktop/Java-B/Bit Manipulation/BitOperations.java","group":"local","local":true} -------------------------------------------------------------------------------- /5_Bit Manipulation/.cph/.FastExponentiation.java_623e029c1cac2b3d5943120746712bc1.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: FastExponentiation","url":"/Users/shradhakhapra/Desktop/Java-B/Bit Manipulation/FastExponentiation.java","tests":[{"id":1652275396011,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/shradhakhapra/Desktop/Java-B/Bit Manipulation/FastExponentiation.java","group":"local","local":true} -------------------------------------------------------------------------------- /5_Bit Manipulation/BitOperations.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/5_Bit Manipulation/BitOperations.class -------------------------------------------------------------------------------- /5_Bit Manipulation/BitOperations.java: -------------------------------------------------------------------------------- 1 | public class BitOperations { 2 | //Get ith Bit 3 | public static int getIthBit(int n, int i) { 4 | int bitMask = 1<>1; 63 | } 64 | return setBits; 65 | } 66 | 67 | //a faster method to count set bits 68 | public static int countSetBits2(int n) { 69 | int setBits = 0; 70 | while(n>0) { 71 | // removes the last set bit from curr number 72 | n = n & (n-1); 73 | setBits++; 74 | } 75 | return setBits; 76 | } 77 | public static void main(String args[]) { 78 | System.out.println(countSetBits(9)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /5_Bit Manipulation/BitwiseOperators.java: -------------------------------------------------------------------------------- 1 | public class BitwiseOperators { 2 | public static void main(String args[]) { 3 | int a = 0; 4 | System.out.println(~a); 5 | } 6 | } -------------------------------------------------------------------------------- /5_Bit Manipulation/FastExponentiation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/5_Bit Manipulation/FastExponentiation.class -------------------------------------------------------------------------------- /5_Bit Manipulation/FastExponentiation.java: -------------------------------------------------------------------------------- 1 | public class FastExponentiation { 2 | //to calculate a^n 3 | public static int pow(int a, int n) { 4 | int ans = 1; 5 | 6 | while(n > 0) { 7 | if((n&1) != 0) { //checking bit is 0 or 1 8 | ans = ans * a; 9 | } 10 | a = a*a; 11 | n = n>>1; 12 | } 13 | 14 | return ans; 15 | } 16 | public static void main(String args[]) { 17 | System.out.println(pow(5,3)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /5_Bit Manipulation/OddEven.java: -------------------------------------------------------------------------------- 1 | public class OddEven { 2 | public static void main(String args[]) { 3 | int n = 10; 4 | int bitMask = 1; 5 | if((n & bitMask) == 0) { 6 | System.out.println("even"); 7 | } else { 8 | System.out.println("odd"); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /7_RecursionBasics/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/7_RecursionBasics/.DS_Store -------------------------------------------------------------------------------- /7_RecursionBasics/RecursionBasics.java: -------------------------------------------------------------------------------- 1 | public class RecursionBasics { 2 | public static void printDec(int n) { 3 | if(n == 1) { 4 | System.out.println(n); 5 | return; 6 | } 7 | System.out.print(n+" "); 8 | printDec(n-1); 9 | } 10 | 11 | public static void printInc(int n) { 12 | if(n == 1) { 13 | System.out.print(n+" "); 14 | return; 15 | } 16 | printInc(n-1); 17 | System.out.print(n+" "); 18 | } 19 | 20 | public static int fact(int n) { 21 | if(n == 0) { 22 | return 1; 23 | } 24 | int fnm1 = fact(n-1); 25 | int fn = n * fact(n-1); 26 | return fn; 27 | } 28 | 29 | public static int calcSum(int n) { 30 | if(n == 1) { 31 | return 1; 32 | } 33 | int Snm1 = calcSum(n-1); 34 | int Sn = n + Snm1; 35 | return Sn; 36 | } 37 | 38 | //calculate nth term in fibonacci 39 | public static int fib(int n) { 40 | if(n == 0 || n == 1) { 41 | return n; 42 | } 43 | int fnm1 = fib(n-1); 44 | int fnm2 = fib(n-2); 45 | int fn = fnm1 + fnm2; 46 | return fn; 47 | } 48 | 49 | public static boolean isSorted(int arr[], int i) { 50 | if(i == arr.length-1) { 51 | return true; 52 | } 53 | 54 | if(arr[i] > arr[i+1]) { 55 | return false; 56 | } 57 | 58 | return isSorted(arr, i+1); 59 | } 60 | 61 | public static int firstOccurence(int arr[], int key, int i) { 62 | if(i == arr.length) { 63 | return -1; 64 | } 65 | if(arr[i] == key) { 66 | return i; 67 | } 68 | 69 | return firstOccurence(arr, key, i+1); 70 | } 71 | 72 | public static int lastOccurence(int arr[], int key, int i) { 73 | if(i == arr.length) { 74 | return -1; 75 | } 76 | int isFound = lastOccurence(arr, key, i+1); 77 | if(isFound == -1 && arr[i] == key) { 78 | return i; 79 | } 80 | 81 | return isFound; 82 | } 83 | 84 | public static int power(int x, int n) { 85 | if(n == 0) { 86 | return 1; 87 | } 88 | // int xnm1 = power(x, n-1); 89 | // int xn = x * xnm1; 90 | // return xn; 91 | 92 | return x * power(x, n-1); 93 | } 94 | 95 | public static int optimizedPower(int x, int n) { 96 | if(n == 0) { 97 | return 1; 98 | } 99 | 100 | if(n % 2 == 0) { //even 101 | return optimizedPower(x, n/2) * optimizedPower(x, n/2); 102 | } else { //odd 103 | return x * optimizedPower(x, n/2) * optimizedPower(x, n/2); 104 | } 105 | } 106 | 107 | public static int tilingProblem(int n) { // 2 x n (floor size) 108 | //base case 109 | if(n == 0 || n == 1) { 110 | return 1; 111 | } 112 | 113 | //kaam 114 | //vertical choice 115 | int fnm1 = tilingProblem(n-1); 116 | 117 | //horizontal choice 118 | int fnm2 = tilingProblem(n-2); 119 | 120 | int totWays = fnm1 + fnm2; 121 | return totWays; 122 | } 123 | 124 | public static void removeDuplicates(String str, int idx, StringBuilder newStr, boolean map[]) { 125 | if(idx == str.length()) { 126 | System.out.println(newStr); 127 | return; 128 | } 129 | 130 | //kaam 131 | char currChar = str.charAt(idx); 132 | if(map[currChar-'a'] == true) { 133 | //duplicate 134 | removeDuplicates(str, idx+1, newStr, map); 135 | } else { 136 | map[currChar-'a'] = true; 137 | removeDuplicates(str, idx+1, newStr.append(currChar), map); 138 | } 139 | } 140 | 141 | public static int friendsPairing(int n) { 142 | if(n == 1 || n == 2) { 143 | return n; 144 | } 145 | 146 | return friendsPairing(n-1) + (n-1) * friendsPairing(n-2); 147 | } 148 | 149 | public static void printBinStrings(int n, int lastPlace, String str) { 150 | //base case 151 | if(n == 0) { 152 | System.out.println(str); 153 | return; 154 | } 155 | 156 | //kaam 157 | printBinStrings(n-1, 0, str+"0"); 158 | 159 | if(lastPlace == 0) { 160 | printBinStrings(n-1, 1, str+"1"); 161 | } 162 | } 163 | public static void main(String args[]) { 164 | printBinStrings(3, 0, ""); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /8_DivideAndConquer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shradha-khapra/ApnaCollege-Alpha/faa3f74e4d8066bfdd98defed7458dc44c6266b1/8_DivideAndConquer/.DS_Store -------------------------------------------------------------------------------- /8_DivideAndConquer/DividenConquer.java: -------------------------------------------------------------------------------- 1 | public class DividenConquer { 2 | public static int search(int arr[], int tar, int si, int ei) { 3 | if(si > ei) { 4 | return -1; 5 | } 6 | //kaam 7 | int mid = si + (ei-si)/2; //(si+ei)/2 8 | 9 | //case FOUND 10 | if(arr[mid] == tar) { 11 | return mid; 12 | } 13 | 14 | //mid on L1 15 | if(arr[si] <= arr[mid]) { 16 | //case a : left 17 | if(arr[si] <= tar && tar <= arr[mid]) { 18 | return search(arr, tar, si, mid-1); 19 | } else { 20 | //case b : right 21 | return search(arr, tar, mid+1, ei); 22 | } 23 | } 24 | 25 | //mid on L2 26 | else { 27 | //case c : right 28 | if(arr[mid] <= tar && tar <= arr[ei]) { 29 | return search(arr, tar, mid+1, ei); 30 | } else { 31 | //case d : left 32 | return search(arr, tar, si, mid-1); 33 | } 34 | } 35 | } 36 | public static void main(String args[]) { 37 | int arr[] = {4, 5, 6, 7, 0, 1, 2}; 38 | int target = 6; //output -> 4 39 | int tarIdx = search(arr, target, 0, arr.length-1); 40 | System.out.println(tarIdx); 41 | } 42 | } -------------------------------------------------------------------------------- /8_DivideAndConquer/MergeSort.java: -------------------------------------------------------------------------------- 1 | public class MergeSort { 2 | //sorting method (to do step1(divide) & step2(sort parts)) 3 | public static void mergeSort(int arr[], int si, int ei) { 4 | if(si >= ei) { 5 | return; 6 | } 7 | int mid = si + (ei - si)/2; // or = (si + ei) / 2; 8 | mergeSort(arr, si, mid); 9 | mergeSort(arr, mid+1, ei); 10 | 11 | merge(arr, si, mid, ei); 12 | } 13 | 14 | //merge method to merge the sorted parts 15 | public static void merge(int arr[], int si, int mid, int ei) { 16 | int temp[] = new int[ei-si+1]; 17 | int i = si; //idx for 1st sorted part 18 | int j = mid+1; //idx for 2nd sorted part 19 | int k = 0; //idx for temp; 20 | 21 | while(i <= mid && j <= ei) { 22 | if(arr[i] < arr[j]) { 23 | temp[k] = arr[i]; 24 | i++; 25 | } else { 26 | temp[k] = arr[j]; 27 | j++; 28 | } 29 | k++; 30 | } 31 | 32 | //for leftover elements of 1st sorted part 33 | while(i <= mid) { 34 | temp[k++] = arr[i++]; 35 | } 36 | 37 | //for leftover elements of 2nd sorted part 38 | while(j <= ei) { 39 | temp[k++] = arr[j++]; 40 | } 41 | 42 | //copy temp to original array 43 | for(k=0, i=si; k= ei) { 11 | return; 12 | } 13 | 14 | //pivot variable stores correct idx for pivot element 15 | int pIdx = partition(arr, si, ei); 16 | quickSort(arr, si, pIdx-1); 17 | quickSort(arr, pIdx+1, ei); 18 | } 19 | 20 | public static int partition(int arr[], int si, int ei) { 21 | int pivot = arr[ei]; 22 | int i = si-1; // makes space for elements smaller than pivot 23 | 24 | for(int j=si; j ei) { 5 | return -1; 6 | } 7 | 8 | int mid = (si+ei)/2; 9 | if(tar == arr[mid]) { 10 | return mid; 11 | } 12 | 13 | //mid on L1 14 | if(arr[si] <= arr[mid]) { 15 | if(arr[si] <= tar && tar <= arr[mid] ) { //go left 16 | return search(arr, tar, si, mid-1); 17 | } else { //go right 18 | return search(arr, tar, mid+1, ei); 19 | } 20 | } 21 | 22 | //mid on L2 23 | else { 24 | if(arr[mid] <= tar && tar <= arr[ei]) { //go right 25 | return search(arr, tar, mid+1, ei); 26 | } else { //go left 27 | return search(arr, tar, si, mid-1); 28 | } 29 | } 30 | } 31 | 32 | //using iteration 33 | public int search(int[] arr, int target) { 34 | int si = 0; 35 | int ei = arr.length-1; 36 | 37 | while(si <= ei) { 38 | int mid = (si+ei)/2; 39 | if(arr[mid] == target) { 40 | return mid; 41 | } 42 | //falls on L1 43 | if(arr[si] <= arr[mid]) { 44 | if(arr[si] <= target && target <= arr[mid]) { 45 | //go left 46 | ei = mid-1; 47 | } else { 48 | //go right 49 | si = mid+1; 50 | } 51 | } 52 | 53 | //falls on L2 54 | else { 55 | if(arr[mid] <= target && target <= arr[ei]) { 56 | //go right 57 | si = mid+1; 58 | } else { 59 | //go left 60 | ei = mid-1; 61 | } 62 | } 63 | } 64 | 65 | return -1; 66 | } 67 | public static void main(String args[]) { 68 | // int arr[] = {4,5,6,7,0,1,2}; 69 | // int target = 0; 70 | //expected output = 4; 71 | int arr[] = {4, 5, 6, 1, 2, 3}; 72 | int target = 3; 73 | //expected output = 5; 74 | System.out.println(search(arr, target, 0, arr.length-1)); 75 | 76 | } 77 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alpha 2 | Alpha - Java Placement Course 3 | 4 | 5 | ## Message for AlphaITs <3 6 | Please feel free to add the rest of the code files or any additional topic that you feel will be helpful by creating a pull request for this repo. 7 | 8 | Please note - topics like Space & Time Complexity, OOPs have not been included. 9 | --------------------------------------------------------------------------------