├── Lec1 ├── Odd_Even.java ├── Post_Pre_Inc_and_dec.java └── Test.java ├── Lec10 ├── CamapreTo.java ├── Playing_with_Good_Strings.java ├── Reverse_Words_in_a_String.java ├── String_Basic_Fun.java ├── String_demo.java └── lenWise_SubString.java ├── Lec11 ├── Circular_Sum.java └── MaximumSumSubArray.java ├── Lec12 ├── Kartik_Bhaiya_and_String.java ├── Maximum_Sum_of_Window_Size_k.java └── Product_Less_Than_K.java ├── Lec13 ├── ArrayList_demo.java ├── Diagonal_Traverse.java └── Wrapper_Class_Demo.java ├── Lec14 ├── Fact.java ├── Fact_Tail.java ├── Last_occ.java ├── Power.java ├── Print_Dec.java └── Print_INC.java ├── Lec15 ├── CoinToss.java ├── Coin_Toss_No_2Head.java ├── Fibo.java └── SubSequne.java ├── Lec16 ├── Count_Sequnce.java ├── Dice_Problem.java ├── Duplicate_Permutation.java └── Permutation_String.java ├── Lec17 ├── Generate_Parentheses.java ├── Lexico_Counting.java └── TOH.java ├── Lec18 ├── Coin_Prermuation.java ├── Queen_Combination.java └── Queen_Permutation.java ├── Lec19 ├── Letter_Tile_Possibilities.java └── Palindrome_Partitioning.java ├── Lec2 ├── Fibo.java ├── GCD.java ├── Inverse_of_number.java ├── Loops_Demo.java ├── Pattern_1.java ├── Pattern_11.java ├── Pattern_3.java ├── Pattern_6.java ├── pattern_15.java ├── pattern_9.java ├── pattren_13.java └── pattrern_2.java ├── Lec20 ├── Combination_Sum_II.java ├── Rat_In_Maze.java └── Word_Search.java ├── Lec21 └── Queen_Problem.java ├── Lec22 ├── Merge_Sort.java ├── Merge_Two_Sorted_Array.java ├── Partition_in_Array.java ├── Quick_Sort.java └── Time_and_Space_Demo.java ├── Lec23 ├── Axis_Orbit.java ├── Check_Number.java ├── Sieve_of_Eratosthenes.java └── Time_and_space_2.java ├── Lec24 ├── Power_LogN.java ├── Rand_Quick_Sort.java └── Random_Number.java ├── Lec25 ├── Person.java ├── Person_Client.java ├── Student.java └── Student_Client.java ├── Lec26 ├── Queue.java ├── Queue_Client.java ├── Stack.java ├── Stack_Client.java ├── Student.java └── Student_Client.java ├── Lec27 ├── C.java ├── Client.java ├── DynamicQueue.java ├── DynamicStack.java ├── Method_Overloading.java ├── P.java ├── Student.java └── Student_Client.java ├── Lec28 ├── Insert_Down.java ├── Next_Greater_element.java ├── Reverse_Stack.java └── Stock_Sapn.java ├── Lec29 ├── Histogram.java ├── Maximal_Rectangle.java ├── Maximum_Score.java └── Smallest_Number_From_DI_String.java ├── Lec3 ├── Char_Data_Demo.java ├── Conversion_Fahrenheit_to_Celsius.java ├── Data_Type_Demo.java ├── Dec_to_Bin.java └── Loops.java ├── Lec30 ├── Number_of_Valid_Subarrays.java ├── Pattern_132.java └── SlidingWindowMaximum.java ├── Lec31 ├── Add_Two_Polynomials.java ├── Cycle_Client.java ├── Cycle_Create.java ├── LinkedList.java ├── LinkedList_Client.java ├── Linked_List_Cycle.java ├── Mid_Of_LinkedList.java ├── ReverseLinked.java └── k_Group.java ├── Lec32 ├── BinaryTree_Client.java ├── Binary_Tree.java ├── FlipEquivalent.java ├── PathSum.java └── SymmetricTree.java ├── Lec33 ├── Balanced_Binary_Tree.java ├── Diameter_Opt.java ├── Diameter_of_Binary_Tree.java └── Level_OrderTraversal.java ├── Lec34 ├── Create_Level_Order.java ├── LCA.java └── Sum_Root_to_Leaf_Numbers.java ├── Lec35 ├── BinarySearch_Tree.java ├── Binary_Tree_Cameras.java ├── Delete_BST.java ├── Insert_BST.java ├── Largest_BST_Subtree.java ├── Maximum_Sum_BST.java └── Validate_BST.java ├── Lec36 └── Distribute_Coins.java ├── Lec37 ├── Cars.java ├── Generic_Heap.java ├── Genric_Heap_Client.java ├── Heap.java ├── Heap_Clint.java ├── Kth_Largest_Element.java ├── Merge_Rope.java └── Merge_k_Sorted_Lists.java ├── Lec38 └── Build_Binary_Expression.java ├── Lec39 └── Patching_Array.java ├── Lec4 ├── Pattern_26.java ├── Pattern_27.java ├── Pattern_Double_Sided_Arrow.java └── Pattrrn_25.java ├── Lec40 ├── Graph.java ├── Graph_Bipartite.java ├── Graph_Client.java └── valid_Tree.java ├── Lec41 └── Dijkstra_Algo.java ├── Lec42 ├── Alien_Dictionary.java ├── CourseSchedule_II.java ├── Course_Schedule.java ├── DSU.java ├── Is_Cycle.java └── Topological_Sort.java ├── Lec45 ├── Longest_Cycle_in_a_Graph.java ├── MST_0_1.java ├── MST_0_1_Opti.java ├── Minimize_Malware_Spread.java └── Prims.java ├── Lec46 └── Fibo.java ├── Lec49 ├── Divisor_Game.java ├── House_Robber.java ├── Lis.java └── Lis_Nlong_N.java ├── Lec5 ├── FUnction_demo_3.java ├── Fun_demo.java ├── Function_demo_2.java ├── Is_Armstrong.java └── Pascal_Triangle.java ├── Lec50 ├── Coin_Change_II.java ├── Distinct_Subsequences.java ├── Longest_Common_Subsequence.java ├── Max_Dot_Product.java └── Uncrossed_Lines.java ├── Lec51 ├── Edit_Distance.java ├── Minimum_Falling_Path_Sum.java ├── Minimum_Falling_Path_Sum_II.java ├── Valentine_Magic.java └── Wine_Problem.java ├── Lec52 └── Counting_Sort.java ├── Lec53 ├── Dungeon_Game.java └── MCM.java ├── Lec54 ├── Burst_Balloons.java ├── Cell_Mitosis.java ├── House_Robber_III.java └── Knight_Dialer.java ├── Lec55 ├── Hashing.java ├── Longest_Duplicate_Substring.java └── NAJPF_Spoj.java ├── Lec56 ├── Odd_Even.java ├── Set_Bit.java ├── Set_bit_or_not.java ├── Shift_Opr.java ├── Single_Numbe_II.java ├── Single_Number.java ├── Single_Number_III.java ├── Tavas_and_SaDDas.java └── magic_number.java ├── Lec57 ├── BUSYMAN.java ├── Coin_I.java ├── Ehab_the_Xorcist.java ├── Inc_Exc_Rule.java ├── Preparing_Olympiad.java └── SubSeqnce_Using_Bitmalsing.java ├── Lec58 ├── Count_Good_Substrings.java ├── Longest_Happy_Prefix.java └── NHAY.java ├── Lec59 ├── Maximum_XOR_of_Two_Numbers.java ├── Minimize_XOR.java ├── Trie.java ├── Trie_client.java └── Tries_Contacts.java ├── Lec6 ├── Array_Demo.java ├── Array_Reverse.java ├── Linear_Search.java ├── Min_of_Array.java ├── Reversal_Ago.java ├── Rotate_Array.java ├── Swap_Demo2.java ├── Swap_Demo_1.java ├── Swap_Demo_3.java └── User_Input.java ├── Lec60 ├── Different_Ways_to_Add_Parentheses.java └── Maximum_XOR_With_an_Element_From_Array.java ├── Lec61 ├── Kth_Smallest_Product_of_Two_Sorted_Arrays.java ├── Median_of_Two_Sorted_Arrays.java └── Two_Arrays_and_Sum_of_Functions.java ├── Lec62 ├── Day_at_the_Beach.java ├── Find_Duplicate_Subtrees.java ├── Recover_Binary_Search_Tree.java └── Recover_Binary_Search_Tree_Space_Op.java ├── Lec63 ├── Additive_Number.java ├── Find_Median_from_Data_Stream.java ├── Rock_and_Lever.java └── Vertical_Order_Traversal.java ├── Lec64 ├── Catalan_number.java └── Number_of_Ways_to_Divide_a_Long_Corridor.java ├── Lec7 ├── Bubble_Sort.java ├── Gas_Station.java ├── Next_Permutation.java ├── Product_of_Array_Except_Self.java └── Rain_Water.java ├── Lec8 ├── AGGRCOWS.java ├── Binary_Search.java ├── Book_Allocation.java ├── First_Bad_Version.java └── Square_root.java └── Lec9 ├── Divisible_Subarrays.java ├── Matrix_Search.java ├── Spiral_Print.java └── Two_DArray_Demo.java /Lec1/Odd_Even.java: -------------------------------------------------------------------------------- 1 | package Lec1; 2 | 3 | import java.util.*; 4 | 5 | public class Odd_Even { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | if (n > 7) { 12 | System.out.println("hey"); 13 | } else if (n % 2 == 0) { 14 | System.out.println("Even"); 15 | } else { 16 | System.out.println("odd"); 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lec1/Post_Pre_Inc_and_dec.java: -------------------------------------------------------------------------------- 1 | package Lec1; 2 | 3 | public class Post_Pre_Inc_and_dec { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int x = 9; 8 | System.out.println(x++);// x = x+1; 9 | System.out.println(x); 10 | int c = x + x++ + 8 + x + x--; 11 | System.out.println(c); 12 | System.out.println(--x); 13 | 14 | int p = 11 + x++ + ++x + --x + x-- + 7 + x + 1; 15 | System.out.println(p); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec1/Test.java: -------------------------------------------------------------------------------- 1 | package Lec1; 2 | 3 | public class Test { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println("Welcome to Coding Blocks"); 8 | System.out.print("Welcome to Coding Blocks"); 9 | System.out.print("hey"); 10 | System.out.println("Bye"); 11 | int a= 7; 12 | int b=9; 13 | int c= a+b; 14 | System.out.println(c); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lec10/CamapreTo.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | public class CamapreTo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "raj"; 8 | String str2 = "raju"; 9 | System.out.println(iscampareto(str, str2)); 10 | 11 | } 12 | 13 | public static int iscampareto(String s1, String s2) { 14 | int i = 0; 15 | int j = 0; 16 | while (i < s1.length() && j < s2.length()) { 17 | if (s1.charAt(i) > s2.charAt(j)) { 18 | return s1.charAt(i) - s2.charAt(j); 19 | } else if (s1.charAt(i) < s2.charAt(j)) { 20 | return s2.charAt(i) - s1.charAt(j); 21 | } 22 | i++; 23 | j++; 24 | } 25 | return s1.length() - s2.length(); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec10/Playing_with_Good_Strings.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | public class Playing_with_Good_Strings { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "sqjwqffhaaioqabkkaeejhvjh"; 8 | System.out.println(lenofgoodString(str)); 9 | 10 | } 11 | 12 | public static int lenofgoodString(String s) { 13 | int ans = 0; 14 | int c = 0; 15 | for (int i = 0; i < s.length(); i++) { 16 | char ch = s.charAt(i); 17 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { 18 | c++; 19 | } else { 20 | ans = Math.max(ans, c); 21 | c = 0; 22 | } 23 | 24 | } 25 | ans = Math.max(ans, c); 26 | return ans; 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec10/Reverse_Words_in_a_String.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | public class Reverse_Words_in_a_String { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println(reverseWord("a good example")); 8 | } 9 | 10 | public static String reverseWord(String s) { 11 | s = s.trim(); 12 | String ans = ""; 13 | String[] arr = s.split("\s+"); 14 | for (int i = arr.length - 1; i >= 0; i--) { 15 | ans = ans + arr[i] + " "; 16 | } 17 | return ans.trim(); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lec10/String_Basic_Fun.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | public class String_Basic_Fun { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "Helloheyokaybye"; 8 | System.out.println(str.length()); 9 | System.out.println(str.charAt(4)); 10 | // String str1 = "Helloheyokaybye"; 11 | // System.out.println(str.equals(str1)); 12 | String str1 = "raj"; 13 | String str2 = "rajs"; 14 | System.out.println(str1.compareTo(str2)); 15 | System.out.println(str.substring(3, 5)); 16 | String s1 = " Coding Blocks "; 17 | System.out.println(s1); 18 | System.out.println(s1.trim()); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lec10/String_demo.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | import java.util.Scanner; 4 | 5 | public class String_demo { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | String s = "Hello"; 10 | String s1 = new String("Hello"); 11 | String s2 = "Hello"; 12 | System.out.println(s); 13 | System.out.println(s1); 14 | System.out.println(s2); 15 | System.out.println(s == s1); 16 | System.out.println(s==s2); 17 | String s4 = "Hello"+"Bye"; 18 | String s5 = "Hello"+"Bye"; 19 | s= s +"Bye"; 20 | System.out.println(s4==s5); 21 | System.out.println(s4==s); 22 | Scanner sc = new Scanner(System.in); 23 | String ss = sc.nextLine(); 24 | System.out.println(ss); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Lec10/lenWise_SubString.java: -------------------------------------------------------------------------------- 1 | package Lec10; 2 | 3 | public class lenWise_SubString { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "Hello"; 8 | SubString(str); 9 | 10 | } 11 | 12 | public static void SubString(String str) { 13 | for (int len = 1; len <= str.length(); len++) { 14 | for (int j = len; j <= str.length(); j++) { 15 | int i = j - len; 16 | System.out.println(str.substring(i, j)); 17 | 18 | } 19 | } 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lec11/Circular_Sum.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Circular_Sum { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 8, -8, 9, -9, 10, -11, 12 }; 8 | 9 | } 10 | 11 | public static int maxsum(int[] arr) { 12 | int linearsum = kadensAlgo(arr); 13 | int sum = 0; 14 | for (int i = 0; i < arr.length; i++) { 15 | sum = sum + arr[i]; 16 | arr[i] = arr[i] * -1; 17 | } 18 | int middlesum = kadensAlgo(arr); 19 | sum += middlesum; 20 | if (sum == 0) { 21 | return linearsum; 22 | } 23 | return Math.max(sum, linearsum); 24 | 25 | } 26 | 27 | public static int kadensAlgo(int[] arr) { 28 | int ans = Integer.MIN_VALUE; 29 | int sum = 0; 30 | for (int i = 0; i < arr.length; i++) { 31 | sum += arr[i]; 32 | ans = Math.max(ans, sum); 33 | if (sum < 0) { 34 | sum = 0; 35 | } 36 | } 37 | return ans; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lec11/MaximumSumSubArray.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class MaximumSumSubArray { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 3, -7, 4, 6 }; 8 | System.out.println(maxsum(arr)); 9 | 10 | } 11 | 12 | public static int maxsum(int[] arr) { 13 | int ans = Integer.MIN_VALUE; 14 | for (int i = 0; i < arr.length; i++) { 15 | int sum = 0; 16 | for (int j = i; j < arr.length; j++) { 17 | sum += arr[j]; 18 | ans = Math.max(ans, sum); 19 | } 20 | 21 | } 22 | return ans; 23 | } 24 | 25 | public static int kadensAlgo(int[] arr) { 26 | int ans = Integer.MIN_VALUE; 27 | int sum = 0; 28 | for (int i = 0; i < arr.length; i++) { 29 | sum += arr[i]; 30 | ans = Math.max(ans, sum); 31 | if (sum < 0) { 32 | sum = 0; 33 | } 34 | } 35 | return ans; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Lec12/Kartik_Bhaiya_and_String.java: -------------------------------------------------------------------------------- 1 | package Lec12; 2 | 3 | public class Kartik_Bhaiya_and_String { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "aaabababbababaaaaaab"; 8 | int k = 3; 9 | int flipa = perfectnessofastring(str, 'a', k); 10 | int flipb = perfectnessofastring(str, 'b', k); 11 | System.out.println(Math.max(flipa, flipb)); 12 | 13 | } 14 | 15 | public static int perfectnessofastring(String str, char ch, int k) { 16 | 17 | int si = 0; 18 | int ei = 0; 19 | int ans = 0; 20 | int flip = 0; 21 | while (ei < str.length()) { 22 | if (str.charAt(ei) == ch) { 23 | flip++; 24 | } 25 | 26 | while (flip > k) { 27 | 28 | if (str.charAt(si) == ch) { 29 | flip--; 30 | } 31 | si++; 32 | } 33 | ans = Math.max(ans, ei - si + 1); 34 | ei++; 35 | 36 | } 37 | return ans; 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Lec12/Maximum_Sum_of_Window_Size_k.java: -------------------------------------------------------------------------------- 1 | package Lec12; 2 | 3 | public class Maximum_Sum_of_Window_Size_k { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 3, 4, 1, 5, 6, 7, 1, 8, 9, 11, 5, 7 }; 8 | int k = 3; 9 | System.out.println(MaximumSumofwindowsizek(arr, k)); 10 | } 11 | 12 | public static int MaximumSumofwindowsizek(int[] arr, int k) { 13 | int sum = 0; 14 | for (int i = 0; i < k; i++) { 15 | sum = sum + arr[i]; 16 | } 17 | int ans = sum; 18 | for (int i = k; i < arr.length; i++) { 19 | sum += arr[i]; 20 | 21 | sum -= arr[i - k]; 22 | ans = Math.max(ans, sum); 23 | 24 | } 25 | return ans; 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Lec12/Product_Less_Than_K.java: -------------------------------------------------------------------------------- 1 | package Lec12; 2 | 3 | public class Product_Less_Than_K { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 1, 2, 4, 3, 5, 2, 3 }; 8 | int k = 10; 9 | System.out.println(NoofSubArray(arr, k)); 10 | 11 | } 12 | 13 | public static int NoofSubArray(int[] arr, int k) { 14 | 15 | int ans = 0; 16 | int si = 0; 17 | int ei = 0; 18 | int p = 1; 19 | while (ei < arr.length) { 20 | // window grow 21 | p = p * arr[ei]; 22 | 23 | // window shrink kro 24 | while (p >= k && si<=ei) { 25 | p = p / arr[si]; 26 | si++; 27 | } 28 | 29 | // ans cal 30 | ans = ans + ei - si + 1; 31 | ei++; 32 | 33 | } 34 | return ans; 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Lec13/ArrayList_demo.java: -------------------------------------------------------------------------------- 1 | package Lec13; 2 | 3 | import java.util.*; 4 | 5 | public class ArrayList_demo { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | ArrayList list = new ArrayList<>(78); 10 | System.out.println(list); 11 | // add 12 | list.add(10); 13 | list.add(20); 14 | list.add(30); 15 | System.out.println(list); 16 | 17 | System.out.println(list); 18 | 19 | // Get 20 | 21 | System.out.println(list.get(2)); 22 | 23 | // remove 24 | System.out.println(list.remove(1)); 25 | System.out.println(list); 26 | 27 | // update 28 | System.out.println(list.set(1, -90)); 29 | System.out.println(list); 30 | Collections.sort(list); 31 | System.out.println(list); 32 | for (int i = 0; i < list.size(); i++) { 33 | System.out.print(list.get(i) + " "); 34 | } 35 | System.out.println(); 36 | for (int v : list) { 37 | System.out.print(v + " "); 38 | } 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Lec13/Diagonal_Traverse.java: -------------------------------------------------------------------------------- 1 | package Lec13; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Diagonal_Traverse { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 10 | DiagonalTraverse(arr); 11 | 12 | } 13 | 14 | public static int [] DiagonalTraverse(int[][] arr) { 15 | 16 | int n = arr.length; 17 | int m = arr[0].length; 18 | int[] ans = new int[n * m]; 19 | int k = 0; 20 | for (int d = 0; d < m + n - 1; d++) { 21 | int r, c = 0; 22 | if (d < m) { 23 | r = 0; 24 | c = d; 25 | } else { 26 | c = m - 1; 27 | r = d - m + 1; 28 | } 29 | ArrayList list = new ArrayList<>(); 30 | while (r < n && c >= 0) { 31 | list.add(arr[r][c]); 32 | r++; 33 | c--; 34 | } 35 | if (d % 2 == 0) { 36 | for (int i = list.size() - 1; i >= 0; i--) { 37 | ans[k] = list.get(i); 38 | k++; 39 | } 40 | } else { 41 | for (int i = 0; i < list.size(); i++) { 42 | ans[k] = list.get(i); 43 | k++; 44 | } 45 | 46 | } 47 | 48 | } 49 | return ans; 50 | 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lec13/Wrapper_Class_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec13; 2 | 3 | public class Wrapper_Class_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int a = 10; 8 | Integer a1 = 10; 9 | System.out.println(a); 10 | System.out.println(a1); 11 | 12 | a1 = a;// AutoBoxing 13 | int x = 156; 14 | Integer y = 156; 15 | x = y;// unBoxing 16 | System.out.println(x == y); 17 | 18 | Integer c = 16; 19 | Integer c1 = 16; 20 | Integer d = 167; 21 | Integer d1 = 167; 22 | System.out.println(c == c1); 23 | System.out.println(d == d1); 24 | 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec14/Fact.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Fact { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 5; 8 | System.out.println(fact(n)); 9 | 10 | } 11 | 12 | public static int fact(int n) { 13 | if(n==0) { 14 | return 1; 15 | } 16 | //int fn = fact(n - 1); 17 | return fact(n - 1)*n; 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec14/Fact_Tail.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Fact_Tail { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 5; 8 | System.out.println(fact(n, 1)); 9 | 10 | } 11 | public static int fact(int n,int ans) { 12 | if(n==0) { 13 | return ans; 14 | } 15 | 16 | return fact(n-1, ans*n); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec14/Last_occ.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Last_occ { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 31, 5, 3, 6, 3, 7, 8 }; 8 | int item = 3; 9 | System.out.println(LastIndex(arr, item, arr.length-1)); 10 | 11 | } 12 | 13 | public static int LastIndex(int[] arr, int item, int i) { 14 | if(i<0) { 15 | return -1; 16 | } 17 | 18 | if(arr[i]==item) { 19 | return i; 20 | } 21 | 22 | return LastIndex(arr, item, i-1); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec14/Power.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Power { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int a =3; 8 | int b=4; 9 | System.out.println(pow(a, b)); 10 | 11 | } 12 | public static int pow(int a, int b) { 13 | if(b==0) { 14 | return 1; 15 | } 16 | 17 | int ans = pow(a, b-1); 18 | return ans*a; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec14/Print_Dec.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Print_Dec { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n=5; 8 | PD(n); 9 | 10 | } 11 | public static void PD(int n) { 12 | if(n==0) { 13 | return; 14 | } 15 | System.out.println(n); 16 | PD(n-1); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec14/Print_INC.java: -------------------------------------------------------------------------------- 1 | package Lec14; 2 | 3 | public class Print_INC { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n =5; 8 | PI(n); 9 | 10 | } 11 | public static void PI(int n) { 12 | if(n==0) { 13 | return; 14 | } 15 | PI(n-1); 16 | System.out.println(n); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec15/CoinToss.java: -------------------------------------------------------------------------------- 1 | package Lec15; 2 | 3 | public class CoinToss { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 3; 8 | CoinTossss(n, ""); 9 | 10 | } 11 | 12 | public static void CoinTossss(int n, String ans) { 13 | 14 | if (n == 0) { 15 | System.out.println(ans); 16 | return; 17 | } 18 | 19 | CoinTossss(n - 1, ans + "H"); 20 | CoinTossss(n - 1, ans + "T"); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec15/Coin_Toss_No_2Head.java: -------------------------------------------------------------------------------- 1 | package Lec15; 2 | 3 | public class Coin_Toss_No_2Head { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 3; 8 | CoinTossss(n, ""); 9 | 10 | } 11 | 12 | public static void CoinTossss(int n, String ans) { 13 | 14 | if (n == 0) { 15 | System.out.println(ans); 16 | return; 17 | } 18 | if (ans.length() == 0 || ans.charAt(ans.length() - 1) != 'H') { 19 | CoinTossss(n - 1, ans + "H"); 20 | } 21 | CoinTossss(n - 1, ans + "T"); 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lec15/Fibo.java: -------------------------------------------------------------------------------- 1 | package Lec15; 2 | 3 | public class Fibo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 5; 8 | System.out.println(fib(n)); 9 | 10 | } 11 | 12 | public static int fib(int n) { 13 | if (n == 0 || n == 1) { 14 | return n; 15 | } 16 | 17 | int f1 = fib(n - 1); 18 | int f2 = fib(n - 2); 19 | return f1 + f2; 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lec15/SubSequne.java: -------------------------------------------------------------------------------- 1 | package Lec15; 2 | 3 | public class SubSequne { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "abc"; 8 | printpermutation(str, ""); 9 | 10 | } 11 | 12 | public static void printpermutation(String ques, String ans) { 13 | if (ques.length() == 0) { 14 | System.out.println(ans); 15 | return; 16 | } 17 | char ch = ques.charAt(0); 18 | printpermutation(ques.substring(1), ans); 19 | printpermutation(ques.substring(1), ans + ch); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lec16/Count_Sequnce.java: -------------------------------------------------------------------------------- 1 | package Lec16; 2 | 3 | public class Count_Sequnce { 4 | static int count = 0; 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | String str = "abc"; 9 | // printpermutation(str, ""); 10 | // System.out.println(count); 11 | System.out.println(Countpermutation(str, "")); 12 | 13 | } 14 | 15 | public static void printpermutation(String ques, String ans) { 16 | if (ques.length() == 0) { 17 | System.out.println(ans); 18 | count++; 19 | return; 20 | } 21 | char ch = ques.charAt(0); 22 | printpermutation(ques.substring(1), ans); 23 | printpermutation(ques.substring(1), ans + ch); 24 | 25 | } 26 | 27 | public static int Countpermutation(String ques, String ans) { 28 | if (ques.length() == 0) { 29 | //System.out.println(ans); 30 | return 1; 31 | } 32 | char ch = ques.charAt(0); 33 | int ans1 = Countpermutation(ques.substring(1), ans); 34 | int ans2 = Countpermutation(ques.substring(1), ans + ch); 35 | return ans1 + ans2; 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lec16/Dice_Problem.java: -------------------------------------------------------------------------------- 1 | package Lec16; 2 | 3 | public class Dice_Problem { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 4; 8 | PrintPath(0, n, ""); 9 | 10 | } 11 | 12 | public static void PrintPath(int curr, int end, String ans) { 13 | if (curr == end) { 14 | System.out.println(ans); 15 | return; 16 | } 17 | if (curr > end) { 18 | return; 19 | } 20 | // PrintPath(curr + 1, end, ans + 1); 21 | // PrintPath(curr + 2, end, ans + 2); 22 | // PrintPath(curr + 3, end, ans + 3); 23 | 24 | for (int dice = 1; dice <= 3; dice++) { 25 | PrintPath(curr + dice, end, ans + dice); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lec16/Duplicate_Permutation.java: -------------------------------------------------------------------------------- 1 | package Lec16; 2 | 3 | public class Duplicate_Permutation { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "abca"; 8 | printanswer(str, ""); 9 | 10 | } 11 | 12 | public static void printanswer(String ques, String ans) { 13 | if (ques.length() == 0) { 14 | System.out.println(ans); 15 | return; 16 | } 17 | for (int i = 0; i < ques.length(); i++) { 18 | char ch = ques.charAt(i); 19 | boolean f = false; 20 | for (int j = i + 1; j < ques.length(); j++) { 21 | if (ques.charAt(j) == ch) { 22 | f = true; 23 | break; 24 | } 25 | 26 | } 27 | if (f == false) { 28 | String s1 = ques.substring(0, i); 29 | String s2 = ques.substring(i + 1); 30 | printanswer(s1 + s2, ans + ch); 31 | } 32 | 33 | } 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Lec16/Permutation_String.java: -------------------------------------------------------------------------------- 1 | package Lec16; 2 | 3 | public class Permutation_String { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "abc"; 8 | printanswer(str, ""); 9 | 10 | } 11 | 12 | public static void printanswer(String ques, String ans) { 13 | if (ques.length() == 0) { 14 | System.out.println(ans); 15 | return; 16 | } 17 | for (int i = 0; i < ques.length(); i++) { 18 | char ch = ques.charAt(i); 19 | 20 | String s1 = ques.substring(0, i); 21 | String s2 = ques.substring(i + 1); 22 | printanswer(s1 + s2, ans + ch); 23 | 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Lec17/Generate_Parentheses.java: -------------------------------------------------------------------------------- 1 | package Lec17; 2 | 3 | import java.util.*; 4 | 5 | public class Generate_Parentheses { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int n = 3; 10 | List list = new ArrayList<>(); 11 | GenerateParentheses(n, 0, 0, "", list); 12 | System.out.println(list); 13 | 14 | } 15 | 16 | public static void GenerateParentheses(int n, int open, int close, String ans, List list) { 17 | if (open == n && close == n) { 18 | //System.out.println(ans); 19 | list.add(ans); 20 | return ; 21 | } 22 | if (open < n) { 23 | GenerateParentheses(n, open + 1, close, ans + "(", list); 24 | } 25 | if (close < open) { 26 | GenerateParentheses(n, open, close + 1, ans + ")", list); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lec17/Lexico_Counting.java: -------------------------------------------------------------------------------- 1 | package Lec17; 2 | 3 | public class Lexico_Counting { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 1000; 8 | LexicoConting(0, n); 9 | 10 | } 11 | 12 | public static void LexicoConting(int curr, int n) { 13 | if (curr > n) { 14 | return; 15 | } 16 | System.out.println(curr); 17 | 18 | int i = 0; 19 | if (curr == 0) { 20 | i = 1; 21 | } 22 | for (; i <= 9; i++) { 23 | LexicoConting(curr * 10 + i, n); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Lec17/TOH.java: -------------------------------------------------------------------------------- 1 | package Lec17; 2 | 3 | public class TOH { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 3; 8 | String src = "A"; 9 | String hlp = "B"; 10 | String des = "C"; 11 | toh(n, src, hlp, des); 12 | 13 | } 14 | 15 | public static void toh(int n, String src, String hlp, String des) { 16 | if (n == 0) { 17 | return; 18 | } 19 | toh(n - 1, src, des, hlp); 20 | System.out.println("Move " + n + "th disc from " + src + " to " + des); 21 | 22 | toh(n - 1, hlp, src, des); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec18/Coin_Prermuation.java: -------------------------------------------------------------------------------- 1 | package Lec18; 2 | 3 | public class Coin_Prermuation { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] coin = { 2, 3, 5, 7 }; 8 | int amount = 10; 9 | Printanswer(coin, amount, ""); 10 | 11 | } 12 | 13 | public static void Printanswer(int[] coin, int amount, String ans) { 14 | if (amount == 0) { 15 | System.out.println(ans); 16 | return; 17 | 18 | } 19 | for (int i = 0; i < coin.length; i++) { 20 | if (amount >= coin[i]) { 21 | Printanswer(coin, amount - coin[i], ans + coin[i]); 22 | 23 | } 24 | } 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec18/Queen_Combination.java: -------------------------------------------------------------------------------- 1 | package Lec18; 2 | 3 | public class Queen_Combination { 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n = 4; 7 | int tq = 2; 8 | boolean[] board = new boolean[n]; 9 | Printanswer(board, tq, 0, "",0); 10 | 11 | } 12 | 13 | public static void Printanswer(boolean[] board, int tq, int qpsf, String ans,int idx) { 14 | if (qpsf == tq) { 15 | System.out.println(ans); 16 | return; 17 | } 18 | for (int i = idx; i < board.length; i++) { 19 | 20 | if (board[i] == false) { 21 | board[i] = true; 22 | 23 | Printanswer(board, tq,qpsf+1, ans + "b" + i + "q"+qpsf,i+1); 24 | board[i] = false; 25 | } 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lec18/Queen_Permutation.java: -------------------------------------------------------------------------------- 1 | package Lec18; 2 | 3 | public class Queen_Permutation { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 4; 8 | int tq = 2; 9 | boolean[] board = new boolean[n]; 10 | Printanswer(board, tq, 0, ""); 11 | 12 | } 13 | 14 | public static void Printanswer(boolean[] board, int tq, int qpsf, String ans) { 15 | if (qpsf == tq) { 16 | System.out.println(ans); 17 | return; 18 | } 19 | for (int i = 0; i < board.length; i++) { 20 | 21 | if (board[i] == false) { 22 | board[i] = true; 23 | 24 | Printanswer(board, tq,qpsf+1, ans + "b" + i + "q"+qpsf); 25 | board[i] = false; 26 | } 27 | } 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec19/Letter_Tile_Possibilities.java: -------------------------------------------------------------------------------- 1 | package Lec19; 2 | 3 | public class Letter_Tile_Possibilities { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "AAB"; 8 | int[] freq = new int[26]; 9 | for (int i = 0; i < str.length(); i++) { 10 | int idx = str.charAt(i) - 'A'; 11 | freq[idx]++;// freq[idx]=freq[idx]+1; 12 | 13 | } 14 | // for (int i = 0; i < freq.length; i++) { 15 | // System.out.print(freq[i] + " "); 16 | // } 17 | System.out.println(Tile_Possibilities(freq, "")); 18 | 19 | } 20 | 21 | public static int Tile_Possibilities(int[] freq, String ans) { 22 | // System.out.println(ans); 23 | int count = 0; 24 | for (int i = 0; i < freq.length; i++) { 25 | if (freq[i] >= 1) { 26 | char ch = (char) (i + 'A'); 27 | freq[i]--; 28 | count += Tile_Possibilities(freq, ans + ch) + 1; 29 | freq[i]++; 30 | } 31 | 32 | } 33 | return count; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Lec19/Palindrome_Partitioning.java: -------------------------------------------------------------------------------- 1 | package Lec19; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Palindrome_Partitioning { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | String str = "nitin"; 11 | List list = new ArrayList<>(); 12 | List> ans = new ArrayList<>(); 13 | Partitioning(str, list, ans); 14 | System.out.println(ans); 15 | 16 | } 17 | 18 | public static void Partitioning(String ques, List list, List> ans) { 19 | if (ques.length() == 0) { 20 | // System.out.println(list); 21 | ans.add(new ArrayList<>(list)); 22 | return; 23 | } 24 | for (int cut = 1; cut <= ques.length(); cut++) { 25 | String s = ques.substring(0, cut); 26 | if (isPalindrome(s)) { 27 | list.add(s); 28 | Partitioning(ques.substring(cut), list, ans); 29 | list.remove(list.size() - 1); 30 | } 31 | 32 | } 33 | 34 | } 35 | 36 | public static boolean isPalindrome(String s) { 37 | int i = 0; 38 | int j = s.length() - 1; 39 | while (i < j) { 40 | if (s.charAt(i) != s.charAt(j)) { 41 | return false; 42 | } 43 | i++; 44 | j--; 45 | } 46 | return true; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Lec2/Fibo.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Fibo { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int a = 0; 12 | int b = 1; 13 | int i = 1; 14 | while (i <= n) { 15 | int c = a + b; 16 | a = b; 17 | b = c; 18 | i++; 19 | 20 | } 21 | System.out.println(a); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec2/GCD.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GCD { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | 11 | int divident = sc.nextInt(); 12 | int divisor = sc.nextInt(); 13 | while (divident % divisor != 0) { 14 | int rem = divident % divisor; 15 | divident = divisor; 16 | divisor = rem; 17 | } 18 | System.out.println(divisor); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lec2/Inverse_of_number.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Inverse_of_number { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int sum=0; 12 | int pos=1; 13 | while(n>0) { 14 | int rem = n%10; 15 | sum = (int) (sum + pos*Math.pow(10, rem-1)); 16 | n = n/10; 17 | pos++; 18 | 19 | } 20 | System.out.println(sum); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec2/Loops_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | public class Loops_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int i = 0; 8 | while (i < 5) { 9 | System.out.println(i); 10 | 11 | } 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Lec2/Pattern_1.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_1 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = n; 13 | while (row <= n) { 14 | 15 | int i = 1; 16 | while (i <= star) { 17 | System.out.print("* "); 18 | i++; 19 | } 20 | 21 | // next line 22 | row++; 23 | System.out.println(); 24 | 25 | } 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec2/Pattern_11.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_11 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | while (row <= n) { 15 | int i = 1; 16 | while (i <= space) { 17 | System.out.print(" "); 18 | i++; 19 | } 20 | int j = 1; 21 | while (j <= star) { 22 | if (j % 2 == 0) { 23 | System.out.print(" "); 24 | } else { 25 | System.out.print("* "); 26 | } 27 | j++; 28 | } 29 | space--; 30 | star += 2; 31 | row++; 32 | System.out.println(); 33 | } 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Lec2/Pattern_3.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_3 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | while (row <= n) { 15 | 16 | int i = 1; 17 | while (i <= space) { 18 | System.out.print(" "); 19 | i++; 20 | } 21 | 22 | int j = 1; 23 | while (j <= star) { 24 | System.out.print("* "); 25 | j++; 26 | } 27 | space--; 28 | star++; 29 | row++; 30 | System.out.println(); 31 | 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec2/Pattern_6.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_6 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = n; 13 | int space = 0; 14 | while (row <= n) { 15 | int i = 1; 16 | while (i <= space) { 17 | System.out.print(" "); 18 | i++; 19 | } 20 | int j = 1; 21 | while (j <= star) { 22 | System.out.print("* "); 23 | j++; 24 | } 25 | space += 2;// space = space + 2; 26 | star--; 27 | System.out.println(); 28 | row++; 29 | 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec2/pattern_15.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattern_15 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = n; 13 | int space = 0; 14 | while (row <= 2 * n - 1) { 15 | int i = 1; 16 | while (i <= space) { 17 | System.out.print(" "); 18 | i++; 19 | } 20 | int j = 1; 21 | while (j <= star) { 22 | System.out.print("* "); 23 | j++; 24 | } 25 | if (row < n) { 26 | space += 2;// space = space + 2; 27 | star--; 28 | } else { 29 | space -= 2;// space = space - 2; 30 | star++; 31 | } 32 | System.out.println(); 33 | row++; 34 | 35 | } 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lec2/pattern_9.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattern_9 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | while (row <= n) { 15 | int i = 1; 16 | while (i <= space) { 17 | System.out.print(" "); 18 | i++; 19 | } 20 | int j = 1; 21 | while (j <= star) { 22 | System.out.print("* "); 23 | j++; 24 | } 25 | space--; 26 | star += 2; 27 | row++; 28 | System.out.println(); 29 | } 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec2/pattren_13.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattren_13 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | while (row <= 2 * n - 1) { 14 | int i = 1; 15 | while (i <= star) { 16 | System.out.print("* "); 17 | i++; 18 | } 19 | // Mirroring 20 | if (row < n) { 21 | star++; 22 | } else { 23 | star--; 24 | } 25 | row++; 26 | System.out.println(); 27 | 28 | } 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lec2/pattrern_2.java: -------------------------------------------------------------------------------- 1 | package Lec2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattrern_2 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | while (row <= n) { 14 | int i = 1; 15 | while (i <= star) { 16 | System.out.print("* "); 17 | i++; 18 | } 19 | star++; 20 | row++; 21 | System.out.println(); 22 | 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Lec20/Combination_Sum_II.java: -------------------------------------------------------------------------------- 1 | package Lec20; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Combination_Sum_II { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 10, 1, 2, 7, 6, 1, 5 }; 10 | int target = 8; 11 | Arrays.sort(arr); 12 | Combination(arr, target, "", 0); 13 | 14 | } 15 | 16 | public static void Combination(int[] arr, int target, String ans, int idx) { 17 | if (target == 0) { 18 | System.out.println(ans); 19 | return; 20 | } 21 | 22 | for (int i = idx; i < arr.length; i++) { 23 | if (i != idx && arr[i] == arr[i - 1]) { 24 | continue; 25 | } 26 | 27 | if (target >= arr[i]) { 28 | Combination(arr, target - arr[i], ans + arr[i] + " ", i + 1); 29 | 30 | } 31 | 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec20/Word_Search.java: -------------------------------------------------------------------------------- 1 | package Lec20; 2 | 3 | public class Word_Search { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } }; 8 | String word = "ABCCED"; 9 | 10 | for (int i = 0; i < board.length; i++) { 11 | for (int j = 0; j < board[0].length; j++) { 12 | 13 | if (board[i][j] == word.charAt(0)) { 14 | 15 | boolean ans = SearchWord(board, i, j, word, 0); 16 | if (ans == true) { 17 | System.out.println(ans); 18 | return; 19 | } 20 | } 21 | 22 | } 23 | 24 | } 25 | System.out.println(false); 26 | 27 | } 28 | 29 | private static boolean SearchWord(char[][] board, int cr, int cc, String word, int idx) { 30 | // TODO Auto-generated method stub 31 | if (idx == word.length()) { 32 | return true; 33 | } 34 | 35 | if (cr < 0 || cc < 0 || cr >= board.length || cc >= board[0].length || board[cr][cc] != word.charAt(idx)) { 36 | return false; 37 | 38 | } 39 | 40 | board[cr][cc] = '*'; 41 | int[] r = { -1, 1, 0, 0 }; 42 | int[] c = { 0, 0, -1, 1 }; 43 | for (int i = 0; i < c.length; i++) { 44 | boolean ans = SearchWord(board, cr + r[i], cc + c[i], word, idx + 1); 45 | if(ans==true) { 46 | return ans; 47 | } 48 | 49 | } 50 | board[cr][cc] =word.charAt(idx); 51 | 52 | return false; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Lec21/Queen_Problem.java: -------------------------------------------------------------------------------- 1 | package Lec21; 2 | 3 | public class Queen_Problem { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 4; 8 | boolean[][] board = new boolean[n][n]; 9 | N_Queen(board, n, 0); 10 | 11 | } 12 | 13 | public static void N_Queen(boolean[][] board, int tq, int row) { 14 | if (tq == 0) { 15 | Display(board); 16 | return; 17 | } 18 | for (int col = 0; col < board.length; col++) { 19 | if (issafe(board, row, col) == true) { 20 | board[row][col] = true; 21 | N_Queen(board, tq - 1, row + 1); 22 | board[row][col] = false; 23 | } 24 | 25 | } 26 | 27 | } 28 | 29 | public static boolean issafe(boolean[][] board, int row, int col) { 30 | // TODO Auto-generated method stub 31 | 32 | int r = row; 33 | // vertical 34 | while (r >= 0) { 35 | if (board[r][col] == true) { 36 | return false; 37 | } 38 | r--; 39 | } 40 | 41 | // left Diagonal 42 | r = row; 43 | int c = col; 44 | while (r >= 0 && c >= 0) { 45 | if (board[r][c] == true) { 46 | return false; 47 | } 48 | c--; 49 | r--; 50 | } 51 | // left Diagonal 52 | r = row; 53 | c = col; 54 | while (r >= 0 && c < board.length) { 55 | if (board[r][c] == true) { 56 | return false; 57 | } 58 | c++; 59 | r--; 60 | } 61 | return true; 62 | } 63 | 64 | private static void Display(boolean[][] board) { 65 | // TODO Auto-generated method stub 66 | for (int i = 0; i < board.length; i++) { 67 | for (int j = 0; j < board.length; j++) { 68 | System.out.print(board[i][j] + " "); 69 | } 70 | System.out.println(); 71 | } 72 | System.out.println(); 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Lec22/Merge_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec22; 2 | 3 | public class Merge_Sort { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 3, 1, 4, 5, 8, -11 }; 8 | int[] ans = Sort(arr, 0, arr.length - 1); 9 | for (int i = 0; i < ans.length; i++) { 10 | System.out.print(ans[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static int[] Sort(int[] arr, int si, int ei) { 16 | if (si == ei) { 17 | int[] bs = new int[1]; 18 | bs[0] = arr[si]; 19 | return bs; 20 | } 21 | int mid = (si + ei) / 2; 22 | int[] fs = Sort(arr, si, mid); 23 | int[] ss = Sort(arr, mid + 1, ei); 24 | return MergeTwoArray(fs, ss); 25 | 26 | } 27 | 28 | public static int[] MergeTwoArray(int[] arr1, int[] arr2) { 29 | 30 | int i = 0; 31 | int j = 0; 32 | int k = 0; 33 | int[] ans = new int[arr1.length + arr2.length]; 34 | while (i < arr1.length && j < arr2.length) { 35 | if (arr1[i] < arr2[j]) { 36 | ans[k] = arr1[i]; 37 | i++; 38 | k++; 39 | } else { 40 | ans[k] = arr2[j]; 41 | j++; 42 | k++; 43 | } 44 | } 45 | while (i < arr1.length) { 46 | ans[k] = arr1[i]; 47 | i++; 48 | k++; 49 | } 50 | while (j < arr2.length) { 51 | ans[k] = arr2[j]; 52 | j++; 53 | k++; 54 | } 55 | return ans; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Lec22/Merge_Two_Sorted_Array.java: -------------------------------------------------------------------------------- 1 | package Lec22; 2 | 3 | public class Merge_Two_Sorted_Array { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr1 = { 3, 5, 7, 9, 11 }; 8 | 9 | int[] arr2 = { 1, 2, 3, 4, 6, 7 }; 10 | int[] ans = MergeTwoArray(arr1, arr2); 11 | for (int i = 0; i < ans.length; i++) { 12 | System.out.print(ans[i] + " "); 13 | } 14 | } 15 | 16 | public static int[] MergeTwoArray(int[] arr1, int[] arr2) { 17 | 18 | int i = 0; 19 | int j = 0; 20 | int k = 0; 21 | int[] ans = new int[arr1.length + arr2.length]; 22 | while (i < arr1.length && j < arr2.length) { 23 | if (arr1[i] < arr2[j]) { 24 | ans[k] = arr1[i]; 25 | i++; 26 | k++; 27 | } else { 28 | ans[k] = arr2[j]; 29 | j++; 30 | k++; 31 | } 32 | } 33 | while (i < arr1.length) { 34 | ans[k] = arr1[i]; 35 | i++; 36 | k++; 37 | } 38 | while (j < arr2.length) { 39 | ans[k] = arr2[j]; 40 | j++; 41 | k++; 42 | } 43 | return ans; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Lec22/Partition_in_Array.java: -------------------------------------------------------------------------------- 1 | package Lec22; 2 | 3 | public class Partition_in_Array { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 7, 2, 3, 8, 9, 1, 4 }; 8 | int ans = partition(arr, 0, arr.length - 1); 9 | System.out.println(ans); 10 | for (int i = 0; i < arr.length; i++) { 11 | System.out.print(arr[i] + " "); 12 | } 13 | 14 | } 15 | 16 | public static int partition(int[] arr, int si, int ei) { 17 | // TODO Auto-generated method stub 18 | int item = arr[ei]; 19 | int pi = si; 20 | for (int i = si; i < ei; i++) { 21 | if (arr[i] < item) { 22 | int temp = arr[i]; 23 | arr[i] = arr[pi]; 24 | arr[pi] = temp; 25 | pi++; 26 | } 27 | 28 | } 29 | int temp = arr[ei]; 30 | arr[ei] = arr[pi]; 31 | arr[pi] = temp; 32 | return pi; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec22/Quick_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec22; 2 | 3 | public class Quick_Sort { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 7, 2, 3, 8, 9, 1, 4 }; 8 | sort(arr, 0, arr.length - 1); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static void sort(int[] arr, int si, int ei) { 16 | if (si > ei) { 17 | return; 18 | } 19 | int pi = partition(arr, si, ei); 20 | sort(arr, si, pi - 1); 21 | sort(arr, pi + 1, ei); 22 | } 23 | 24 | public static int partition(int[] arr, int si, int ei) { 25 | // TODO Auto-generated method stub 26 | int item = arr[ei]; 27 | int pi = si; 28 | for (int i = si; i < ei; i++) { 29 | if (arr[i] < item) { 30 | int temp = arr[i]; 31 | arr[i] = arr[pi]; 32 | arr[pi] = temp; 33 | pi++; 34 | } 35 | 36 | } 37 | int temp = arr[ei]; 38 | arr[ei] = arr[pi]; 39 | arr[pi] = temp; 40 | return pi; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Lec22/Time_and_Space_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec22; 2 | 3 | public class Time_and_Space_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | // long start = System.currentTimeMillis(); 8 | // 9 | // for (int i = 0; i < 1000000; i++) { 10 | // 11 | // } 12 | // 13 | // long end = System.currentTimeMillis(); 14 | // System.out.println(end - start); 15 | 16 | System.out.println("Hello"); 17 | System.out.println("Hello"); 18 | System.out.println("Hello"); 19 | System.out.println("Hello"); 20 | System.out.println("Hello"); 21 | 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lec23/Axis_Orbit.java: -------------------------------------------------------------------------------- 1 | package Lec23; 2 | 3 | public class Axis_Orbit { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String s = "nitin"; 8 | System.out.println(countPalindromic(s)); 9 | 10 | } 11 | 12 | public static int countPalindromic(String s) { 13 | int count = 0; 14 | // odd 15 | for (int axis = 0; axis < s.length(); axis++) { 16 | for (int orbit = 0; orbit < s.length() && axis - orbit >= 0 && axis + orbit < s.length(); orbit++) { 17 | if (s.charAt(axis - orbit) != s.charAt(axis + orbit)) { 18 | break; 19 | } 20 | count++; 21 | 22 | } 23 | 24 | } 25 | // even 26 | for (double axis = 0.5; axis < s.length(); axis++) { 27 | for (double orbit = 0.5; orbit < s.length() && axis - orbit >= 0 && axis + orbit < s.length(); orbit++) { 28 | if (s.charAt((int) (axis - orbit)) != s.charAt((int) (axis + orbit))) { 29 | break; 30 | } 31 | count++; 32 | 33 | } 34 | 35 | } 36 | return count; 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lec23/Check_Number.java: -------------------------------------------------------------------------------- 1 | package Lec23; 2 | 3 | public class Check_Number { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println(isPrime(53)); 8 | 9 | } 10 | 11 | public static boolean isPrime(int n) { 12 | int div = 2; 13 | while (div * div <= n) { 14 | if (n % div == 0) { 15 | return false; 16 | } 17 | div++; 18 | } 19 | 20 | return true; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lec23/Sieve_of_Eratosthenes.java: -------------------------------------------------------------------------------- 1 | package Lec23; 2 | 3 | public class Sieve_of_Eratosthenes { 4 | public static void main(String[] args) { 5 | 6 | int n = 100; 7 | PrimeSieve(n); 8 | 9 | } 10 | 11 | public static void PrimeSieve(int n) { 12 | // kisi Index pe false set hai us index ko prime number consider krna hai 13 | // kisi Index pe true set hai us index ko not prime number consider krna hai 14 | boolean[] arr = new boolean[n + 1]; 15 | arr[0] = true; 16 | arr[1] = true; 17 | 18 | for (int i = 2; i*i< arr.length; i++) { 19 | if (arr[i] == false) { 20 | for (int j = 2; i * j <= n; j++) { 21 | arr[i * j] = true;// not prime set kiye ho 22 | } 23 | } 24 | } 25 | 26 | for (int i = 0; i < arr.length; i++) { 27 | if (arr[i] == false) { 28 | System.out.print(i + " "); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lec24/Power_LogN.java: -------------------------------------------------------------------------------- 1 | package Lec24; 2 | 3 | public class Power_LogN { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int a = 3; 8 | int b = 9; 9 | System.out.println(Power(a, b)); 10 | 11 | } 12 | 13 | public static int Power(int a, int b) { 14 | 15 | if (b == 0) { 16 | return 1; 17 | } 18 | int ans = Power(a, b / 2); 19 | ans = ans * ans; 20 | if (b % 2 == 1) { 21 | ans = ans * a; 22 | } 23 | return ans; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec24/Rand_Quick_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec24; 2 | 3 | import java.util.Random; 4 | 5 | public class Rand_Quick_Sort { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 5, 7, 2, 3, 8, 9, 1, 4 }; 10 | sort(arr, 0, arr.length - 1); 11 | for (int i = 0; i < arr.length; i++) { 12 | System.out.print(arr[i] + " "); 13 | } 14 | 15 | } 16 | 17 | public static void sort(int[] arr, int si, int ei) { 18 | if (si > ei) { 19 | return; 20 | } 21 | int pi = partition(arr, si, ei); 22 | sort(arr, si, pi - 1); 23 | sort(arr, pi + 1, ei); 24 | } 25 | 26 | public static int partition(int[] arr, int si, int ei) { 27 | // TODO Auto-generated method stub 28 | Swap(arr, si, ei); 29 | int item = arr[ei]; 30 | int pi = si; 31 | for (int i = si; i < ei; i++) { 32 | if (arr[i] < item) { 33 | int temp = arr[i]; 34 | arr[i] = arr[pi]; 35 | arr[pi] = temp; 36 | pi++; 37 | } 38 | 39 | } 40 | int temp = arr[ei]; 41 | arr[ei] = arr[pi]; 42 | arr[pi] = temp; 43 | return pi; 44 | } 45 | 46 | public static void Swap(int[] arr, int si, int ei) { 47 | Random r = new Random(); 48 | int idx = r.nextInt(ei - si + 1) + si; 49 | int temp = arr[idx]; 50 | arr[idx] = arr[ei]; 51 | arr[ei] = temp; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lec24/Random_Number.java: -------------------------------------------------------------------------------- 1 | package Lec24; 2 | 3 | import java.util.Random; 4 | 5 | public class Random_Number { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | // System.out.println(Math.random()); 10 | int lo = 10; 11 | int hi = 100; 12 | Random r = new Random(); 13 | for (int i = 0; i < 40; i++) { 14 | int rn = r.nextInt(hi - lo + 1) + lo; 15 | System.out.print(rn + " "); 16 | 17 | } 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec25/Person.java: -------------------------------------------------------------------------------- 1 | package Lec25; 2 | 3 | public class Person { 4 | 5 | int age; 6 | String name; 7 | static int count = 0; 8 | 9 | public void Intro_yourSelf() { 10 | 11 | System.out.println("My Name is " + this.name + " and age is " + this.age); 12 | 13 | } 14 | 15 | public void SayHey(String name) { 16 | 17 | System.out.println(name + " Say Hey " + this.name); 18 | 19 | } 20 | 21 | public static void fun() { 22 | System.out.println("I am in Fun"); 23 | } 24 | static { 25 | System.out.println("In Static "); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Lec25/Person_Client.java: -------------------------------------------------------------------------------- 1 | package Lec25; 2 | 3 | public class Person_Client { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println("Hello"); 8 | Person p = new Person(); 9 | System.out.println(p.name); 10 | System.out.println(p.age); 11 | p.name = "Kaju"; 12 | p.age = 21; 13 | p.Intro_yourSelf(); 14 | Person p1 = new Person(); 15 | p1.name = "kamlesh"; 16 | p1.age = 18; 17 | p1.Intro_yourSelf(); 18 | p.SayHey("Ram"); 19 | System.out.println(p1.count); 20 | Person.count += 21; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lec25/Student.java: -------------------------------------------------------------------------------- 1 | package Lec25; 2 | 3 | public class Student { 4 | 5 | int age = 19; 6 | String name = "Kunal"; 7 | 8 | // public Student() { 9 | // // TODO Auto-generated constructor stub 10 | // } 11 | 12 | public Student(String name, int age) { 13 | // TODO Auto-generated constructor stub 14 | this.name = name; 15 | this.age = age; 16 | } 17 | 18 | public void Intro_yourSelf() { 19 | 20 | System.out.println("My Name is " + this.name + " and age is " + this.age); 21 | 22 | } 23 | 24 | public void SayHey(String name) { 25 | 26 | System.out.println(name + " Say Hey " + this.name); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec25/Student_Client.java: -------------------------------------------------------------------------------- 1 | package Lec25; 2 | 3 | public class Student_Client { 4 | public static void main(String[] args) { 5 | System.out.println("Hey"); 6 | Student s = new Student("Kaju", 21); 7 | s.age=40; 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Lec26/Queue.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Queue { 4 | protected int[] arr; 5 | protected int front = 0; 6 | protected int size = 0; 7 | 8 | public Queue() { 9 | // TODO Auto-generated constructor stub 10 | arr = new int[5]; 11 | } 12 | 13 | public Queue(int n) { 14 | // TODO Auto-generated constructor stub 15 | arr = new int[n]; 16 | } 17 | 18 | public boolean isEmpty() { 19 | return size == 0; 20 | } 21 | 22 | public boolean isfull() { 23 | return size == arr.length; 24 | } 25 | 26 | public void Enqueue(int item) throws Exception { 27 | if (isfull()) { 28 | throw new Exception("Bklol Queue full h"); 29 | } 30 | int idx = (front + size) % arr.length; 31 | arr[idx] = item; 32 | size++; 33 | } 34 | 35 | public int dequeue() throws Exception { 36 | if (isEmpty()) { 37 | throw new Exception("Queue khali hai"); 38 | } 39 | int rv = arr[front]; 40 | front = (front + 1) % arr.length; 41 | size--; 42 | return rv; 43 | } 44 | 45 | public int GetFront() { 46 | int rv = arr[front]; 47 | 48 | return rv; 49 | } 50 | 51 | public void Display() { 52 | for (int i = 0; i < size; i++) { 53 | int idx = (front + i) % arr.length; 54 | System.out.print(arr[idx] + " "); 55 | 56 | } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Lec26/Queue_Client.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Queue_Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | // TODO Auto-generated method stub 7 | Queue q = new Queue(); 8 | q.Enqueue(10); 9 | q.Enqueue(20); 10 | q.Enqueue(30); 11 | q.Enqueue(40); 12 | System.out.println(q.dequeue()); 13 | System.out.println(q.dequeue()); 14 | q.Enqueue(50); 15 | q.Enqueue(60); 16 | q.Display(); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Lec26/Stack.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Stack { 4 | protected int[] arr; 5 | private int top; 6 | 7 | public Stack() { 8 | // TODO Auto-generated constructor stub 9 | arr = new int[5]; 10 | top = -1; 11 | } 12 | 13 | public Stack(int n) { 14 | // TODO Auto-generated constructor stub 15 | arr = new int[n]; 16 | top = -1; 17 | } 18 | 19 | public boolean isEmpty() { 20 | 21 | return top == -1; 22 | 23 | } 24 | 25 | public boolean isfull() { 26 | 27 | return top == arr.length - 1; 28 | 29 | } 30 | 31 | public void push(int item) throws Exception { 32 | if (isfull()) { 33 | throw new Exception("BkLol Stack full hai"); 34 | } 35 | top++; 36 | arr[top] = item; 37 | 38 | } 39 | 40 | public int pop() throws Exception { 41 | if (isEmpty()) { 42 | throw new Exception("BkLol Stack khali hai"); 43 | } 44 | int rv = arr[top]; 45 | top--; 46 | return rv; 47 | } 48 | 49 | public int peek() throws Exception { 50 | if (isEmpty()) { 51 | throw new Exception("BkLol Stack khali hai"); 52 | } 53 | int rv = arr[top]; 54 | 55 | return rv; 56 | } 57 | 58 | public void display() { 59 | for (int i = 0; i <= top; i++) { 60 | System.out.print(arr[i] + " "); 61 | } 62 | System.out.println(); 63 | } 64 | 65 | public int size() { 66 | return top + 1; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Lec26/Stack_Client.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Stack_Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | // TODO Auto-generated method stub 7 | Stack s = new Stack(); 8 | s.push(10); 9 | s.push(20); 10 | s.push(30); 11 | s.push(40); 12 | s.push(50); 13 | s.display(); 14 | //s.push(60); 15 | System.out.println(s.peek()); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec26/Student.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Student { 4 | 5 | private int age = 19; 6 | private String name = "Kaju"; 7 | 8 | public int getAge() { 9 | return age; 10 | } 11 | 12 | // public void setAge(int age) throws Exception { 13 | // if (age < 0) { 14 | // throw new Exception("Bklol -ve age nhi hota hai"); 15 | // } 16 | // this.age = age; 17 | // } 18 | 19 | public void setAge(int age) throws Exception { 20 | try { 21 | if (age < 0) { 22 | throw new Exception("Bklol -ve age nhi hota hai"); 23 | } 24 | this.age = age; 25 | } catch (Exception e) { 26 | // TODO: handle exception 27 | e.printStackTrace(); 28 | } 29 | 30 | finally { 31 | System.out.println("I am in finaly blocks"); 32 | } 33 | 34 | } 35 | 36 | public String getName() { 37 | 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Student(String name, int age) { 46 | // TODO Auto-generated constructor stub 47 | this.name = name; 48 | this.age = age; 49 | } 50 | 51 | public void Intro_yourSelf() { 52 | 53 | System.out.println("My Name is " + this.name + " and age is " + this.age); 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Lec26/Student_Client.java: -------------------------------------------------------------------------------- 1 | package Lec26; 2 | 3 | public class Student_Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | // TODO Auto-generated method stub 7 | Student s = new Student("kunal", 24); 8 | s.setAge(-9); 9 | System.out.println(s.getAge()); 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Lec27/C.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | public class C extends P { 4 | int d = 2; 5 | int d2 = 20; 6 | 7 | @Override 8 | public void fun() { 9 | System.out.println("Fun in C"); 10 | } 11 | 12 | public void fun2() { 13 | System.out.println("Fun2 in C"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lec27/Client.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | // case 1 8 | // P obj = new P(); 9 | // System.out.println(obj.d);//1 10 | // System.out.println(obj.d1);//10 11 | // obj.fun(); 12 | // obj.fun1(); 13 | // case 2 14 | // P obj = new C(); 15 | // System.out.println(obj.d);// 1 16 | // System.out.println(obj.d1);// 10 17 | // System.out.println(((C) (obj)).d2); 18 | // System.out.println(((C) (obj)).d);// 2 19 | // obj.fun(); 20 | // obj.fun1(); 21 | // ((C) (obj)).fun2(); 22 | 23 | // Case 3 24 | // C obj = new P(); 25 | // System.out.println(obj.d); 26 | // System.out.println(obj.d1); 27 | // System.out.println(obj.d2); 28 | 29 | // Case 4 30 | C obj = new C(); 31 | System.out.println(obj.d);// 2 32 | System.out.println(obj.d1);// 10 33 | System.out.println(obj.d2);// 20 34 | System.out.println(((P) (obj)).d);// 1 35 | obj.fun(); 36 | obj.fun1(); 37 | obj.fun2(); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Lec27/DynamicQueue.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | import Lec26.Queue; 4 | 5 | public class DynamicQueue extends Queue { 6 | @Override 7 | public void Enqueue(int item) throws Exception { 8 | if (isfull()) { 9 | int[] a = new int[2 * arr.length]; 10 | for (int i = 0; i < size; i++) { 11 | int idx = (front + i) % arr.length; 12 | a[i] = arr[idx]; 13 | 14 | } 15 | arr=a; 16 | front=0; 17 | } 18 | super.Enqueue(item); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec27/DynamicStack.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | import Lec26.Stack; 4 | 5 | public class DynamicStack extends Stack { 6 | 7 | @Override 8 | public void push(int item) throws Exception { 9 | if (this.isfull()) { 10 | int[] a = new int[2 * arr.length]; 11 | for (int i = 0; i < arr.length; i++) { 12 | a[i] = arr[i]; 13 | } 14 | arr = a; 15 | 16 | } 17 | super.push(item); 18 | 19 | } 20 | 21 | public static void main(String[] args) throws Exception { 22 | DynamicStack ds = new DynamicStack(); 23 | ds.push(10); 24 | ds.push(20); 25 | ds.push(30); 26 | ds.push(40); 27 | ds.push(50); 28 | ds.push(60); 29 | ds.display(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lec27/Method_Overloading.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | public class Method_Overloading { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println(add(2, 3)); 8 | System.out.println(add(2, 1, 7)); 9 | System.out.println(add(5, 7, 9.8f)); 10 | //System.out.println(add(2, 3, 4, 5, 6, -9, 77, 11, 2, 3, 4, 89)); 11 | 12 | } 13 | 14 | public static int add(int a, int b) { 15 | return a + b; 16 | } 17 | 18 | public static int add(int a, int b, int c) { 19 | return a + b + c; 20 | } 21 | 22 | public static int add(int a, int b, float c) { 23 | return (int) (a + b + c); 24 | } 25 | 26 | public static int add(int x, int... a) { 27 | int sum = 0; 28 | for (int i = 0; i < a.length; i++) { 29 | sum += a[i]; 30 | } 31 | return sum; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec27/P.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | public class P { 4 | int d = 1; 5 | int d1 = 10; 6 | 7 | public void fun() { 8 | System.out.println("Fun in P"); 9 | } 10 | 11 | public void fun1() { 12 | System.out.println("Fun1 in P"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lec27/Student.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | public class Student { 4 | int age = 22; 5 | String name = "Raj"; 6 | 7 | @Override 8 | public String toString() { 9 | return this.name + " and " + this.age; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Lec27/Student_Client.java: -------------------------------------------------------------------------------- 1 | package Lec27; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Student_Client { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Student s = new Student(); 10 | System.out.println(s); 11 | 12 | 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Lec28/Insert_Down.java: -------------------------------------------------------------------------------- 1 | package Lec28; 2 | 3 | import java.util.Stack; 4 | 5 | public class Insert_Down { 6 | public static void main(String[] args) { 7 | Stack st = new Stack<>(); 8 | st.push(10); 9 | st.push(20); 10 | st.push(30); 11 | st.push(40); 12 | st.push(50); 13 | System.out.println(st); 14 | Insert(st, -3); 15 | System.out.println(st); 16 | 17 | } 18 | 19 | public static void Insert(Stack st, int item) { 20 | if (st.isEmpty()) { 21 | st.push(item); 22 | return; 23 | } 24 | int ii = st.pop(); 25 | Insert(st, item); 26 | st.push(ii); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lec28/Next_Greater_element.java: -------------------------------------------------------------------------------- 1 | package Lec28; 2 | 3 | import java.util.Stack; 4 | 5 | public class Next_Greater_element { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 10, 2, 1, 3, 5, 4, 8, 7 }; 10 | NGE(arr); 11 | 12 | } 13 | 14 | public static void NGE(int[] arr) { 15 | 16 | int[] ans = new int[arr.length]; 17 | Stack st = new Stack<>(); 18 | for (int i = 0; i < arr.length; i++) { 19 | while (!st.isEmpty() && arr[i] > arr[st.peek()]) { 20 | ans[st.pop()] = arr[i]; 21 | } 22 | st.push(i); 23 | 24 | } 25 | while (!st.isEmpty()) { 26 | ans[st.pop()] = -1; 27 | } 28 | for (int i = 0; i < ans.length; i++) { 29 | System.out.println(arr[i] + " " + ans[i]); 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec28/Reverse_Stack.java: -------------------------------------------------------------------------------- 1 | package Lec28; 2 | 3 | import java.util.Stack; 4 | 5 | public class Reverse_Stack { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Stack st = new Stack<>(); 10 | st.push(10); 11 | st.push(20); 12 | st.push(30); 13 | st.push(40); 14 | st.push(50); 15 | System.out.println(st); 16 | revsere(st); 17 | System.out.println(st); 18 | } 19 | 20 | private static void revsere(Stack st) { 21 | // TODO Auto-generated method stub 22 | 23 | if (st.isEmpty()) { 24 | return; 25 | } 26 | 27 | int x = st.pop(); 28 | revsere(st); 29 | 30 | Insert(st, x); 31 | 32 | } 33 | 34 | public static void Insert(Stack st, int item) { 35 | if (st.isEmpty()) { 36 | st.push(item); 37 | return; 38 | } 39 | int ii = st.pop(); 40 | Insert(st, item); 41 | st.push(ii); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Lec28/Stock_Sapn.java: -------------------------------------------------------------------------------- 1 | package Lec28; 2 | 3 | import java.util.Stack; 4 | 5 | public class Stock_Sapn { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 10, 2, 1, 3, 5, 4, 7, 8, 19 }; 10 | 11 | } 12 | 13 | public static void CalSpan(int[] arr) { 14 | int[] ans = new int[arr.length]; 15 | Stack st = new Stack<>(); 16 | for (int i = 0; i < arr.length; i++) { 17 | while (!st.isEmpty() && arr[i] >= arr[st.peek()]) { 18 | st.pop(); 19 | } 20 | if (!st.isEmpty()) { 21 | ans[i] = i - st.peek(); 22 | } else { 23 | ans[i] = i + 1; 24 | } 25 | 26 | st.push(i); 27 | 28 | } 29 | 30 | for (int i = 0; i < ans.length; i++) { 31 | System.out.println(arr[i] + " " + ans[i]); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec29/Histogram.java: -------------------------------------------------------------------------------- 1 | package Lec29; 2 | 3 | import java.util.Stack; 4 | 5 | public class Histogram { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 2, 3, 5, 4, 6, 1, 7 }; 10 | System.out.println(Area(arr)); 11 | } 12 | 13 | public static int Area(int[] arr) { 14 | 15 | Stack st = new Stack<>(); 16 | int maxarea = 0; 17 | for (int i = 0; i < arr.length; i++) { 18 | while (!st.isEmpty() && arr[i] < arr[st.peek()]) { 19 | int h = arr[st.pop()]; 20 | int r = i; 21 | if (st.isEmpty()) { 22 | maxarea = Math.max(maxarea, h * r); 23 | } else { 24 | int l = st.peek(); 25 | maxarea = Math.max(maxarea, h * (r - l - 1)); 26 | } 27 | } 28 | 29 | st.push(i); 30 | } 31 | int r = arr.length; 32 | 33 | while (!st.isEmpty()) { 34 | int h = arr[st.pop()]; 35 | 36 | if (st.isEmpty()) { 37 | maxarea = Math.max(maxarea, h * r); 38 | } else { 39 | int l = st.peek(); 40 | maxarea = Math.max(maxarea, h * (r - l - 1)); 41 | } 42 | } 43 | return maxarea; 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Lec29/Maximal_Rectangle.java: -------------------------------------------------------------------------------- 1 | package Lec29; 2 | 3 | import java.util.Stack; 4 | 5 | public class Maximal_Rectangle { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[][] matrix = { { 1, 0, 1, 0, 0 }, { 1, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 0 } }; 10 | int[] ans = new int[matrix[0].length]; 11 | int area = 0; 12 | for (int i = 0; i < matrix.length; i++) { 13 | for (int j = 0; j < matrix[0].length; j++) { 14 | 15 | if (matrix[i][j] == 1) { 16 | ans[j]++; 17 | } else { 18 | ans[j] = 0; 19 | } 20 | 21 | } 22 | area = Math.max(area, Area(ans)); 23 | 24 | } 25 | System.out.println(area); 26 | 27 | } 28 | 29 | public static int Area(int[] arr) { 30 | 31 | Stack st = new Stack<>(); 32 | int maxarea = 0; 33 | for (int i = 0; i < arr.length; i++) { 34 | while (!st.isEmpty() && arr[i] < arr[st.peek()]) { 35 | int h = arr[st.pop()]; 36 | int r = i; 37 | if (st.isEmpty()) { 38 | maxarea = Math.max(maxarea, h * r); 39 | } else { 40 | int l = st.peek(); 41 | maxarea = Math.max(maxarea, h * (r - l - 1)); 42 | } 43 | } 44 | 45 | st.push(i); 46 | } 47 | int r = arr.length; 48 | 49 | while (!st.isEmpty()) { 50 | int h = arr[st.pop()]; 51 | 52 | if (st.isEmpty()) { 53 | maxarea = Math.max(maxarea, h * r); 54 | } else { 55 | int l = st.peek(); 56 | maxarea = Math.max(maxarea, h * (r - l - 1)); 57 | } 58 | } 59 | return maxarea; 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Lec29/Maximum_Score.java: -------------------------------------------------------------------------------- 1 | package Lec29; 2 | 3 | import java.util.Stack; 4 | 5 | public class Maximum_Score { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 1, 4, 3, 7, 4, 5 }; 10 | int k = 3; 11 | System.out.println(Area(arr, k)); 12 | 13 | } 14 | 15 | public static int Area(int[] arr, int k) { 16 | 17 | Stack st = new Stack<>(); 18 | int maxarea = 0; 19 | for (int i = 0; i < arr.length; i++) { 20 | while (!st.isEmpty() && arr[i] < arr[st.peek()]) { 21 | int h = arr[st.pop()]; 22 | int r = i; 23 | if (r - 1 >= k) { 24 | if (st.isEmpty()) { 25 | maxarea = Math.max(maxarea, h * r); 26 | } else { 27 | int l = st.peek(); 28 | if (l + 1 <= k) { 29 | maxarea = Math.max(maxarea, h * (r - l - 1)); 30 | } 31 | } 32 | 33 | } 34 | } 35 | 36 | st.push(i); 37 | } 38 | int r = arr.length; 39 | 40 | while (!st.isEmpty()) { 41 | int h = arr[st.pop()]; 42 | 43 | if (r - 1 >= k) { 44 | if (st.isEmpty()) { 45 | maxarea = Math.max(maxarea, h * r); 46 | } else { 47 | int l = st.peek(); 48 | if (l + 1 <= k) { 49 | maxarea = Math.max(maxarea, h * (r - l - 1)); 50 | } 51 | } 52 | } 53 | 54 | } 55 | return maxarea; 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Lec29/Smallest_Number_From_DI_String.java: -------------------------------------------------------------------------------- 1 | package Lec29; 2 | 3 | import java.util.Stack; 4 | 5 | public class Smallest_Number_From_DI_String { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | String str = "DDIDDDI"; 10 | System.out.println(FormNumber(str)); 11 | 12 | } 13 | 14 | public static String FormNumber(String s) { 15 | int[] ans = new int[s.length() + 1]; 16 | int count = 1; 17 | Stack st = new Stack<>(); 18 | for (int i = 0; i <= s.length(); i++) { 19 | if (i == s.length() || s.charAt(i) == 'I') { 20 | ans[i] = count++; 21 | while (!st.isEmpty()) { 22 | ans[st.pop()] = count++; 23 | } 24 | 25 | } else { 26 | st.push(i); 27 | } 28 | 29 | } 30 | String str = ""; 31 | for (int val : ans) { 32 | str += val; 33 | } 34 | return str; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Lec3/Char_Data_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec3; 2 | 3 | public class Char_Data_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | char ch = 'a'; 8 | int x = (ch); 9 | System.out.println(x); 10 | System.out.println((int) (ch)); 11 | ch++;// ch = (char)(ch +1); 12 | System.out.println(ch); 13 | ch -= 1;// ch = (char)(ch +1); 14 | System.out.println(ch); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lec3/Conversion_Fahrenheit_to_Celsius.java: -------------------------------------------------------------------------------- 1 | package Lec3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Conversion_Fahrenheit_to_Celsius { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int minf = sc.nextInt(); 11 | int maxf = sc.nextInt(); 12 | int steps = sc.nextInt(); 13 | for (int i = minf; i <= maxf; i += steps) { 14 | int c = (int) ((5.0 / 9) * (i - 32)); 15 | System.out.println(i + "\t" + c); 16 | 17 | } 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec3/Data_Type_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Data_Type_Demo { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | byte b = (byte) (300); 11 | short s = 78; 12 | int i = 87; 13 | long l = 75667; 14 | System.out.println(b); 15 | b = (byte) (i); 16 | i = b; 17 | 18 | long ll = 2878_71987768686868l; 19 | 20 | float f = 8.9f; 21 | double d = 7.09; 22 | 23 | System.out.println(ll); 24 | System.out.println(f); 25 | System.out.println(d); 26 | int x1 = sc.nextInt(); 27 | long l1 = sc.nextLong(); 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec3/Dec_to_Bin.java: -------------------------------------------------------------------------------- 1 | package Lec3; 2 | 3 | public class Dec_to_Bin { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 68; 8 | int sum = 0; 9 | int mul = 1; 10 | while (n > 0) { 11 | int rem = n % 2; 12 | sum = sum + rem * mul; 13 | n = n / 2; 14 | mul *= 10; 15 | } 16 | System.out.println(sum); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Lec3/Loops.java: -------------------------------------------------------------------------------- 1 | package Lec3; 2 | 3 | public class Loops { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | // for (byte i = 0; i < 128; i++) {// i = byte(i+1); 8 | // System.out.println(i); 9 | // } 10 | 11 | // for (byte i = 0; i <= 127; i++) {// i = byte(i+1); 12 | // System.out.println(i); 13 | // } 14 | for (byte i = 0; i < 127; i++) {// i = byte(i+1); 15 | System.out.println(i); 16 | } 17 | boolean b = false; 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lec30/Number_of_Valid_Subarrays.java: -------------------------------------------------------------------------------- 1 | package Lec30; 2 | 3 | import java.util.Stack; 4 | 5 | public class Number_of_Valid_Subarrays { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 1, 4, 2, 5, 3 }; 10 | System.out.println(ValidSubArray(arr)); 11 | } 12 | 13 | public static int ValidSubArray(int[] arr) { 14 | 15 | Stack st = new Stack<>(); 16 | int ans = 0; 17 | for (int i = 0; i < arr.length; i++) { 18 | while (!st.isEmpty() && arr[i] < st.peek()) { 19 | st.pop(); 20 | } 21 | st.push(arr[i]); 22 | ans += st.size(); 23 | } 24 | return ans; 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec30/Pattern_132.java: -------------------------------------------------------------------------------- 1 | package Lec30; 2 | 3 | import java.util.Stack; 4 | 5 | public class Pattern_132 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 3, 1, 4, 2 }; 10 | System.out.println(Pattern132(arr)); 11 | } 12 | 13 | public static boolean Pattern132(int[] arr) { 14 | int[] min = new int[arr.length]; 15 | min[0] = arr[0]; 16 | for (int i = 1; i < min.length; i++) { 17 | min[i] = Math.min(min[i - 1], arr[i]); 18 | } 19 | Stack st = new Stack<>(); 20 | for (int j = arr.length - 1; j >= 0; j--) { 21 | int ith = min[j]; 22 | while (!st.isEmpty() && ith >= arr[st.peek()]) { 23 | st.pop(); 24 | } 25 | if (!st.isEmpty() && arr[j] > arr[st.peek()]) { 26 | return true; 27 | } 28 | st.push(j); 29 | } 30 | return false; 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec30/SlidingWindowMaximum.java: -------------------------------------------------------------------------------- 1 | package Lec30; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class SlidingWindowMaximum { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | int[] arr = { 1, 3, -1, -3, 5, 3, 6, 7 }; 11 | 12 | } 13 | 14 | public static int[] maxSlidingWindow(int[] arr, int k) { 15 | 16 | Deque dq = new LinkedList(); 17 | int[] ans = new int[arr.length - k + 1]; 18 | int j = 1; 19 | for (int i = 0; i < k; i++) { 20 | while (!dq.isEmpty() && arr[i] > arr[dq.getLast()]) { 21 | dq.removeLast(); 22 | } 23 | dq.add(i);// add last 24 | 25 | } 26 | ans[0]=arr[dq.getFirst()]; 27 | for (int i = k; i < arr.length; i++) { 28 | while (!dq.isEmpty() && arr[i] > arr[dq.getLast()]) { 29 | dq.removeLast(); 30 | } 31 | dq.add(i); 32 | if (!dq.isEmpty() && dq.getFirst() == i - k) { 33 | dq.removeFirst(); 34 | } 35 | ans[j] = arr[dq.getFirst()]; 36 | j++; 37 | 38 | } 39 | return ans; 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Lec31/Add_Two_Polynomials.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | public class Add_Two_Polynomials { 4 | 5 | class PolyNode { 6 | int coefficient, power; 7 | PolyNode next = null; 8 | 9 | PolyNode() { 10 | } 11 | 12 | PolyNode(int x, int y) { 13 | this.coefficient = x; 14 | this.power = y; 15 | } 16 | 17 | PolyNode(int x, int y, PolyNode next) { 18 | this.coefficient = x; 19 | this.power = y; 20 | this.next = next; 21 | } 22 | } 23 | 24 | class Solution { 25 | public PolyNode addPoly(PolyNode poly1, PolyNode poly2) { 26 | PolyNode Dummy = new PolyNode(); 27 | PolyNode temp = Dummy; 28 | while (poly1 != null && poly2 != null) { 29 | if (poly1.power == poly2.power) { 30 | if (poly1.coefficient + poly2.coefficient != 0) { 31 | PolyNode nn = new PolyNode(poly1.coefficient + poly2.coefficient, poly1.power); 32 | Dummy.next = nn; 33 | Dummy = Dummy.next; 34 | } 35 | poly1 = poly1.next; 36 | poly2 = poly2.next; 37 | 38 | } else if (poly1.power > poly2.power) { 39 | PolyNode nn = new PolyNode(poly1.coefficient, poly1.power); 40 | Dummy.next = nn; 41 | Dummy = Dummy.next; 42 | poly1 = poly1.next; 43 | 44 | } else { 45 | PolyNode nn = new PolyNode(poly2.coefficient, poly2.power); 46 | Dummy.next = nn; 47 | Dummy = Dummy.next; 48 | poly2 = poly2.next; 49 | } 50 | } 51 | if (poly1 != null) { 52 | Dummy.next = poly1; 53 | 54 | } 55 | if (poly2 != null) { 56 | Dummy.next = poly2; 57 | 58 | } 59 | return temp.next; 60 | 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Lec31/Cycle_Client.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | public class Cycle_Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | // TODO Auto-generated method stub 7 | Cycle_Create cc = new Cycle_Create(); 8 | cc.addlast(1); 9 | cc.addlast(2); 10 | cc.addlast(3); 11 | cc.addlast(4); 12 | cc.addlast(5); 13 | cc.addlast(6); 14 | cc.addlast(7); 15 | cc.addlast(8); 16 | cc.CreateCycle(); 17 | cc.CycleRemoval1(); 18 | cc.display(); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lec31/LinkedList_Client.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | public class LinkedList_Client { 4 | 5 | public static void main(String[] args) throws Exception { 6 | // TODO Auto-generated method stub 7 | LinkedList ll = new LinkedList(); 8 | ll.addFirst(1); 9 | ll.addFirst(2); 10 | ll.addFirst(3); 11 | ll.display(); 12 | ll.addlast(-9); 13 | ll.display(); 14 | ll.Addatindex(80, 3); 15 | ll.display(); 16 | // System.out.println(ll.getFirst()); 17 | // System.out.println(ll.getlast()); 18 | // System.out.println(ll.getatindex(3)); 19 | // System.out.println(ll.removefirst()); 20 | // System.out.println(ll.removelast()); 21 | System.out.println(ll.removeatindex(2)); 22 | ll.display(); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec31/Linked_List_Cycle.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | import Lec31.Mid_Of_LinkedList.ListNode; 4 | 5 | public class Linked_List_Cycle { 6 | public class ListNode { 7 | int val; 8 | ListNode next; 9 | 10 | ListNode() { 11 | } 12 | 13 | ListNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | ListNode(int val, ListNode next) { 18 | this.val = val; 19 | this.next = next; 20 | } 21 | } 22 | 23 | public class Solution { 24 | public boolean hasCycle(ListNode head) { 25 | ListNode slow = head; 26 | ListNode fast = head; 27 | while (fast != null && fast.next != null) { 28 | slow = slow.next; 29 | fast = fast.next.next; 30 | if (slow == fast) { 31 | return true; 32 | } 33 | } 34 | return false; 35 | 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Lec31/Mid_Of_LinkedList.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | 4 | public class Mid_Of_LinkedList { 5 | public class ListNode { 6 | int val; 7 | ListNode next; 8 | 9 | ListNode() { 10 | } 11 | 12 | ListNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | ListNode(int val, ListNode next) { 17 | this.val = val; 18 | this.next = next; 19 | } 20 | } 21 | 22 | class Solution { 23 | public ListNode middleNode(ListNode head) { 24 | ListNode slow = head; 25 | ListNode fast = head; 26 | while (fast != null && fast.next != null) { 27 | slow = slow.next; 28 | fast = fast.next.next; 29 | } 30 | return slow; 31 | 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec31/ReverseLinked.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | public class ReverseLinked { 4 | 5 | public class ListNode { 6 | int val; 7 | ListNode next; 8 | 9 | ListNode() { 10 | } 11 | 12 | ListNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | ListNode(int val, ListNode next) { 17 | this.val = val; 18 | this.next = next; 19 | } 20 | } 21 | 22 | class Solution { 23 | public ListNode reverseList(ListNode head) { 24 | 25 | ListNode prev = null; 26 | ListNode curr = head; 27 | while (curr != null) { 28 | ListNode ahead = curr.next; 29 | curr.next = prev; 30 | prev = curr; 31 | curr = ahead; 32 | } 33 | return prev; 34 | 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lec31/k_Group.java: -------------------------------------------------------------------------------- 1 | package Lec31; 2 | 3 | import java.util.Stack; 4 | 5 | 6 | 7 | public class k_Group { 8 | 9 | public class ListNode { 10 | int val; 11 | ListNode next; 12 | 13 | ListNode() { 14 | } 15 | 16 | ListNode(int val) { 17 | this.val = val; 18 | } 19 | 20 | ListNode(int val, ListNode next) { 21 | this.val = val; 22 | this.next = next; 23 | } 24 | } 25 | 26 | class Solution { 27 | public ListNode reverseKGroup(ListNode head, int k) { 28 | Stack st = new Stack<>(); 29 | ListNode Dummy = new ListNode(); 30 | ListNode temp = Dummy; 31 | ListNode start = null; 32 | while (head != null) { 33 | if (st.size() == 0) { 34 | start = head; 35 | } 36 | st.push(head); 37 | if (st.size() == k) { 38 | while (!st.isEmpty()) { 39 | ListNode nn = new ListNode(st.pop().val); 40 | Dummy.next = nn; 41 | Dummy = Dummy.next; 42 | } 43 | } 44 | head = head.next; 45 | 46 | } 47 | if (st.size() > 0) { 48 | Dummy.next = start; 49 | } 50 | return temp.next; 51 | 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Lec32/BinaryTree_Client.java: -------------------------------------------------------------------------------- 1 | package Lec32; 2 | 3 | public class BinaryTree_Client { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Binary_Tree bt = new Binary_Tree(); 8 | ///bt.Display(); 9 | //bt.PreOrder(); 10 | //bt.PostOrder(); 11 | bt.InOrder(); 12 | System.out.println(bt.Search(-60)); 13 | System.out.println(bt.Max()); 14 | // 10 true 20 true 40 false false true 50 false false true 30 false true 60 true 70 false false false 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Lec32/FlipEquivalent.java: -------------------------------------------------------------------------------- 1 | package Lec32; 2 | 3 | 4 | 5 | public class FlipEquivalent { 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | 11 | TreeNode() { 12 | } 13 | 14 | TreeNode(int val) { 15 | this.val = val; 16 | } 17 | 18 | TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | class Solution { 25 | public boolean flipEquiv(TreeNode root1, TreeNode root2) { 26 | if (root1 == null && root2 == null) { 27 | return true; 28 | } 29 | if (root1 == null || root2 == null) { 30 | return false; 31 | } 32 | if (root1.val != root2.val) { 33 | return false; 34 | } 35 | boolean flip = flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left); 36 | boolean noflip = flipEquiv(root1.right, root2.right) && flipEquiv(root1.left, root2.left); 37 | 38 | return flip || noflip; 39 | 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Lec32/PathSum.java: -------------------------------------------------------------------------------- 1 | package Lec32; 2 | 3 | 4 | public class PathSum { 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class Solution { 25 | public boolean hasPathSum(TreeNode root, int targetSum) { 26 | if (root == null) { 27 | return false; 28 | } 29 | if (root.left == null && root.right == null) { 30 | return root.val - targetSum == 0; 31 | } 32 | 33 | boolean left = hasPathSum(root.left, targetSum - root.val); 34 | boolean right = hasPathSum(root.right, targetSum - root.val); 35 | return left || right; 36 | 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lec32/SymmetricTree.java: -------------------------------------------------------------------------------- 1 | package Lec32; 2 | 3 | public class SymmetricTree { 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() { 10 | } 11 | 12 | TreeNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | TreeNode(int val, TreeNode left, TreeNode right) { 17 | this.val = val; 18 | this.left = left; 19 | this.right = right; 20 | } 21 | } 22 | 23 | class Solution { 24 | public boolean isSymmetric(TreeNode root) { 25 | return Symmetric(root, root); 26 | 27 | } 28 | 29 | private boolean Symmetric(TreeNode node1, TreeNode node2) { 30 | // TODO Auto-generated method stub 31 | if (node1 == null && node2 == null) { 32 | return true; 33 | } 34 | if (node1 == null || node2 == null) { 35 | return false; 36 | } 37 | if (node1.val != node2.val) { 38 | return false; 39 | } 40 | boolean left = Symmetric(node1.left, node2.right); 41 | boolean right = Symmetric(node1.right, node2.left); 42 | 43 | return left && right; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Lec33/Balanced_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | package Lec33; 2 | 3 | 4 | 5 | 6 | 7 | public class Balanced_Binary_Tree { 8 | public class TreeNode { 9 | int val; 10 | TreeNode left; 11 | TreeNode right; 12 | 13 | TreeNode() { 14 | } 15 | 16 | TreeNode(int val) { 17 | this.val = val; 18 | } 19 | 20 | TreeNode(int val, TreeNode left, TreeNode right) { 21 | this.val = val; 22 | this.left = left; 23 | this.right = right; 24 | } 25 | } 26 | 27 | class Solution { 28 | public boolean isBalanced(TreeNode root) { 29 | if(root==null) { 30 | return true; 31 | } 32 | 33 | boolean left = isBalanced(root.left); 34 | boolean right = isBalanced(root.right); 35 | boolean isbal = Math.abs(ht(root.left) - ht(root.right)) <= 1; 36 | return left && right && isbal; 37 | 38 | } 39 | 40 | public int ht(TreeNode root) { 41 | if (root == null) { 42 | return -1; 43 | } 44 | int lh = ht(root.left); 45 | int rh = ht(root.right); 46 | return Math.max(lh, rh) + 1; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Lec33/Diameter_Opt.java: -------------------------------------------------------------------------------- 1 | package Lec33; 2 | 3 | public class Diameter_Opt { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | public class DiaPair { 25 | int ht = -1; 26 | int d = 0; 27 | } 28 | 29 | class Solution { 30 | public int diameterOfBinaryTree(TreeNode root) { 31 | return diameter(root).d; 32 | } 33 | 34 | public DiaPair diameter(TreeNode root) { 35 | if (root == null) { 36 | return new DiaPair(); 37 | } 38 | DiaPair ldp = diameter(root.left); 39 | DiaPair rdp = diameter(root.right); 40 | DiaPair sdp = new DiaPair(); 41 | 42 | int sd = ldp.ht + rdp.ht + 2; 43 | sdp.d = Math.max(ldp.d, Math.max(rdp.d, sd)); 44 | sdp.ht = Math.max(ldp.ht, rdp.ht) + 1; 45 | return sdp; 46 | 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Lec33/Diameter_of_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | package Lec33; 2 | 3 | public class Diameter_of_Binary_Tree { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class Solution { 25 | public int diameterOfBinaryTree(TreeNode root) { 26 | if (root == null) { 27 | return 0; 28 | } 29 | 30 | int ld = diameterOfBinaryTree(root.left); 31 | int rd = diameterOfBinaryTree(root.right); 32 | int sd = ht(root.left) + ht(root.right) + 2; 33 | return Math.max(ld, Math.max(rd, sd)); 34 | } 35 | 36 | public int ht(TreeNode root) { 37 | if (root == null) { 38 | return -1; 39 | } 40 | int lh = ht(root.left); 41 | int rh = ht(root.right); 42 | return Math.max(lh, rh) + 1; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Lec33/Level_OrderTraversal.java: -------------------------------------------------------------------------------- 1 | package Lec33; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Queue; 7 | 8 | public class Level_OrderTraversal { 9 | public class TreeNode { 10 | int val; 11 | TreeNode left; 12 | TreeNode right; 13 | 14 | TreeNode() { 15 | } 16 | 17 | TreeNode(int val) { 18 | this.val = val; 19 | } 20 | 21 | TreeNode(int val, TreeNode left, TreeNode right) { 22 | this.val = val; 23 | this.left = left; 24 | this.right = right; 25 | } 26 | } 27 | 28 | class Solution { 29 | public List> levelOrder(TreeNode root) { 30 | Queue q = new LinkedList<>(); 31 | Queue q1 = new LinkedList<>(); 32 | List> ans = new ArrayList<>(); 33 | List ll = new ArrayList<>(); 34 | q.add(root); 35 | while (!q.isEmpty()) { 36 | TreeNode rv = q.remove(); 37 | ll.add(rv.val); 38 | if (rv.left != null) { 39 | q1.add(rv.left); 40 | } 41 | if (rv.right != null) { 42 | q1.add(rv.right); 43 | } 44 | if (q.isEmpty()) { 45 | ans.add(ll); 46 | ll = new ArrayList(); 47 | q = q1; 48 | q1=new LinkedList<>(); 49 | } 50 | } 51 | return ans; 52 | 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Lec34/Create_Level_Order.java: -------------------------------------------------------------------------------- 1 | package Lec34; 2 | 3 | import java.util.*; 4 | 5 | public class Create_Level_Order { 6 | 7 | public class Node { 8 | int val; 9 | Node left; 10 | Node right; 11 | } 12 | 13 | private Node root; 14 | Scanner sc = new Scanner(System.in); 15 | 16 | public Create_Level_Order() { 17 | // TODO Auto-generated constructor stub 18 | this.root = CreateTree(); 19 | } 20 | 21 | private Node CreateTree() { 22 | // TODO Auto-generated method stub 23 | Queue q = new LinkedList<>(); 24 | Node nn = new Node(); 25 | int val = sc.nextInt();// 10 26 | nn.val = val; 27 | root = nn; 28 | q.add(nn); 29 | while (!q.isEmpty()) { 30 | Node rv = q.remove(); 31 | int c1 = sc.nextInt(); 32 | int c2 = sc.nextInt(); 33 | if (c1 != -1) { 34 | Node node = new Node(); 35 | node.val = c1; 36 | rv.left = node; 37 | q.add(node); 38 | 39 | } 40 | if (c2 != -1) { 41 | Node node = new Node(); 42 | node.val = c2; 43 | rv.right = node; 44 | q.add(node); 45 | 46 | } 47 | } 48 | return root; 49 | } 50 | 51 | public void PreOrder() { 52 | PreOrder(root); 53 | System.out.println(); 54 | } 55 | 56 | private void PreOrder(Node node) { 57 | // TODO Auto-generated method stub 58 | if (node == null) { 59 | return; 60 | } 61 | 62 | System.out.print(node.val + " "); 63 | PreOrder(node.left); 64 | PreOrder(node.right); 65 | 66 | } 67 | 68 | public static void main(String[] args) { 69 | Create_Level_Order cl = new Create_Level_Order(); 70 | cl.PreOrder(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Lec34/LCA.java: -------------------------------------------------------------------------------- 1 | package Lec34; 2 | 3 | public class LCA { 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() { 10 | } 11 | 12 | TreeNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | TreeNode(int val, TreeNode left, TreeNode right) { 17 | this.val = val; 18 | this.left = left; 19 | this.right = right; 20 | } 21 | } 22 | 23 | class Solution { 24 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 25 | if (root == null) { 26 | return root; 27 | } 28 | if (root == p || root == q) { 29 | return root; 30 | } 31 | TreeNode left = lowestCommonAncestor(root.left, p, q); 32 | TreeNode right = lowestCommonAncestor(root.right, p, q); 33 | if (left != null && right != null) { 34 | return root; 35 | } 36 | else if (left != null) { 37 | return left; 38 | } 39 | else 40 | return right; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Lec34/Sum_Root_to_Leaf_Numbers.java: -------------------------------------------------------------------------------- 1 | package Lec34; 2 | 3 | public class Sum_Root_to_Leaf_Numbers { 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() { 10 | } 11 | 12 | TreeNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | TreeNode(int val, TreeNode left, TreeNode right) { 17 | this.val = val; 18 | this.left = left; 19 | this.right = right; 20 | } 21 | } 22 | 23 | class Solution { 24 | public int sumNumbers(TreeNode root) { 25 | return Sum(root, 0); 26 | } 27 | 28 | public int Sum(TreeNode root, int ans) { 29 | if (root == null) { 30 | return 0; 31 | } 32 | 33 | if (root.left == null && root.right == null) { 34 | return ans * 10 + root.val; 35 | } 36 | 37 | int left = Sum(root.left, ans * 10 + root.val); 38 | int right = Sum(root.right, ans * 10 + root.val); 39 | return left + right; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lec35/BinarySearch_Tree.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | public class BinarySearch_Tree { 4 | 5 | public class Node { 6 | int val; 7 | Node left; 8 | Node right; 9 | } 10 | 11 | private Node root; 12 | 13 | public BinarySearch_Tree(int[] in) { 14 | // TODO Auto-generated constructor stub 15 | this.root = CreateTree(in, 0, in.length - 1); 16 | } 17 | 18 | private Node CreateTree(int[] in, int si, int ei) { 19 | // TODO Auto-generated method stub 20 | if (si > ei) { 21 | return null; 22 | } 23 | int mid = (si + ei) / 2; 24 | Node nn = new Node(); 25 | nn.val = in[mid]; 26 | nn.left = CreateTree(in, si, mid - 1); 27 | nn.right = CreateTree(in, mid + 1, ei); 28 | return nn; 29 | } 30 | 31 | public void PreOrder() { 32 | PreOrder(root); 33 | System.out.println(); 34 | } 35 | 36 | private void PreOrder(Node node) { 37 | // TODO Auto-generated method stub 38 | if (node == null) { 39 | return; 40 | } 41 | 42 | System.out.print(node.val + " "); 43 | PreOrder(node.left); 44 | PreOrder(node.right); 45 | 46 | } 47 | 48 | public static void main(String[] args) { 49 | int[] in = { 10, 20, 30, 40, 50, 60, 70, 80, 90 }; 50 | BinarySearch_Tree bst = new BinarySearch_Tree(in); 51 | bst.PreOrder(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Lec35/Binary_Tree_Cameras.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | public class Binary_Tree_Cameras { 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() { 10 | } 11 | 12 | TreeNode(int val) { 13 | this.val = val; 14 | } 15 | 16 | TreeNode(int val, TreeNode left, TreeNode right) { 17 | this.val = val; 18 | this.left = left; 19 | this.right = right; 20 | } 21 | } 22 | 23 | class Solution { 24 | int count = 0; 25 | 26 | public int minCameraCover(TreeNode root) { 27 | int c =CountCamera(root); 28 | 29 | if(c==-1) { 30 | count++; 31 | } 32 | return count; 33 | 34 | } 35 | 36 | public int CountCamera(TreeNode root) { 37 | if (root == null) { 38 | return 1; 39 | } 40 | int left = CountCamera(root.left); 41 | int right = CountCamera(root.right); 42 | if (left == -1 || right == -1) { 43 | count++; 44 | return 2; 45 | } 46 | 47 | if (left == 1 && right == 1) { 48 | 49 | return -1; 50 | } else { 51 | return 1; 52 | } 53 | 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Lec35/Delete_BST.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | public class Delete_BST { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class Solution { 25 | public TreeNode deleteNode(TreeNode root, int key) { 26 | if (root == null) { 27 | return root; 28 | } 29 | 30 | if (root.val < key) { 31 | root.right = deleteNode(root.right, key); 32 | } else if (root.val > key) { 33 | root.left = deleteNode(root.left, key); 34 | } else { 35 | // 1 or 0 child 36 | if (root.left == null) { 37 | return root.right; 38 | } else if (root.right == null) { 39 | return root.left; 40 | } else { 41 | int min = min(root.right); 42 | root.right=deleteNode(root.right, min); 43 | root.val=min; 44 | } 45 | 46 | } 47 | return root; 48 | 49 | } 50 | 51 | public int min(TreeNode root) { 52 | if (root == null) { 53 | return Integer.MAX_VALUE; 54 | } 55 | 56 | int left = min(root.left); 57 | return Math.min(left, root.val); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Lec35/Insert_BST.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | public class Insert_BST { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class Solution { 25 | public TreeNode insertIntoBST(TreeNode root, int val) { 26 | if (root == null) { 27 | return new TreeNode(val); 28 | } 29 | 30 | if (root.val < val) { 31 | root.right = insertIntoBST(root.right, val); 32 | } else { 33 | root.left = insertIntoBST(root.left, val); 34 | } 35 | return root; 36 | 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lec35/Largest_BST_Subtree.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | 4 | 5 | public class Largest_BST_Subtree { 6 | 7 | public class TreeNode { 8 | int val; 9 | TreeNode left; 10 | TreeNode right; 11 | 12 | TreeNode() { 13 | } 14 | 15 | TreeNode(int val) { 16 | this.val = val; 17 | } 18 | 19 | TreeNode(int val, TreeNode left, TreeNode right) { 20 | this.val = val; 21 | this.left = left; 22 | this.right = right; 23 | } 24 | } 25 | 26 | class BstPair { 27 | boolean isbst = true; 28 | long min = Long.MAX_VALUE; 29 | long max = Long.MIN_VALUE; 30 | int size = 0; 31 | 32 | } 33 | 34 | class Solution { 35 | public int largestBSTSubtree(TreeNode root) { 36 | return VaildBst(root).size; 37 | } 38 | 39 | public BstPair VaildBst(TreeNode root) { 40 | if (root == null) { 41 | return new BstPair(); 42 | } 43 | 44 | BstPair lbp = VaildBst(root.left); 45 | BstPair rbp = VaildBst(root.right); 46 | BstPair sbp = new BstPair(); 47 | sbp.max = Math.max(lbp.max, Math.max(rbp.max, root.val)); 48 | sbp.min = Math.min(lbp.min, Math.min(rbp.min, root.val)); 49 | if (lbp.isbst && rbp.isbst && lbp.max < root.val && rbp.min > root.val) { 50 | sbp.isbst = true; 51 | sbp.size = lbp.size + rbp.size + 1; 52 | } else { 53 | sbp.isbst = false; 54 | sbp.size = Math.max(lbp.size, rbp.size); 55 | 56 | } 57 | 58 | return sbp; 59 | 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Lec35/Maximum_Sum_BST.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | import Lec35.Largest_BST_Subtree.BstPair; 4 | import Lec35.Largest_BST_Subtree.TreeNode; 5 | 6 | public class Maximum_Sum_BST { 7 | public class TreeNode { 8 | int val; 9 | TreeNode left; 10 | TreeNode right; 11 | 12 | TreeNode() { 13 | } 14 | 15 | TreeNode(int val) { 16 | this.val = val; 17 | } 18 | 19 | TreeNode(int val, TreeNode left, TreeNode right) { 20 | this.val = val; 21 | this.left = left; 22 | this.right = right; 23 | } 24 | } 25 | 26 | class BstPair { 27 | boolean isbst = true; 28 | long min = Long.MAX_VALUE; 29 | long max = Long.MIN_VALUE; 30 | int sum = 0;// bt sum 31 | int ans = 0;// bstt ans 32 | 33 | } 34 | 35 | class Solution { 36 | public int maxSumBST(TreeNode root) { 37 | return VaildBst(root).ans; 38 | 39 | } 40 | 41 | public BstPair VaildBst(TreeNode root) { 42 | if (root == null) { 43 | return new BstPair(); 44 | } 45 | 46 | BstPair lbp = VaildBst(root.left); 47 | BstPair rbp = VaildBst(root.right); 48 | BstPair sbp = new BstPair(); 49 | sbp.max = Math.max(lbp.max, Math.max(rbp.max, root.val)); 50 | sbp.min = Math.min(lbp.min, Math.min(rbp.min, root.val)); 51 | if (lbp.isbst && rbp.isbst && lbp.max < root.val && rbp.min > root.val) { 52 | sbp.isbst = true; 53 | 54 | } else { 55 | sbp.isbst = false; 56 | 57 | } 58 | 59 | return sbp; 60 | 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Lec35/Validate_BST.java: -------------------------------------------------------------------------------- 1 | package Lec35; 2 | 3 | public class Validate_BST { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class BstPair { 25 | boolean isbst = true; 26 | long min = Long.MAX_VALUE; 27 | long max = Long.MIN_VALUE; 28 | 29 | } 30 | 31 | class Solution { 32 | public boolean isValidBST(TreeNode root) { 33 | return VaildBst(root).isbst; 34 | } 35 | 36 | public BstPair VaildBst(TreeNode root) { 37 | if (root == null) { 38 | return new BstPair(); 39 | } 40 | 41 | BstPair lbp = VaildBst(root.left); 42 | BstPair rbp = VaildBst(root.right); 43 | BstPair sbp = new BstPair(); 44 | sbp.max = Math.max(lbp.max, Math.max(rbp.max, root.val)); 45 | sbp.min = Math.min(lbp.min, Math.min(rbp.min, root.val)); 46 | if (lbp.isbst && rbp.isbst && lbp.max < root.val && rbp.min > root.val) { 47 | sbp.isbst = true; 48 | } else 49 | sbp.isbst = false; 50 | 51 | return sbp; 52 | 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Lec36/Distribute_Coins.java: -------------------------------------------------------------------------------- 1 | package Lec36; 2 | 3 | public class Distribute_Coins { 4 | 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | 10 | TreeNode() { 11 | } 12 | 13 | TreeNode(int val) { 14 | this.val = val; 15 | } 16 | 17 | TreeNode(int val, TreeNode left, TreeNode right) { 18 | this.val = val; 19 | this.left = left; 20 | this.right = right; 21 | } 22 | } 23 | 24 | class Solution { 25 | int move = 0; 26 | 27 | public int distributeCoins(TreeNode root) { 28 | 29 | distributeCoins1(root); 30 | return move; 31 | } 32 | 33 | public int distributeCoins1(TreeNode root) { 34 | if (root == null) { 35 | return 0; 36 | } 37 | int left = distributeCoins1(root.left); 38 | int right = distributeCoins1(root.right); 39 | move += Math.abs(left) + Math.abs(right); 40 | return left + right + root.val - 1; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Lec37/Cars.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | public class Cars implements Comparable { 4 | 5 | int price; 6 | int speed; 7 | String color; 8 | 9 | public Cars() { 10 | // TODO Auto-generated constructor stub 11 | } 12 | 13 | public Cars(int price, int speed, String color) { 14 | // TODO Auto-generated constructor stub 15 | this.price = price; 16 | this.speed = speed; 17 | this.color = color; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "P : " + this.price + " S: " + this.speed + " C:" + this.color; 23 | 24 | } 25 | 26 | @Override 27 | public int compareTo(Cars o) { 28 | // TODO Auto-generated method stub 29 | //return this.price - o.price; 30 | //return o.speed-this.speed; 31 | return this.color.compareTo(o.color); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec37/Generic_Heap.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Generic_Heap> { 6 | 7 | private ArrayList list = new ArrayList<>(); 8 | 9 | public void add(T item) { 10 | list.add(item); 11 | upheapify(list.size() - 1); 12 | 13 | } 14 | 15 | private void upheapify(int ci) { 16 | // TODO Auto-generated method stub 17 | int pi = (ci - 1) / 2; 18 | if (islarger(list.get(pi), list.get(ci))>0) {//list.get(pi) > list.get(ci) 19 | Swap(pi, ci); 20 | upheapify(pi); 21 | } 22 | 23 | } 24 | 25 | public void Swap(int i, int j) { 26 | T ith = list.get(i); 27 | T jth = list.get(j); 28 | list.set(i, jth); 29 | list.set(j, ith); 30 | } 31 | 32 | public T remove() { 33 | Swap(0, list.size() - 1); 34 | T rv = list.remove(list.size() - 1); 35 | downheapify(0); 36 | return rv; 37 | } 38 | 39 | private void downheapify(int pi) { 40 | // TODO Auto-generated method stub 41 | int lci = 2 * pi + 1; 42 | int rci = 2 * pi + 2; 43 | int mini = pi; 44 | if (lci < list.size() && islarger(list.get(mini), list.get(lci))>0) {//list.get(mini) > list.get(lci) 45 | mini = lci; 46 | } 47 | if (rci < list.size() && islarger(list.get(mini), list.get(rci))>0) { 48 | mini = rci; 49 | } 50 | if (mini != pi) { 51 | Swap(mini, pi); 52 | downheapify(mini); 53 | } 54 | 55 | } 56 | 57 | public T min() { 58 | return list.get(0); 59 | } 60 | 61 | public int size() { 62 | return list.size(); 63 | } 64 | 65 | public void Display() { 66 | System.out.println(list); 67 | } 68 | 69 | public int islarger(T o1, T o2) { 70 | return o1.compareTo(o2); 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Lec37/Genric_Heap_Client.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | public class Genric_Heap_Client { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Generic_Heap gp = new Generic_Heap<>(); 8 | gp.add(new Cars(200, 10, "White")); 9 | gp.add(new Cars(1000, 20, "Black")); 10 | gp.add(new Cars(345, 3, "Yellow")); 11 | gp.add(new Cars(34, 89, "Grey")); 12 | gp.add(new Cars(8907, 6, "Red")); 13 | gp.Display(); 14 | System.out.println(gp.remove()); 15 | gp.Display(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Lec37/Heap.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Heap { 6 | 7 | private ArrayList list = new ArrayList<>(); 8 | 9 | public void add(int item) { 10 | list.add(item); 11 | upheapify(list.size() - 1); 12 | 13 | } 14 | 15 | private void upheapify(int ci) { 16 | // TODO Auto-generated method stub 17 | int pi = (ci - 1) / 2; 18 | if (list.get(pi) > list.get(ci)) { 19 | Swap(pi, ci); 20 | upheapify(pi); 21 | } 22 | 23 | } 24 | 25 | public void Swap(int i, int j) { 26 | int ith = list.get(i); 27 | int jth = list.get(j); 28 | list.set(i, jth); 29 | list.set(j, ith); 30 | } 31 | 32 | public int remove() { 33 | Swap(0, list.size() - 1); 34 | int rv = list.remove(list.size() - 1); 35 | downheapify(0); 36 | return rv; 37 | } 38 | 39 | private void downheapify(int pi) { 40 | // TODO Auto-generated method stub 41 | int lci = 2 * pi + 1; 42 | int rci = 2 * pi + 2; 43 | int mini = pi; 44 | if (lci list.get(lci)) { 45 | mini = lci; 46 | } 47 | if (rci list.get(rci)) { 48 | mini = rci; 49 | } 50 | if (mini != pi) { 51 | Swap(mini, pi); 52 | downheapify(mini); 53 | } 54 | 55 | } 56 | public int min() 57 | { 58 | return list.get(0); 59 | } 60 | public int size() { 61 | return list.size(); 62 | } 63 | public void Display() { 64 | System.out.println(list); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Lec37/Heap_Clint.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | public class Heap_Clint { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Heap hp = new Heap(); 8 | hp.add(10); 9 | hp.add(20); 10 | hp.add(30); 11 | hp.add(5); 12 | hp.add(7); 13 | hp.add(3); 14 | hp.add(2); 15 | hp.add(-10); 16 | hp.add(-20); 17 | System.out.println(hp.min()); 18 | hp.Display(); 19 | System.out.println(hp.remove()); 20 | hp.Display(); 21 | System.out.println(hp.remove()); 22 | hp.Display(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lec37/Kth_Largest_Element.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | import java.util.Collections; 4 | import java.util.PriorityQueue; 5 | 6 | public class Kth_Largest_Element { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | int[] arr = { 3, 2, 3, 1, 2, 4, 5, 5, 6 }; 11 | int k = 3; 12 | System.out.println(KthLargest(arr, k)); 13 | 14 | } 15 | 16 | public static int KthLargest(int[] arr, int k) { 17 | PriorityQueue pq = new PriorityQueue<>();// min heap 18 | // PriorityQueue pq = new 19 | // PriorityQueue<>(Collections.reverseOrder());max heap 20 | for (int i = 0; i < k; i++) { 21 | pq.add(arr[i]); 22 | 23 | } 24 | for (int i = k; i < arr.length; i++) { 25 | if (arr[i] > pq.peek()) { 26 | pq.poll();// remove 27 | pq.add(arr[i]); 28 | } 29 | 30 | } 31 | return pq.peek(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec37/Merge_Rope.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | public class Merge_Rope { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 2, 1, 3, 4 }; 10 | System.out.println(Cost(arr)); 11 | 12 | } 13 | public static int Cost(int [] arr) { 14 | int sum =0; 15 | PriorityQueue pq = new PriorityQueue<>(); 16 | for (int i = 0; i < arr.length; i++) { 17 | pq.add(arr[i]); 18 | } 19 | while(pq.size()>1) { 20 | int a = pq.poll(); 21 | int b = pq.poll(); 22 | sum +=(a+b); 23 | pq.add(a+b); 24 | } 25 | return sum; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec37/Merge_k_Sorted_Lists.java: -------------------------------------------------------------------------------- 1 | package Lec37; 2 | 3 | import java.util.Comparator; 4 | import java.util.PriorityQueue; 5 | 6 | public class Merge_k_Sorted_Lists { 7 | 8 | public class ListNode { 9 | int val; 10 | ListNode next; 11 | 12 | ListNode() { 13 | } 14 | 15 | ListNode(int val) { 16 | this.val = val; 17 | } 18 | 19 | ListNode(int val, ListNode next) { 20 | this.val = val; 21 | this.next = next; 22 | } 23 | } 24 | 25 | class Solution { 26 | public ListNode mergeKLists(ListNode[] lists) { 27 | PriorityQueue pq = new PriorityQueue<>(new Comparator() { 28 | 29 | @Override 30 | public int compare(ListNode o1, ListNode o2) { 31 | // TODO Auto-generated method stub 32 | return o1.val - o2.val; 33 | } 34 | }); 35 | for (int i = 0; i < lists.length; i++) { 36 | if (lists[i] != null) { 37 | pq.add(lists[i]); 38 | } 39 | 40 | } 41 | ListNode Dummy = new ListNode(); 42 | ListNode temp = Dummy; 43 | while (!pq.isEmpty()) { 44 | ListNode r = pq.poll(); 45 | Dummy.next = r; 46 | Dummy = Dummy.next; 47 | if (r.next != null) { 48 | pq.add(r.next); 49 | } 50 | } 51 | return temp.next; 52 | 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Lec39/Patching_Array.java: -------------------------------------------------------------------------------- 1 | package Lec39; 2 | 3 | public class Patching_Array { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 1, 5, 10 }; 8 | int n = 33; 9 | System.out.println(minPatches(arr, n)); 10 | } 11 | 12 | public static int minPatches(int[] nums, int n) { 13 | long missing = 1; 14 | int count = 0; 15 | int i = 0; 16 | while (missing <= n) { 17 | if (i < nums.length && missing >= nums[i]) { 18 | missing += nums[i]; 19 | i++; 20 | } else { 21 | count++; 22 | missing += missing; 23 | } 24 | 25 | } 26 | return count; 27 | 28 | } 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /Lec4/Pattern_26.java: -------------------------------------------------------------------------------- 1 | package Lec4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_26 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | 15 | while (row <= n) { 16 | int i = 1; 17 | while (i <= space) { 18 | System.out.print("\t"); 19 | i++; 20 | } 21 | int val=1; 22 | int j = 1; 23 | while (j <= star) { 24 | System.out.print(val+"\t"); 25 | j++; 26 | val++; 27 | } 28 | space--; 29 | star += 2; 30 | row++; 31 | System.out.println(); 32 | } 33 | 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Lec4/Pattern_27.java: -------------------------------------------------------------------------------- 1 | package Lec4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_27 { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | while (row <= n) { 15 | int i = 1; 16 | while (i <= space) { 17 | System.out.print(" "); 18 | i++; 19 | } 20 | int j = 1; 21 | int val = 1; 22 | while (j <= star) { 23 | System.out.print(val + " "); 24 | if (j <= star / 2) { 25 | val++; 26 | } else { 27 | val--; 28 | } 29 | j++; 30 | } 31 | space--; 32 | star += 2; 33 | row++; 34 | System.out.println(); 35 | } 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lec4/Pattern_Double_Sided_Arrow.java: -------------------------------------------------------------------------------- 1 | package Lec4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern_Double_Sided_Arrow { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int sp1 = n - 1; 12 | int sp2 = -1; 13 | int star = 1; 14 | int row = 1; 15 | int val=1; 16 | while (row <= n) { 17 | int i = 1; 18 | while (i <= sp1) { 19 | System.out.print(" "); 20 | i++; 21 | } 22 | int j = 1; 23 | int p=val; 24 | while (j <= star) { 25 | System.out.print(p+" "); 26 | p--; 27 | j++; 28 | } 29 | 30 | int k = 1; 31 | while (k <= sp2) { 32 | System.out.print(" "); 33 | k++; 34 | } 35 | 36 | int l = 1; 37 | p= p+1; 38 | if(row==1 || row==n) { 39 | l=2; 40 | } 41 | 42 | while (l <= star) { 43 | System.out.print(p+" "); 44 | p++; 45 | l++; 46 | } 47 | if (row <= n / 2) { 48 | star++; 49 | sp1 -= 2; 50 | sp2 += 2; 51 | val++; 52 | } else { 53 | star--; 54 | sp1 += 2; 55 | sp2 -= 2; 56 | val--; 57 | } 58 | row++; 59 | System.out.println(); 60 | 61 | } 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Lec4/Pattrrn_25.java: -------------------------------------------------------------------------------- 1 | package Lec4; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattrrn_25 { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 1; 12 | int star = 1; 13 | int space = n - 1; 14 | int val=1; 15 | while (row <= n) { 16 | int i = 1; 17 | while (i <= space) { 18 | System.out.print("\t"); 19 | i++; 20 | } 21 | int j = 1; 22 | while (j <= star) { 23 | System.out.print(val+"\t"); 24 | j++; 25 | val++; 26 | } 27 | space--; 28 | star += 2; 29 | row++; 30 | System.out.println(); 31 | } 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec40/Graph_Bipartite.java: -------------------------------------------------------------------------------- 1 | package Lec40; 2 | 3 | import java.util.*; 4 | 5 | public class Graph_Bipartite { 6 | 7 | class Solution { 8 | public boolean isBipartite(int[][] graph) { 9 | Map visited = new HashMap<>(); 10 | Queue q = new LinkedList<>(); 11 | for (int src = 0; src < graph.length; src++) { 12 | if (visited.containsKey(src)) { 13 | continue; 14 | } 15 | q.add(new BipartitePair(src, 0)); 16 | while (!q.isEmpty()) { 17 | // 1. remove 18 | BipartitePair rv = q.poll();// (1,0) 19 | // 2. if Already Visited then Ignore kro 20 | if (visited.containsKey(rv.vtx)) { 21 | 22 | if (visited.get(rv.vtx) != rv.lv) 23 | return false; 24 | else 25 | continue; 26 | } 27 | // 3. visited 28 | visited.put(rv.vtx, rv.lv); 29 | 30 | // 5. add nbrs 31 | for (int nbrs : graph[rv.vtx]) { 32 | if (!visited.containsKey(nbrs)) { 33 | q.add(new BipartitePair(nbrs, rv.lv + 1)); 34 | } 35 | } 36 | 37 | } 38 | } 39 | return true; 40 | } 41 | 42 | } 43 | 44 | class BipartitePair { 45 | int vtx; 46 | int lv; 47 | 48 | public BipartitePair(int src, int lv) { 49 | // TODO Auto-generated constructor stub 50 | this.vtx = src; 51 | this.lv = lv; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Lec40/Graph_Client.java: -------------------------------------------------------------------------------- 1 | package Lec40; 2 | 3 | import java.util.HashSet; 4 | 5 | public class Graph_Client { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Graph g = new Graph(7); 10 | 11 | g.AddEdge(1, 4, 6); 12 | g.AddEdge(1, 2, 10); 13 | g.AddEdge(2, 3, 7); 14 | g.AddEdge(3, 4, 5); 15 | g.AddEdge(4, 5, 1); 16 | g.AddEdge(5, 6, 4); 17 | g.AddEdge(7, 5, 2); 18 | g.AddEdge(6, 7, 3); 19 | // g.display(); 20 | // g.removevertex(4); 21 | // System.out.println(); 22 | // g.display(); 23 | // System.out.println(g.haspath(1, 6, new HashSet<>())); 24 | //g.PrintAllpath(1, 6, new HashSet<>(), ""); 25 | System.out.println(g.BFS(1, 6)); 26 | System.out.println(g.DFS(1, 6)); 27 | g.DFT(); 28 | g.BFT(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec40/valid_Tree.java: -------------------------------------------------------------------------------- 1 | package Lec40; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Stack; 8 | 9 | public class valid_Tree { 10 | class Solution { 11 | public boolean validTree(int n, int[][] edges) { 12 | HashMap> map = new HashMap<>(); 13 | for (int i = 0; i < n; i++) { 14 | map.put(i, new ArrayList<>()); 15 | } 16 | for (int i = 0; i < edges.length; i++) { 17 | int v1 = edges[i][0]; 18 | int v2 = edges[i][1]; 19 | map.get(v1).add(v2); 20 | map.get(v2).add(v1); 21 | } 22 | return isValid(map); 23 | 24 | } 25 | 26 | public boolean isValid(HashMap> map) { 27 | 28 | HashSet visited = new HashSet<>(); 29 | Stack st = new Stack<>(); 30 | int count = 0; 31 | for (int src : map.keySet()) { 32 | if (visited.contains(src)) { 33 | continue; 34 | } 35 | count++; 36 | st.push(src); 37 | while (!st.isEmpty()) { 38 | // 1. remove 39 | int rv = st.pop(); 40 | // 2. if Already Visited then Ignore kro 41 | if (visited.contains(rv)) { 42 | return false; 43 | } 44 | // 3. visited 45 | visited.add(rv); 46 | 47 | // 5. add nbrs 48 | for (int nbrs : map.get(rv)) { 49 | if (!visited.contains(nbrs)) { 50 | st.push(nbrs); 51 | } 52 | } 53 | 54 | } 55 | } 56 | return count == 1; 57 | } 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Lec42/CourseSchedule_II.java: -------------------------------------------------------------------------------- 1 | package Lec42; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Queue; 8 | 9 | public class CourseSchedule_II { 10 | 11 | class Solution { 12 | public int[] findOrder(int numCourses, int[][] prerequisites) { 13 | HashMap> map = new HashMap<>(); 14 | for (int i = 0; i < numCourses; i++) { 15 | map.put(i, new ArrayList<>()); 16 | } 17 | for (int i = 0; i < prerequisites.length; i++) { 18 | int v1 = prerequisites[i][0]; 19 | int v2 = prerequisites[i][1]; 20 | map.get(v2).add(v1); 21 | } 22 | return TopologicalSort(map); 23 | } 24 | 25 | public int[] TopologicalSort(HashMap> map) { 26 | int[] in = Indegre(map); 27 | int[] course = new int[map.size()]; 28 | int idx = 0; 29 | Queue q = new LinkedList<>(); 30 | for (int i = 0; i < in.length; i++) { 31 | if (in[i] == 0) { 32 | q.add(i); 33 | } 34 | } 35 | int count = 0; 36 | while (!q.isEmpty()) { 37 | int rv = q.remove(); 38 | course[idx++] = rv; 39 | count++; 40 | for (int nbrs : map.get(rv)) { 41 | in[nbrs]--; 42 | if (in[nbrs] == 0) { 43 | q.add(nbrs); 44 | } 45 | } 46 | 47 | } 48 | return count == map.size() ? course : new int[0]; 49 | 50 | } 51 | 52 | public int[] Indegre(HashMap> map) { 53 | int[] in = new int[map.size()]; 54 | for (int key : map.keySet()) { 55 | for (int nbrs : map.get(key)) { 56 | in[nbrs]++; 57 | } 58 | } 59 | return in; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Lec42/Course_Schedule.java: -------------------------------------------------------------------------------- 1 | package Lec42; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Queue; 8 | 9 | public class Course_Schedule { 10 | class Solution { 11 | public boolean canFinish(int numCourses, int[][] prerequisites) { 12 | HashMap> map = new HashMap<>(); 13 | for (int i = 0; i < numCourses; i++) { 14 | map.put(i, new ArrayList<>()); 15 | } 16 | for (int i = 0; i < prerequisites.length; i++) { 17 | int v1 = prerequisites[i][0]; 18 | int v2 = prerequisites[i][1]; 19 | map.get(v2).add(v1); 20 | } 21 | return TopologicalSort(map); 22 | 23 | } 24 | 25 | public boolean TopologicalSort(HashMap> map) { 26 | int[] in = Indegre(map); 27 | Queue q = new LinkedList<>(); 28 | for (int i = 0; i < in.length; i++) { 29 | if (in[i] == 0) { 30 | q.add(i); 31 | } 32 | } 33 | int count = 0; 34 | while (!q.isEmpty()) { 35 | int rv = q.remove(); 36 | count++; 37 | for (int nbrs : map.get(rv)) { 38 | in[nbrs]--; 39 | if (in[nbrs] == 0) { 40 | q.add(nbrs); 41 | } 42 | } 43 | 44 | } 45 | return count == map.size(); 46 | 47 | } 48 | 49 | public int[] Indegre(HashMap> map) { 50 | int[] in = new int[map.size() ]; 51 | for (int key : map.keySet()) { 52 | for (int nbrs : map.get(key)) { 53 | in[nbrs]++; 54 | } 55 | } 56 | return in; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Lec42/DSU.java: -------------------------------------------------------------------------------- 1 | package Lec42; 2 | 3 | import java.util.HashMap; 4 | 5 | public class DSU { 6 | class Node { 7 | int vtx; 8 | Node parent; 9 | int rank; 10 | 11 | } 12 | 13 | HashMap map = new HashMap<>(); 14 | 15 | public void CreateSet(int v) { 16 | Node node = new Node(); 17 | node.vtx = v; 18 | node.parent = node; 19 | node.rank = 0; 20 | map.put(v, node); 21 | } 22 | 23 | public int find(int v) { 24 | Node nn = map.get(v); 25 | 26 | return find(nn).vtx; 27 | } 28 | 29 | private Node find(Node nn) { 30 | if (nn.parent == nn) { 31 | return nn; 32 | } 33 | Node node = find(nn.parent); 34 | nn.parent = node;// path Compression 35 | return node; 36 | 37 | } 38 | 39 | public void Union(int v1, int v2) { 40 | Node n1 = map.get(v1); 41 | Node re1 = find(n1); 42 | Node n2 = map.get(v2); 43 | Node re2 = find(n2); 44 | if (re1.rank == re2.rank) { 45 | re1.parent = re2; 46 | re2.rank++; 47 | } else if (re1.rank < re2.rank) { 48 | re1.parent = re2; 49 | } else { 50 | re2.parent = re1; 51 | } 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Lec42/Topological_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec42; 2 | 3 | import java.util.*; 4 | 5 | public class Topological_Sort { 6 | private HashMap> map = new HashMap<>(); 7 | 8 | public Topological_Sort(int v) { 9 | // TODO Auto-generated constructor stub 10 | for (int i = 1; i <= v; i++) { 11 | map.put(i, new HashMap<>()); 12 | } 13 | } 14 | 15 | public void addEdge(int v1, int v2, int cost) { 16 | map.get(v1).put(v2, cost);// v1--> v2 17 | 18 | } 19 | 20 | public void TopologicalSort() { 21 | int[] in = Indegre(); 22 | Queue q = new LinkedList<>(); 23 | for (int i = 1; i < in.length; i++) { 24 | if (in[i] == 0) { 25 | q.add(i); 26 | } 27 | } 28 | while (!q.isEmpty()) { 29 | int rv = q.remove(); 30 | System.out.print(rv); 31 | for (int nbrs : map.get(rv).keySet()) { 32 | in[nbrs]--; 33 | if (in[nbrs] == 0) { 34 | q.add(nbrs); 35 | } 36 | } 37 | 38 | } 39 | 40 | } 41 | 42 | public int[] Indegre() { 43 | int[] in = new int[map.size() + 1]; 44 | for (int key : map.keySet()) { 45 | for (int nbrs : map.get(key).keySet()) { 46 | in[nbrs]++; 47 | } 48 | } 49 | return in; 50 | } 51 | public static void main(String[] args) { 52 | Topological_Sort t = new Topological_Sort(8); 53 | t.addEdge(1, 2,0); 54 | //t.addEdge(2, 1); 55 | t.addEdge(2, 4,0); 56 | t.addEdge(3, 1,0); 57 | t.addEdge(3, 2,0); 58 | t.addEdge(4, 5,0); 59 | t.addEdge(4, 6,0); 60 | t.addEdge(5, 6,0); 61 | t.addEdge(7, 8,0); 62 | System.out.println(Arrays.toString(t.Indegre())); 63 | t.TopologicalSort(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Lec45/Longest_Cycle_in_a_Graph.java: -------------------------------------------------------------------------------- 1 | package Lec45; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Longest_Cycle_in_a_Graph { 7 | class Solution { 8 | public int longestCycle(int[] edges) { 9 | 10 | int[] in = new int[edges.length]; 11 | for (int i = 0; i < in.length; i++) { 12 | if (edges[i] != -1) { 13 | in[edges[i]]++; 14 | } 15 | } 16 | boolean []visited = new boolean [edges.length]; 17 | Queue q = new LinkedList<>(); 18 | for (int i = 0; i [] arr; 8 | static TreeSet set; 9 | 10 | public static void main(String[] args) { 11 | // TODO Auto-generated method stub 12 | Scanner sc = new Scanner(System.in); 13 | int n = sc.nextInt(); 14 | int m = sc.nextInt(); 15 | arr = new TreeSet[n]; 16 | for (int i = 0; i < n; i++) { 17 | arr[i] = new TreeSet<>(); 18 | } 19 | for (int i = 0; i < m; i++) { 20 | int a = sc.nextInt() - 1; 21 | int b = sc.nextInt() - 1; 22 | arr[a].add(b); 23 | arr[b].add(a); 24 | } 25 | set = new TreeSet<>(); 26 | // for (HashSet a : arr) { 27 | // System.out.print(a + " "); 28 | // } 29 | for (int i = 0; i < n; i++) { 30 | set.add(i); 31 | } 32 | int ans = 0; 33 | for (int i = 0; i < n; i++) { 34 | if (set.remove(i)) { 35 | ans++; 36 | dfs(i); 37 | } 38 | } 39 | System.out.println(ans - 1); 40 | 41 | } 42 | 43 | public static void dfs(int x) { 44 | List ll = new ArrayList(); 45 | for (int i : set) { 46 | if (!arr[x].contains(i)) { 47 | ll.add(i); 48 | } 49 | } 50 | 51 | for (int i : ll) { 52 | set.remove(i); 53 | } 54 | for (int i : ll) { 55 | dfs(i); 56 | } 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Lec45/Minimize_Malware_Spread.java: -------------------------------------------------------------------------------- 1 | package Lec45; 2 | import java.util.*; 3 | public class Minimize_Malware_Spread { 4 | 5 | public class DSU { 6 | public class Node { 7 | int val; 8 | Node parent; 9 | int rank; 10 | 11 | } 12 | 13 | HashMap map = new HashMap<>(); 14 | 15 | public void createSet(int v) { 16 | Node nn = new Node(); 17 | nn.val = v; 18 | nn.parent = nn; 19 | nn.rank = 1; 20 | map.put(v, nn); 21 | } 22 | 23 | public int find(int v) { 24 | Node nn = map.get(v); 25 | 26 | return find(nn).val; 27 | 28 | } 29 | 30 | private Node find(Node nn) { 31 | if (nn.parent == nn) { 32 | return nn; 33 | } 34 | 35 | return nn.parent=find(nn.parent); 36 | 37 | } 38 | 39 | public void union(int v1, int v2) { 40 | Node n1 = map.get(v1); 41 | Node n2 = map.get(v2); 42 | 43 | Node re1 = find(n1); 44 | Node re2 = find(n2); 45 | 46 | re1.parent = re2; 47 | re2.rank = re1.rank + re2.rank; 48 | 49 | } 50 | 51 | public int size(int v) { 52 | Node nn = map.get(v); 53 | return find(nn).rank; 54 | } 55 | 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Lec46/Fibo.java: -------------------------------------------------------------------------------- 1 | package Lec46; 2 | 3 | public class Fibo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 150; 8 | int[] dp = new int[n + 1]; 9 | System.out.println(fibTD(n, dp)); 10 | System.out.println(fibBU(n)); 11 | System.out.println(fib(n)); 12 | 13 | } 14 | 15 | public static int fib(int n) { 16 | if (n == 0 || n == 1) { 17 | return n; 18 | } 19 | 20 | int f1 = fib(n - 1); 21 | int f2 = fib(n - 2); 22 | return f1 + f2; 23 | 24 | } 25 | 26 | public static int fibTD(int n, int[] dp) { 27 | if (n == 0 || n == 1) { 28 | return n; 29 | } 30 | if (dp[n] != 0) {// dp Apply kra hai 31 | return dp[n]; 32 | } 33 | int f1 = fibTD(n - 1, dp);// dp[n-1] 34 | int f2 = fibTD(n - 2, dp);// dp[n-2] 35 | return dp[n] = f1 + f2;// dp[n]=dp[n-1]+dp[n-2] 36 | 37 | } 38 | 39 | public static int fibBU(int n) { 40 | int[] dp = new int[n + 1]; 41 | dp[0] = 0; 42 | dp[1] = 1; 43 | for (int i = 2; i < dp.length; i++) { 44 | dp[i] = dp[i - 1] + dp[i - 2]; 45 | } 46 | return dp[n]; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Lec49/Divisor_Game.java: -------------------------------------------------------------------------------- 1 | package Lec49; 2 | 3 | public class Divisor_Game { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println(divisorGame(901)); 8 | 9 | } 10 | 11 | public static boolean divisorGame(int n) { 12 | return n % 2 == 0; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lec49/House_Robber.java: -------------------------------------------------------------------------------- 1 | package Lec49; 2 | 3 | import java.util.Arrays; 4 | 5 | public class House_Robber { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 2, 7, 9, 3, 1 }; 10 | int[] dp = new int[arr.length]; 11 | Arrays.fill(dp, -1); 12 | System.out.println(Robber(arr, 0)); 13 | System.out.println(RobberTD(arr, 0, dp)); 14 | } 15 | 16 | public static int Robber(int[] arr, int i) { 17 | if (i >= arr.length) { 18 | return 0; 19 | } 20 | int rob = arr[i] + Robber(arr, i + 2); 21 | int Dontrob = Robber(arr, i + 1); 22 | return Math.max(rob, Dontrob); 23 | 24 | } 25 | 26 | public static int RobberTD(int[] arr, int i, int[] dp) { 27 | if (i >= arr.length) { 28 | return 0; 29 | } 30 | if (dp[i] != -1) { 31 | return dp[i]; 32 | } 33 | int rob = arr[i] + RobberTD(arr, i + 2, dp); 34 | int Dontrob = RobberTD(arr, i + 1, dp); 35 | return dp[i] = Math.max(rob, Dontrob); 36 | 37 | } 38 | 39 | public static int RobberBU(int[] arr) { 40 | if(arr.length==1) { 41 | return arr[0]; 42 | } 43 | int[] dp = new int[arr.length]; 44 | dp[0] = arr[0]; 45 | dp[1] = Math.max(arr[0], arr[1]); 46 | for (int i = 2; i < dp.length; i++) { 47 | int rob = arr[i] + dp[i - 2]; 48 | int Dontrob = dp[i - 1]; 49 | dp[i] = Math.max(rob, Dontrob); 50 | } 51 | return dp[arr.length-1]; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lec49/Lis.java: -------------------------------------------------------------------------------- 1 | package Lec49; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Lis { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 10, 9, 2, 5, 3, 7, 101, 18 }; 10 | System.out.println(lis(arr)); 11 | } 12 | 13 | public static int lis(int[] arr) { 14 | 15 | int[] dp = new int[arr.length]; 16 | Arrays.fill(dp, 1); 17 | for (int i = 1; i < dp.length; i++) { 18 | for (int j = i - 1; j >= 0; j--) { 19 | if (arr[i] > arr[j]) { 20 | dp[i] = Math.max(dp[i], dp[j] + 1); 21 | } 22 | } 23 | } 24 | int max = dp[0]; 25 | for (int i = 1; i < dp.length; i++) { 26 | max = Math.max(dp[i], max); 27 | } 28 | return max; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec49/Lis_Nlong_N.java: -------------------------------------------------------------------------------- 1 | package Lec49; 2 | 3 | public class Lis_Nlong_N { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 10, 9, 2, 5, 3, 7, 101, 18 }; 8 | System.out.println(lis(arr)); 9 | } 10 | 11 | public static int lis(int[] arr) { 12 | int[] dp = new int[arr.length]; 13 | dp[0] = arr[0]; 14 | int len = 1; 15 | for (int i = 1; i < arr.length; i++) { 16 | if (arr[i] > dp[len - 1]) { 17 | dp[len] = arr[i]; 18 | len++; 19 | } else { 20 | int ith = Search(dp, 0, len - 1, arr[i]); 21 | dp[ith] = arr[i]; 22 | } 23 | 24 | } 25 | return len; 26 | } 27 | 28 | private static int Search(int[] dp, int si, int ei, int item) { 29 | // TODO Auto-generated method stub 30 | int ans = 0; 31 | while (si <= ei) { 32 | int mid = (si + ei) / 2; 33 | if (dp[mid] >= item) { 34 | ans = mid; 35 | ei = mid - 1; 36 | } else { 37 | si = mid + 1; 38 | } 39 | } 40 | return ans; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Lec5/FUnction_demo_3.java: -------------------------------------------------------------------------------- 1 | package Lec5; 2 | 3 | public class FUnction_demo_3 { 4 | static int val = 100; 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | System.out.println("hello"); 9 | int a = 5; 10 | int b = 7; 11 | System.out.println(val); 12 | System.out.println(addition(b, a)); 13 | System.out.println(val); 14 | System.out.println("bye"); 15 | } 16 | 17 | public static int addition(int a, int b) { 18 | int val = 90; 19 | int c = a + b; 20 | FUnction_demo_3.val += 4; 21 | System.out.println(val); 22 | return c; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lec5/Fun_demo.java: -------------------------------------------------------------------------------- 1 | package Lec5; 2 | 3 | public class Fun_demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println("hello"); 8 | addition(); 9 | System.out.println("bye"); 10 | } 11 | public static void addition() { 12 | int a = 5; 13 | int b = 7; 14 | int c = a + b; 15 | sub(); 16 | System.out.println(c); 17 | } 18 | public static void sub() { 19 | int a = 5; 20 | int b = 7; 21 | int c = a - b; 22 | System.out.println(c); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec5/Function_demo_2.java: -------------------------------------------------------------------------------- 1 | package Lec5; 2 | 3 | public class Function_demo_2 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.println("hello"); 8 | System.out.println(addition()); 9 | System.out.println("bye"); 10 | } 11 | 12 | public static int addition() { 13 | int a = 5; 14 | int b = 7; 15 | int c = a + b; 16 | System.out.println(sub()); 17 | return c; 18 | } 19 | 20 | public static int sub() { 21 | int a = 5; 22 | int b = 7; 23 | int c = a - b; 24 | return c; 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec5/Is_Armstrong.java: -------------------------------------------------------------------------------- 1 | package Lec5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Is_Armstrong { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | System.out.println(Armstrong(n)); 12 | 13 | } 14 | 15 | public static boolean Armstrong(int n) { 16 | int cod = Countofdigit(n); 17 | int sum = 0; 18 | int k = n; 19 | while (n > 0) { 20 | int rem = n % 10; 21 | sum = (int) (sum + Math.pow(rem, cod)); 22 | n = n / 10; 23 | } 24 | return sum == k; 25 | 26 | } 27 | 28 | public static int Countofdigit(int n) { 29 | int count = 0; 30 | while (n > 0) { 31 | count++; 32 | n = n / 10; 33 | 34 | } 35 | return count; 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lec5/Pascal_Triangle.java: -------------------------------------------------------------------------------- 1 | package Lec5; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pascal_Triangle { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int row = 0; 12 | int star = 1; 13 | while (row < n) { 14 | 15 | int i = 0; 16 | int val = 1; 17 | while (i < star) { 18 | System.out.print(val + " "); 19 | val = (val * (row - i)) / (i + 1); 20 | i++; 21 | } 22 | row++; 23 | star++; 24 | System.out.println(); 25 | 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec50/Coin_Change_II.java: -------------------------------------------------------------------------------- 1 | package Lec50; 2 | 3 | public class Coin_Change_II { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] coin = { 1, 2, 5 }; 8 | int amount = 5; 9 | System.out.println(Coin_Change(coin, amount, 0)); 10 | } 11 | 12 | public static int Coin_Change(int[] coin, int amount, int idx) { 13 | if (amount == 0) { 14 | return 1; 15 | } 16 | if (idx == coin.length) { 17 | return 0; 18 | } 19 | int inc = 0, exc = 0; 20 | if (coin[idx] <= amount) { 21 | inc = Coin_Change(coin, amount - coin[idx], idx);// dp[amount - coin[idx]][idx 22 | } 23 | exc = Coin_Change(coin, amount, idx + 1);//// dp[amount - coin[idx]][idx-1] 24 | return inc + exc; 25 | 26 | } 27 | 28 | public static int Coin_Change(int[] coin, int amount) { 29 | int[][] dp = new int[coin.length + 1][amount + 1]; 30 | for (int i = 0; i < dp.length; i++) { 31 | dp[i][0] = 1; 32 | } 33 | for (int i = 1; i < dp.length; i++) { 34 | for (int am = 1; am < dp[0].length; am++) { 35 | 36 | int inc = 0, exc = 0; 37 | if (coin[i - 1] <= am) { 38 | inc = dp[i][am - coin[i - 1]]; 39 | } 40 | exc = dp[i - 1][am]; 41 | dp[i][am] = inc + exc; 42 | } 43 | } 44 | return dp[dp.length - 1][dp[0].length - 1]; 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Lec50/Distinct_Subsequences.java: -------------------------------------------------------------------------------- 1 | package Lec50; 2 | 3 | public class Distinct_Subsequences { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String s = "rabbbit"; 8 | String t = "rabbit"; 9 | System.out.println(numDistinct(s, t, 0, 0)); 10 | } 11 | 12 | // i for s j for t ; 13 | // s for coin t for amount 14 | public static int numDistinct(String s, String t, int i, int j) { 15 | if (j == t.length()) { 16 | return 1; 17 | } 18 | if (i == s.length()) { 19 | return 0; 20 | } 21 | 22 | int inc = 0, exc = 0; 23 | if (s.charAt(i) == t.charAt(j)) { 24 | inc = numDistinct(s, t, i + 1, j + 1); 25 | } 26 | exc = numDistinct(s, t, i + 1, j); 27 | return inc + exc; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lec50/Uncrossed_Lines.java: -------------------------------------------------------------------------------- 1 | package Lec50; 2 | 3 | public class Uncrossed_Lines { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr1 = { 1, 4, 2 }; 8 | int[] arr2 = { 1, 2, 4 }; 9 | System.out.println(Lines(arr1, arr2)); 10 | 11 | } 12 | 13 | public static int Lines(int[] arr1, int[] arr2) { 14 | int[][] dp = new int[arr1.length + 1][arr2.length + 1]; 15 | for (int i = 1; i < dp.length; i++) { 16 | for (int j = 1; j < dp[0].length; j++) { 17 | int ans = 0; 18 | if (arr1[i - 1] == arr2[j - 1]) { 19 | ans = 1 + dp[i - 1][j - 1]; 20 | } else { 21 | ans = Math.max(dp[i - 1][j], dp[i][j - 1]); 22 | } 23 | dp[i][j] = ans; 24 | 25 | } 26 | } 27 | return dp[dp.length - 1][dp[0].length - 1]; 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec51/Edit_Distance.java: -------------------------------------------------------------------------------- 1 | package Lec51; 2 | 3 | public class Edit_Distance { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String s1 = "FOOD"; 8 | String s2 = "MONKEY"; 9 | System.out.println(MinOps(s1, s2, 0, 0)); 10 | 11 | } 12 | 13 | public static int MinOps(String s1, String s2, int i, int j) { 14 | if (i == s1.length()) { 15 | return s2.length() - j; 16 | } 17 | if (j == s2.length()) { 18 | return s1.length() - i; 19 | } 20 | int ans = 0; 21 | if (s1.charAt(i) == s2.charAt(j)) { 22 | ans = MinOps(s1, s2, i + 1, j + 1); 23 | } else { 24 | int I = MinOps(s1, s2, i, j + 1); 25 | int D = MinOps(s1, s2, i + 1, j); 26 | int R = MinOps(s1, s2, i + 1, j + 1); 27 | ans = Math.min(I, Math.min(D, R)) + 1; 28 | } 29 | return ans; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec51/Minimum_Falling_Path_Sum.java: -------------------------------------------------------------------------------- 1 | package Lec51; 2 | 3 | public class Minimum_Falling_Path_Sum { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[][] arr = { { 2, 1, 3 }, { 6, 5, 4 }, { 7, 8, 9 } }; 8 | int ans = Integer.MAX_VALUE; 9 | for (int i = 0; i < arr[0].length; i++) { 10 | ans = Math.min(ans, Falling_Path_Sum(arr, 0, i)); 11 | } 12 | System.out.println(ans); 13 | } 14 | 15 | public static int Falling_Path_Sum(int[][] arr, int i, int j) { 16 | if (j < 0 || j >= arr[0].length) { 17 | return Integer.MAX_VALUE; 18 | } 19 | if (i == arr.length - 1) { 20 | return arr[i][j]; 21 | } 22 | int a = Falling_Path_Sum(arr, i + 1, j - 1); 23 | int b = Falling_Path_Sum(arr, i + 1, j); 24 | int c = Falling_Path_Sum(arr, i + 1, j + 1); 25 | return arr[i][j] + Math.min(a, Math.min(b, c)); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec51/Minimum_Falling_Path_Sum_II.java: -------------------------------------------------------------------------------- 1 | package Lec51; 2 | 3 | public class Minimum_Falling_Path_Sum_II { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 8 | int ans = Integer.MAX_VALUE; 9 | for (int i = 0; i < arr[0].length; i++) { 10 | ans = Math.min(ans, Falling_Path_Sum(arr, 0, i)); 11 | } 12 | System.out.println(ans); 13 | } 14 | 15 | public static int Falling_Path_Sum(int[][] arr, int i, int j) { 16 | if (i == arr.length - 1) { 17 | return arr[i][j]; 18 | } 19 | int ans = Integer.MAX_VALUE; 20 | for (int col = 0; col < arr[0].length; col++) { 21 | if (col != j) { 22 | ans = Math.min(ans, Falling_Path_Sum(arr, i + 1, col)); 23 | } 24 | } 25 | return ans + arr[i][j]; 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec51/Valentine_Magic.java: -------------------------------------------------------------------------------- 1 | package Lec51; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Valentine_Magic { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] boys = { 2, 11, 3 }; 10 | int[] girls = { 5, 7, 3, 2 }; 11 | Arrays.sort(boys); 12 | Arrays.sort(girls); 13 | System.out.println(MinDiff(boys, girls, 0, 0)); 14 | 15 | } 16 | 17 | public static int MinDiff(int[] boys, int[] girls, int i, int j) { 18 | if (i == boys.length) { 19 | return 0; 20 | } 21 | if (j == girls.length) { 22 | return 10000000; 23 | } 24 | int pair = Math.abs(boys[i] - girls[j]) + MinDiff(boys, girls, i + 1, j + 1); 25 | int NoPair = MinDiff(boys, girls, i, j + 1); 26 | return Math.min(pair, NoPair); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec51/Wine_Problem.java: -------------------------------------------------------------------------------- 1 | package Lec51; 2 | 3 | public class Wine_Problem { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] wine = { 2, 3, 5, 1, 4 }; 8 | System.out.println(MaxProfit(wine, 0, wine.length - 1, 1)); 9 | System.out.println(MaxProfitBU(wine)); 10 | 11 | } 12 | 13 | public static int MaxProfit(int[] wine, int i, int j, int year) { 14 | if (i > j) { 15 | return 0; 16 | } 17 | int first = wine[i] * year + MaxProfit(wine, i + 1, j, year + 1); 18 | int last = wine[j] * year + MaxProfit(wine, i, j - 1, year + 1); 19 | return Math.max(first, last); 20 | } 21 | 22 | public static int MaxProfitBU(int[] wine) { 23 | int[][] dp = new int[wine.length][wine.length]; 24 | for (int i = 0; i < dp.length; i++) { 25 | dp[i][i] = wine[i] * wine.length; 26 | } 27 | int year = wine.length - 1; 28 | for (int dia = 1; dia < dp.length; dia++) { 29 | for (int j = dia; j < wine.length; j++) { 30 | int i = j - dia; 31 | int first = wine[i] * year + dp[i + 1][j]; 32 | int last = wine[j] * year + dp[i][j - 1]; 33 | dp[i][j] = Math.max(first, last); 34 | } 35 | year--; 36 | } 37 | return dp[0][dp.length - 1]; 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Lec52/Counting_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec52; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Counting_Sort { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 2, 1, 1, 0, 1, 2, 5, 4, 0, 2, 8, 7, 9, 2, 6, 1, 9 }; 10 | arr = Sort(arr); 11 | for (int i = 0; i < arr.length; i++) { 12 | System.out.print(arr[i] + " "); 13 | } 14 | } 15 | 16 | public static int[] Sort(int[] arr) { 17 | int max = Arrays.stream(arr).max().getAsInt(); 18 | int[] frq = new int[max + 1]; 19 | for (int i = 0; i < arr.length; i++) { 20 | frq[arr[i]]++; 21 | 22 | } 23 | for (int i = 1; i < frq.length; i++) { 24 | frq[i] = frq[i - 1] + frq[i]; 25 | } 26 | int[] ans = new int[arr.length]; 27 | for (int i = arr.length - 1; i >= 0; i--) { 28 | int pos = frq[arr[i]]; 29 | ans[pos - 1] = arr[i]; 30 | frq[arr[i]]--; 31 | 32 | } 33 | return ans; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Lec53/Dungeon_Game.java: -------------------------------------------------------------------------------- 1 | package Lec53; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Dungeon_Game { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[][] arr = { { -2, -3, 3 }, { -5, -10, 1 }, { 10, 30, -5 } }; 10 | int[][] dp = new int[arr.length][arr[0].length]; 11 | for (int[] a : dp) { 12 | Arrays.fill(a, -1); 13 | } 14 | System.out.println(MinimumHP(arr, 0, 0, dp)); 15 | } 16 | 17 | public static int MinimumHP(int[][] arr, int cr, int cc, int[][] dp) { 18 | if (cr == arr.length || cc == arr[0].length) { 19 | return Integer.MAX_VALUE; 20 | } 21 | 22 | if (cr == arr.length - 1 && cc == arr[0].length - 1) { 23 | 24 | return arr[cr][cc] > 0 ? 1 : 1 - arr[cr][cc]; 25 | 26 | } 27 | if (dp[cr][cc] != -1) { 28 | return dp[cr][cc]; 29 | } 30 | int left = MinimumHP(arr, cr, cc + 1, dp); 31 | int right = MinimumHP(arr, cr + 1, cc, dp); 32 | 33 | int hp = Math.min(left, right) - arr[cr][cc]; 34 | 35 | return dp[cr][cc] = hp > 0 ? hp : 1; 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Lec53/MCM.java: -------------------------------------------------------------------------------- 1 | package Lec53; 2 | 3 | import java.util.Arrays; 4 | 5 | public class MCM { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int[] arr = { 4, 2, 3, 5, 1 }; 10 | // M1 = 4*2 11 | // M2 = 2*3 12 | // M3 = 3*5 13 | // M4 = 5*1 14 | int[][] dp = new int[arr.length][arr.length]; 15 | for (int[] a : dp) { 16 | Arrays.fill(a, -1); 17 | } 18 | 19 | System.out.println(Mcm(arr, 0, arr.length - 1, dp)); 20 | System.out.println(McmBU(arr)); 21 | 22 | } 23 | 24 | public static int Mcm(int[] arr, int i, int j, int[][] dp) { 25 | 26 | if (i + 1 == j) { 27 | return 0; 28 | } 29 | // dp Apply 30 | if (dp[i][j] != -1) { 31 | return dp[i][j]; 32 | } 33 | int ans = Integer.MAX_VALUE; 34 | for (int k = i + 1; k < j; k++) { 35 | int fs = Mcm(arr, i, k, dp);// m1--ek Martix 36 | int ss = Mcm(arr, k, j, dp);// ( m2m3m4 )--> ek Matrix milega 37 | int self = arr[i] * arr[k] * arr[j]; 38 | ans = Math.min(ans, fs + ss + self); 39 | 40 | } 41 | return dp[i][j] = ans; 42 | 43 | } 44 | 45 | public static int McmBU(int[] arr) { 46 | int[][] dp = new int[arr.length][arr.length]; 47 | 48 | for (int slide = 2; slide < dp.length; slide++) { 49 | for (int j = slide; j < dp.length; j++) { 50 | int i = j - slide; 51 | int ans = Integer.MAX_VALUE; 52 | for (int k = i + 1; k < j; k++) { 53 | int fs = dp[i][k];// m1--ek Martix 54 | int ss = dp[k][j];// ( m2m3m4 )--> ek Matrix milega 55 | int self = arr[i] * arr[k] * arr[j]; 56 | ans = Math.min(ans, fs + ss + self); 57 | 58 | } 59 | dp[i][j] = ans; 60 | 61 | } 62 | 63 | } 64 | return dp[0][arr.length - 1]; 65 | 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Lec54/Burst_Balloons.java: -------------------------------------------------------------------------------- 1 | package Lec54; 2 | 3 | public class Burst_Balloons { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] nums = { 3, 1, 5, 8 }; 8 | int[] arr = new int[nums.length + 2]; 9 | arr[0] = arr[arr.length - 1] = 1; 10 | for (int i = 1; i < arr.length - 1; i++) { 11 | arr[i] = nums[i - 1]; 12 | } 13 | System.out.println(Burst(arr, 0, arr.length - 1)); 14 | 15 | } 16 | 17 | public static int Burst(int[] arr, int si, int ei) { 18 | if (si + 1 == ei) { 19 | return 0; 20 | } 21 | int ans = Integer.MIN_VALUE; 22 | for (int k = si + 1; k < ei; k++) { 23 | int left = Burst(arr, si, k); 24 | int right = Burst(arr, k, ei); 25 | int self = arr[si] * arr[k] * arr[ei]; 26 | ans = Math.max(ans, self + left + right); 27 | 28 | } 29 | return ans; 30 | 31 | } 32 | 33 | public static int Burst(int[] arr) { 34 | int[][] dp = new int[arr.length][arr.length]; 35 | for (int slide = 2; slide < dp.length; slide++) { 36 | for (int j = slide; j < dp.length; j++) { 37 | int i = j - slide; 38 | int ans = Integer.MIN_VALUE; 39 | for (int k = i + 1; k < j; k++) { 40 | int left = dp[i][k]; 41 | int right = dp[k][j]; 42 | int self = arr[i] * arr[k] * arr[j]; 43 | ans = Math.max(ans, self + left + right); 44 | 45 | } 46 | dp[i][j] = ans; 47 | } 48 | 49 | } 50 | return dp[0][dp.length - 1]; 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lec54/Cell_Mitosis.java: -------------------------------------------------------------------------------- 1 | package Lec54; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Cell_Mitosis { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int x = sc.nextInt(); 12 | int y = sc.nextInt(); 13 | int z = sc.nextInt(); 14 | System.out.println(mincost( x, y, z, n)); 15 | 16 | } 17 | 18 | public static long mincost(int x, int y, int z, int n) { 19 | long[] dp = new long[n + 1]; 20 | for (int i = 2; i < dp.length; i++) { 21 | if (i % 2 == 0) { 22 | dp[i] = Math.min(dp[i / 2] + x, dp[i - 1] + y); 23 | } else { 24 | dp[i] = Math.min(dp[(i + 1) / 2] + x + z, dp[i - 1] + y); 25 | 26 | } 27 | 28 | } 29 | return dp[n]; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec54/House_Robber_III.java: -------------------------------------------------------------------------------- 1 | package Lec54; 2 | 3 | import java.util.HashMap; 4 | 5 | public class House_Robber_III { 6 | 7 | public class TreeNode { 8 | int val; 9 | TreeNode left; 10 | TreeNode right; 11 | 12 | TreeNode() { 13 | } 14 | 15 | TreeNode(int val) { 16 | this.val = val; 17 | } 18 | 19 | TreeNode(int val, TreeNode left, TreeNode right) { 20 | this.val = val; 21 | this.left = left; 22 | this.right = right; 23 | } 24 | } 25 | 26 | class Solution { 27 | public int rob(TreeNode root) { 28 | 29 | return house_rob(root, new HashMap<>()); 30 | 31 | } 32 | 33 | public int house_rob(TreeNode root, HashMap map) { 34 | 35 | if (root == null) { 36 | return 0; 37 | } 38 | if (map.containsKey(root)) { 39 | return map.get(root); 40 | } 41 | 42 | int rob = root.val; 43 | if (root.left != null) { 44 | rob += house_rob(root.left.left, map) + house_rob(root.left.right, map); 45 | } 46 | if (root.right != null) { 47 | rob += house_rob(root.right.left, map) + house_rob(root.right.right, map); 48 | } 49 | 50 | int dontrob = house_rob(root.left, map) + house_rob(root.right, map); 51 | int ans = Math.max(rob, dontrob); 52 | map.put(root, ans); 53 | return ans; 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Lec54/Knight_Dialer.java: -------------------------------------------------------------------------------- 1 | package Lec54; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Knight_Dialer { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int n = 3131; 10 | long ans = 0; 11 | int[][][] dp = new int[n][4][3]; 12 | for (int[][] arr : dp) { 13 | for (int[] a : arr) { 14 | Arrays.fill(a, -1); 15 | } 16 | 17 | } 18 | for (int i = 0; i < 4; i++) { 19 | for (int j = 0; j < 3; j++) { 20 | ans =(ans+ count(n - 1, i, j, dp))%1000_000_007; 21 | } 22 | } 23 | System.out.println(ans); 24 | 25 | } 26 | 27 | public static int count(int n, int cr, int cc, int[][][] dp) { 28 | if ((cr < 0 || cc < 0 || cr >= 4 || cc >= 3) || (cr == 3 && cc == 0) || (cr == 3 && cc == 2)) { 29 | return 0; 30 | } 31 | if (n == 0) { 32 | return 1; 33 | } 34 | if (dp[n][cr][cc] != -1) { 35 | return dp[n][cr][cc]; 36 | } 37 | 38 | int[] r = { -2, -2, 2, 2, -1, 1, -1, 1 }; 39 | int[] c = { -1, 1, -1, 1, -2, -2, 2, 2 }; 40 | long ans = 0; 41 | for (int i = 0; i < c.length; i++) { 42 | ans = (ans+count(n - 1, cr + r[i], cc + c[i], dp))%1000_000_007; 43 | } 44 | return dp[n][cr][cc] = (int) (ans % 1000_000_007); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Lec55/Hashing.java: -------------------------------------------------------------------------------- 1 | package Lec55; 2 | 3 | public class Hashing { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String s = "apple"; 8 | System.out.println(hascode(s)); 9 | } 10 | 11 | public static int hascode(String s) { 12 | long mod = 1000_000_007; 13 | long pow = 1; 14 | long ans = 0; 15 | int pr = 31; 16 | for (int i = 0; i < s.length(); i++) { 17 | char ch = s.charAt(i); 18 | ans = (ans + ((ch - 'a' + 1) * pow) % mod) % mod; 19 | ans = (ans + mod) % mod; 20 | pow = (pow * pr) % mod; 21 | } 22 | return (int)(ans); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Lec55/Longest_Duplicate_Substring.java: -------------------------------------------------------------------------------- 1 | package Lec55; 2 | 3 | import java.util.*; 4 | 5 | public class Longest_Duplicate_Substring { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | String s = "abcd"; 10 | int lo = 0; 11 | int hi = s.length(); 12 | String ans = ""; 13 | while (lo <= hi) { 14 | int mid = (lo + hi) / 2; 15 | 16 | String res = Solve(s, mid); 17 | 18 | if (res.length() > 0) { 19 | lo = mid + 1; 20 | ans = res; 21 | } else { 22 | hi = mid - 1; 23 | } 24 | } 25 | System.out.println(ans); 26 | 27 | } 28 | 29 | private static String Solve(String s, int mid) { 30 | // TODO Auto-generated method stub 31 | long mod = 1000_000_007; 32 | long pow = 1; 33 | long hv = 0; 34 | int pr = 31; 35 | for (int ei = mid - 1; ei >= 0; ei--) { 36 | hv = (hv + ((s.charAt(ei) - 'a' + 1) * pow) % mod) % mod; 37 | if (ei > 0) { 38 | pow = (pow * pr) % mod; 39 | } 40 | 41 | } 42 | HashMap> map = new HashMap<>(); 43 | map.put(hv, new ArrayList<>()); 44 | map.get(hv).add(0); 45 | for (int ei = mid, si = 0; ei < s.length(); ei++, si++) { 46 | hv = (hv - (((s.charAt(si) - 'a' + 1) * pow) % mod) + mod) % mod; 47 | 48 | hv = (hv * pr) % mod; 49 | hv = (hv + (s.charAt(ei) - 'a' + 1)) % mod; 50 | if (map.containsKey(hv)) { 51 | String curr = s.substring(si + 1, ei + 1); 52 | for (int st : map.get(hv)) { 53 | if (curr.equals(s.substring(st, st + mid))) { 54 | return curr; 55 | } 56 | } 57 | map.get(hv).add(si + 1); 58 | 59 | } else { 60 | map.put(hv, new ArrayList<>()); 61 | 62 | map.get(hv).add(si + 1); 63 | } 64 | 65 | } 66 | return ""; 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Lec56/Odd_Even.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | public class Odd_Even { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n = 792; 7 | if ((n & 1) > 0) { 8 | System.out.println("odd"); 9 | } else { 10 | System.out.println("even"); 11 | } 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Lec56/Set_Bit.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Set_Bit { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 67; 8 | System.out.println(countsetbit(n)); 9 | System.out.println(fastCount(n)); 10 | } 11 | 12 | public static int countsetbit(int n) { 13 | int count = 0; 14 | while (n > 0) { 15 | if ((n & 1) != 0) { 16 | count++; 17 | } 18 | n >>= 1; 19 | } 20 | return count; 21 | } 22 | public static int fastCount(int n) { 23 | int count = 0; 24 | while (n > 0) { 25 | count++; 26 | n = n & (n - 1); 27 | } 28 | return count; 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lec56/Set_bit_or_not.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Set_bit_or_not { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 84; 8 | int pos = 3; 9 | System.out.println(setornot(n, pos)); 10 | 11 | } 12 | 13 | public static boolean setornot(int n, int pos) { 14 | 15 | int mask = (1 << pos); 16 | if ((n & mask) > 0) { 17 | return true; 18 | } else { 19 | return false; 20 | 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec56/Shift_Opr.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Shift_Opr { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n=5; 8 | System.out.println(n>>1);// right shift 9 | int n1=-5; 10 | System.out.println(n1>>1);// right shift 11 | System.out.println(n<<2);//left shit 12 | System.out.println(n1<<2); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Lec56/Single_Numbe_II.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Single_Numbe_II { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 0, 1, 0, 1, 0, 1, 99 }; 8 | 9 | } 10 | 11 | public static int Single_Number(int[] arr) { 12 | int[] bit = new int[32]; 13 | for (int i = 0; i < arr.length; i++) { 14 | int num = arr[i]; 15 | int pos = 0; 16 | while (num > 0) { 17 | if ((num & 1) > 0) { 18 | bit[pos]++; 19 | } 20 | pos++; 21 | num >>= 1; 22 | } 23 | 24 | } 25 | for (int i = 0; i < bit.length; i++) { 26 | bit[i] %= 3; 27 | } 28 | return 0; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec56/Single_Number.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Single_Number { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 4, 1, 2, 1, 2 }; 8 | System.out.println(Single_Number1(arr)); 9 | 10 | } 11 | public static int Single_Number1(int []arr) { 12 | int ans=0; 13 | for (int i = 0; i < arr.length; i++) { 14 | ans=ans^arr[i]; 15 | } 16 | return ans; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Lec56/Single_Number_III.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class Single_Number_III { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 1, 2, 1, 3, 2, 5 }; 8 | Single_Number(arr); 9 | 10 | } 11 | 12 | public static void Single_Number(int[] arr) { 13 | int ans = 0; 14 | for (int i = 0; i < arr.length; i++) { 15 | ans = ans ^ arr[i]; 16 | } 17 | int mask = (ans & (~(ans - 1))); 18 | int a = 0; 19 | for (int i = 0; i < arr.length; i++) { 20 | if ((mask & arr[i]) != 0) { 21 | a ^= arr[i]; 22 | } 23 | } 24 | int b = ans ^ a; 25 | System.out.println(a + " " + b); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec56/Tavas_and_SaDDas.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Tavas_and_SaDDas { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | String str = sc.next(); 11 | System.out.println(index(str)); 12 | 13 | } 14 | 15 | public static int index(String str) { 16 | int n = str.length(); 17 | int idx = (1 << n) - 2; 18 | int pos = 0; 19 | for (int i = str.length()-1; i >= 0; i--) { 20 | if (str.charAt(i) == '7') { 21 | idx = idx + (1 << pos); 22 | } 23 | pos++; 24 | } 25 | return idx+1; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec56/magic_number.java: -------------------------------------------------------------------------------- 1 | package Lec56; 2 | 3 | public class magic_number { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n=7; 8 | System.out.println(number(n)); 9 | 10 | } 11 | public static int number(int n) { 12 | int ans=0; 13 | int mul=5; 14 | while(n>0) { 15 | if((n&1)!=0) { 16 | ans= ans + mul; 17 | } 18 | mul*=5; 19 | n>>=1; 20 | } 21 | return ans; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec57/BUSYMAN.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Scanner; 6 | 7 | public class BUSYMAN { 8 | 9 | public static void main(String[] args) { 10 | // TODO Auto-generated method stub 11 | Scanner sc = new Scanner(System.in); 12 | int t = sc.nextInt(); 13 | while (t-- > 0) { 14 | int n = sc.nextInt(); 15 | Pair[] arr = new Pair[n]; 16 | for (int i = 0; i < arr.length; i++) { 17 | int star = sc.nextInt(); 18 | int end = sc.nextInt(); 19 | arr[i] = new Pair(star, end); 20 | } 21 | Arrays.sort(arr, new Comparator() { 22 | 23 | @Override 24 | public int compare(Pair o1, Pair o2) { 25 | // TODO Auto-generated method stub 26 | return o1.end - o2.end; 27 | 28 | } 29 | }); 30 | int activitie = 1; 31 | int end = arr[0].end; 32 | for (int i = 1; i < arr.length; i++) { 33 | if (end <= arr[i].start) { 34 | activitie++; 35 | end = arr[i].end; 36 | } 37 | } 38 | System.out.println(activitie); 39 | 40 | } 41 | 42 | } 43 | 44 | public static class Pair { 45 | int start; 46 | int end; 47 | 48 | public Pair(int star, int end) { 49 | // TODO Auto-generated constructor stub 50 | this.start = star; 51 | this.end = end; 52 | } 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Lec57/Coin_I.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Coin_I { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | double[] p = { 0.30, 0.60, 0.80 }; 10 | int n = p.length; 11 | double[][] dp = new double[(n + 4) / 2][n]; 12 | for (double[] a : dp) { 13 | Arrays.fill(a, -1); 14 | } 15 | System.out.println(Answer(p, 0, (n + 1) / 2, dp)); 16 | 17 | } 18 | 19 | public static double Answer(double[] p, int i, int h, double[][] dp) { 20 | if (h == 0) { 21 | return 1; 22 | 23 | } 24 | if (i == p.length) { 25 | return 0; 26 | } 27 | if (dp[h][i] != -1) { 28 | return dp[h][i]; 29 | } 30 | 31 | double ans = p[i] * Answer(p, i + 1, h - 1, dp) + (1 - p[i]) * Answer(p, i + 1, h, dp); 32 | return dp[h][i] = ans; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec57/Ehab_the_Xorcist.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Ehab_the_Xorcist { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | long u = sc.nextLong(); 11 | long v = sc.nextLong(); 12 | if (u > v || (v - u) % 2 != 0) { 13 | System.out.println(-1); 14 | } else if (u == v) { 15 | if (v == 0) { 16 | System.out.println(0); 17 | } else { 18 | System.out.println(1); 19 | System.out.println(v); 20 | } 21 | } else { 22 | long x = (v - u) / 2; 23 | if ((u & x) == 0) { 24 | System.out.println(2); 25 | System.out.println(u + x + " " + x); 26 | } else { 27 | System.out.println(3); 28 | System.out.println(u + " " + x + " " + x); 29 | } 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec57/Inc_Exc_Rule.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | public class Inc_Exc_Rule { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 1000; 8 | int[] arr = { 2, 3, 5 }; 9 | System.out.println(count(n, arr)); 10 | 11 | } 12 | 13 | public static int count(int n, int[] arr) { 14 | int ans = 0; 15 | for (int i = 1; i < (1 << arr.length); i++) { 16 | int c = countnumber(i, n, arr); 17 | if (countsetbit(i) % 2 == 0) { 18 | ans -= c; 19 | } else { 20 | ans += c; 21 | } 22 | } 23 | return ans; 24 | } 25 | 26 | private static int countnumber(int i, int n, int[] arr) { 27 | // TODO Auto-generated method stub 28 | int pod = 1; 29 | int pos = 0; 30 | while (i > 0) { 31 | if ((i & 1) != 0) { 32 | pod *= arr[pos]; 33 | } 34 | i >>= 1; 35 | pos++; 36 | } 37 | return n / pod; 38 | } 39 | 40 | public static int countsetbit(int n) { 41 | int count = 0; 42 | while (n > 0) { 43 | n = (n & (n - 1)); 44 | count++; 45 | } 46 | return count; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Lec57/Preparing_Olympiad.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | public class Preparing_Olympiad { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 3; 8 | int l = 5; 9 | int r = 6; 10 | int x = 1; 11 | int[] arr = { 1, 2, 3 }; 12 | System.out.println(countcontest(arr, l, r, x)); 13 | 14 | } 15 | 16 | public static int countcontest(int[] arr, int l, int r, int x) { 17 | int n = arr.length; 18 | int ans = 0; 19 | for (int i = 3; i < (1 << n); i++) { 20 | if (countsetbit(i) >= 2) { 21 | if (isitpossible(arr, l, r, x, i)) { 22 | ans++; 23 | } 24 | } 25 | 26 | } 27 | return ans; 28 | } 29 | 30 | private static boolean isitpossible(int[] arr, int l, int r, int x, int i) { 31 | // TODO Auto-generated method stub 32 | int min = Integer.MAX_VALUE; 33 | int max = Integer.MIN_VALUE; 34 | int sum = 0; 35 | int pos = 0; 36 | while (i > 0) { 37 | if ((i & 1) != 0) { 38 | sum += arr[pos]; 39 | min = Math.min(min, arr[pos]); 40 | max = Math.max(max, arr[pos]); 41 | } 42 | i >>= 1; 43 | pos++; 44 | } 45 | return sum >= l & sum <= r & (max - min) >= x; 46 | } 47 | 48 | public static int countsetbit(int n) { 49 | int count = 0; 50 | while (n > 0) { 51 | n = (n & (n - 1)); 52 | count++; 53 | } 54 | return count; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Lec57/SubSeqnce_Using_Bitmalsing.java: -------------------------------------------------------------------------------- 1 | package Lec57; 2 | 3 | public class SubSeqnce_Using_Bitmalsing { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "abc"; 8 | SubSequne(str); 9 | 10 | } 11 | 12 | public static void SubSequne(String str) { 13 | int n = str.length(); 14 | for (int i = 0; i < (1 << n); i++) { 15 | pattern(i, str); 16 | 17 | } 18 | } 19 | 20 | private static void pattern(int i, String str) { 21 | // TODO Auto-generated method stub 22 | int pos = 0; 23 | while (i > 0) { 24 | if ((i & 1) != 0) { 25 | System.out.print(str.charAt(pos)); 26 | } 27 | i >>= 1; 28 | pos++; 29 | } 30 | System.out.println(); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec58/Count_Good_Substrings.java: -------------------------------------------------------------------------------- 1 | package Lec58; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Count_Good_Substrings { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | String str = sc.next(); 11 | Good_Substrings(str);; 12 | } 13 | public static void Good_Substrings(String str) { 14 | long[][] arr = new long[2][2]; 15 | long odd = 0, even = 0; 16 | for (int i = 1; i <= str.length(); i++) { 17 | odd++; 18 | if (i % 2 != 0) { 19 | odd += arr[str.charAt(i - 1) - 'a'][0]; 20 | even += arr[str.charAt(i - 1) - 'a'][1]; 21 | arr[str.charAt(i - 1) - 'a'][0]++; 22 | } else { 23 | odd += arr[str.charAt(i - 1) - 'a'][1]; 24 | even += arr[str.charAt(i - 1) - 'a'][0]; 25 | arr[str.charAt(i - 1) - 'a'][1]++; 26 | } 27 | } 28 | System.out.println(even + " " + odd); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec58/Longest_Happy_Prefix.java: -------------------------------------------------------------------------------- 1 | package Lec58; 2 | 3 | public class Longest_Happy_Prefix { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String str = "ababab"; 8 | System.out.println(lps(str)); 9 | 10 | } 11 | 12 | public static String lps(String str) { 13 | int[] dp = new int[str.length()]; 14 | int len = 0; 15 | for (int i = 1; i < dp.length;) { 16 | if (str.charAt(len) == str.charAt(i)) { 17 | len++; 18 | dp[i] = len; 19 | i++; 20 | } else { 21 | if (len > 0) { 22 | len = dp[len - 1]; 23 | } else { 24 | dp[i] = 0; 25 | i++; 26 | } 27 | 28 | } 29 | 30 | } 31 | return str.substring(0, dp[str.length() - 1]); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lec58/NHAY.java: -------------------------------------------------------------------------------- 1 | package Lec58; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class NHAY { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | Scanner sc = new Scanner(System.in); 11 | while (sc.hasNextInt()) { 12 | int n = sc.nextInt(); 13 | String pat = sc.next(); 14 | String text = sc.next(); 15 | lps(text, pat); 16 | } 17 | 18 | } 19 | 20 | public static void lps(String text, String pat) { 21 | String str = pat + "#" + text; 22 | int[] dp = new int[str.length()]; 23 | int len = 0; 24 | for (int i = 1; i < dp.length;) { 25 | if (str.charAt(len) == str.charAt(i)) { 26 | len++; 27 | dp[i] = len; 28 | i++; 29 | } else { 30 | if (len > 0) { 31 | len = dp[len - 1]; 32 | } else { 33 | dp[i] = 0; 34 | i++; 35 | } 36 | 37 | } 38 | 39 | } 40 | // System.out.println(Arrays.toString(dp)); 41 | for (int i = pat.length(); i < dp.length; i++) { 42 | if (dp[i] == pat.length()) { 43 | System.out.println(i - 2 * pat.length()); 44 | } 45 | 46 | } 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Lec59/Maximum_XOR_of_Two_Numbers.java: -------------------------------------------------------------------------------- 1 | package Lec59; 2 | 3 | public class Maximum_XOR_of_Two_Numbers { 4 | public static class Node { 5 | Node zero; 6 | Node one; 7 | } 8 | 9 | public static void Insert(Node root, int val) { 10 | Node curr = root; 11 | for (int i = 31; i >= 0; i--) { 12 | if ((val & (1 << i)) == 0) { 13 | if (curr.zero != null) { 14 | curr = curr.zero; 15 | } else { 16 | Node nn = new Node(); 17 | curr.zero = nn; 18 | curr = nn; 19 | } 20 | } else { 21 | if (curr.one != null) { 22 | curr = curr.one; 23 | } else { 24 | Node nn = new Node(); 25 | curr.one = nn; 26 | curr = nn; 27 | } 28 | } 29 | 30 | } 31 | 32 | } 33 | 34 | public static int getmaxXor(Node root, int val) { 35 | int num = 0; 36 | Node curr = root; 37 | for (int i = 31; i >= 0; i--) { 38 | int bit = (val & (1 << i)); 39 | if (bit == 0) { 40 | if (curr.one != null) { 41 | num += (1 << i); 42 | curr = curr.one; 43 | } else { 44 | curr = curr.zero; 45 | } 46 | } else { 47 | if (curr.zero != null) { 48 | num += (1 << i); 49 | curr = curr.zero; 50 | } else { 51 | curr = curr.one; 52 | } 53 | } 54 | 55 | } 56 | return num; 57 | 58 | } 59 | 60 | public static void main(String[] args) { 61 | // TODO Auto-generated method stub 62 | Node root = new Node(); 63 | int[] arr = { 3, 10, 5, 25, 2, 8 }; 64 | for (int val : arr) { 65 | Insert(root, val); 66 | } 67 | int xor = Integer.MIN_VALUE; 68 | for (int val : arr) { 69 | xor = Math.max(xor, getmaxXor(root, val)); 70 | } 71 | System.out.println(xor); 72 | 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Lec59/Minimize_XOR.java: -------------------------------------------------------------------------------- 1 | package Lec59; 2 | 3 | public class Minimize_XOR { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int nums1 = 3; 8 | int nums2 = 5; 9 | System.out.println(XOR(nums1, nums2)); 10 | } 11 | 12 | public static int XOR(int nums1, int num2) { 13 | int count = SetBit(num2); 14 | int ans = 0; 15 | for (int i = 31; i >= 0 && count > 0; i--) { 16 | if ((nums1 & (1 << i)) != 0) { 17 | ans += (1 << i); 18 | count--; 19 | } 20 | } 21 | for (int i = 0; i <= 31 && count > 0; i++) { 22 | if ((nums1 & (1 << i)) == 0) { 23 | ans += (1 << i); 24 | count--; 25 | } 26 | } 27 | return ans; 28 | 29 | } 30 | 31 | public static int SetBit(int num2) { 32 | int count = 0; 33 | while (num2 > 0) { 34 | count++; 35 | num2 = (num2 & (num2 - 1)); 36 | } 37 | return count; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lec59/Trie.java: -------------------------------------------------------------------------------- 1 | package Lec59; 2 | 3 | import java.util.*; 4 | 5 | public class Trie { 6 | public class Node { 7 | char ch; 8 | HashMap child = new HashMap<>(); 9 | boolean isterminal = false; 10 | } 11 | 12 | private Node root; 13 | 14 | public Trie() { 15 | // TODO Auto-generated constructor stub 16 | Node nn = new Node(); 17 | nn.ch = '*'; 18 | this.root = nn; 19 | } 20 | 21 | public void insert(String word) { 22 | Node curr = root; 23 | for (int i = 0; i < word.length(); i++) { 24 | char ch = word.charAt(i); 25 | if (curr.child.containsKey(ch)) { 26 | curr = curr.child.get(ch); 27 | } else { 28 | Node nn = new Node(); 29 | nn.ch = ch; 30 | curr.child.put(ch, nn); 31 | curr = nn; 32 | } 33 | 34 | } 35 | curr.isterminal = true; 36 | 37 | } 38 | 39 | public boolean search(String word) { 40 | Node curr = root; 41 | for (int i = 0; i < word.length(); i++) { 42 | char ch = word.charAt(i); 43 | if (!curr.child.containsKey(ch)) { 44 | return false; 45 | } 46 | curr = curr.child.get(ch); 47 | 48 | } 49 | return curr.isterminal; 50 | } 51 | 52 | public boolean startsWith(String prefix) { 53 | Node curr = root; 54 | for (int i = 0; i < prefix.length(); i++) { 55 | char ch = prefix.charAt(i); 56 | if (!curr.child.containsKey(ch)) { 57 | return false; 58 | } 59 | curr = curr.child.get(ch); 60 | 61 | } 62 | return true; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Lec59/Trie_client.java: -------------------------------------------------------------------------------- 1 | package Lec59; 2 | 3 | public class Trie_client { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Trie t = new Trie(); 8 | t.insert("raj"); 9 | t.insert("raja"); 10 | t.insert("apple"); 11 | t.insert("mango"); 12 | t.insert("man"); 13 | t.insert("ankit"); 14 | t.insert("ankita"); 15 | t.insert("amisha"); 16 | System.out.println(t.search("raja")); 17 | System.out.println(t.startsWith("appk")); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec59/Tries_Contacts.java: -------------------------------------------------------------------------------- 1 | package Lec59; 2 | 3 | import java.util.HashMap; 4 | public class Tries_Contacts { 5 | public class Node { 6 | char ch; 7 | HashMap child = new HashMap<>(); 8 | boolean isterminal = false; 9 | int count = 0; 10 | } 11 | 12 | private Node root; 13 | 14 | public Tries_Contacts() { 15 | // TODO Auto-generated constructor stub 16 | Node nn = new Node(); 17 | nn.ch = '*'; 18 | this.root = nn; 19 | } 20 | 21 | public void insert(String word) { 22 | Node curr = root; 23 | for (int i = 0; i < word.length(); i++) { 24 | char ch = word.charAt(i); 25 | if (curr.child.containsKey(ch)) { 26 | curr = curr.child.get(ch); 27 | curr.count++; 28 | } else { 29 | Node nn = new Node(); 30 | nn.ch = ch; 31 | nn.count = 1; 32 | curr.child.put(ch, nn); 33 | curr = nn; 34 | } 35 | 36 | } 37 | curr.isterminal = true; 38 | 39 | } 40 | 41 | public int startsWith(String prefix) { 42 | Node curr = root; 43 | for (int i = 0; i < prefix.length(); i++) { 44 | char ch = prefix.charAt(i); 45 | if (!curr.child.containsKey(ch)) { 46 | return 0; 47 | } 48 | curr = curr.child.get(ch); 49 | 50 | } 51 | return curr.count; 52 | } 53 | public static void main(String[] args) { 54 | Tries_Contacts t = new Tries_Contacts(); 55 | t.insert("hack"); 56 | t.insert("hackerrank"); 57 | System.out.println(t.startsWith("hac")); 58 | System.out.println(t.startsWith("hak")); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Lec6/Array_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Array_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = new int[5]; 8 | System.out.println(arr); 9 | // print 10 | System.out.println(arr[0]); 11 | System.out.println(arr[1]); 12 | System.out.println(arr[2]); 13 | System.out.println(arr[3]); 14 | System.out.println(arr[4]); 15 | // update 16 | arr[0] = 7; 17 | arr[1] = -7; 18 | arr[2] = 3; 19 | arr[3] = 7; 20 | arr[4] = 78; 21 | // print 22 | System.out.println(arr[0]); 23 | System.out.println(arr[1]); 24 | System.out.println(arr[2]); 25 | System.out.println(arr[3]); 26 | System.out.println(arr[4]); 27 | int [] other = arr; 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Lec6/Array_Reverse.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Array_Reverse { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 3, 4, 5, 6, 88, 90, -1, 23, 5 }; 8 | Range_reverse(arr, 3, 7); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static void reverse(int[] arr) { 16 | int i = 0; 17 | int j = arr.length - 1; 18 | while (i < j) { 19 | int temp = arr[i]; 20 | arr[i] = arr[j]; 21 | arr[j] = temp; 22 | i++; 23 | j--; 24 | } 25 | 26 | } 27 | 28 | public static void Range_reverse(int[] arr, int i, int j) { 29 | 30 | while (i < j) { 31 | int temp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = temp; 34 | i++; 35 | j--; 36 | } 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Lec6/Linear_Search.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Linear_Search { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 3, 2, 4, 1, 7 }; 8 | int item = -4; 9 | System.out.println(search(arr, item)); 10 | 11 | } 12 | 13 | public static int search(int[] arr, int item) { 14 | 15 | for (int i = 0; i < arr.length; i++) { 16 | if (arr[i] == item) { 17 | return i; 18 | } 19 | 20 | } 21 | return -1; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lec6/Min_of_Array.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Min_of_Array { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 3, 5, -9, 7, -11 }; 8 | System.out.println(Min(arr)); 9 | 10 | } 11 | 12 | public static int Min(int[] arr) { 13 | 14 | int mini=0; 15 | for (int i = 1; i < arr.length; i++) { 16 | if(arr[i] 0) { 20 | int temp = arr[n - 1]; 21 | for (int i = n - 2; i >= 0; i--) { 22 | arr[i + 1] = arr[i]; 23 | } 24 | arr[0] = temp; 25 | k--; 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec6/Swap_Demo2.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Swap_Demo2 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 3, 2, 4, 1, 7 }; 8 | System.out.println(arr[0] + " " + arr[1]); 9 | Swap(arr, 0, 1); 10 | System.out.println(arr[0] + " " + arr[1]); 11 | 12 | } 13 | 14 | public static void Swap(int[] arr, int i, int j) { 15 | int temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec6/Swap_Demo_1.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Swap_Demo_1 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 3, 2, 4, 1, 7 }; 8 | // System.out.println(arr.length); 9 | System.out.println(arr[0] + " " + arr[1]); 10 | Swap(arr[0], arr[1]); 11 | System.out.println(arr[0] + " " + arr[1]); 12 | 13 | } 14 | 15 | public static void Swap(int a, int b) { 16 | int temp = a; 17 | a = b; 18 | b = temp; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Lec6/Swap_Demo_3.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | public class Swap_Demo_3 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 3, 2, 4 }; 8 | int[] other = { -5, -3, -2, -1 }; 9 | System.out.println(arr[0] + " " + other[1]); 10 | Swap(arr, other); 11 | System.out.println(arr[0] + " " + other[1]); 12 | 13 | } 14 | 15 | public static void Swap(int [] arr, int [] other) { 16 | 17 | int [] temp = arr; 18 | arr=other; 19 | other=temp; 20 | 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Lec6/User_Input.java: -------------------------------------------------------------------------------- 1 | package Lec6; 2 | 3 | import java.util.Scanner; 4 | 5 | public class User_Input { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int[] arr = new int[n]; 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = sc.nextInt(); 14 | 15 | } 16 | Display(arr); 17 | 18 | } 19 | 20 | public static void Display(int[] arr) { 21 | for (int i = 0; i < arr.length; i++) { 22 | System.out.print(arr[i] + " "); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec60/Different_Ways_to_Add_Parentheses.java: -------------------------------------------------------------------------------- 1 | package Lec60; 2 | 3 | import java.util.*; 4 | 5 | public class Different_Ways_to_Add_Parentheses { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | String s = "2*3-4*5"; 10 | System.out.println(Different_Ways(s)); 11 | 12 | } 13 | 14 | public static List Different_Ways(String s) { 15 | if (s.indexOf('+') == -1 && s.indexOf('-') == -1 && s.indexOf('*') == -1) { 16 | List ll = new ArrayList<>(); 17 | ll.add(Integer.parseInt(s)); 18 | return ll; 19 | } 20 | List ans = new ArrayList<>(); 21 | for (int i = 0; i < s.length(); i++) { 22 | char ch = s.charAt(i); 23 | if (isoperator(ch)) { 24 | List left = Different_Ways(s.substring(0, i)); 25 | List right = Different_Ways(s.substring(i + 1)); 26 | for (int a : left) { 27 | for (int b : right) { 28 | int val = Cal(a, b, ch); 29 | ans.add(val); 30 | } 31 | } 32 | } 33 | } 34 | return ans; 35 | 36 | } 37 | 38 | public static boolean isoperator(char ch) { 39 | if (ch == '+' || ch == '-' || ch == '*') { 40 | return true; 41 | } 42 | return false; 43 | } 44 | 45 | public static int Cal(int a, int b, char ch) { 46 | if (ch == '+') { 47 | return a + b; 48 | } else if (ch == '-') { 49 | return a - b; 50 | } else 51 | return a * b; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Lec61/Kth_Smallest_Product_of_Two_Sorted_Arrays.java: -------------------------------------------------------------------------------- 1 | package Lec61; 2 | 3 | public class Kth_Smallest_Product_of_Two_Sorted_Arrays { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr1 = { -4, -2, 0, 3 }; 8 | int[] arr2 = { 2, 4 }; 9 | int k = 6; 10 | System.out.println(kthProduct(arr1, arr2, k)); 11 | } 12 | 13 | public static long kthProduct(int[] arr1, int[] arr2, long k) { 14 | long si = -1000_000_0000l; 15 | long ei = 1000_000_0000l; 16 | long ans = 0; 17 | while (si <= ei) { 18 | long mid = (si + ei) / 2; 19 | if (countofproduct(arr1, arr2, mid) >= k) { 20 | ans = mid; 21 | ei = mid - 1; 22 | } else { 23 | si = mid + 1; 24 | } 25 | } 26 | return ans; 27 | 28 | } 29 | 30 | private static long countofproduct(int[] arr1, int[] arr2, long pro) { 31 | // TODO Auto-generated method stub 32 | long ans = 0; 33 | for (int e1 : arr1) { 34 | if (e1 >= 0) { 35 | long count = 0; 36 | int lo = 0; 37 | int hi = arr2.length - 1; 38 | while (lo <= hi) { 39 | int mid = (lo + hi) / 2; 40 | if ((long) (e1) * arr2[mid] <= pro) { 41 | count = mid + 1; 42 | lo = mid + 1; 43 | } else { 44 | hi = mid - 1; 45 | } 46 | } 47 | ans += count; 48 | } else { 49 | long count = 0; 50 | int lo = 0; 51 | int hi = arr2.length - 1; 52 | while (lo <= hi) { 53 | int mid = (lo + hi) / 2; 54 | if ((long) (e1) * arr2[mid] <= pro) { 55 | count = arr2.length - mid; 56 | hi = mid - 1; 57 | } else { 58 | lo = mid + 1; 59 | } 60 | } 61 | ans += count; 62 | } 63 | 64 | } 65 | return ans; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Lec61/Median_of_Two_Sorted_Arrays.java: -------------------------------------------------------------------------------- 1 | package Lec61; 2 | 3 | public class Median_of_Two_Sorted_Arrays { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr1 = { 1, 3 }; 8 | int[] arr2 = { 2 }; 9 | if (arr1.length < arr2.length) { 10 | System.out.println(findMedian(arr1, arr2)); 11 | } else { 12 | System.out.println(findMedian(arr2, arr1)); 13 | } 14 | } 15 | 16 | public static double findMedian(int[] arr1, int[] arr2) { 17 | int lo = 0; 18 | int hi = arr1.length; 19 | int n = arr1.length; 20 | int m = arr2.length; 21 | while (lo <= hi) { 22 | int cut1 = (lo + hi) / 2; 23 | int cut2 = (n + m + 1) / 2 - cut1; 24 | int l1 = cut1 == 0 ? Integer.MIN_VALUE : arr1[cut1 - 1]; 25 | int r1 = cut1 == n ? Integer.MAX_VALUE : arr1[cut1]; 26 | int l2 = cut2 == 0 ? Integer.MIN_VALUE : arr2[cut2 - 1]; 27 | int r2 = cut2 == m ? Integer.MAX_VALUE : arr2[cut2]; 28 | if (l1 <= r2 && l2 <= r1) { 29 | if ((n + m) % 2 == 0) { 30 | return (Math.max(l1, l2) + Math.min(r1, r2)) / 2.0; 31 | } else { 32 | return Math.max(l1, l2); 33 | } 34 | 35 | } else if (l1 > r2) { 36 | hi = cut1 - 1; 37 | } else { 38 | lo = cut1 + 1; 39 | } 40 | } 41 | return 0.0; 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Lec61/Two_Arrays_and_Sum_of_Functions.java: -------------------------------------------------------------------------------- 1 | package Lec61; 2 | 3 | import java.util.*; 4 | 5 | public class Two_Arrays_and_Sum_of_Functions { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | long[] a = new long[n]; 12 | int mod = 998244353; 13 | for (int i = 0; i < a.length; i++) { 14 | long x = sc.nextLong(); 15 | a[i] = (x * (i + 1) * (n - i)); 16 | 17 | } 18 | Long[] b = new Long[n]; 19 | for (int i = 0; i < b.length; i++) { 20 | b[i] = sc.nextLong(); 21 | } 22 | 23 | long ans = 0; 24 | Arrays.sort(a); 25 | Arrays.sort(b, Collections.reverseOrder()); 26 | for (int i = 0; i < b.length; i++) { 27 | ans = ans + ((a[i] % mod) * b[i]); 28 | ans %= mod; 29 | } 30 | System.out.println(ans); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec62/Day_at_the_Beach.java: -------------------------------------------------------------------------------- 1 | package Lec62; 2 | 3 | import java.util.*; 4 | 5 | public class Day_at_the_Beach { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int[] arr = new int[n]; 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = sc.nextInt(); 14 | } 15 | int[] left = new int[n + 1]; 16 | left[0] = Integer.MIN_VALUE; 17 | for (int i = 1; i < left.length; i++) { 18 | left[i] = Math.max(left[i - 1], arr[i - 1]); 19 | } 20 | int[] right = new int[n + 1]; 21 | right[n] = Integer.MAX_VALUE; 22 | for (int i = n - 1; i >= 0; i--) { 23 | right[i] = Math.min(right[i + 1], arr[i]); 24 | } 25 | int part = 1; 26 | for (int i = 1; i < n; i++) { 27 | if (left[i] <= right[i]) { 28 | part++; 29 | } 30 | } 31 | System.out.println(part); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec62/Find_Duplicate_Subtrees.java: -------------------------------------------------------------------------------- 1 | package Lec62; 2 | 3 | import java.util.*; 4 | 5 | public class Find_Duplicate_Subtrees { 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | 11 | TreeNode() { 12 | } 13 | 14 | TreeNode(int val) { 15 | this.val = val; 16 | } 17 | 18 | TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | 25 | class Solution { 26 | HashMap map = new HashMap<>(); 27 | HashSet set = new HashSet<>(); 28 | 29 | public List findDuplicateSubtrees(TreeNode root) { 30 | DuplicateSubtrees(root); 31 | return new ArrayList<>(set); 32 | } 33 | 34 | public String DuplicateSubtrees(TreeNode root) { 35 | if (root == null) { 36 | return "x"; 37 | } 38 | String left = DuplicateSubtrees(root.left); 39 | String right = DuplicateSubtrees(root.right); 40 | String s = root.val + " " + left + " " + right; 41 | if (map.containsKey(s)) { 42 | set.add(map.get(s)); 43 | } else { 44 | map.put(s, root); 45 | } 46 | return s; 47 | 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Lec62/Recover_Binary_Search_Tree.java: -------------------------------------------------------------------------------- 1 | package Lec62; 2 | 3 | import java.util.*; 4 | 5 | public class Recover_Binary_Search_Tree { 6 | public class TreeNode { 7 | int val; 8 | TreeNode left; 9 | TreeNode right; 10 | 11 | TreeNode() { 12 | } 13 | 14 | TreeNode(int val) { 15 | this.val = val; 16 | } 17 | 18 | TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | 25 | class Solution { 26 | public void recoverTree(TreeNode root) { 27 | List list = new ArrayList<>(); 28 | Inorder(root, list); 29 | TreeNode fist = null; 30 | TreeNode sec = null; 31 | for (int i = 0; i < list.size() - 1; i++) { 32 | if (list.get(i).val > list.get(i + 1).val) { 33 | fist = list.get(i); 34 | break; 35 | } 36 | } 37 | for (int i = list.size() - 1; i > 0; i--) { 38 | if (list.get(i).val < list.get(i - 1).val) { 39 | sec = list.get(i); 40 | break; 41 | } 42 | } 43 | int temp = fist.val; 44 | fist.val = sec.val; 45 | sec.val = temp; 46 | } 47 | 48 | public void Inorder(TreeNode root, List list) { 49 | if (root == null) { 50 | return; 51 | } 52 | Inorder(root.left, list); 53 | list.add(root); 54 | Inorder(root.right, list); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Lec62/Recover_Binary_Search_Tree_Space_Op.java: -------------------------------------------------------------------------------- 1 | package Lec62; 2 | 3 | import java.util.List; 4 | 5 | import Lec62.Recover_Binary_Search_Tree.TreeNode; 6 | 7 | public class Recover_Binary_Search_Tree_Space_Op { 8 | public class TreeNode { 9 | int val; 10 | TreeNode left; 11 | TreeNode right; 12 | 13 | TreeNode() { 14 | } 15 | 16 | TreeNode(int val) { 17 | this.val = val; 18 | } 19 | 20 | TreeNode(int val, TreeNode left, TreeNode right) { 21 | this.val = val; 22 | this.left = left; 23 | this.right = right; 24 | } 25 | } 26 | 27 | class Solution { 28 | 29 | TreeNode prev; 30 | TreeNode fist; 31 | TreeNode sec; 32 | public void recoverTree(TreeNode root) { 33 | Inorder(root); 34 | int temp = fist.val; 35 | fist.val = sec.val; 36 | sec.val = temp; 37 | } 38 | public void Inorder(TreeNode root) { 39 | if (root == null) { 40 | return; 41 | } 42 | Inorder(root.left); 43 | if (prev != null && prev.val > root.val) { 44 | if (fist == null) { 45 | fist = prev; 46 | } 47 | sec = root; 48 | 49 | } 50 | prev = root; 51 | Inorder(root.right); 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Lec63/Additive_Number.java: -------------------------------------------------------------------------------- 1 | package Lec63; 2 | 3 | import java.math.BigInteger; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | public class Additive_Number { 8 | 9 | public static void main(String[] args) { 10 | // TODO Auto-generated method stub 11 | String str = "112358"; 12 | List ll = new LinkedList<>(); 13 | System.out.println(IS_Additive(str, ll)); 14 | } 15 | 16 | public static boolean IS_Additive(String ques, List ll) { 17 | if (ques.length() == 0) { 18 | if (ll.size() >= 3) { 19 | return true; 20 | } 21 | 22 | return false; 23 | } 24 | for (int i = 1; i <= ques.length(); i++) { 25 | String s1 = ques.substring(0, i); 26 | if (nonleading(s1) && validAdditive(ll, s1)) { 27 | ll.add(new BigInteger(s1)); 28 | boolean ans = IS_Additive(ques.substring(i), ll); 29 | if (ans) { 30 | return ans; 31 | } 32 | ll.remove(ll.size() - 1); 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | private static boolean validAdditive(List ll, String s1) { 39 | // TODO Auto-generated method stub 40 | if (ll.size() <= 1) { 41 | return true; 42 | } 43 | BigInteger f = ll.get(ll.size() - 1); 44 | BigInteger s = ll.get(ll.size() - 2); 45 | BigInteger ans = new BigInteger(s1); 46 | 47 | return f.add(s).equals(ans); 48 | } 49 | 50 | private static boolean nonleading(String s1) { 51 | // TODO Auto-generated method stub 52 | if (s1.length() == 1) { 53 | return true; 54 | } 55 | return s1.charAt(0) != '0'; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Lec63/Find_Median_from_Data_Stream.java: -------------------------------------------------------------------------------- 1 | package Lec63; 2 | 3 | import java.util.Collections; 4 | import java.util.PriorityQueue; 5 | 6 | public class Find_Median_from_Data_Stream { 7 | class MedianFinder { 8 | 9 | PriorityQueue min = new PriorityQueue<>(); 10 | PriorityQueue max = new PriorityQueue<>(Collections.reverseOrder()); 11 | 12 | public MedianFinder() { 13 | 14 | } 15 | 16 | public void addNum(int num) { 17 | max.add(num); 18 | min.add(max.poll()); 19 | if (max.size() < min.size()) { 20 | max.add(min.poll()); 21 | } 22 | 23 | } 24 | 25 | public double findMedian() { 26 | return max.size() > min.size() ? max.peek() : (max.peek() + min.peek()) / 2.0; 27 | 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Lec63/Rock_and_Lever.java: -------------------------------------------------------------------------------- 1 | package Lec63; 2 | 3 | public class Rock_and_Lever { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 1, 4, 3, 7, 10 }; 8 | System.out.println(CountPair(arr)); 9 | 10 | } 11 | 12 | public static long CountPair(int[] arr) { 13 | boolean[] visited = new boolean[arr.length]; 14 | long ans = 0; 15 | for (int i = 31; i >= 0; i--) { 16 | long c = 0; 17 | for (int j = 0; j < arr.length; j++) { 18 | if ((arr[j] & (1 << i)) != 0 && !visited[j]) { 19 | c++; 20 | visited[j] = true; 21 | } 22 | 23 | } 24 | ans = ans + (c * (c - 1)) / 2; 25 | 26 | } 27 | return ans; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec64/Catalan_number.java: -------------------------------------------------------------------------------- 1 | package Lec64; 2 | 3 | public class Catalan_number { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 4; 8 | System.out.println(Catalannumber(n)); 9 | 10 | } 11 | 12 | public static int Catalannumber(int n) { 13 | if (n == 0 || n == 1) { 14 | return 1; 15 | } 16 | 17 | int ans = 0; 18 | for (int i = 1; i <= n; i++) { 19 | ans = ans + Catalannumber(i - 1) * Catalannumber(n - i); 20 | } 21 | return ans; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lec64/Number_of_Ways_to_Divide_a_Long_Corridor.java: -------------------------------------------------------------------------------- 1 | package Lec64; 2 | 3 | public class Number_of_Ways_to_Divide_a_Long_Corridor { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | String s = "PPSPSPPSPSPSSSS"; 8 | 9 | } 10 | 11 | public static int noCorridor(String s) { 12 | int totalseats = 0; 13 | int seat = 0; 14 | int plant = 0; 15 | int mod = (int) ((1e9) + 7); 16 | long ans = 1; 17 | boolean firstCorridor = false; 18 | for (int i = 0; i < s.length(); i++) { 19 | if (s.charAt(i) == 'S') { 20 | totalseats++; 21 | seat++; 22 | } else if (seat == 0) { 23 | plant++; 24 | } 25 | if (seat == 2) { 26 | if (firstCorridor) { 27 | ans = ans * (plant + 1); 28 | ans %= mod; 29 | } 30 | firstCorridor = true; 31 | seat = 0; 32 | plant = 0; 33 | } 34 | } 35 | if (totalseats == 0 || (totalseats & 1) != 0) { 36 | return 0; 37 | } 38 | return (int) ans; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Lec7/Bubble_Sort.java: -------------------------------------------------------------------------------- 1 | package Lec7; 2 | 3 | public class Bubble_Sort { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 5, 4, 3, 2, 1 }; 8 | sort(arr); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static void sort(int[] arr) { 16 | 17 | for (int turn = 1; turn arr[i + 1]) { 21 | int temp = arr[i]; 22 | arr[i] = arr[i + 1]; 23 | arr[i + 1] = temp; 24 | } 25 | } 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec7/Gas_Station.java: -------------------------------------------------------------------------------- 1 | package Lec7; 2 | 3 | public class Gas_Station { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | int[] gas = { 1, 2, 3, 4, 5 }; 9 | int[] cost = { 3, 4, 5, 1, 2 }; 10 | 11 | } 12 | 13 | public static int canCompleteCircuits(int[] gas, int[] cost) { 14 | int total = 0; 15 | int curr = 0; 16 | int si = 0; 17 | for (int i = 0; i < cost.length; i++) { 18 | total = total + (gas[i] - cost[i]); 19 | curr = curr + (gas[i] - cost[i]); 20 | if(curr<0) { 21 | curr=0; 22 | si=i+1; 23 | } 24 | } 25 | 26 | if (total < 0) { 27 | return -1; 28 | } 29 | return si; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec7/Next_Permutation.java: -------------------------------------------------------------------------------- 1 | package Lec7; 2 | 3 | public class Next_Permutation { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 2, 7, 8, 9, 3, 5, 4, 2, 1 }; 8 | NextPermutation(arr); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static void NextPermutation(int[] arr) { 16 | 17 | int p = 0; 18 | for (int i = arr.length - 2; i >= 0; i--) { 19 | 20 | if (arr[i] < arr[i + 1]) { 21 | p = i; 22 | break; 23 | } 24 | } 25 | int q = 0; 26 | for (int i = arr.length - 1; i > p; i--) { 27 | if (arr[i] > arr[p]) { 28 | q = i; 29 | break; 30 | } 31 | } 32 | 33 | if (p == 0 && q == 0) { 34 | Range_reverse(arr, 0, arr.length - 1); 35 | return; 36 | } 37 | // Swap p and q index 38 | int temp = arr[p]; 39 | arr[p] = arr[q]; 40 | arr[q] = temp; 41 | Range_reverse(arr, p + 1, arr.length - 1); 42 | 43 | } 44 | 45 | public static void Range_reverse(int[] arr, int i, int j) { 46 | 47 | while (i < j) { 48 | int temp = arr[i]; 49 | arr[i] = arr[j]; 50 | arr[j] = temp; 51 | i++; 52 | j--; 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Lec7/Product_of_Array_Except_Self.java: -------------------------------------------------------------------------------- 1 | package Lec7; 2 | 3 | public class Product_of_Array_Except_Self { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 5, 4, 3, 2, 4 }; 8 | int[] ans = product(arr); 9 | for (int i = 0; i < ans.length; i++) { 10 | System.out.print(ans[i] + " "); 11 | } 12 | 13 | } 14 | 15 | public static int[] product(int[] arr) { 16 | int[] left = new int[arr.length]; 17 | left[0] = 1; 18 | for (int i = 1; i < left.length; i++) { 19 | left[i] = left[i - 1] * arr[i - 1]; 20 | } 21 | int[] right = new int[arr.length]; 22 | int n = arr.length; 23 | right[n - 1] = 1; 24 | for (int i = n - 2; i >= 0; i--) { 25 | right[i] = right[i + 1] * arr[i + 1]; 26 | } 27 | for (int i = 0; i < right.length; i++) { 28 | left[i] = left[i] * right[i]; 29 | } 30 | return left; 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec7/Rain_Water.java: -------------------------------------------------------------------------------- 1 | package Lec7; 2 | 3 | public class Rain_Water { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[] arr = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 }; 8 | System.out.println(Rain_Traping(arr)); 9 | 10 | } 11 | 12 | public static int Rain_Traping(int[] arr) { 13 | 14 | int[] left = new int[arr.length]; 15 | left[0] = arr[0]; 16 | for (int i = 1; i < left.length; i++) { 17 | left[i] = Math.max(left[i - 1], arr[i]); 18 | } 19 | int[] right = new int[arr.length]; 20 | int n = arr.length; 21 | right[n - 1] = arr[n - 1]; 22 | for (int i = n - 2; i >= 0; i--) { 23 | right[i] = Math.max(right[i + 1], arr[i]); 24 | } 25 | int sum = 0; 26 | for (int i = 0; i < right.length; i++) { 27 | sum = sum + Math.min(left[i], right[i]) - arr[i]; 28 | } 29 | return sum; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Lec8/AGGRCOWS.java: -------------------------------------------------------------------------------- 1 | package Lec8; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class AGGRCOWS { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | Scanner sc = new Scanner(System.in); 11 | int t = sc.nextInt(); 12 | while (t-- > 0) { 13 | int nos = sc.nextInt(); 14 | int noc = sc.nextInt(); 15 | int[] stalls = new int[nos]; 16 | for (int i = 0; i < stalls.length; i++) { 17 | stalls[i] = sc.nextInt(); 18 | } 19 | Arrays.sort(stalls); 20 | System.out.println(largestminimumdistance(stalls, noc)); 21 | 22 | } 23 | 24 | } 25 | 26 | public static int largestminimumdistance(int[] stalls, int noc) { 27 | 28 | int lo = 0; 29 | int hi = stalls[stalls.length - 1] - stalls[0]; 30 | int ans = 0; 31 | while (lo <= hi) { 32 | int mid = (lo + hi) / 2; 33 | if (isitpossible(stalls, mid, noc) == true) { 34 | ans = mid; 35 | lo = mid + 1; 36 | } else { 37 | hi = mid - 1; 38 | } 39 | } 40 | return ans; 41 | 42 | } 43 | 44 | public static boolean isitpossible(int[] stalls, int mid, int noc) { 45 | 46 | int cow = 1; 47 | int pos = stalls[0]; 48 | int i = 1; 49 | while (i < stalls.length) { 50 | if (stalls[i] - pos >= mid) { 51 | cow++; 52 | pos = stalls[i]; 53 | } 54 | 55 | if (cow >= noc) { 56 | return true; 57 | } 58 | i++; 59 | } 60 | return false; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Lec8/Binary_Search.java: -------------------------------------------------------------------------------- 1 | package Lec8; 2 | 3 | public class Binary_Search { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | int[] arr = { 2, 3, 4, 5, 6, 7, 11, 13, 14, 18, 20, 22, 27 }; 9 | System.out.println(Search(arr, 13)); 10 | } 11 | 12 | public static int Search(int[] arr, int item) { 13 | 14 | int si = 0; 15 | int ei = arr.length - 1; 16 | while (si <= ei) { 17 | 18 | int mid = (si + ei) / 2; 19 | 20 | if (arr[mid] == item) { 21 | return mid; 22 | 23 | } else if (arr[mid] > item) { 24 | 25 | ei = mid - 1; 26 | } else { 27 | si = mid + 1; 28 | } 29 | } 30 | return -1; 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Lec8/Book_Allocation.java: -------------------------------------------------------------------------------- 1 | package Lec8; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Book_Allocation { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | int page[] = { 10, 20, 30, 40 }; 10 | int nos = 2; 11 | System.out.println(minpage(page, nos)); 12 | 13 | } 14 | 15 | public static int minpage(int[] page, int nos) { 16 | int hi = 0; 17 | for (int i = 0; i < page.length; i++) { 18 | hi += page[i]; 19 | } 20 | int lo = 0; 21 | int ans = 0; 22 | 23 | while (lo <= hi) { 24 | int mid = (lo + hi) / 2; 25 | if (isitpossible(page, mid, nos) == true) { 26 | ans = mid; 27 | hi = mid - 1; 28 | } else { 29 | lo = mid + 1; 30 | } 31 | } 32 | return ans; 33 | 34 | } 35 | 36 | private static boolean isitpossible(int[] page, int mid, int nos) { 37 | // TODO Auto-generated method stub 38 | int s = 1; 39 | int reapapge = 0; 40 | int i = 0; 41 | while (i < page.length) { 42 | if (reapapge + page[i] <= mid) { 43 | reapapge += page[i]; 44 | i++; 45 | } else { 46 | reapapge = 0; 47 | s++; 48 | 49 | } 50 | if (s > nos) { 51 | return false; 52 | } 53 | } 54 | 55 | return true; 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Lec8/First_Bad_Version.java: -------------------------------------------------------------------------------- 1 | package Lec8; 2 | 3 | public class First_Bad_Version { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | public int firstBadVersion(int n) { 11 | int lo = 1; 12 | int hi = n; 13 | int ans = 0; 14 | while (lo <= hi) { 15 | int mid = lo + (hi - lo) / 2; 16 | if (isBadVersion(mid) == true) { 17 | ans = mid; 18 | hi = mid - 1; 19 | } else { 20 | lo = mid + 1; 21 | } 22 | } 23 | return ans; 24 | 25 | } 26 | 27 | private boolean isBadVersion(int mid) { 28 | // TODO Auto-generated method stub 29 | 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lec8/Square_root.java: -------------------------------------------------------------------------------- 1 | package Lec8; 2 | 3 | public class Square_root { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n = 47; 8 | System.out.println(root(n)); 9 | 10 | } 11 | 12 | public static int root(int n) { 13 | int lo = 1; 14 | int hi = n; 15 | int ans = 0; 16 | while (lo <= hi) { 17 | int mid = (lo + hi) / 2; 18 | if (mid * mid <= n) { 19 | ans = mid; 20 | lo = mid + 1; 21 | } else { 22 | hi = mid - 1; 23 | } 24 | } 25 | return ans; 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Lec9/Divisible_Subarrays.java: -------------------------------------------------------------------------------- 1 | package Lec9; 2 | 3 | public class Divisible_Subarrays { 4 | public static void main(String[] args) { 5 | int[] arr = { 1, 2, 3, 4, 6 }; 6 | System.out.println(GoodSubArray(arr)); 7 | } 8 | 9 | public static long GoodSubArray(int[] arr) { 10 | long sum = 0; 11 | int n = arr.length; 12 | int[] frq = new int[arr.length]; 13 | frq[0] = 1; 14 | for (int i = 0; i < frq.length; i++) { 15 | sum = sum + arr[i]; 16 | int idx = (int) (sum % n); 17 | if (idx < 0) { 18 | idx += n; 19 | } 20 | frq[idx] = frq[idx] + 1; 21 | 22 | } 23 | 24 | long ans = 0; 25 | for (int i = 0; i < frq.length; i++) { 26 | if (frq[i] >= 2) { 27 | long p = frq[i]; 28 | ans = ans + (p * (p - 1)) / 2; 29 | } 30 | } 31 | return ans; 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec9/Matrix_Search.java: -------------------------------------------------------------------------------- 1 | package Lec9; 2 | 3 | public class Matrix_Search { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[][] arr = { { 1, 4, 7, 11, 15 }, { 2, 5, 8, 12, 19 }, { 3, 6, 9, 16, 22 }, { 10, 13, 14, 17, 24 }, 8 | { 18, 21, 23, 26, 30 } }; 9 | System.out.println(MatrixSearch(arr, 11)); 10 | 11 | } 12 | 13 | public static boolean MatrixSearch(int[][] arr, int item) { 14 | int row = 0; 15 | int col = arr[0].length - 1; 16 | while (row < arr.length && col >= 0) { 17 | if (arr[row][col] == item) { 18 | return true; 19 | } else if (arr[row][col] > item) { 20 | col--; 21 | } else { 22 | row++; 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec9/Spiral_Print.java: -------------------------------------------------------------------------------- 1 | package Lec9; 2 | 3 | public class Spiral_Print { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int[][] arr = { { 1, 2, 3, 4 }, 8 | { 5, 6, 7, 8 }, 9 | { 9, 10, 11, 12 }, 10 | };// 3*4 11 | PrintSpiral(arr); 12 | } 13 | 14 | public static void PrintSpiral(int[][] arr) { 15 | // TODO Auto-generated method stub 16 | int minr = 0; 17 | int maxr = arr.length - 1; 18 | int minc = 0; 19 | int maxc = arr[0].length - 1; 20 | int count = 0; 21 | int te = arr.length * arr[0].length; 22 | while (count < te) { 23 | for (int i = minc; i <= maxc && count < te; i++) { 24 | System.out.print(arr[minr][i] + " "); 25 | count++; 26 | 27 | } 28 | minr++; 29 | for (int i = minr; i <= maxr && count < te; i++) { 30 | System.out.print(arr[i][maxc] + " "); 31 | count++; 32 | } 33 | maxc--; 34 | for (int i = maxc; i >= minc && count < te; i--) { 35 | System.out.print(arr[maxr][i] + " "); 36 | count++; 37 | } 38 | maxr--; 39 | for (int i = maxr; i >= minr && count < te; i--) { 40 | System.out.print(arr[i][minc] + " "); 41 | count++; 42 | } 43 | minc++; 44 | 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Lec9/Two_DArray_Demo.java: -------------------------------------------------------------------------------- 1 | package Lec9; 2 | 3 | public class Two_DArray_Demo { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int [][] arr=new int [3][4]; 8 | int [][]other = arr; 9 | System.out.println(arr[0]); 10 | // row 11 | System.out.println(arr.length); 12 | 13 | // col 14 | System.out.println(arr[0].length); 15 | 16 | } 17 | 18 | } 19 | --------------------------------------------------------------------------------