├── Lec41
├── Dynmaic_StackI.java
├── DSAI.java
├── Abstract_Demo.java
├── Pair.java
├── Stack_Client.java
├── Payment_Menthod.java
├── StackI.java
├── Genrics_Demo.java
├── Stack_Class.java
├── Abstract_Client.java
├── InterFace_Client.java
└── Construct_Binary_Tree_from_Preorder_and_Inorder.java
├── Lec31
├── Student.java
├── P.java
├── C.java
├── Student_Client.java
├── Sum_Of_Number.java
└── Client.java
├── README.md
├── Lec1
├── Test.java
├── Variable_Demo.java
├── Loops_Demo.java
├── Simple_Interest.java
├── Sum_N_Number.java
├── Assignment_Operators.java
├── Odd_Even.java
├── Maximum_of_3_Number.java
├── Arithmetic_Operators.java
└── Increment_Decrement.java
├── Lec2
├── Pattern0.java
├── Pattrern1.java
├── Pattern2.java
├── Pattern4.java
├── Pattern6.java
├── Pattern9.java
├── Pattern12.java
├── Pattern22.java
└── Pattern21.java
├── Lec54
├── Odd_Even.java
├── Check_ith_Bit_Set.java
├── Single_Number.java
├── Left_Rigth_Shift.java
├── Magic_Number.java
├── Count_Set_Bit.java
├── Tavas_and_SaDDas.java
├── Single_Number_III.java
└── Print_SubSeqnunce_Using_BitMasking.java
├── Lec4
├── Forloops_Demo.java
├── Fibo.java
├── Sum_of_Digit.java
├── Revese_Number.java
├── Check_Prime.java
├── Break_Statment.java
├── GCD.java
└── Pascal_Triangle.java
├── Lec16
├── String_to_Number.java
├── PrintSubSubString_lengthwise.java
├── Wrapper_Class_Demo.java
└── Finding_Cb_Numebr.java
├── Lec18
├── Print_Inc.java
├── Print_Dec.java
├── Fact.java
├── Fact_Tail.java
├── Power.java
└── First_Occc.java
├── Lec55
├── Trie_Client.java
├── Counting_Sort.java
├── Tries_Contacts.java
└── Trie.java
├── Lec29
├── Student_Client.java
├── Stack_Client.java
├── Queue_Client.java
├── Stack_Java.java
├── Student.java
├── Stack.java
└── Queue.java
├── Lec6
├── Typcasting_2.java
├── Loops_Demo.java
├── Typcasting_3.java
├── Conversion_Fahrenheit_to_Celsius.java
├── Typcasting.java
└── Inverse_of_number.java
├── Lec19
├── Fibo.java
├── Coin_Toss.java
├── Coin_Toss_2.java
└── SubSqunce.java
├── Lec5
├── Bin_to_Dec.java
├── Dec_to_Bin.java
└── Data_Types.java
├── Lec27
├── Random_Number.java
├── Pow_Log_N.java
├── Sieve_of_Eratosthenes.java
└── Randomized_quick_Sort.java
├── Lec14
├── Two_D_Arrays_Demo.java
├── Matrix_Search.java
├── Wave_Print.java
├── Transpose.java
├── User_Input_2_D.java
└── Spiral_Print.java
├── Lec8
├── Arrays_Swap_1.java
├── Linear_Search.java
├── Arrays_Swap_2.java
├── Arrays_Swap_demo_3.java
├── Revese_Array.java
├── Arrays_Input.java
├── Reverse_Array_Range.java
├── Array_Demo.java
└── Max_Value.java
├── Lec25
├── Check_Prime.java
├── N_Queen.java
└── Sudoku_Solver.java
├── Lec43
├── Heap_Client.java
├── PriorityQueue_Demo.java
├── Kth_Largest_Element_in_an_Array.java
├── Minimum_Sum_Pair.java
├── Merge_k_Sorted_Lists.java
└── Heap.java
├── Lec24
├── Toh.java
├── Word_Search.java
└── Rat_Chases_its_cheese.java
├── Doub_Class_Array
├── ArrayList_Demo_Part_2.java
├── Sorting_In_Linear_Time.java
├── Circular_Array_Maxmum.java
├── Maximum_Sum_Path.java
└── ArrayList_Demo.java
├── Lec17
├── Freq_Array.java
├── Maximum_Sub_Array_Sum_of_Size_k.java
├── Subarray_Product_Less_Than_K.java
└── Kartik_Bhaiya_And_Strings.java
├── Lec20
├── Lexco_counting.java
├── Math_Path.java
├── Board_Path.java
└── Generate_Parentheses.java
├── Lec15
├── Check_Palindromic.java
├── String_Demo.java
├── SubString_Print.java
├── String_Addition.java
├── Reverse_Words_in_a_String.java
├── String_Equals.java
├── String_LexcoGraphic_Order.java
└── Playing_with_Good_Strings.java
├── Lec7
├── Fun_Demo_1.java
├── Fun_Demo_2.java
├── Fun_Demo_3.java
├── Is_Armstrong_Number.java
└── Odd_and_Even_back_in_Delhi.java
├── Lec28
├── PerSon_Client.java
├── Student.java
├── StudentClient.java
└── Person.java
├── Lec37
└── BinaryTree_Client.java
├── Lec35
├── Linked_List_Cycle.java
├── Middle_of_LinkedList.java
└── Reverse_Linked_List.java
├── Lec10
├── Maximum_SubArray_Sum.java
├── Kadens_Algo.java
├── Bubble_Sort.java
├── Insertion_Sort.java
└── Selection_Sort.java
├── Lec11
├── Kth_Root.java
├── First_Bad_Version.java
└── Binary_Search.java
├── Lec22
├── Coin_Permutation.java
├── Coin_Combination.java
├── QueenPermuation.java
└── Queen_Cimbination.java
├── Lec45
├── HashMap_Client.java
├── StringBulider_Vs_String.java
├── Stringbuilder.java
└── Group_Anagrams.java
├── Lec3
├── Pattern13.java
├── Pattern14.java
├── Pattern25.java
├── Pattern16.java
├── Pattern27.java
├── Pattern17.java
├── Pattern_Rhombus.java
└── Pattern19.java
├── Lec50
├── Min_Cost_Climbing_Stairs.java
├── Longest_Increasing_Subsequence.java
├── Fibo.java
└── House_Robber.java
├── Lec30
├── Queue_Reverse.java
├── Stack_Revserse.java
├── Stack_Reverse.java
├── Construct_Smallest_Number_From_DI_String.java
└── Celebrity_Problem.java
├── Lec33
├── Dynamic_Stack.java
├── Dynamic_Queue.java
├── Queue_Using_Stack.java
└── Stack_Using_Queue.java
├── Lec9
├── Rotate_Array.java
├── ReverSal_Algo.java
├── Trapping_Rain_Water.java
└── Product_of_Array_Except_Self.java
├── Lec52
├── Minimum_Path_Sum.java
├── Valentine_Magic.java
├── Edit_distance.java
├── Minimum_Falling_Path_Sum_2.java
└── Minimum_Falling_Path_Sum.java
├── Lec23
├── Two_2d_List.java
├── Combination_Sum.java
└── Palindrome_Partitioning.java
├── Lec36
├── Intersection_of_Two_Linked_Lists.java
├── Merge_Two_Sorted_Lists.java
└── Sort.java
├── Lec51
├── Knapsack_Zero_One.java
├── Uncrossed_Lines.java
├── Distinct_Subsequences.java
├── Coin_Change_II.java
└── Longest_Common_Subsequence.java
├── Lec46
└── Graph_Client.java
├── Lec32
├── Next_Greater_Element.java
├── Stock_Span.java
└── Histogram.java
├── Lec39
├── LCA.java
├── Binary_Tree_Right_Side_View.java
├── Diameter_of_Binary_Tree.java
├── Diameter_of_Binary_Tree_Opt.java
├── Balanced_Binary_Tree.java
└── Create_Tree_Using_Level_Order.java
├── Lec34
└── LinkedList_Client.java
├── Lec42
├── Cars.java
├── BUSYMAN.java
└── Cras_Client.java
├── Lec40
├── Insert_into_a_Binary_Search_Tree.java
├── BinarySearchTree.java
├── Validate_Binary_Search_Tree.java
└── Delete_Node_in_a_BST.java
├── Lec21
├── KeyPaid.java
├── Permutation_2.java
└── Permutation.java
├── Lec38
├── Path_Sum.java
├── Sum_Root_to_Leaf_Numbers.java
├── Flip_Equivalen.java
└── Symmetric_Tree.java
├── Lec26
├── Partition_in_Array.java
├── Merge_Two_Sorted_Array.java
├── Axis_Orbit.java
├── Quick_Sort.java
└── Merge_Sort.java
├── Lec44
├── Longest_Consecutive_Sequence.java
├── Set_Demo.java
└── Intersection_of_Two_Arrays_II.java
├── Lec53
├── Lis_Nlog_N.java
└── Wine_Problem.java
├── Lec48
└── DisJointSet.java
├── Lec13
├── Book_Allocation.java
└── Aggressive_cows.java
└── Lec47
├── Vaild_Tree.java
└── Is_Bipartite.java
/Lec41/Dynmaic_StackI.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | //B
4 | public interface Dynmaic_StackI {
5 | public void Display();
6 |
7 | public void fun();
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/Lec41/DSAI.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | // A
4 | public interface DSAI extends StackI, Dynmaic_StackI {
5 |
6 | public int size();
7 |
8 | public boolean isEmpty();
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/Lec31/Student.java:
--------------------------------------------------------------------------------
1 | package Lec31;
2 |
3 | public class Student {
4 | String name = "Kaju";
5 | int age = 20;
6 |
7 | @Override
8 | public String toString() {
9 | return name + " " + age;
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/Lec41/Abstract_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public abstract class Abstract_Demo {
4 |
5 | public abstract void Payment();
6 |
7 | public abstract int No_of_item();
8 |
9 | public void Viewitem() {
10 |
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Java Logical Operators
2 | Logical operators are used to determine the logic between variables or values:
3 |
4 | 
5 |
--------------------------------------------------------------------------------
/Lec31/P.java:
--------------------------------------------------------------------------------
1 | package Lec31;
2 |
3 | public class P {
4 | int d = 2;
5 | int d2 = 20;
6 |
7 | public void fun() {
8 | System.out.println("Fun in P");
9 | }
10 |
11 | public void fun2() {
12 | System.out.println("Fun2 in P");
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/Lec41/Pair.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class Pair {
4 |
5 | B x;
6 | M y;
7 |
8 | public static void main(String[] args) {
9 | Pair p = new Pair<>();
10 |
11 | Pair p1 = new Pair<>();
12 |
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/Lec1/Test.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Test {
4 | public static void main(String[] args) {
5 | System.out.println("Welcome to Coding Blocks");
6 | System.out.print("Hey");
7 | System.out.println("Bye");
8 | System.out.print("okay");
9 |
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/Lec1/Variable_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Variable_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a = 10;
8 | System.out.println(a);
9 | a = 5;
10 | System.out.println(a);
11 |
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/Lec31/C.java:
--------------------------------------------------------------------------------
1 | package Lec31;
2 |
3 | public class C extends P {
4 |
5 | int d = 1;
6 | int d1 = 10;
7 |
8 | @Override
9 | public void fun() {
10 | System.out.println("Fun in C");
11 | }
12 |
13 | public void fun1() {
14 | System.out.println("Fun1 in C");
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec2/Pattern0.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | public class Pattern0 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | int star = 5;
9 | int i = 1;
10 | while (i <= star) {
11 | System.out.print("* ");
12 | i++;
13 | }
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec1/Loops_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Loops_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int i = 1;
9 | while (i <= n) {
10 | System.out.println("hey");
11 | i = i + 1;
12 |
13 | }
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec1/Simple_Interest.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Simple_Interest {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int p=1000;
8 | int t=2;
9 | int r=5;
10 | int si=(p*r*t)/100;
11 | System.out.println(si);
12 |
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Lec31/Student_Client.java:
--------------------------------------------------------------------------------
1 | package Lec31;
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 |
11 | System.out.println(s);
12 |
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Lec54/Odd_Even.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Odd_Even {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 7;
8 | if ((n & 1) != 0) {
9 | System.out.println("Odd");
10 | } else {
11 | System.out.println("Even");
12 | }
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Lec4/Forloops_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | public class Forloops_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | for (int i = 1; i <= 5; i++) {
9 |
10 | if (i == 2) {
11 | continue;
12 | }
13 | System.out.print(i + " ");
14 |
15 | }
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Lec16/String_to_Number.java:
--------------------------------------------------------------------------------
1 | package Lec16;
2 |
3 | public class String_to_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s = "65676897";
8 | int n = Integer.parseInt(s);
9 | long l = Long.parseLong(s);
10 | System.out.println(n);
11 | System.out.println(l);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/Lec18/Print_Inc.java:
--------------------------------------------------------------------------------
1 | package Lec18;
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 |
--------------------------------------------------------------------------------
/Lec4/Fibo.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | public class Fibo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 6;
8 | int a = 0;
9 | int b = 1;
10 | for (int i = 1; i <= n; i++) {
11 | int c = a + b;
12 | a = b;
13 | b = c;
14 | }
15 | System.out.println(a);
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Lec4/Sum_of_Digit.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | public class Sum_of_Digit {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 3846;
8 | int sum = 0;
9 | while (n > 0) {
10 | int rem = n % 10;
11 | sum = sum + rem;
12 | n = n / 10;
13 | }
14 | System.out.println(sum);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec1/Sum_N_Number.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Sum_N_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int i = 1;
9 | int sum = 0;
10 | while (i <= n) {
11 | sum = sum + i;// sum +=i;
12 | i = i + 1;//i++;
13 | }
14 | System.out.println(sum);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec18/Print_Dec.java:
--------------------------------------------------------------------------------
1 | package Lec18;
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 |
12 | public static void PD(int n) {
13 | if (n == 0) {
14 | return;
15 | }
16 | System.out.println(n);
17 | PD(n - 1);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec41/Stack_Client.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | //Kush
4 | public class Stack_Client {
5 |
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 | Stack_Class s = new Stack_Class();
9 | }
10 |
11 | // public final void fun() {
12 | //
13 | // }
14 | //
15 | // public final void fun(int x) {
16 | //
17 | // }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Lec55/Trie_Client.java:
--------------------------------------------------------------------------------
1 | package Lec55;
2 |
3 | public class Trie_Client {
4 | public static void main(String[] args) {
5 | Trie t = new Trie();
6 | t.insert("apple");
7 | t.insert("raj");
8 | t.insert("rajesh");
9 | t.insert("ankit");
10 | t.insert("ankita");
11 | t.insert("vishal");
12 | t.insert("kaju");
13 | t.insert("bitu");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Lec4/Revese_Number.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | public class Revese_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 3846;
8 | int sum = 0;
9 | while (n > 0) {
10 | int rem = n % 10;
11 | sum = sum * 10 + rem;
12 | n = n / 10;
13 | }
14 | System.out.println(sum);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec41/Payment_Menthod.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class Payment_Menthod extends Abstract_Demo {
4 |
5 | @Override
6 | public void Payment() {
7 | // TODO Auto-generated method stub
8 |
9 | }
10 |
11 | @Override
12 | public int No_of_item() {
13 | // TODO Auto-generated method stub
14 | return 0;
15 | }
16 |
17 | public void fun() {
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec29/Student_Client.java:
--------------------------------------------------------------------------------
1 | package Lec29;
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("Raj", 19);
8 | System.out.println(s.getAge());
9 | s.setAge(-9);
10 | System.out.println(s.getAge());
11 |
12 | System.out.println("Hey");
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Lec18/Fact.java:
--------------------------------------------------------------------------------
1 | package Lec18;
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(fac(n));
9 | }
10 |
11 | public static int fac(int n) {
12 | // base case
13 | if (n == 0) {
14 | return 1;
15 | }
16 |
17 | int fn = fac(n - 1);// sp
18 | return fn * n;
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Lec54/Check_ith_Bit_Set.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Check_ith_Bit_Set {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 75;
8 | int i = 3;
9 | int mask = 1 << i;
10 | if ((n & mask) != 0) {
11 | System.out.println("Set hai ");
12 | } else {
13 | System.out.println("Set nhi hai ");
14 | }
15 |
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/Lec6/Typcasting_2.java:
--------------------------------------------------------------------------------
1 | package Lec6;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Typcasting_2 {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | float f = (8.9f);
10 | System.out.println(f);
11 | double d = 8.9;
12 | Scanner sc = new Scanner(System.in);
13 | f = sc.nextFloat();
14 | d = sc.nextDouble();
15 |
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/Lec18/Fact_Tail.java:
--------------------------------------------------------------------------------
1 | package Lec18;
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(fac(n, 1));
9 | }
10 |
11 | public static int fac(int n, int ans) {
12 | // base case
13 | if (n == 0) {
14 | return ans;
15 | }
16 |
17 | return fac(n - 1, ans * n);
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec18/Power.java:
--------------------------------------------------------------------------------
1 | package Lec18;
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 = 5;
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 | int ans = pow(a, b - 1);
17 | return ans * a;
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec19/Fibo.java:
--------------------------------------------------------------------------------
1 | package Lec19;
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 | public static int fib(int n) {
11 | if (n == 0 || n == 1) {
12 | return n;
13 | }
14 |
15 | int f1 = fib(n - 1);
16 | int f2 = fib(n - 2);
17 | return f1 + f2;
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Lec5/Bin_to_Dec.java:
--------------------------------------------------------------------------------
1 | package Lec5;
2 |
3 | public class Bin_to_Dec {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 10101;
8 | int sum = 0;
9 | int mul = 1;
10 | while (n > 0) {
11 | int rem = n % 10;
12 | sum = sum + rem * mul;
13 | n = n / 10;
14 | mul = mul * 2;
15 |
16 | }
17 | System.out.println(sum);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec5/Dec_to_Bin.java:
--------------------------------------------------------------------------------
1 | package Lec5;
2 |
3 | public class Dec_to_Bin {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 23;
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 = mul * 10;
15 |
16 | }
17 | System.out.println(sum);
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec1/Assignment_Operators.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Assignment_Operators {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int x = 5;
8 | x += 3;// x = x+ 3;
9 | System.out.println(x);
10 | int p = 9;
11 | p -= 5;// p = p-s;
12 | System.out.println(p);
13 | int a = 9;
14 | a *= 5;// a= a*5;
15 | System.out.println(a);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/Lec19/Coin_Toss.java:
--------------------------------------------------------------------------------
1 | package Lec19;
2 |
3 | public class Coin_Toss {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 3;
8 | CoinToss(n, "");
9 |
10 | }
11 |
12 | public static void CoinToss(int n, String ans) {
13 | if (n == 0) {
14 | System.out.println(ans);
15 | return;
16 | }
17 | CoinToss(n - 1, ans + "H");
18 | CoinToss(n - 1, ans + "T");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Lec27/Random_Number.java:
--------------------------------------------------------------------------------
1 | package Lec27;
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 | int si = 10;
10 | int ei = 100;
11 | Random rn = new Random();
12 | for (int i = 0; i < 10; i++) {
13 | int item = rn.nextInt(ei - si + 1) + si;
14 | System.out.print(item + " ");
15 |
16 | }
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec1/Odd_Even.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Odd_Even {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 79;
8 | if (n >= 50) {
9 | System.out.println("hey");
10 | }
11 | if (n > 100) {
12 | System.out.println("bye");
13 | }
14 | if (n % 2 == 0) {
15 | System.out.println("Even");
16 | } else {
17 | System.out.println("Odd");
18 | }
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec14/Two_D_Arrays_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec14;
2 |
3 | public class Two_D_Arrays_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[][] arr = new int[3][4];
8 | System.out.println(arr);
9 | int[][] other = arr;
10 | System.out.println(arr.length);// row ki lenght
11 | System.out.println(arr[0].length);// col ki lenght
12 | System.out.println(arr[1][2]);
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Lec6/Loops_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec6;
2 |
3 | public class Loops_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | // for (byte i = 0; i < 128; i++) {
8 | // System.out.println(i);
9 | // }
10 |
11 | // for (byte i = 0; i <=127; i++) {
12 | // System.out.println(i);
13 | // }
14 |
15 | for (byte i = 0; i < 127; i++) {
16 | System.out.println(i);
17 | }
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec41/StackI.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | // samarth
4 | // C
5 | public interface StackI {
6 |
7 | public void push(int item);
8 |
9 | public void fun();
10 |
11 | public int pop();
12 |
13 | public int peek();
14 |
15 | int x = 90;
16 |
17 | // // java 8
18 | // static void f1() {
19 | //
20 | // }
21 | //
22 | // default void f2() {
23 | //
24 | // }
25 | //
26 | // // java 9
27 | // private void name() {
28 | //
29 | // }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec54/Single_Number.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Single_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] nums = { 4, 1, 2, 1, 2 };
8 | System.out.println(Number(nums));
9 | }
10 |
11 | public static int Number(int[] nums) {
12 | int ans = 0;
13 | for (int i = 0; i < nums.length; i++) {
14 | ans = ans ^ nums[i];
15 | }
16 | return ans;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Lec8/Arrays_Swap_1.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Arrays_Swap_1 {
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 | int[] arr = { 3, 5, 1, 7, 8 };
7 | System.out.println(arr[0] + " " + arr[1]);
8 | Swap(arr[0], arr[1]);
9 | System.out.println(arr[0] + " " + arr[1]);
10 | }
11 | public static void Swap(int a, int b) {
12 | int temp = a;
13 | a = b;
14 | b = temp;
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec1/Maximum_of_3_Number.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Maximum_of_3_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a = 7;
8 | int b = 3;
9 | int c = 5;
10 | if (a >= b && a >= c) {
11 | System.out.println(a);
12 | }
13 |
14 | else if (b >= a && b >= c) {
15 | System.out.println(b);
16 | } else {
17 |
18 | System.out.println(c);
19 | }
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec2/Pattrern1.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | public class Pattrern1 {
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 | int n = 5;
7 | int row = 1;
8 | int star = n;
9 | while (row <= n) {
10 | // star
11 | int i = 1;
12 | while (i <= star) {
13 | System.out.print("* ");
14 | i++;
15 | }
16 | // next prep
17 | row++;
18 | System.out.println();
19 |
20 | }
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec1/Arithmetic_Operators.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Arithmetic_Operators {
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 | int a = 8;
7 | int b = 9;
8 | int c = a + b;
9 | System.out.println(c);
10 | c = a - b;
11 | System.out.println(c);
12 | c = a * b;
13 | System.out.println(c);
14 | c = a / b;
15 | System.out.println(c);
16 | c = a % b;
17 | System.out.println(c);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Lec54/Left_Rigth_Shift.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Left_Rigth_Shift {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | System.out.println(n << 2);// left shift
9 | int n1 = -5;
10 | System.out.println(n1 << 2);// left shift
11 | int a = 17;
12 | System.out.println(a >> 2);// right shift
13 | int a1 = -17;
14 | System.out.println(a1 >> 2);// right shift
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/Lec25/Check_Prime.java:
--------------------------------------------------------------------------------
1 | package Lec25;
2 |
3 | public class Check_Prime {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 79;
8 | System.out.println(IsPrime(n));
9 |
10 | }
11 |
12 | public static boolean IsPrime(int n) {
13 | int div = 2;
14 | while (div * div <= n) {
15 | if (n % div != 0) {
16 | return false;
17 | }
18 | div++;
19 |
20 | }
21 | return true;
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec2/Pattern2.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | public class Pattern2 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int row = 1;
9 | int star = 1;
10 | while (row <= n) {
11 | // star
12 | int i = 1;
13 | while (i <= star) {
14 | System.out.print("* ");
15 | i++;
16 | }
17 | // next Row Prep
18 | System.out.println();
19 | row++;
20 | star++;
21 |
22 | }
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec29/Stack_Client.java:
--------------------------------------------------------------------------------
1 | package Lec29;
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 | System.out.println(s.peek());
13 | s.Display();
14 | // System.out.println(s.pop());
15 | // s.Display();
16 | s.push(50);
17 | s.Display();
18 | s.push(60);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec27/Pow_Log_N.java:
--------------------------------------------------------------------------------
1 | package Lec27;
2 |
3 | public class Pow_Log_N {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a = 3;
8 | int n = 5;
9 | System.out.println(pow(a, n));
10 |
11 | }
12 |
13 | public static int pow(int a, int n) {
14 | if (n == 0) {
15 | return 1;
16 | }
17 | int ans = pow(a, n /2);
18 | ans = ans * ans;
19 | if (n % 2 != 0) {
20 | ans *= a;
21 | }
22 | return ans;
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Lec29/Queue_Client.java:
--------------------------------------------------------------------------------
1 | package Lec29;
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 | q.Display();
13 | System.out.println(q.Dequeue());
14 | System.out.println(q.Dequeue());
15 | q.Enqueue(50);
16 | q.Enqueue(60);
17 | q.Display();
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec43/Heap_Client.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | public class Heap_Client {
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(1);
12 | hp.Add(5);
13 | hp.Add(7);
14 | hp.Add(3);
15 | hp.Add(2);
16 | hp.Add(-4);
17 | hp.Display();
18 | System.out.println();
19 | System.out.println(hp.remove());
20 | hp.Display();
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec5/Data_Types.java:
--------------------------------------------------------------------------------
1 | package Lec5;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Data_Types {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | byte b = 30;
10 | short s = 5;
11 | int i = 9;
12 | long l = 16;
13 | byte b1 = -5;
14 | Scanner sc = new Scanner(System.in);
15 | b = sc.nextByte();
16 | s = sc.nextShort();
17 | l = sc.nextLong();
18 | boolean x=true;
19 | boolean y=false;
20 |
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec8/Linear_Search.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Linear_Search {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8 };
8 | int item = 9;
9 | System.out.println(Search(arr, item));
10 | }
11 |
12 | public static int Search(int[] arr, int item) {
13 | for (int i = 0; i < arr.length; i++) {
14 | if (arr[i] == item) {
15 | return i;
16 | }
17 |
18 | }
19 | return -1;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec24/Toh.java:
--------------------------------------------------------------------------------
1 | package Lec24;
2 |
3 | public class Toh {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 3;
8 | TOH(n, "A", "C", "B");
9 |
10 | }
11 |
12 | public static void TOH(int n, String src, String hlp, String des) {
13 | if (n == 0) {
14 | return;
15 | }
16 | TOH(n - 1, src, des, hlp);
17 | System.out.println("Move " + n + "th disk form " + src + " to " + des);
18 | TOH(n - 1, hlp, src, des);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec8/Arrays_Swap_2.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Arrays_Swap_2 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8 };
8 | System.out.println(arr[0] + " " + arr[1]);// 3 5
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 |
--------------------------------------------------------------------------------
/Doub_Class_Array/ArrayList_Demo_Part_2.java:
--------------------------------------------------------------------------------
1 | package Doub_Class_Array;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class ArrayList_Demo_Part_2 {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | ArrayList list = new ArrayList<>(7);
10 | list.add(10);
11 | list.add(20);
12 | list.add(30);
13 | list.add(105);
14 | list.add(210);
15 | list.add(310);
16 | list.add(1);
17 | list.add(11);
18 |
19 | System.out.println(list);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec6/Typcasting_3.java:
--------------------------------------------------------------------------------
1 | package Lec6;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Typcasting_3 {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | char ch = 'a';
10 | System.out.println((int) (ch));
11 | ch++;// ch = (char)(ch+1);
12 | System.out.println(ch);
13 | ch = (char) (ch + 1);
14 | System.out.println(ch);
15 | Scanner sc = new Scanner(System.in);
16 | ch=sc.next().charAt(0);
17 | System.out.println(ch);
18 |
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec17/Freq_Array.java:
--------------------------------------------------------------------------------
1 | package Lec17;
2 |
3 | public class Freq_Array {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s = "dsfghjkfdaafsdhgfasdewrtyuioipsdfghjklzxcvbnm";
8 | int[] frq = new int[26];
9 | for (int i = 0; i < s.length(); i++) {
10 | char ch = s.charAt(i);
11 | frq[ch - 'a']++;// frq[ch - 'a']= frq[ch - 'a']+1;
12 |
13 | }
14 | for (int i = 0; i < frq.length; i++) {
15 | System.out.print(frq[i] + " ");
16 | }
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec19/Coin_Toss_2.java:
--------------------------------------------------------------------------------
1 | package Lec19;
2 |
3 | public class Coin_Toss_2 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 3;
8 | CoinToss(n, "");
9 | }
10 |
11 | public static void CoinToss(int n, String ans) {
12 | if (n == 0) {
13 | System.out.println(ans);
14 | return;
15 | }
16 |
17 | if (ans.length() == 0 || ans.charAt(ans.length() - 1) != 'H') {
18 | CoinToss(n - 1, ans + "H");
19 | }
20 | CoinToss(n - 1, ans + "T");
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec20/Lexco_counting.java:
--------------------------------------------------------------------------------
1 | package Lec20;
2 |
3 | public class Lexco_counting {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 1000;
8 | Counting(n, 0);
9 |
10 | }
11 |
12 | public static void Counting(int n, int curr) {
13 | if (curr > n) {
14 | return;
15 | }
16 | System.out.println(curr);
17 | int i = 0;
18 | if (curr == 0) {
19 | i = 1;
20 | }
21 | for (; i <= 9; i++) {
22 | Counting(n, curr * 10 + i);
23 | }
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Lec15/Check_Palindromic.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class Check_Palindromic {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s = "naan";
8 | System.out.println(IsPalindrom(s));
9 | }
10 |
11 | public static boolean IsPalindrom(String s) {
12 | int i = 0;
13 | int j = s.length() - 1;
14 | while (i < j) {
15 | if (s.charAt(i) != s.charAt(j)) {
16 | return false;
17 | }
18 | i++;
19 | j--;
20 | }
21 | return true;
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec4/Check_Prime.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Check_Prime {
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 count = 0;
12 | for (int i = 2; i < n; i++) {
13 | if (n % i == 0) {
14 | count++;
15 | }
16 | }
17 | if (count >= 1) {
18 | System.out.println("Not prime");
19 | } else {
20 | System.out.println("Prime");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec7/Fun_Demo_1.java:
--------------------------------------------------------------------------------
1 | package Lec7;
2 |
3 | public class Fun_Demo_1 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | System.out.println("Hello");
8 | Add();
9 | System.out.println("Bye");
10 |
11 | }
12 | public static void Add() {
13 | int a = 9;
14 | int b = 7;
15 | int c = a + b;
16 | Sub();
17 | System.out.println(c);
18 | }
19 | public static void Sub() {
20 | int a = 9;
21 | int b = 7;
22 | int c = a - b;
23 | System.out.println(c);
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Lec15/String_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class String_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s1 = "hello";
8 | String s2 = "hello";
9 | String s3 = new String("hello");
10 | String s4 = new String("hello");
11 | System.out.println(s1 == s2);
12 | System.out.println(s1 == s3);
13 | System.out.println(s1 == s4);
14 | int[] arr = new int[5];
15 | System.out.println(arr.length);
16 | System.out.println(s1.length());
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec28/PerSon_Client.java:
--------------------------------------------------------------------------------
1 | package Lec28;
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("Raj", 22);
9 | // System.out.println(p.name);
10 | // System.out.println(p.age);
11 | Person p1 = new Person("Pooja", 21);
12 | // System.out.println(p1.name);
13 | // System.out.println(p1.age);
14 | // p1.age = -90;
15 | p1.setAge(19);
16 | System.out.println(p1.getAge());
17 |
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/Lec7/Fun_Demo_2.java:
--------------------------------------------------------------------------------
1 | package Lec7;
2 |
3 | public class Fun_Demo_2 {
4 | public static void main(String[] args) {
5 | // TODO Auto-generated method stub
6 | System.out.println("hello");
7 | // int ans = Add();
8 | // System.out.println(ans);
9 | System.out.println(Add());
10 | }
11 | public static int Add() {
12 | int a = 8;
13 | int b = 7;
14 | int c = a + b - Sub();
15 | return c;
16 |
17 | }
18 | public static int Sub() {
19 | int a = 9;
20 | int b = 7;
21 | int c = a - b;
22 | return c;
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Lec8/Arrays_Swap_demo_3.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Arrays_Swap_demo_3 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8 };
8 | int[] other = { 31, 51, 11, 71, 81 };
9 | System.out.println(arr[0] + " " + other[0]);
10 | Swap(arr, other);
11 | System.out.println(arr[0] + " " + other[0]);
12 |
13 | }
14 |
15 | public static void Swap(int[] arr, int[] other) {
16 | int[] temp = arr;
17 | arr = other;
18 | other = temp;
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec41/Genrics_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class Genrics_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String[] arr = { "Ankit", "Ankita", "Kaju", "Amisha", " Raj", "Vishal", "Pooja" };
8 | Display(arr);
9 | Integer[] arr1 = { 1, 2, 3, 4, 5 };
10 | Display(arr1);
11 |
12 | }
13 |
14 | public static void Display(Kaju[] arr) {
15 | for (int i = 0; i < arr.length; i++) {
16 | System.out.print(arr[i] + " ");
17 | }
18 | System.out.println();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Lec16/PrintSubSubString_lengthwise.java:
--------------------------------------------------------------------------------
1 | package Lec16;
2 |
3 | public class PrintSubSubString_lengthwise {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String str = "hello";
8 | Substring(str);
9 | }
10 |
11 | public static void Substring(String str) {
12 |
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 | }
25 |
--------------------------------------------------------------------------------
/Lec18/First_Occc.java:
--------------------------------------------------------------------------------
1 | package Lec18;
2 |
3 | public class First_Occc {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 2, 4, 3, 5, 6, 3, 7, 3, 56, 3, 11 };
8 | int item = 3;
9 | System.out.println(Index(arr, item, 0));
10 |
11 | }
12 |
13 | public static int Index(int[] arr, int item, int idx) {
14 | if (idx == arr.length) {
15 | return -1;
16 | }
17 | if (arr[idx] == item) {
18 | return idx;
19 | }
20 | return Index(arr, item, idx + 1);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec4/Break_Statment.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Break_Statment {
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 | Scanner sc = new Scanner(System.in);
9 | int n = sc.nextInt();
10 | int count = 0;
11 | for (int i = 2; i < n; i++) {
12 | if (n % i == 0) {
13 | count++;
14 | break;
15 | }
16 | }
17 | if (count >= 1) {
18 | System.out.println("Not prime");
19 | } else {
20 | System.out.println("Prime");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec6/Conversion_Fahrenheit_to_Celsius.java:
--------------------------------------------------------------------------------
1 | package Lec6;
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 = i + steps) {
14 | int c = (int) ((5 / 9.0) * (i - 32));
15 | System.out.println(i + "\t" + c);
16 |
17 | }
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/Lec6/Typcasting.java:
--------------------------------------------------------------------------------
1 | package Lec6;
2 |
3 | public class Typcasting {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | // int a = 077;
8 | // System.out.println(a);
9 | byte b = (byte) (140);
10 | b = 2;
11 | System.out.println(b);
12 | byte b1 = (byte) (300);
13 | System.out.println(b1);
14 | byte c1 = (byte) (428);
15 | System.out.println(c1);
16 | int i = 17;
17 | i = b;
18 | b = -5;
19 | i = b;
20 | long l=768987997898888l;
21 | System.out.println(l);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec37/BinaryTree_Client.java:
--------------------------------------------------------------------------------
1 | package Lec37;
2 |
3 | public class BinaryTree_Client {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | // 10 true 20 true 40 false false true 50 false false true 30 false true 60 true 70 false false false
8 | BinaryTree bt = new BinaryTree();
9 | bt.Display();
10 | System.out.println(bt.max());
11 | System.out.println(bt.find(60));
12 | System.out.println(bt.ht());
13 | bt.PreOrder();
14 | bt.INOrder();
15 | bt.POStOrder();
16 | bt.levelOrder();
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Lec4/GCD.java:
--------------------------------------------------------------------------------
1 | package Lec4;
2 |
3 | public class GCD {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int dividend = 36;
8 | int divisor = 60;
9 |
10 | while (dividend % divisor != 0) {
11 | int rem = dividend % divisor;
12 | dividend = divisor;
13 | divisor = rem;
14 |
15 | }
16 | System.out.println(divisor);
17 |
18 | // for (; dividend % divisor != 0;) {
19 | // int rem = dividend % divisor;
20 | // dividend = divisor;
21 | // divisor = rem;
22 | // }
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec6/Inverse_of_number.java:
--------------------------------------------------------------------------------
1 | package Lec6;
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 pos = 1;
12 | int sum = 0;
13 | while (n > 0) {
14 | int rem = n % 10;
15 | // sum = (int) (sum + pos * Math.pow(10, rem - 1));
16 | sum += pos * Math.pow(10, rem - 1);
17 | n = n / 10;
18 | pos++;
19 | }
20 | System.out.println(sum);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec54/Magic_Number.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Magic_Number {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 11;
8 | System.out.println(Nth_Magic_Number(n));
9 |
10 | }
11 |
12 | public static int Nth_Magic_Number(int n) {
13 | int mul = 5;
14 | int ans = 0;
15 | while (n > 0) {
16 | // int rem = n % 2;
17 | // ans = ans + rem * mul;
18 | if ((n & 1) != 0) {
19 | ans = ans + mul;
20 | }
21 | mul *= 5;
22 | n >>= 1;
23 | }
24 | return ans;
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Lec8/Revese_Array.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Revese_Array {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8, 6 };
8 | Revese(arr);
9 | for (int i = 0; i < arr.length; i++) {
10 | System.out.print(arr[i] + " ");
11 | }
12 | }
13 |
14 | public static void Revese(int[] arr) {
15 | int i = 0;
16 | int j = arr.length - 1;
17 | while (i < j) {
18 | int temp = arr[i];
19 | arr[i] = arr[j];
20 | arr[j] = temp;
21 | i++;
22 | j--;
23 | }
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec8/Arrays_Input.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Arrays_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 | Display(arr);
16 | }
17 |
18 | public static void Display(int[] arr) {
19 | for (int i = 0; i < arr.length; i++) {
20 | System.out.print(arr[i] + " ");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec16/Wrapper_Class_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec16;
2 |
3 | public class Wrapper_Class_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a = 9;
8 | Integer a1 = 91;
9 | System.out.println(a);
10 | System.out.println(a1);
11 | // auto-boxing
12 | a1 = a;
13 | int b = 11;
14 | Integer b1 = 189;
15 | // un-boxing
16 | b = b1;
17 |
18 | Integer c1 = 18;
19 | Integer c2 = 18;
20 | Integer c3 = 189;
21 | Integer c4 = 189;
22 | System.out.println(c1 == c2);
23 | System.out.println(c3 == c4);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec35/Linked_List_Cycle.java:
--------------------------------------------------------------------------------
1 | package Lec35;
2 |
3 | public class Linked_List_Cycle {
4 | class ListNode {
5 | int val;
6 | ListNode next;
7 |
8 | ListNode(int x) {
9 | val = x;
10 | next = null;
11 | }
12 | }
13 |
14 | public class Solution {
15 | public boolean hasCycle(ListNode head) {
16 | ListNode slow = head;
17 | ListNode fast = head;
18 | while (fast != null && fast.next != null) {
19 | slow = slow.next;
20 | fast = fast.next.next;
21 | if (slow == fast) {
22 | return true;
23 | }
24 | }
25 | return false;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Lec4/Pascal_Triangle.java:
--------------------------------------------------------------------------------
1 | package Lec4;
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 | int i = 0;
15 | int val=1;
16 | while (i < star) {
17 | System.out.print(val+" ");
18 | val=((row-i)*val)/(i+1);
19 | i++;
20 | }
21 | row++;
22 | star++;
23 | System.out.println();
24 | }
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Lec10/Maximum_SubArray_Sum.java:
--------------------------------------------------------------------------------
1 | package Lec10;
2 |
3 | public class Maximum_SubArray_Sum {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
8 | System.out.println(MaxSum(arr));
9 | }
10 |
11 | public static int MaxSum(int[] arr) {
12 | int ans = Integer.MIN_VALUE;
13 | for (int i = 0; i < arr.length; i++) {
14 | int sum = 0;
15 | for (int j = i; j < arr.length; j++) {
16 | sum = sum + arr[j];
17 | ans = Math.max(ans, sum);
18 | }
19 | }
20 | return ans;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec11/Kth_Root.java:
--------------------------------------------------------------------------------
1 | package Lec11;
2 |
3 | public class Kth_Root {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 143;
8 | int k = 3;
9 | System.out.println(kthroot(n, k));
10 |
11 | }
12 |
13 | public static int kthroot(int n, int k) {
14 | int lo = 1;
15 | int hi = n;
16 | int ans = 0;
17 | while (lo <= hi) {
18 | int mid = (lo + hi) / 2;
19 | if (Math.pow(mid, k) <= n) {
20 | ans = mid;
21 | lo = mid + 1;
22 | } else {
23 | hi = mid - 1;
24 | }
25 | }
26 | return ans;
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Lec28/Student.java:
--------------------------------------------------------------------------------
1 | package Lec28;
2 |
3 | public class Student {
4 |
5 | String name;
6 | int age;
7 |
8 | public void Intro_yourSelf() {
9 | SayMentor_Name();
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(this.name + " Say Hey " + name);
18 |
19 | }
20 |
21 | public static void SayMentor_Name() {
22 |
23 | System.out.println("Mentor Name Monu Bhaiya");
24 |
25 | }
26 |
27 | static {
28 | System.out.println("I am a in static blocks");
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Lec8/Reverse_Array_Range.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Reverse_Array_Range {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8, 6, 9, 11, 15, 17, 18, 16, 23, 27 };
8 |
9 | Revese(arr, 3, 11);
10 | for (int i = 0; i < arr.length; i++) {
11 | System.out.print(arr[i] + " ");
12 | }
13 | }
14 |
15 | public static void Revese(int[] arr, int i, int j) {
16 |
17 | while (i < j) {
18 | int temp = arr[i];
19 | arr[i] = arr[j];
20 | arr[j] = temp;
21 | i++;
22 | j--;
23 | }
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec29/Stack_Java.java:
--------------------------------------------------------------------------------
1 | package Lec29;
2 |
3 | import java.util.Stack;
4 |
5 | public class Stack_Java {
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 | st.push(60);
16 | st.push(70);
17 | System.out.println(st.peek());
18 | System.out.println(st);
19 | System.out.println(st.pop());
20 | System.out.println(st);
21 | System.out.println(st.isEmpty());
22 | System.out.println(st.size());
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec8/Array_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Array_Demo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a;
8 | int[] arr = new int[5];
9 | System.out.println(arr);
10 | int[] other = arr;
11 | arr[0] = 10;
12 | arr[1] = 2;
13 | arr[2] = 6;
14 | arr[3] = 7;
15 | arr[4] = 9;
16 | System.out.println(arr[0]);
17 | System.out.println(arr[1]);
18 | System.out.println(arr[2]);
19 | System.out.println(arr[3]);
20 | System.out.println(arr[4]);
21 | System.out.println(other.length);// Array ki length
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec10/Kadens_Algo.java:
--------------------------------------------------------------------------------
1 | package Lec10;
2 |
3 | public class Kadens_Algo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
8 | System.out.println(MaxSum(arr));
9 | }
10 |
11 | public static int MaxSum(int[] arr) {
12 | // TODO Auto-generated method stub
13 | int ans = Integer.MIN_VALUE;
14 | int sum = 0;
15 | for (int i = 0; i < arr.length; i++) {
16 | sum += arr[i];
17 | ans = Math.max(ans, sum);
18 | if (sum < 0) {
19 | sum = 0;
20 | }
21 | }
22 | return ans;
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec41/Stack_Class.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | // vishal
4 | public class Stack_Class implements StackI {
5 |
6 | @Override
7 | public void push(int item) {
8 | // TODO Auto-generated method stub
9 |
10 | }
11 |
12 | @Override
13 | public int pop() {
14 | // TODO Auto-generated method stub
15 | // StackI.x += 3;
16 | System.out.println(StackI.x);
17 | return 0;
18 | }
19 |
20 | @Override
21 | public int peek() {
22 | // TODO Auto-generated method stub
23 | return 0;
24 | }
25 |
26 | @Override
27 | public void fun() {
28 | // TODO Auto-generated method stub
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec15/SubString_Print.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class SubString_Print {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 |
8 | String s = "Hellobye";
9 | // System.out.println(s.substring(2, 5));
10 | // System.out.println(s.substring(5, 8));
11 | // System.out.println(s.substring(2));
12 | PrintSubString(s);
13 |
14 | }
15 |
16 | public static void PrintSubString(String s) {
17 | for (int i = 0; i < s.length(); i++) {
18 | for (int j = i + 1; j <= s.length(); j++) {
19 | System.out.println(s.substring(i, j));
20 | }
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Lec22/Coin_Permutation.java:
--------------------------------------------------------------------------------
1 | package Lec22;
2 |
3 | public class Coin_Permutation {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] coin = { 2, 3, 5 };
8 | int amount = 8;
9 | Permutation(coin, amount, "");
10 |
11 | }
12 |
13 | public static void Permutation(int[] coin, int amount, String ans) {
14 | if (amount == 0) {
15 | System.out.println(ans);
16 | return;
17 | }
18 | for (int i = 0; i < coin.length; i++) {
19 | if (amount >= coin[i]) {
20 | Permutation(coin, amount - coin[i], ans + coin[i]);
21 | }
22 | }
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec45/HashMap_Client.java:
--------------------------------------------------------------------------------
1 | package Lec45;
2 |
3 | public class HashMap_Client {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | HashMap map = new HashMap<>();
8 | map.put("Raj", 89);
9 | map.put("Ankit", 79);
10 | map.put("Kaju", 99);
11 | map.put("Ankita", 59);
12 | map.put("Vishal", 77);
13 | map.put("Pooja", 88);
14 | map.put("Anku", 88);
15 | map.put("Puneet", 73);
16 | map.put("Ankita", 38);
17 | System.out.println(map.get("Ankita"));
18 | System.out.println(map.get("Ankit"));
19 | System.out.println(map);
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec3/Pattern13.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern13 {
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 | // star
15 | int i = 1;
16 | while (i <= star) {
17 | System.out.print("* ");
18 | i++;
19 | }
20 | // mirror
21 | if (row < n) {
22 | star++;
23 | } else {
24 | star--;
25 | }
26 | row++;
27 | System.out.println();
28 | }
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec10/Bubble_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec10;
2 | public class Bubble_Sort {
3 | public static void main(String[] args) {
4 | // TODO Auto-generated method stub
5 | int[] arr = { 4, 5, 3, 2, 1 };
6 | Sort(arr);
7 | for (int i = 0; i < arr.length; i++) {
8 | System.out.print(arr[i] + " ");
9 | }
10 |
11 | }
12 | public static void Sort(int[] arr) {
13 | for (int turn = 1; turn < arr.length; turn++) {
14 | for (int i = 0; i < arr.length - turn; i++) {
15 | if (arr[i] > arr[i + 1]) {
16 | int temp = arr[i];
17 | arr[i] = arr[i + 1];
18 | arr[i + 1] = temp;
19 | }
20 |
21 | }
22 | }
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec2/Pattern4.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | public class Pattern4 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int row = 1;
9 | int star = 1;
10 | int space = n - 1;
11 | while (row <= n) {
12 | // space
13 | int i = 1;
14 | while (i <= space) {
15 | System.out.print(" ");
16 | i++;
17 | }
18 | // star
19 | int j = 1;
20 | while (j <= star) {
21 | System.out.print("* ");
22 | j++;
23 | }
24 | // next row ki Prep
25 | row++;
26 | star++;
27 | space--;
28 | System.out.println();
29 | }
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/Lec22/Coin_Combination.java:
--------------------------------------------------------------------------------
1 | package Lec22;
2 |
3 | public class Coin_Combination {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] coin = { 2, 3, 5 };
8 | int amount = 8;
9 | Combination(coin, amount, "", 0);
10 |
11 | }
12 |
13 | public static void Combination(int[] coin, int amount, String ans, int idx) {
14 | if (amount == 0) {
15 | System.out.println(ans);
16 | return;
17 | }
18 | for (int i = idx; i < coin.length; i++) {
19 | if (amount >= coin[i]) {
20 | Combination(coin, amount - coin[i], ans + coin[i], i);
21 | }
22 | }
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec43/PriorityQueue_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | import java.util.Collections;
4 | import java.util.PriorityQueue;
5 |
6 | public class PriorityQueue_Demo {
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 | // PriorityQueue hp = new PriorityQueue<>();// min heap
11 | PriorityQueue hp = new PriorityQueue<>(Collections.reverseOrder()); // max heap
12 | hp.add(10);
13 | hp.add(20);
14 | hp.add(30);
15 | hp.add(1);
16 | hp.add(5);
17 | hp.add(7);
18 | hp.add(3);
19 | hp.add(2);
20 | hp.add(-4);
21 | System.out.println(hp);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec28/StudentClient.java:
--------------------------------------------------------------------------------
1 | package Lec28;
2 |
3 | public class StudentClient {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | System.out.println("Hello");
8 | Student s = new Student();
9 | System.out.println(s.name);
10 | System.out.println(s.age);
11 | s.name = "Kaju";
12 | s.age = 18;
13 | s.Intro_yourSelf();
14 | Student s1 = new Student();
15 | s1.name = "Kamlesh";
16 | s1.age = 17;
17 | s1.Intro_yourSelf();
18 | s1.SayHey("Ramesh");
19 | Student.SayMentor_Name();
20 |
21 | }
22 | static {
23 | System.out.println("I am a in static blocks in Main");
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec45/StringBulider_Vs_String.java:
--------------------------------------------------------------------------------
1 | package Lec45;
2 |
3 | public class StringBulider_Vs_String {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String_Demo();
8 | //StringBulider_Demo();
9 |
10 | }
11 |
12 | public static void String_Demo() {
13 | String s = "";
14 | for (int i = 0; i < 1000000; i++) {
15 | s += i;
16 | }
17 | //System.out.println(s);
18 | }
19 |
20 | public static void StringBulider_Demo() {
21 | StringBuilder sb = new StringBuilder();
22 | for (int i = 0; i < 1000000; i++) {
23 | sb.append(i);
24 | }
25 | //System.out.println(sb);
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Lec43/Kth_Largest_Element_in_an_Array.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | import java.util.PriorityQueue;
4 |
5 | public class Kth_Largest_Element_in_an_Array {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 3, 2, 3, 1, 2, 4, 5, 5, 6 };
10 | int k = 4;
11 | PriorityQueue pq = new PriorityQueue<>();
12 | for (int i = 0; i < k; i++) {
13 | pq.add(arr[i]);
14 | }
15 | for (int i = k; i < arr.length; i++) {
16 | if (pq.peek() < arr[i]) {
17 | pq.remove();// pq.poll();
18 | pq.add(arr[i]);
19 | }
20 | }
21 | System.out.println(pq.peek());
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec50/Min_Cost_Climbing_Stairs.java:
--------------------------------------------------------------------------------
1 | package Lec50;
2 |
3 | public class Min_Cost_Climbing_Stairs {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 };
8 | int Zero_th = Climbing_Stairs(arr, 0);
9 | int first = Climbing_Stairs(arr, 1);
10 | System.out.println(Math.min(Zero_th, first));
11 | }
12 |
13 | public static int Climbing_Stairs(int[] arr, int i) {
14 | if (i >= arr.length) {
15 | return 0;
16 | }
17 | int f = Climbing_Stairs(arr, i + 1);
18 | int s = Climbing_Stairs(arr, i + 2);
19 | return Math.min(f, s) + arr[i];
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Lec54/Count_Set_Bit.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Count_Set_Bit {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 75;
8 | System.out.println(CountSetBit(n));
9 | System.out.println(FastCountSetBit(n));
10 | }
11 |
12 | public static int FastCountSetBit(int n) {
13 | int c = 0;
14 | while (n > 0) {
15 | c++;
16 | n = (n & (n - 1));
17 | }
18 | return c;
19 | }
20 |
21 | public static int CountSetBit(int n) {
22 | int c = 0;
23 | while (n > 0) {
24 | if ((n & 1) != 0) {
25 | c++;
26 | }
27 | n >>= 1;// n = n>>1;
28 | }
29 | return c;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec7/Fun_Demo_3.java:
--------------------------------------------------------------------------------
1 | package Lec7;
2 |
3 | public class Fun_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 = 7;
10 | int b = 11;
11 | System.out.println(val);//
12 | System.out.println(Add(a, b));
13 | System.out.println(val);//
14 |
15 | }
16 |
17 | public static int Add(int a, int b) {
18 | int c = a + b;
19 | int val = 60;
20 | Fun_Demo_3.val = Fun_Demo_3.val + 5;
21 | return c - Sub(c, b);
22 |
23 | }
24 |
25 | public static int Sub(int a, int b) {
26 |
27 | int c = a - b;
28 | return c;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec11/First_Bad_Version.java:
--------------------------------------------------------------------------------
1 | package Lec11;
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 static 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 | public static boolean isBadVersion(int mid) {
28 | // TODO Auto-generated method stub
29 | return false;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec22/QueenPermuation.java:
--------------------------------------------------------------------------------
1 | package Lec22;
2 |
3 | public class QueenPermuation {
4 | public static void main(String[] args) {
5 | int n = 4;
6 | boolean[] board = new boolean[n];
7 | int tq = 2;
8 | Permuation(board, tq, "", 0);
9 |
10 | }
11 |
12 | public static void Permuation(boolean[] board, int tq, String ans, int qpsf) {
13 | if (qpsf == tq) {
14 | System.out.println(ans);
15 | return;
16 | }
17 | for (int i = 0; i < board.length; i++) {
18 | if (board[i] == false) {
19 | board[i] = true;
20 | Permuation(board, tq, ans + "b" + i + "q" + qpsf, qpsf + 1);
21 | board[i] = false;// UNDO
22 | }
23 |
24 | }
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Lec30/Queue_Reverse.java:
--------------------------------------------------------------------------------
1 | package Lec30;
2 |
3 | import Lec29.Queue;
4 |
5 | public class Queue_Reverse {
6 |
7 | public static void main(String[] args) throws Exception {
8 | // TODO Auto-generated method stub
9 | Queue q = new Queue();
10 | q.Enqueue(10);
11 | q.Enqueue(20);
12 | q.Enqueue(30);
13 | q.Enqueue(40);
14 | q.Enqueue(50);
15 | q.Display();
16 | Reverse(q);
17 | q.Display();
18 |
19 | }
20 |
21 | private static void Reverse(Queue q) throws Exception {
22 | // TODO Auto-generated method stub
23 | if (q.Isempty()) {
24 | return;
25 | }
26 | int x = q.Dequeue();
27 | Reverse(q);
28 | q.Enqueue(x);
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec30/Stack_Revserse.java:
--------------------------------------------------------------------------------
1 | package Lec30;
2 |
3 | import java.util.Stack;
4 |
5 | public class Stack_Revserse {
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 | Insert(st, -6);
17 | System.out.println(st);
18 |
19 | }
20 |
21 | public static void Insert(Stack st, int item) {
22 | if (st.isEmpty()) {
23 | st.push(item);
24 | return;
25 | }
26 | int x = st.pop();
27 | Insert(st, item);
28 | st.push(x);
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec33/Dynamic_Stack.java:
--------------------------------------------------------------------------------
1 | package Lec33;
2 |
3 | import Lec29.Stack;
4 |
5 | public class Dynamic_Stack extends Stack {
6 | @Override
7 | public void push(int item) throws Exception {
8 |
9 | if (isfull()) {
10 | int[] ar = new int[2 * arr.length];
11 | for (int i = 0; i < arr.length; i++) {
12 | ar[i] = arr[i];
13 | }
14 | arr = ar;
15 | }
16 | super.push(item);
17 |
18 | }
19 |
20 | public static void main(String[] args) throws Exception {
21 | Dynamic_Stack ds = new Dynamic_Stack();
22 | ds.push(10);
23 | ds.push(20);
24 | ds.push(30);
25 | ds.push(40);
26 | ds.push(50);
27 | ds.push(60);
28 | ds.Display();
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec9/Rotate_Array.java:
--------------------------------------------------------------------------------
1 | package Lec9;
2 |
3 | public class Rotate_Array {
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 | int k = 3;
9 | RotateArray(arr, k);
10 | for (int i = 0; i < arr.length; i++) {
11 | System.out.print(arr[i] + " ");
12 | }
13 |
14 | }
15 |
16 | public static void RotateArray(int[] arr, int k) {
17 | int n = arr.length;
18 | k = k % n;
19 | for (int j = 1; j <= k; j++) {
20 |
21 | int item = arr[n - 1];
22 | for (int i = n - 2; i >= 0; i--) {
23 | arr[i + 1] = arr[i];
24 | }
25 | arr[0] = item;
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec2/Pattern6.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | public class Pattern6 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int row = 1;
9 | int space = 0;
10 | int star = n;
11 | while (row <= n) {
12 | // space
13 | int i = 1;
14 | while (i <= space) {
15 | System.out.print(" ");
16 | i++;
17 | }
18 |
19 | // star
20 | int j = 1;
21 | while (j <= star) {
22 | System.out.print("* ");
23 | j++;
24 | }
25 |
26 | // next Row ki prep
27 | row++;
28 | space += 2;// space = space+2;
29 | star--;
30 | System.out.println();
31 |
32 | }
33 |
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Lec15/String_Addition.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class String_Addition {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s1 = "hello";
8 | String s2 = "Bye";
9 | String s = s1 + s2;
10 | String s4 = "Kaju" + s1;
11 | String s5 = "Kaju" + " katli";
12 | // String s3 = s1.concat(s2);
13 | System.out.println(s);
14 | // System.out.println(s3);
15 | System.out.println("Hey" + 10 + 30 + "Bye" + 4 + 2);
16 | System.out.println(2 + 3 + "Bye" + 5 + 5);
17 | System.out.println("Hey" + (10 + 30) + "Bye" + 4 + 2);
18 | System.out.println(10 + 30 + ("hey" + "bye") + "Bye" + 4 + 2);
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Lec35/Middle_of_LinkedList.java:
--------------------------------------------------------------------------------
1 | package Lec35;
2 |
3 | public class Middle_of_LinkedList {
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 middleNode(ListNode head) {
24 |
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 | }
31 | return slow;
32 |
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Lec54/Tavas_and_SaDDas.java:
--------------------------------------------------------------------------------
1 | package Lec54;
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 s = sc.next();
11 | System.out.println(index(s));
12 |
13 | }
14 |
15 | public static int index(String str) {
16 | int n = str.length();
17 | int count = (1 << n) - 2;// n-1 len tk ke count
18 | int pos = 0;
19 | for (int i = str.length() - 1; i >= 0; i--) {
20 | if (str.charAt(i) == '7') {
21 | count += (1 << pos);
22 | }
23 | pos++;
24 | }
25 | return count + 1;
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec41/Abstract_Client.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class Abstract_Client {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | // Abstract_Demo ab = new Abstract_Demo();
8 | // Payment_Menthod ab = new Payment_Menthod();
9 | // ab.Payment();
10 | Abstract_Demo ab = new Abstract_Demo() {
11 |
12 | @Override
13 | public void Payment() {
14 | // TODO Auto-generated method stub
15 |
16 | }
17 |
18 | @Override
19 | public int No_of_item() {
20 | // TODO Auto-generated method stub
21 | return 0;
22 | }
23 |
24 | };
25 | // Abstract_Demo ab1 = new Abstract_Demo() {
26 | //
27 | // };
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Lec52/Minimum_Path_Sum.java:
--------------------------------------------------------------------------------
1 | package Lec52;
2 |
3 | public class Minimum_Path_Sum {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[][] arr = { { 1, 3, 1 }, { 1, 5, 1 }, { 4, 2, 1 } };
8 | System.out.println(Path_Sum(arr, 0, 0));
9 | }
10 |
11 | public static int Path_Sum(int[][] arr, int r, int c) {
12 | if (r == arr.length - 1 && c == arr[0].length - 1) {
13 | return arr[r][c];
14 | }
15 | if (r >= arr.length || c >= arr[0].length) {
16 | return Integer.MAX_VALUE;
17 | }
18 | int right = Path_Sum(arr, r, c + 1);
19 | int down = Path_Sum(arr, r + 1, c);
20 | return Math.min(right, down) + arr[r][c];
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Lec11/Binary_Search.java:
--------------------------------------------------------------------------------
1 | package Lec11;
2 |
3 | public class Binary_Search {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 2, 3, 4, 5, 7, 8, 9, 11, 13, 17, 18, 19, 20, 21 };
8 | int item = 13;
9 | System.out.println(Search(arr, item));
10 |
11 | }
12 |
13 | public static int Search(int[] arr, int item) {
14 | int lo = 0;
15 | int hi = arr.length - 1;
16 | while (lo <= hi) {
17 | int mid = (lo + hi) / 2;
18 | if (arr[mid] == item) {
19 | return mid;
20 | } else if (arr[mid] > item) {
21 | hi = mid - 1;
22 | } else {
23 | lo = mid + 1;
24 | }
25 |
26 | }
27 | return -1;
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec20/Math_Path.java:
--------------------------------------------------------------------------------
1 | package Lec20;
2 |
3 | public class Math_Path {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int m = 3;// row
8 | int n = 3;// col
9 | PrintPath(0, 0, n - 1, m - 1, "");
10 |
11 | }
12 | // cc --> curr col
13 | // cr--> curr row
14 | // ec --> end col
15 | // er --> end row
16 |
17 | public static void PrintPath(int cc, int cr, int ec, int er, String ans) {
18 | if (cc == ec && cr == er) {
19 | System.out.println(ans);
20 | return;
21 | }
22 | if (cc > ec || cr > er) {
23 | return;
24 | }
25 | PrintPath(cc + 1, cr, ec, er, ans + "H");
26 | PrintPath(cc, cr + 1, ec, er, ans + "V");
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec17/Maximum_Sub_Array_Sum_of_Size_k.java:
--------------------------------------------------------------------------------
1 | package Lec17;
2 |
3 | public class Maximum_Sub_Array_Sum_of_Size_k {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 2, 1, 3, 4, 5, 6, 7, 8, 9, 4, 5 };
8 | int k = 3;
9 | System.out.println(MaxSum(arr, k));
10 |
11 | }
12 |
13 | public static int MaxSum(int[] arr, int k) {
14 | int sum = 0;
15 | int ans = 0;
16 | for (int i = 0; i < k; i++) {
17 | sum += arr[i];
18 | }
19 | ans = sum;
20 |
21 | for (int i = k; i < arr.length; i++) {
22 | sum += arr[i];// grow
23 | sum -= arr[i - k];// shrink
24 | ans = Math.max(ans, sum);
25 | }
26 | return ans;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec23/Two_2d_List.java:
--------------------------------------------------------------------------------
1 | package Lec23;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Two_2d_List {
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 | List ll = new ArrayList<>();
11 | ll.add(1);
12 | ll.add(2);
13 | ll.add(3);
14 | List ll1 = new ArrayList<>();
15 | ll1.add(10);
16 | ll1.add(20);
17 | ll1.add(30);
18 | List ll2 = new ArrayList<>();
19 | ll2.add(101);
20 | ll2.add(201);
21 | ll2.add(301);
22 | List> ans = new ArrayList<>();
23 | ans.add(ll);
24 | ans.add(ll1);
25 | ans.add(ll2);
26 | System.out.println(ans);
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Lec36/Intersection_of_Two_Linked_Lists.java:
--------------------------------------------------------------------------------
1 | package Lec36;
2 |
3 | public class Intersection_of_Two_Linked_Lists {
4 | public class ListNode {
5 | int val;
6 | ListNode next;
7 |
8 | ListNode(int x) {
9 | val = x;
10 | next = null;
11 | }
12 | }
13 |
14 | public class Solution {
15 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
16 | ListNode v = headA;
17 | ListNode vgf = headB;
18 | while (v != vgf) {
19 | if (v == null) {
20 | v = headB;
21 | } else {
22 | v = v.next;
23 | }
24 | if (vgf == null) {
25 | vgf = headA;
26 | } else {
27 | vgf = vgf.next;
28 | }
29 | }
30 | return v;
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Lec14/Matrix_Search.java:
--------------------------------------------------------------------------------
1 | package Lec14;
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(Search(arr, 16));
10 |
11 | }
12 | public static boolean Search(int[][] arr, int item) {
13 | int r = 0;
14 | int c = arr[0].length - 1;
15 | while (r < arr.length && c >= 0) {
16 | if (arr[r][c] == item) {
17 | return true;
18 | } else if (arr[r][c] > item) {
19 | c--;
20 | } else {
21 | r++;
22 | }
23 | }
24 | return false;
25 |
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Lec45/Stringbuilder.java:
--------------------------------------------------------------------------------
1 | package Lec45;
2 |
3 | public class Stringbuilder {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | StringBuilder sb = new StringBuilder();
8 | sb.append("Hello");
9 | sb.append("Bye");
10 | sb.append("HelloBye");
11 | sb.append(1);
12 | System.out.println(sb.length());
13 | System.out.println(sb.capacity());
14 | System.out.println(sb);
15 | System.out.println(sb.delete(0, 3));
16 | System.out.println(sb);
17 | String s = sb.toString();
18 | System.out.println(sb.substring(4));
19 | System.out.println(sb.substring(4,7));
20 | ///System.out.println(sb.reverse());
21 | System.out.println(sb);
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Lec51/Knapsack_Zero_One.java:
--------------------------------------------------------------------------------
1 | package Lec51;
2 |
3 | public class Knapsack_Zero_One {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] wt = { 1, 2, 3, 2, 2 };
8 | int[] val = { 8, 4, 0, 5, 3 };
9 | int cap = 4;
10 | System.out.println(Knapsack(wt, val, cap, 0));
11 |
12 | }
13 |
14 | public static int Knapsack(int[] wt, int[] val, int cap, int idx) {
15 |
16 | if (cap == 0 || idx == wt.length) {
17 | return 0;
18 | }
19 | int inc = 0, exc = 0;
20 | if (cap >= wt[idx]) {
21 | inc = val[idx] + Knapsack(wt, val, cap - wt[idx], idx + 1);
22 | }
23 | exc = Knapsack(wt, val, cap, idx + 1);
24 | return Math.max(inc, exc);
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Lec8/Max_Value.java:
--------------------------------------------------------------------------------
1 | package Lec8;
2 |
3 | public class Max_Value {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 3, 5, 1, 7, 8, 6 };
8 | System.out.println(Max(arr));
9 | }
10 |
11 | public static int Max1(int[] arr) {
12 | int m = Integer.MIN_VALUE;// -2^31
13 | for (int i = 0; i < arr.length; i++) {
14 | // if (arr[i] > m) {
15 | // m = arr[i];
16 | // }
17 | m = Math.max(m, arr[i]);
18 | }
19 | return m;
20 | }
21 |
22 | public static int Max(int[] arr) {
23 | int m = arr[0];
24 | for (int i = 1; i < arr.length; i++) {
25 | if (arr[i] > m) {
26 | m = arr[i];
27 | }
28 | }
29 | return m;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec14/Wave_Print.java:
--------------------------------------------------------------------------------
1 | package Lec14;
2 |
3 | public class Wave_Print {
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, 10 }, { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 } };
8 | WavePrint(arr);
9 | }
10 |
11 | public static void WavePrint(int[][] arr) {
12 | for (int col = 0; col < arr[0].length; col++) {
13 | if (col % 2 == 0) {
14 | for (int row = 0; row < arr.length; row++) {
15 | System.out.print(arr[row][col] + " ");
16 | }
17 | } else {
18 | for (int row = arr.length - 1; row >= 0; row--) {
19 | System.out.print(arr[row][col] + " ");
20 | }
21 | }
22 |
23 | }
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Lec15/Reverse_Words_in_a_String.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Reverse_Words_in_a_String {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Scanner sc = new Scanner(System.in);
10 | // String s = sc.nextLine();
11 | // System.out.println(s);
12 | String s = " the sky is blue ";
13 | System.out.println(Reverse_Words(s));
14 |
15 | }
16 |
17 | public static String Reverse_Words(String s) {
18 | s = s.trim();
19 | String[] arr = s.split("\s+");
20 | String ans = "";
21 | for (int i = arr.length - 1; i >= 0; i--) {
22 | ans = ans + arr[i] + " ";
23 | }
24 |
25 | return ans.trim();
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec20/Board_Path.java:
--------------------------------------------------------------------------------
1 | package Lec20;
2 |
3 | public class Board_Path {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 4;
8 | Printans(n, 0, "");
9 |
10 | }
11 |
12 | // curr--> Currently me kahan pe hu
13 | // ending Goal --> n
14 | public static void Printans(int n, int curr, String ans) {
15 | if (curr == n) {
16 | System.out.print(ans + " ");
17 | return;
18 | }
19 | if (curr > n) {
20 | return;
21 | }
22 | for (int dice = 1; dice <= 3; dice++) {
23 | Printans(n, curr + dice, ans + dice);
24 | }
25 | // Printans(n, curr + 1, ans + 1);
26 | // Printans(n, curr + 2, ans + 2);
27 | // Printans(n, curr + 3, ans + 3);
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec54/Single_Number_III.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Single_Number_III {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 1, 2, 1, 3, 2, 5 };
10 | System.out.println(Arrays.toString(Single_Number(arr)));
11 |
12 | }
13 |
14 | public static int[] Single_Number(int[] arr) {
15 | int ans = 0;
16 | for (int i = 0; i < arr.length; i++) {
17 | ans ^= arr[i];
18 | }
19 | int mask = (ans & (~(ans - 1)));
20 | int a = 0;
21 | for (int i = 0; i < arr.length; i++) {
22 | if ((arr[i] & mask) != 0) {
23 | a ^= arr[i];
24 | }
25 | }
26 | int b = ans ^ a;
27 | return new int[] { a, b };
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec10/Insertion_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec10;
2 |
3 | public class Insertion_Sort {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 1, 8, 2, 7, 9, 11, -8 };
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 | for (int i = 1; i < arr.length; i++) {
17 | InsertLastElement(arr, i);
18 | }
19 | }
20 |
21 | public static void InsertLastElement(int[] arr, int i) {
22 |
23 | int item = arr[i];
24 | int j = i - 1;
25 | while (j >= 0 && arr[j] > item) {
26 | arr[j + 1] = arr[j];
27 | j--;
28 | }
29 | arr[j + 1] = item;
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/Lec28/Person.java:
--------------------------------------------------------------------------------
1 | package Lec28;
2 |
3 | public class Person {
4 | private String name = "Kaju";
5 | private int age = 17;
6 |
7 | public Person() {
8 | // TODO Auto-generated constructor stub
9 | }
10 |
11 | public Person(String name, int age) {
12 | this.name = name;
13 | this.age = age;
14 |
15 | }
16 |
17 | public String getName() {
18 | return name;
19 | }
20 |
21 | public void setName(String name) {
22 | this.name = name;
23 | }
24 |
25 | public int getAge() {
26 | return age;
27 | }
28 |
29 | public void setAge(int age) {
30 | this.age = age;
31 | }
32 |
33 | // public void setAge(int age) {
34 | // this.age = age;
35 | // }
36 | //
37 | // public int getAge() {
38 | // return age;
39 | // }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/Lec22/Queen_Cimbination.java:
--------------------------------------------------------------------------------
1 | package Lec22;
2 |
3 | public class Queen_Cimbination {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 4;
8 | boolean[] board = new boolean[n];
9 | int tq = 2;
10 | Cimbination(board, tq, "", 0,0);
11 |
12 | }
13 |
14 | public static void Cimbination(boolean[] board, int tq, String ans, int qpsf, int idx) {
15 | if (qpsf == tq) {
16 | System.out.println(ans);
17 | return;
18 | }
19 | for (int i = idx; i < board.length; i++) {
20 | if (board[i] == false) {
21 | board[i] = true;
22 | Cimbination(board, tq, ans + "b" + i + "q" + qpsf, qpsf + 1, i + 1);
23 | board[i] = false;// UNDO
24 | }
25 |
26 | }
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Lec15/String_Equals.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class String_Equals {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s2 = "hello";
8 | String s3 = new String("hello");
9 | // System.out.println(s2 == s3);
10 | // System.out.println(s2.charAt(1));
11 | // System.out.println(s2.equals(s3));
12 | System.out.println(eqauls(s2, s3));
13 | }
14 | public static boolean eqauls(String s1, String s2) {
15 | if (s1 == s2) {
16 | return true;
17 | }
18 | if (s1.length() != s2.length()) {
19 | return false;
20 | }
21 | for (int i = 0; i < s1.length(); i++) {
22 | if (s1.charAt(i) != s2.charAt(i)) {
23 | return false;
24 | }
25 | }
26 | return true;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec17/Subarray_Product_Less_Than_K.java:
--------------------------------------------------------------------------------
1 | package Lec17;
2 |
3 | public class Subarray_Product_Less_Than_K {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 1, 2, 3, 4, 2 };
8 | int k = 10;
9 | System.out.println(Product_Less_Than_K(arr, k));
10 |
11 | }
12 |
13 | public static int Product_Less_Than_K(int[] arr, int k) {
14 | int si = 0;
15 | int ei = 0;
16 | int p = 1;
17 | int ans = 0;
18 | while (ei < arr.length) {
19 | // growing
20 | p = p * arr[ei];
21 | // shrinking
22 | while (p >= k && si <= ei) {
23 | p = p / arr[si];
24 | si++;
25 | }
26 | // cal
27 | ans = ans + (ei - si + 1);
28 | ei++;
29 | }
30 | return ans;
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Lec2/Pattern9.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | import java.util.*;
4 |
5 | public class Pattern9 {
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 space = n - 1;
13 | int star = 1;
14 | while (row <= n) {
15 | // space
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" ");
19 | i++;
20 | }
21 |
22 | // star
23 | int j = 1;
24 | while (j <= star) {
25 | System.out.print("* ");
26 | j++;
27 | }
28 |
29 | // next row Prep
30 | row++;
31 | System.out.println();
32 | space--;
33 | star += 2;// star = star + 2;
34 |
35 | }
36 |
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Lec46/Graph_Client.java:
--------------------------------------------------------------------------------
1 | package Lec46;
2 |
3 | import java.util.*;
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 | g.AddEdge(1, 4, 6);
11 | g.AddEdge(1, 2, 10);
12 | g.AddEdge(2, 3, 7);
13 | g.AddEdge(3, 4, 5);
14 | g.AddEdge(4, 5, 1);
15 | g.AddEdge(5, 6, 4);
16 | g.AddEdge(7, 5, 2);
17 | g.AddEdge(6, 7, 3);
18 | g.display();
19 | // System.out.println("****************");
20 | // g.removevertex(4);
21 | // g.display();
22 | System.out.println(g.haspath(1, 6, new HashSet<>()));
23 | g.PrintAllPath(1, 6, new HashSet<>(), "");
24 | System.out.println(g.BFS(1, 6));
25 | System.out.println(g.DFS(1, 6));
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/Lec54/Print_SubSeqnunce_Using_BitMasking.java:
--------------------------------------------------------------------------------
1 | package Lec54;
2 |
3 | public class Print_SubSeqnunce_Using_BitMasking {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String str = "abc";
8 | SubSeqnunce(str);
9 |
10 | }
11 |
12 | public static void SubSeqnunce(String str) {
13 | int n = str.length();
14 | for (int i = 0; i < (1 << n); i++) {
15 | Print(str, i);
16 |
17 | }
18 | }
19 |
20 | private static void Print(String str, int i) {
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 | pos++;
28 | i >>= 1;
29 | }
30 | System.out.println();
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Lec14/Transpose.java:
--------------------------------------------------------------------------------
1 | package Lec14;
2 |
3 | public class Transpose {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[][] arr = { { 1, 2, 4, 5 }, { 7, 8, 9, 6 }, { 3, 11, 15, 16 }, { 10, 12, 13, 14 } };
8 | transpose(arr);
9 | // display
10 | for (int i = 0; i < arr.length; i++) {
11 | for (int j = 0; j < arr.length; j++) {
12 | System.out.print(arr[i][j] + " ");
13 | }
14 | System.out.println();
15 | }
16 | }
17 |
18 | public static void transpose(int[][] arr) {
19 |
20 | for (int i = 0; i < arr.length; i++) {
21 | for (int j = i + 1; j < arr[0].length; j++) {
22 | int temp = arr[i][j];
23 | arr[i][j] = arr[j][i];
24 | arr[j][i] = temp;
25 | }
26 | }
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Lec15/String_LexcoGraphic_Order.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | public class String_LexcoGraphic_Order {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s1 = "raj";
8 | String s2 = "rajesh";
9 | // +ve s1>s2
10 | // -ve s2>s1
11 | // 0 s1==s2
12 | // System.out.println(s1.compareTo(s2));
13 | System.out.println(compareTo(s1, s2));
14 |
15 | }
16 | public static int compareTo(String s1, String s2) {
17 | if (s1 == s2) {
18 | return 0;
19 | }
20 | int len = Math.min(s1.length(), s2.length());
21 | for (int i = 0; i < len; i++) {
22 | if (s1.charAt(i) != s2.charAt(i)) {
23 | return s1.charAt(i) - s2.charAt(i);
24 | }
25 | }
26 | return s1.length() - s2.length();
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Lec32/Next_Greater_Element.java:
--------------------------------------------------------------------------------
1 | package Lec32;
2 |
3 | import java.util.*;
4 |
5 | public class Next_Greater_Element {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 11, 13, 21, 3 };
10 | NGE(arr);
11 |
12 | }
13 |
14 | public static void NGE(int[] arr) {
15 | Stack st = new Stack<>();
16 | int[] ans = new int[arr.length];
17 | for (int i = 0; i < arr.length; i++) {
18 | while (!st.isEmpty() && arr[i] > arr[st.peek()]) {
19 | ans[st.pop()] = arr[i];
20 | }
21 | st.push(i);
22 |
23 | }
24 | while (!st.isEmpty()) {
25 | ans[st.pop()] = -1;
26 | }
27 | for (int i = 0; i < ans.length; i++) {
28 | System.out.println(arr[i] + "," + ans[i]);
29 | }
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec39/LCA.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | public class LCA {
4 |
5 | public class TreeNode {
6 | int val;
7 | TreeNode left;
8 | TreeNode right;
9 |
10 | TreeNode(int x) {
11 | val = x;
12 | }
13 | }
14 |
15 | class Solution {
16 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
17 | if (root == null) {
18 | return null;
19 | }
20 | if (root == p || root == q) {
21 | return root;
22 | }
23 | TreeNode left = lowestCommonAncestor(root.left, p, q);
24 | TreeNode right = lowestCommonAncestor(root.right, p, q);
25 | if (right != null && left != null) {
26 | return root;
27 | } else if (left == null) {
28 | return right;
29 | } else {
30 | return left;
31 | }
32 | }
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Lec9/ReverSal_Algo.java:
--------------------------------------------------------------------------------
1 | package Lec9;
2 |
3 | public class ReverSal_Algo {
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 | int k = 3;
9 | RotateArray(arr, k);
10 | for (int i = 0; i < arr.length; i++) {
11 | System.out.print(arr[i] + " ");
12 | }
13 |
14 | }
15 |
16 | public static void RotateArray(int[] arr, int k) {
17 | int n = arr.length;
18 | k = k % n;
19 | Revese(arr, 0, n - 1);
20 | Revese(arr, 0, k - 1);
21 | Revese(arr, k, n - 1);
22 | }
23 |
24 | public static void Revese(int[] arr, int i, int j) {
25 |
26 | while (i < j) {
27 | int temp = arr[i];
28 | arr[i] = arr[j];
29 | arr[j] = temp;
30 | i++;
31 | j--;
32 | }
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Lec34/LinkedList_Client.java:
--------------------------------------------------------------------------------
1 | package Lec34;
2 |
3 | public class LinkedList_Client {
4 |
5 | public static void main(String[] args) throws Exception {
6 | // TODO Auto-generated method stub
7 | LibkedList ll = new LibkedList();
8 | ll.AddFirst(10);
9 | ll.AddFirst(20);
10 | ll.AddFirst(30);
11 | ll.AddLast(40);
12 | ll.AddatIndex(-20, 2);
13 | ll.AddLast(42);
14 | ll.AddLast(67);
15 | ll.AddLast(90);
16 | ll.Display();
17 | // System.out.println(ll.getfirst());
18 | // System.out.println(ll.getlast());
19 | // System.out.println(ll.getindex(2));
20 | System.out.println(ll.removefirst());
21 | ll.Display();
22 | System.out.println(ll.removelast());
23 | ll.Display();
24 | System.out.println(ll.removeatindex(2));
25 | ll.Display();
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Lec43/Minimum_Sum_Pair.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | import java.util.PriorityQueue;
4 | import java.util.Scanner;
5 |
6 | public class Minimum_Sum_Pair {
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 | Scanner sc = new Scanner(System.in);
11 | int n = sc.nextInt();
12 | int[] arr = new int[n];
13 | for (int i = 0; i < arr.length; i++) {
14 | arr[i] = sc.nextInt();
15 | }
16 | PriorityQueue pq = new PriorityQueue<>();// min heap
17 | for (int i = 0; i < arr.length; i++) {
18 | pq.add(arr[i]);
19 | }
20 | int sum = 0;
21 | while (pq.size() > 1) {
22 | int a = pq.poll();
23 | int b = pq.poll();
24 | sum += a + b;
25 | pq.add(a + b);
26 | }
27 | System.out.println(sum);
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec10/Selection_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec10;
2 |
3 | public class Selection_Sort {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 4, 5, 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 | public static void Sort(int[] arr) {
15 | for (int i = 0; i < arr.length; i++) {
16 | int idx = minidx(arr, i);
17 | int temp = arr[i];
18 | arr[i] = arr[idx];
19 | arr[idx] = temp;
20 | }
21 |
22 | }
23 |
24 | public static int minidx(int[] arr, int i) {
25 | int mini = i;
26 | for (int j = i + 1; j < arr.length; j++) {
27 | if (arr[j] < arr[mini]) {
28 | mini = j;
29 | }
30 | }
31 | return mini;
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Lec51/Uncrossed_Lines.java:
--------------------------------------------------------------------------------
1 | package Lec51;
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(lcsBU(arr1, arr2));
10 | }
11 |
12 | public static int lcsBU(int[] s1, int[] s2) {
13 | int[][] dp = new int[s1.length + 1][s2.length + 1];
14 | for (int i = 1; i < dp.length; i++) {
15 | for (int j = 1; j < dp[0].length; j++) {
16 | if (s1[i - 1] == s2[j - 1]) {
17 | dp[i][j] = 1 + dp[i - 1][j - 1];
18 | } else {
19 | int f = dp[i - 1][j];
20 | int s = dp[i][j - 1];
21 | dp[i][j] = Math.max(f, s);
22 | }
23 | }
24 | }
25 | return dp[dp.length - 1][dp[0].length - 1];
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Lec32/Stock_Span.java:
--------------------------------------------------------------------------------
1 | package Lec32;
2 |
3 | import java.util.*;
4 |
5 | public class Stock_Span {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 30, 35, 40, 38, 35 };
10 | Cal_Span(arr);
11 | }
12 |
13 | public static void Cal_Span(int[] arr) {
14 | Stack st = new Stack<>();
15 | int[] ans = new int[arr.length];
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 + 1;
22 | } else {
23 | ans[i] = i - st.peek();
24 | }
25 | st.push(i);
26 |
27 | }
28 | for (int i = 0; i < ans.length; i++) {
29 | System.out.println(arr[i] + "," + ans[i]);
30 | }
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Lec42/Cars.java:
--------------------------------------------------------------------------------
1 | package Lec42;
2 |
3 | public class Cars{// implements Comparable {
4 | int price;
5 | int speed;
6 | String color;
7 |
8 | public Cars() {
9 | // TODO Auto-generated constructor stub
10 | }
11 |
12 | public Cars(int price, int speed, String color) {
13 | // TODO Auto-generated constructor stub
14 | this.price = price;
15 | this.speed = speed;
16 | this.color = color;
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return "P : " + this.price + " S: " + this.speed + " C:" + this.color;
22 |
23 | }
24 |
25 | // @Override
26 | // public int compareTo(Cars o) {
27 | // // TODO Auto-generated method stub
28 | // // return this.price - o.price;
29 | //// return o.speed - this.speed;
30 | // return this.color.compareTo(o.color);
31 | // }
32 | }
33 |
--------------------------------------------------------------------------------
/Lec3/Pattern14.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern14 {
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 <= 2 * n - 1) {
15 | // space
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" ");
19 | i++;
20 | }
21 | // star
22 | int j = 1;
23 | while (j <= star) {
24 | System.out.print("* ");
25 | j++;
26 | }
27 | // mirror
28 | if (row < n) {
29 | star++;
30 | space--;
31 | } else {
32 | star--;
33 | space++;
34 | }
35 | System.out.println();
36 | row++;
37 | }
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/Lec3/Pattern25.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern25 {
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 space = n - 1;
13 | int star = 1;
14 | int val = 1;
15 | while (row <= n) {
16 | // space
17 | int i = 1;
18 | while (i <= space) {
19 | System.out.print(" \t");
20 | i++;
21 | }
22 |
23 | // star
24 | int j = 1;
25 | while (j <= star) {
26 | System.out.print(val + "\t");
27 | val++;
28 | j++;
29 | }
30 |
31 | // next row Prep
32 | row++;
33 | System.out.println();
34 | space--;
35 | star += 2;// star = star + 2;
36 |
37 | }
38 |
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/Lec3/Pattern16.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern16 {
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 = n - 1;
14 | while (row <= 2 * n - 1) {
15 | // space
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" ");
19 | i++;
20 | }
21 | // star
22 | int j = 1;
23 | while (j <= star) {
24 | System.out.print("* ");
25 | j++;
26 | }
27 | // mirror
28 | if (row < n) {
29 | space--;
30 | star--;
31 | } else {
32 | star++;
33 | space++;
34 | }
35 | row++;
36 | System.out.println();
37 |
38 | }
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/Lec1/Increment_Decrement.java:
--------------------------------------------------------------------------------
1 | package Lec1;
2 |
3 | public class Increment_Decrement {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int a = 6;
8 | // System.out.println(a++);// post Increment
9 | // System.out.println(a);
10 | // int b=11;
11 | // System.out.println(b--);// post Decrement
12 | // System.out.println(b);
13 |
14 | // int x = a++ + 6 + a-- - a-- + a++ - a++;//12
15 | // int x = a-- + 7 + a-- + 8 + a++;
16 | // System.out.println(x);
17 | // int b = 10;
18 | // System.out.println(++b);//Per Increment
19 | // System.out.println(b);
20 | //
21 | // int c = 19;
22 | // System.out.println(--c);// Pre Decrement
23 | // System.out.println(c);
24 | int x = a++ + --a - ++a + 11 + a++ - a-- + ++a;
25 | System.out.println(x);
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Lec50/Longest_Increasing_Subsequence.java:
--------------------------------------------------------------------------------
1 | package Lec50;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Longest_Increasing_Subsequence {
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, 6 };
10 | System.out.println(LIS(arr));
11 |
12 | }
13 |
14 | public static int LIS(int[] arr) {
15 | int[] lis = new int[arr.length];
16 | Arrays.fill(lis, 1);
17 | for (int i = 1; i < lis.length; i++) {
18 | for (int j = i - 1; j >=0; j--) {
19 | if (arr[j] < arr[i]) {
20 | int l = lis[j];
21 | lis[i] = Math.max(lis[i], l + 1);
22 | }
23 | }
24 | }
25 | int max = lis[0];
26 | for (int i = 1; i < lis.length; i++) {
27 | max = Math.max(max, lis[i]);
28 | }
29 | return max;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/Lec40/Insert_into_a_Binary_Search_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec40;
2 |
3 | public class Insert_into_a_Binary_Search_Tree {
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 insertIntoBST(TreeNode root, int val) {
25 | if (root == null) {
26 | return new TreeNode(val);
27 | }
28 | if (root.val < val) {
29 | root.right = insertIntoBST(root.right, val);
30 | } else {
31 | root.left = insertIntoBST(root.left, val);
32 | }
33 | return root;
34 |
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Lec14/User_Input_2_D.java:
--------------------------------------------------------------------------------
1 | package Lec14;
2 |
3 | import java.util.Scanner;
4 |
5 | public class User_Input_2_D {
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();// row
11 | int m = sc.nextInt();// col
12 | int[][] arr = new int[n][m];
13 | for (int i = 0; i < arr.length; i++) {
14 | for (int j = 0; j < arr[0].length; j++) {
15 | arr[i][j] = sc.nextInt();
16 | }
17 | }
18 | Display(arr);
19 | }
20 |
21 | public static void Display(int[][] arr) {
22 | // TODO Auto-generated method stub
23 | for (int i = 0; i < arr.length; i++) {
24 | for (int j = 0; j < arr[0].length; j++) {
25 | System.out.print(arr[i][j] + " ");
26 | }
27 | System.out.println();
28 | }
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec52/Valentine_Magic.java:
--------------------------------------------------------------------------------
1 | package Lec52;
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(ValentineMagic(boys, girls, 0, 0));
14 |
15 | }
16 |
17 | public static int ValentineMagic(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 |
25 | int pair = Math.abs(boys[i] - girls[j]) + ValentineMagic(boys, girls, i + 1, j + 1);
26 | int no_pair = ValentineMagic(boys, girls, i, j + 1);
27 | return Math.min(pair, no_pair);
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec27/Sieve_of_Eratosthenes.java:
--------------------------------------------------------------------------------
1 | package Lec27;
2 |
3 | public class Sieve_of_Eratosthenes {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 100;
8 | PrimeSieve(n);
9 |
10 | }
11 |
12 | // index--> false hoga usko prime index
13 | // index--> true hoga usko not prime index
14 | public static void PrimeSieve(int n) {
15 | boolean[] prime = new boolean[n + 1];
16 | prime[0] = true;// not prime
17 | prime[1] = true;// not prime
18 | for (int i = 2; i * i <= n; i++) {
19 | if (prime[i] == false) {
20 | for (int j = 2; i * j < prime.length; j++) {
21 | prime[i * j] = true;
22 | }
23 | }
24 |
25 | }
26 |
27 | for (int i = 2; i < prime.length; i++) {
28 | if (prime[i] == false) {
29 | System.out.print(i + " ");
30 | }
31 | }
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Lec31/Sum_Of_Number.java:
--------------------------------------------------------------------------------
1 | package Lec31;
2 |
3 | public class Sum_Of_Number {
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, 3, 11));
9 | System.out.println(Add(2, 3, 8.9));
10 | System.out.println(Add(2, 3, 4, 4, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5));
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;
20 | }
21 |
22 | public static int Add(int a, int b, double c) {
23 | return (int) (a + b + c);
24 | }
25 |
26 | public static int Add(int x, int... preksha) {
27 | int sum = 0;
28 | for (int i = 0; i < preksha.length; i++) {
29 | sum += preksha[i];
30 | }
31 | return sum;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Lec2/Pattern12.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern12 {
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 space = n - 1;
13 | int star = 1;
14 | while (row <= n) {
15 | // space
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" ");
19 | i++;
20 | }
21 |
22 | // star
23 | int j = 1;
24 | while (j <= star) {
25 | if (j % 2 != 0) {
26 | System.out.print("* ");
27 | } else {
28 | System.out.print("! ");
29 | }
30 | j++;
31 | }
32 |
33 | // next row Prep
34 | row++;
35 | System.out.println();
36 | space--;
37 | star += 2;// star = star + 2;
38 |
39 | }
40 |
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Lec21/KeyPaid.java:
--------------------------------------------------------------------------------
1 | package Lec21;
2 |
3 | import java.util.*;
4 |
5 | public class KeyPaid {
6 | static String[] map = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 | String ques = "23";
11 | List ll = new ArrayList<>();
12 | Combinations(ques, "", ll);
13 | System.out.println(ll);
14 | }
15 |
16 | public static void Combinations(String ques, String ans, List ll) {
17 | if (ques.length() == 0) {
18 | // System.out.print(ans + " ");
19 | ll.add(ans);
20 | return;
21 | }
22 | char ch = ques.charAt(0);
23 | int num = ch - '0';
24 | String press = map[num];
25 | for (int i = 0; i < press.length(); i++) {
26 | Combinations(ques.substring(1), ans + press.charAt(i), ll);
27 | }
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Lec9/Trapping_Rain_Water.java:
--------------------------------------------------------------------------------
1 | package Lec9;
2 |
3 | public class Trapping_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(Trapping(arr));
9 | }
10 |
11 | public static int Trapping(int[] arr) {
12 | int n = arr.length;
13 | int[] left = new int[n];
14 | left[0] = arr[0];
15 | for (int i = 1; i < left.length; i++) {
16 | left[i] = Math.max(left[i - 1], arr[i]);
17 | }
18 | int[] right = new int[n];
19 | right[n - 1] = arr[n - 1];
20 | for (int i = n - 2; i >= 0; i--) {
21 | right[i] = Math.max(right[i + 1], arr[i]);
22 | }
23 | int sum = 0;
24 | for (int i = 0; i < right.length; i++) {
25 | sum = sum + Math.min(left[i], right[i]) - arr[i];
26 | }
27 | return sum;
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/Lec21/Permutation_2.java:
--------------------------------------------------------------------------------
1 | package Lec21;
2 |
3 | public class Permutation_2 {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String ques = "abca";
8 | PrintPermuatation(ques, "");
9 | }
10 |
11 | public static void PrintPermuatation(String ques, String ans) {
12 | if (ques.length() == 0) {
13 | System.out.println(ans);
14 | return;
15 | }
16 |
17 | for (int i = 0; i < ques.length(); i++) {
18 | char ch = ques.charAt(i);
19 | boolean val = true;
20 | for (int j = i + 1; j < ques.length(); j++) {
21 | if (ques.charAt(j) == ch) {
22 | val = false;
23 | break;
24 |
25 | }
26 | }
27 | if (val == true) {
28 | String s1 = ques.substring(0, i);
29 | String s2 = ques.substring(i + 1);
30 | PrintPermuatation(s1 + s2, ans + ch);
31 | }
32 | }
33 |
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Lec38/Path_Sum.java:
--------------------------------------------------------------------------------
1 | package Lec38;
2 |
3 | public class Path_Sum {
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 boolean hasPathSum(TreeNode root, int targetSum) {
26 | if (root == null) {
27 | return false;
28 | }
29 | if (root.left == null && root.right == null) {
30 | return targetSum - root.val == 0;
31 | }
32 | boolean left = hasPathSum(root.left, targetSum - root.val);
33 | boolean right = hasPathSum(root.right, targetSum - root.val);
34 | return left || right;
35 |
36 | }
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Lec7/Is_Armstrong_Number.java:
--------------------------------------------------------------------------------
1 | package Lec7;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Is_Armstrong_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 | System.out.println(Is_ArmstrongNumber(n));
12 | }
13 |
14 | public static boolean Is_ArmstrongNumber(int n) {
15 | int c = countofdigit(n);
16 | int sum = 0;
17 | int p = n;
18 | while (n > 0) {
19 | int rem = n % 10;
20 | sum = (int) (sum + Math.pow(rem, c));
21 | n = n / 10;
22 |
23 | }
24 | if (sum == p) {
25 | return true;
26 | } else {
27 | return false;
28 | }
29 |
30 | }
31 |
32 | public static int countofdigit(int n) {
33 | int count = 0;
34 | while (n > 0) {
35 | count++;
36 | n = n / 10;
37 | }
38 | return count;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Lec3/Pattern27.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern27 {
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 space = n - 1;
13 | int star = 1;
14 | while (row <= n) {
15 | // space
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" ");
19 | i++;
20 | }
21 |
22 | // star
23 | int val = 1;
24 | int j = 1;
25 | while (j <= star) {
26 | System.out.print(val + " ");
27 | if (j <= star / 2) {
28 | val++;
29 | } else {
30 | val--;
31 | }
32 | j++;
33 | }
34 |
35 | // next row Prep
36 | row++;
37 | System.out.println();
38 | space--;
39 | star += 2;// star = star + 2;
40 |
41 | }
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec2/Pattern22.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern22 {
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 = -1;
14 | while (row <= n) {
15 | // star
16 | int i = 1;
17 | while (i <= star) {
18 | System.out.print("* ");
19 | i++;
20 | }
21 | // space
22 | int j = 1;
23 | while (j <= space) {
24 | System.out.print(" ");
25 | j++;
26 | }
27 | int k = 1;
28 | if (row == 1) {
29 | k = 2;
30 | }
31 | while (k <= star) {
32 | System.out.print("* ");
33 | k++;
34 | }
35 | // next Row ki Prep
36 | row++;
37 | System.out.println();
38 | star--;
39 | space += 2;
40 |
41 | }
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec33/Dynamic_Queue.java:
--------------------------------------------------------------------------------
1 | package Lec33;
2 |
3 | import Lec29.Queue;
4 |
5 | public class Dynamic_Queue extends Queue {
6 | @Override
7 | public void Enqueue(int item) throws Exception {
8 | if (isfull()) {
9 | int[] ar = new int[2 * arr.length];
10 | for (int i = 0; i < arr.length; i++) {
11 | int idx = (front + i) % arr.length;
12 | ar[i] = arr[idx];
13 |
14 | }
15 | arr = ar;
16 | front = 0;
17 | }
18 | super.Enqueue(item);
19 |
20 | }
21 |
22 | public static void main(String[] args) throws Exception {
23 | Dynamic_Queue dq = new Dynamic_Queue();
24 | dq.Enqueue(10);
25 | dq.Enqueue(20);
26 | dq.Enqueue(30);
27 | dq.Enqueue(40);
28 | System.out.println(dq.Dequeue());
29 | System.out.println(dq.Dequeue());
30 | dq.Enqueue(50);
31 | dq.Enqueue(60);
32 | dq.Enqueue(70);
33 | dq.Enqueue(80);
34 | dq.Display();
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/Lec30/Stack_Reverse.java:
--------------------------------------------------------------------------------
1 | package Lec30;
2 |
3 | import java.util.Stack;
4 |
5 | public class Stack_Reverse {
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 | Reverse(st);
17 | System.out.println(st);
18 | }
19 |
20 | private static void Reverse(Stack st) {
21 | // TODO Auto-generated method stub
22 | if (st.isEmpty()) {
23 | return;
24 | }
25 | int x = st.pop();
26 | Reverse(st);
27 | Insert(st, x);
28 |
29 | }
30 |
31 | public static void Insert(Stack st, int item) {
32 | if (st.isEmpty()) {
33 | st.push(item);
34 | return;
35 | }
36 | int x = st.pop();
37 | Insert(st, item);
38 | st.push(x);
39 |
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/Lec40/BinarySearchTree.java:
--------------------------------------------------------------------------------
1 | package Lec40;
2 |
3 | public class BinarySearchTree {
4 | class Node {
5 | int val;
6 | Node left;
7 | Node right;
8 |
9 | }
10 |
11 | private Node root;
12 |
13 | public BinarySearchTree(int[] arr) {
14 | // TODO Auto-generated constructor stub
15 | root = CreateTree(arr, 0, arr.length - 1);
16 | }
17 |
18 | private Node CreateTree(int[] arr, 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 = arr[mid];
26 | nn.left = CreateTree(arr, si, mid - 1);
27 | nn.right = CreateTree(arr, mid + 1, ei);
28 | return nn;
29 |
30 | }
31 |
32 | public static void main(String[] args) {
33 | int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80 };
34 | BinarySearchTree bst = new BinarySearchTree(arr);
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/Lec26/Partition_in_Array.java:
--------------------------------------------------------------------------------
1 | package Lec26;
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, 1, 8, 3, 4 };
8 | int idx = Partition(arr, 0, arr.length - 1);
9 | for (int i = 0; i < arr.length; i++) {
10 | System.out.print(arr[i] + " ");
11 | }
12 | System.out.println();
13 | System.out.println(idx);
14 | }
15 |
16 | public static int Partition(int[] arr, int si, int ei) {
17 | // TODO Auto-generated method stub
18 |
19 | int item = arr[ei];
20 | int idx = si;
21 | for (int i = si; i < ei; i++) {
22 | if (arr[i] < item) {
23 | int temp = arr[i];
24 | arr[i] = arr[idx];
25 | arr[idx] = temp;
26 | idx++;
27 | }
28 |
29 | }
30 | int temp = arr[ei];
31 | arr[ei] = arr[idx];
32 | arr[idx] = temp;
33 | return idx;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Lec3/Pattern17.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern17 {
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 / 2;
13 | int space = 1;
14 | while (row <= n) {
15 | // star
16 | int i = 1;
17 | while (i <= star) {
18 | System.out.print("* ");
19 | i++;
20 | }
21 | // space
22 | int j = 1;
23 | while (j <= space) {
24 | System.out.print(" ");
25 | j++;
26 | }
27 | int k = 1;
28 | while (k <= star) {
29 | System.out.print("* ");
30 | k++;
31 | }
32 | if (row <= n / 2) {
33 | star--;
34 | space += 2;
35 | } else {
36 | star++;
37 | space -= 2;
38 | }
39 | row++;
40 | System.out.println();
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Lec52/Edit_distance.java:
--------------------------------------------------------------------------------
1 | package Lec52;
2 |
3 | public class Edit_distance {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String s = "FOOD";
8 | String t = "MONEY";
9 |
10 | }
11 |
12 | // i--> s
13 | // j-->t
14 | public static int Editdistance(String s, String t, int i, int j) {
15 | if (i == s.length()) {
16 | return t.length() - j;
17 | }
18 | if (j == t.length()) {
19 | return s.length() - i;
20 | }
21 | int ans = 0;
22 | if (s.charAt(i) == t.charAt(j)) {
23 | ans = Editdistance(s, t, i + 1, j + 1);// dp[i-1][j-1]
24 | } else {
25 | int D = Editdistance(s, t, i + 1, j);// dp[i-1][j]
26 | int I = Editdistance(s, t, i, j + 1);// dp[i][j-1]
27 | int R = Editdistance(s, t, i + 1, j + 1);// dp[i-1][j-1]
28 | ans = 1 + Math.min(I, Math.min(D, R));
29 |
30 | }
31 | return ans;
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/Lec9/Product_of_Array_Except_Self.java:
--------------------------------------------------------------------------------
1 | package Lec9;
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 = { 1, 2, 3, 4 };
8 | int[] ans = Array_Except_Self(arr);
9 | for (int i = 0; i < ans.length; i++) {
10 | System.out.print(ans[i] + " ");
11 | }
12 | }
13 |
14 | public static int[] Array_Except_Self(int[] arr) {
15 | int n = arr.length;
16 | int[] left = new int[n];
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[n];
22 | right[n - 1] = 1;
23 | for (int i = n - 2; i >= 0; i--) {
24 | right[i] = right[i + 1] * arr[i + 1];
25 | }
26 | for (int i = 0; i < right.length; i++) {
27 | left[i] = left[i] * right[i];
28 | }
29 | return left;
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/Lec2/Pattern21.java:
--------------------------------------------------------------------------------
1 | package Lec2;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern21 {
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 space = 2 * n - 3;
13 | int star = 1;
14 | while (row <= n) {
15 | // star
16 | int i = 1;
17 | while (i <= star) {
18 | System.out.print("* ");
19 | i++;
20 | }
21 | // space
22 | int j = 1;
23 | while (j <= space) {
24 | System.out.print(" ");
25 | j++;
26 | }
27 | // star
28 | int k = 1;
29 | if (row == n) {
30 | k = 2;
31 | }
32 | while (k <= star) {
33 | System.out.print("* ");
34 | k++;
35 | }
36 | // next row prep
37 | row++;
38 | System.out.println();
39 | star++;
40 | space -= 2;
41 | }
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec55/Counting_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec55;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Counting_Sort {
6 | public static void main(String[] args) {
7 | int[] arr = { 2, 1, 1, 0, 1, 2, 5, 4, 0, 2, 8, 7, 9, 2, 6, 1, 9 };
8 | System.out.println(Arrays.toString(Sort(arr)));
9 |
10 | }
11 |
12 | public static int[] Sort(int[] arr) {
13 | int max = Arrays.stream(arr).max().getAsInt();
14 | int[] freq = new int[max + 1];
15 | for (int i = 0; i < arr.length; i++) {
16 | int e = arr[i];
17 | freq[e]++;// freq[e]= freq[e]+1;
18 | }
19 | for (int i = 1; i < freq.length; i++) {
20 | freq[i] = freq[i - 1] + freq[i];
21 | }
22 | int[] ans = new int[arr.length];
23 | for (int i = arr.length - 1; i >= 0; i--) {
24 | // int e = arr[i];
25 | int idx = freq[arr[i]];
26 | ans[idx - 1] = arr[i];
27 | freq[arr[i]]--;
28 |
29 | }
30 | return ans;
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Lec17/Kartik_Bhaiya_And_Strings.java:
--------------------------------------------------------------------------------
1 | package Lec17;
2 |
3 | public class Kartik_Bhaiya_And_Strings {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String str = "aabababbabaaaaaaabbaa";
8 | int k = 3;
9 | int flipa = maxlen(str, k, 'a');
10 | int flipb = maxlen(str, k, 'b');
11 | System.out.println(Math.max(flipa, flipb));
12 |
13 | }
14 |
15 | public static int maxlen(String str, int k, char ch) {
16 | int si = 0;
17 | int ei = 0;
18 | int ans = 0;
19 | int flip = 0;
20 | while (ei < str.length()) {
21 | // growing
22 | if (str.charAt(ei) == ch) {
23 | flip++;
24 | }
25 | // shrinking
26 | while (flip > k) {
27 | if (str.charAt(si) == ch) {
28 | flip--;
29 | }
30 | si++;
31 | }
32 | // ans cal
33 | ans = Math.max(ans, ei - si + 1);
34 | ei++;
35 | }
36 | return ans;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Lec23/Combination_Sum.java:
--------------------------------------------------------------------------------
1 | package Lec23;
2 |
3 | import java.util.*;
4 |
5 | public class Combination_Sum {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] coin = { 2, 3, 5 };
10 | int amount = 8;
11 | List ll = new ArrayList<>();
12 | List> ans = new ArrayList<>();
13 | Combination(coin, amount, ll, 0, ans);
14 | System.out.println(ans);
15 |
16 | }
17 |
18 | public static void Combination(int[] coin, int amount, List ll, int idx, List> ans) {
19 | if (amount == 0) {
20 | // System.out.println(ll);
21 | ans.add(new ArrayList<>(ll));
22 | return;
23 | }
24 | for (int i = idx; i < coin.length; i++) {
25 | if (amount >= coin[i]) {
26 | ll.add(coin[i]);
27 | Combination(coin, amount - coin[i], ll, i, ans);
28 | ll.remove(ll.size() - 1);
29 | }
30 | }
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Lec30/Construct_Smallest_Number_From_DI_String.java:
--------------------------------------------------------------------------------
1 | package Lec30;
2 |
3 | import java.util.Stack;
4 |
5 | public class Construct_Smallest_Number_From_DI_String {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | String s = "IIIDIDDD";
10 | System.out.println(Construct_Smallest(s));
11 |
12 | }
13 |
14 | public static String Construct_Smallest(String s) {
15 | int n = s.length();
16 | int[] ans = new int[n + 1];
17 | Stack st = new Stack<>();
18 | int c = 1;
19 | for (int i = 0; i <= n; i++) {
20 | if (i == n || s.charAt(i) == 'I') {
21 | ans[i] = c;
22 | c++;
23 | while (!st.isEmpty()) {
24 | ans[st.pop()] = c;
25 | c++;
26 |
27 | }
28 |
29 | } else {
30 | st.push(i);
31 | }
32 | }
33 | String str = "";
34 | for (int i = 0; i < ans.length; i++) {
35 | str += ans[i];
36 | }
37 | return str;
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/Lec38/Sum_Root_to_Leaf_Numbers.java:
--------------------------------------------------------------------------------
1 | package Lec38;
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 Numbers(root, 0);
26 | }
27 |
28 | public int Numbers(TreeNode root, int sum) {
29 | if (root == null) {
30 | return 0;
31 | }
32 | if (root.left == null && root.right == null) {
33 | return sum * 10 + root.val;
34 | }
35 |
36 | int l = Numbers(root.left, sum * 10 + root.val);
37 | int r = Numbers(root.right, sum * 10 + root.val);
38 | return l + r;
39 |
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Lec15/Playing_with_Good_Strings.java:
--------------------------------------------------------------------------------
1 | package Lec15;
2 |
3 | import java.util.Scanner;
4 | public class Playing_with_Good_Strings {
5 |
6 | public static void main(String[] args) {
7 | // TODO Auto-generated method stub
8 | Scanner sc = new Scanner(System.in);
9 | String s = sc.next();
10 | System.out.println(longest_Good_Strings(s));
11 |
12 | }
13 |
14 | public static int longest_Good_Strings(String s) {
15 | int ans = 0;
16 | int count = 0;
17 | for (int i = 0; i < s.length(); i++) {
18 | char ch = s.charAt(i);
19 | if (Isvowels(ch) == true) {
20 | count++;
21 | } else {
22 | ans = Math.max(ans, count);
23 | count = 0;
24 | }
25 |
26 | }
27 | ans = Math.max(ans, count);
28 | return ans;
29 | }
30 |
31 | public static boolean Isvowels(char ch) {
32 | if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
33 | return true;
34 | }
35 | return false;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Lec3/Pattern_Rhombus.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern_Rhombus {
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 space = n - 1;
13 | int star = 1;
14 | int val = 1;
15 | while (row <= 2 * n - 1) {
16 | int i = 1;
17 | while (i <= space) {
18 | System.out.print(" \t");
19 | i++;
20 | }
21 | int j = 1;
22 | int p = val;
23 | while (j <= star) {
24 | System.out.print(p + "\t");
25 | if (j <= star / 2) {
26 | p++;
27 | } else {
28 | p--;
29 | }
30 | j++;
31 | }
32 | if (row < n) {
33 | star += 2;
34 | space--;
35 | val++;
36 | } else {
37 | star -= 2;
38 | space++;
39 | val--;
40 | }
41 | row++;
42 | System.out.println();
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Lec7/Odd_and_Even_back_in_Delhi.java:
--------------------------------------------------------------------------------
1 | package Lec7;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Odd_and_Even_back_in_Delhi {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Scanner sc = new Scanner(System.in);
10 | int t = sc.nextInt();// 2
11 | while (t > 0) {
12 | int carno = sc.nextInt();
13 | if (Even_back_in_Delhi(carno) == true) {
14 | System.out.println("Yes");
15 | } else {
16 | System.out.println("No");
17 | }
18 | t--;
19 |
20 | }
21 | }
22 |
23 | public static boolean Even_back_in_Delhi(int n) {
24 | int sumeven = 0;
25 | int sumodd = 0;
26 | while (n > 0) {
27 | int rem = n % 10;
28 | if (rem % 2 == 0) {
29 | sumeven += rem;
30 | } else {
31 | sumodd += rem;
32 | }
33 | n = n / 10;
34 | }
35 | if (sumeven % 4 == 0 || sumodd % 3 == 0) {
36 | return true;
37 | } else {
38 | return false;
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/Lec3/Pattern19.java:
--------------------------------------------------------------------------------
1 | package Lec3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Pattern19 {
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 / 2 + 1;
13 | int space = -1;
14 | while (row <= n) {
15 | // star
16 | int i = 1;
17 | while (i <= star) {
18 | System.out.print("* ");
19 | i++;
20 | }
21 | // space
22 | int j = 1;
23 | while (j <= space) {
24 | System.out.print(" ");
25 | j++;
26 | }
27 | // star
28 | int k = 1;
29 | if (row == 1 || row == n) {
30 | k = 2;
31 | }
32 | while (k <= star) {
33 | System.out.print("* ");
34 | k++;
35 | }
36 | if (row <= n / 2) {
37 | star--;
38 | space += 2;
39 | } else {
40 | star++;
41 | space -= 2;
42 | }
43 | row++;
44 | System.out.println();
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Lec35/Reverse_Linked_List.java:
--------------------------------------------------------------------------------
1 | package Lec35;
2 |
3 | public class Reverse_Linked_List {
4 | public class ListNode {
5 | int val;
6 | ListNode next;
7 |
8 | ListNode() {
9 | }
10 |
11 | ListNode(int val) {
12 | this.val = val;
13 | }
14 |
15 | ListNode(int val, ListNode next) {
16 | this.val = val;
17 | this.next = next;
18 | }
19 | }
20 |
21 | class Solution {
22 | public ListNode reverseList(ListNode head) {
23 | // return reverse(head, null);
24 | ListNode curr = head;
25 | ListNode prev = null;
26 | while (curr != null) {
27 | ListNode ahead = curr.next;
28 | curr.next = prev;
29 | prev = curr;
30 | curr = ahead;
31 | }
32 | return prev;
33 |
34 | }
35 |
36 | public ListNode reverse(ListNode curr, ListNode prev) {
37 | if (curr == null) {
38 | return prev;
39 | }
40 | ListNode temp = reverse(curr.next, curr);
41 | curr.next = prev;
42 | return temp;
43 |
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/Lec41/InterFace_Client.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class InterFace_Client implements DSAI {
4 |
5 | @Override
6 | public void push(int item) {
7 | // TODO Auto-generated method stub
8 |
9 | }
10 |
11 | @Override
12 | public void fun() {
13 | // TODO Auto-generated method stub
14 |
15 | }
16 |
17 | @Override
18 | public int pop() {
19 | // TODO Auto-generated method stub
20 | return 0;
21 | }
22 |
23 | @Override
24 | public int peek() {
25 | // TODO Auto-generated method stub
26 | return 0;
27 | }
28 |
29 | @Override
30 | public void Display() {
31 | // TODO Auto-generated method stub
32 |
33 | }
34 |
35 | @Override
36 | public int size() {
37 | // TODO Auto-generated method stub
38 | return 0;
39 | }
40 |
41 | @Override
42 | public boolean isEmpty() {
43 | // TODO Auto-generated method stub
44 | return false;
45 | }
46 |
47 | public static void main(String[] args) {
48 | InterFace_Client ic = new InterFace_Client();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Lec38/Flip_Equivalen.java:
--------------------------------------------------------------------------------
1 | package Lec38;
2 |
3 | public class Flip_Equivalen {
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 flipEquiv(TreeNode root1, TreeNode root2) {
25 | if (root1 == null && root2 == null) {
26 | return true;
27 | }
28 | if (root1 == null || root2 == null) {
29 | return false;
30 | }
31 | if (root1.val != root2.val) {
32 | return false;
33 | }
34 |
35 | boolean flip = flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left);
36 | boolean noflip = flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right);
37 | return flip || noflip;
38 | }
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/Lec26/Merge_Two_Sorted_Array.java:
--------------------------------------------------------------------------------
1 | package Lec26;
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 = { 2, 3, 4, 5, 7, 8, 11 };
8 | int[] arr2 = { 1, 2, 5, 6, 9, 11, 13, 14 };
9 | int[] ans = Merge(arr1, arr2);
10 | for (int i = 0; i < ans.length; i++) {
11 | System.out.print(ans[i] + " ");
12 | }
13 | }
14 |
15 | public static int[] Merge(int[] arr1, int[] arr2) {
16 | int n = arr1.length;
17 | int m = arr2.length;
18 | int[] ans = new int[n + m];
19 | int i = 0, j = 0, k = 0;
20 | while (i < n && j < m) {
21 | if (arr1[i] < arr2[j]) {
22 | ans[k] = arr1[i];
23 | i++;
24 | k++;
25 | } else {
26 | ans[k] = arr2[j];
27 | j++;
28 | k++;
29 | }
30 | }
31 | while (i < n) {
32 | ans[k] = arr1[i];
33 | i++;
34 | k++;
35 | }
36 | while (j < m) {
37 | ans[k] = arr2[j];
38 | j++;
39 | k++;
40 | }
41 | return ans;
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec26/Axis_Orbit.java:
--------------------------------------------------------------------------------
1 | package Lec26;
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 | // TODO Auto-generated method stub
14 | int odd = 0;
15 | // odd
16 | for (int axis = 0; axis < s.length(); axis++) {
17 | for (int orbit = 0; axis - orbit >= 0 && axis + orbit < s.length(); orbit++) {
18 | if (s.charAt(axis - orbit) != s.charAt(axis + orbit)) {
19 | break;
20 | }
21 | odd++;
22 | }
23 | }
24 | int even = 0;
25 | // even
26 | for (double axis = 0.5; axis < s.length(); axis++) {
27 | for (double orbit = 0.5; axis - orbit >= 0 && axis + orbit < s.length(); orbit++) {
28 | if (s.charAt((int) (axis - orbit)) != s.charAt((int) (axis + orbit))) {
29 | break;
30 | }
31 | even++;
32 | }
33 | }
34 | return odd + even;
35 |
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Lec36/Merge_Two_Sorted_Lists.java:
--------------------------------------------------------------------------------
1 | package Lec36;
2 |
3 | public class Merge_Two_Sorted_Lists {
4 | public class ListNode {
5 | int val;
6 | ListNode next;
7 |
8 | ListNode() {
9 | }
10 |
11 | ListNode(int val) {
12 | this.val = val;
13 | }
14 |
15 | ListNode(int val, ListNode next) {
16 | this.val = val;
17 | this.next = next;
18 | }
19 | }
20 |
21 | class Solution {
22 | public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
23 | ListNode Dummy = new ListNode();
24 | ListNode temp = Dummy;
25 | while (list1 != null && list2 != null) {
26 | if (list1.val < list2.val) {
27 | Dummy.next = list1;
28 | list1 = list1.next;
29 | Dummy = Dummy.next;
30 | } else {
31 | Dummy.next = list2;
32 | list2 = list2.next;
33 | Dummy = Dummy.next;
34 | }
35 | }
36 | if (list1 == null) {
37 | Dummy.next = list2;
38 | }
39 | if (list2 == null) {
40 | Dummy.next = list1;
41 | }
42 | return temp.next;
43 |
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Doub_Class_Array/Sorting_In_Linear_Time.java:
--------------------------------------------------------------------------------
1 | package Doub_Class_Array;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Sorting_In_Linear_Time {
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 | Sort(arr);
16 | for (int i = 0; i < arr.length; i++) {
17 | System.out.println(arr[i]);
18 | }
19 | }
20 |
21 | public static void Sort(int[] arr) {
22 | int left = 0;
23 | int right = arr.length - 1;
24 | int i = 0;
25 | while (i <= right) {
26 | if (arr[i] == 0) {
27 | int temp = arr[i];
28 | arr[i] = arr[left];
29 | arr[left] = temp;
30 | left++;
31 | i++;
32 | } else if (arr[i] == 1) {
33 | i++;
34 | } else {
35 | int temp = arr[i];
36 | arr[i] = arr[right];
37 | arr[right] = temp;
38 | right--;
39 | }
40 | }
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Lec19/SubSqunce.java:
--------------------------------------------------------------------------------
1 | package Lec19;
2 |
3 | public class SubSqunce {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String ques = "abc";
8 | //PrintSubSeq(ques, "");
9 | // System.out.println(count);
10 | System.out.println(CountSubSeqnce(ques, ""));
11 |
12 | }
13 |
14 | // static int count = 0;
15 | public static void PrintSubSeq(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 | PrintSubSeq(ques.substring(1), ans);
23 | PrintSubSeq(ques.substring(1), ans + ch);
24 |
25 | }
26 |
27 | public static int CountSubSeqnce(String ques, String ans) {
28 | if (ques.length() == 0) {
29 | System.out.println(ans);
30 | // count++;
31 | return 1;
32 | }
33 | char ch = ques.charAt(0);
34 | int a1 = CountSubSeqnce(ques.substring(1), ans);
35 | int a2 = CountSubSeqnce(ques.substring(1), ans + ch);
36 | return a1+a2;
37 |
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Lec26/Quick_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec26;
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, 1, 8, 3, 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 idx = Partition(arr, si, ei);
20 | Sort(arr, si, idx - 1);
21 | Sort(arr, idx + 1, ei);
22 |
23 | }
24 |
25 | public static int Partition(int[] arr, int si, int ei) {
26 | // TODO Auto-generated method stub
27 |
28 | int item = arr[ei];
29 | int idx = si;
30 | for (int i = si; i < ei; i++) {
31 | if (arr[i] < item) {
32 | int temp = arr[i];
33 | arr[i] = arr[idx];
34 | arr[idx] = temp;
35 | idx++;
36 | }
37 |
38 | }
39 | int temp = arr[ei];
40 | arr[ei] = arr[idx];
41 | arr[idx] = temp;
42 | return idx;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec31/Client.java:
--------------------------------------------------------------------------------
1 | package Lec31;
2 |
3 | public class Client {
4 | public static void main(String[] args) {
5 | // // Case-1
6 | // P obj = new P();
7 | // System.out.println(obj.d);
8 | // System.out.println(obj.d2);
9 | // obj.fun();
10 | // obj.fun2();
11 | // Case-2
12 | // P obj = new C();
13 | // System.out.println(obj.d);// 2
14 | // System.out.println(obj.d2);// 20
15 | // System.out.println(((C) (obj)).d1);
16 | // System.out.println(((C) (obj)).d);
17 | //
18 | // obj.fun();
19 | // obj.fun2();
20 | // ((C) (obj)).fun1();
21 |
22 | // Case-3
23 | // C obj = new P();
24 | // System.out.println(obj.d);
25 | // System.out.println(obj.d1);
26 | // System.out.println(obj.d2);
27 |
28 | // Case-4
29 | C obj = new C();
30 | System.out.println(obj.d);//1
31 | System.out.println(obj.d2);// 20
32 | System.out.println(obj.d1);//10
33 | System.out.println(((P) (obj)).d);
34 | obj.fun();
35 | obj.fun1();
36 | obj.fun2();
37 | ((P) (obj)).fun();
38 |
39 |
40 |
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Lec51/Distinct_Subsequences.java:
--------------------------------------------------------------------------------
1 | package Lec51;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Distinct_Subsequences {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | String s = "rabbbit";
10 | String t = "rabbit";
11 | int[][] dp = new int[s.length() + 1][t.length()];
12 | for (int[] a : dp) {
13 | Arrays.fill(a, -1);
14 | }
15 | System.out.println(Coin_ChangeTD(s, t, 0, 0, dp));
16 | }
17 |
18 | // i-->s --> coin
19 | // j-->t --> amount
20 | public static int Coin_ChangeTD(String s, String t, int i, int j, int[][] dp) {
21 | if (j == t.length()) {// amount -->0
22 | return 1;
23 | }
24 | if (i == s.length()) {// coin empty
25 | return 0;
26 | }
27 | if (dp[i][j] != -1) {
28 | return dp[i][j];
29 | }
30 | int inc = 0, exc = 0;
31 | if (s.charAt(i) == t.charAt(j)) {
32 | inc = Coin_ChangeTD(s, t, i + 1, j + 1, dp);
33 | }
34 | exc = Coin_ChangeTD(s, t, i + 1, j, dp);
35 | return dp[i][j] = inc + exc;
36 |
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Lec38/Symmetric_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec38;
2 |
3 | public class Symmetric_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 boolean isSymmetric(TreeNode root) {
26 | return Symmetric(root.left, root.right);
27 |
28 | }
29 |
30 | public boolean Symmetric(TreeNode root1, TreeNode root2) {
31 | if (root1 == null && root2 == null) {
32 | return true;
33 | }
34 | if (root1 == null || root2 == null) {
35 | return false;
36 | }
37 | if (root1.val != root2.val) {
38 | return false;
39 | }
40 | boolean left = Symmetric(root1.left, root2.right);
41 | boolean right = Symmetric(root1.right, root2.left);
42 | return left && right;
43 |
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/Lec39/Binary_Tree_Right_Side_View.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | import java.util.*;
4 |
5 | public class Binary_Tree_Right_Side_View {
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 List rightSideView(TreeNode root) {
27 | List ll = new ArrayList<>();
28 | rightSide(root, 1, ll);
29 | return ll;
30 |
31 | }
32 |
33 | int maxd = 0;
34 |
35 | public void rightSide(TreeNode root, int cl, List ll) {
36 | if (root == null) {
37 | return;
38 | }
39 | if (maxd < cl) {
40 | ll.add(root.val);// condition
41 | maxd = cl;
42 | }
43 |
44 | rightSide(root.right, cl + 1, ll);
45 | rightSide(root.left, cl + 1, ll);
46 |
47 | }
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Lec50/Fibo.java:
--------------------------------------------------------------------------------
1 | package Lec50;
2 |
3 | public class Fibo {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int n = 5;
8 | int[] dp = new int[n + 1];
9 | System.out.println(fiboTD(n, dp));
10 | System.out.println(fiboBU(n));
11 |
12 | }
13 |
14 | public static int fibo(int n) {
15 | if (n == 0 || n == 1) {
16 | return n;
17 | }
18 | int f1 = fibo(n - 1);
19 | int f2 = fibo(n - 2);
20 | return f2 + f1;
21 | }
22 |
23 | public static int fiboTD(int n, int[] dp) {
24 | if (n == 0 || n == 1) {
25 | return n;
26 | }
27 | if (dp[n] != 0) {// dp appply kre
28 | return dp[n];
29 | }
30 | int f1 = fiboTD(n - 1, dp);// dp[n-1]
31 | int f2 = fiboTD(n - 2, dp);// dp[n-2]
32 | return dp[n] = f2 + f1;// yaad kra hai
33 | }
34 |
35 | public static int fiboBU(int n) {
36 | int[] dp = new int[n + 1];
37 | dp[0] = 0;
38 | dp[1] = 1;
39 | for (int i = 2; i < dp.length; i++) {
40 | dp[i] = dp[i - 1] + dp[i - 2];
41 | }
42 | return dp[n];
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Lec44/Longest_Consecutive_Sequence.java:
--------------------------------------------------------------------------------
1 | package Lec44;
2 |
3 | import java.util.HashMap;
4 |
5 | public class Longest_Consecutive_Sequence {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 0, 3, 7, 2, 5, 8, 4, 6, 0, 1 };
10 | System.out.println(Consecutive_Sequence(arr));
11 | }
12 |
13 | public static int Consecutive_Sequence(int[] arr) {
14 | HashMap map = new HashMap<>();
15 |
16 | for (int i = 0; i < arr.length; i++) {
17 | if (map.containsKey(arr[i] - 1)) {
18 | map.put(arr[i], false);
19 | } else {
20 | map.put(arr[i], true);
21 | }
22 | if (map.containsKey(arr[i] + 1)) {
23 | map.put(arr[i] + 1, false);
24 | }
25 |
26 | }
27 | int ans = 0;
28 | for (int key : map.keySet()) {
29 | if (map.get(key)) {
30 | int count = 0;
31 | while (map.containsKey(key)) {
32 | count++;
33 | key++;
34 | }
35 | ans = Math.max(ans, count);
36 |
37 | }
38 |
39 | }
40 | return ans;
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Lec53/Lis_Nlog_N.java:
--------------------------------------------------------------------------------
1 | package Lec53;
2 |
3 | public class Lis_Nlog_N {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 0, 8, 4, 2, 12, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };
8 | System.out.println(LIS(arr));
9 | }
10 |
11 | public static int LIS(int[] arr) {
12 | int[] lis = new int[arr.length];
13 | lis[0] = arr[0];
14 | int len = 1;
15 | for (int i = 1; i < arr.length; i++) {
16 | if (arr[i] > lis[len - 1]) {
17 | lis[len] = arr[i];
18 | len++;
19 | } else {
20 | int idx = BinarySearch(lis, 0, len - 1, arr[i]);
21 | lis[idx] = arr[i];
22 | }
23 | }
24 | return len;
25 |
26 | }
27 |
28 | private static int BinarySearch(int[] lis, int si, int ei, int item) {
29 | // TODO Auto-generated method stub
30 | int idx = 0;
31 | while (si <= ei) {
32 | int mid = (si + ei) / 2;
33 | if (lis[mid] >= item) {
34 | idx = mid;
35 | ei = mid - 1;
36 | } else {
37 | si = mid + 1;
38 | }
39 | }
40 | return idx;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Lec29/Student.java:
--------------------------------------------------------------------------------
1 | package Lec29;
2 |
3 | public class Student {
4 | private String name = "Kaju";
5 | private int age = 17;
6 |
7 | public Student(String name, int age) {
8 | // TODO Auto-generated constructor stub
9 | this.age = age;
10 | this.name = name;
11 | }
12 |
13 | public void setAge(int age) {
14 |
15 | try {
16 | if (age < 0) {
17 | throw new Exception("bklol age -ve nhi hota hai");
18 | }
19 | this.age = age;
20 | } catch (Exception e) {
21 | // TODO: handle exception
22 | e.printStackTrace();
23 |
24 | } finally {
25 | System.out.println("I am in finaly blocks");
26 |
27 | }
28 |
29 | }
30 |
31 | // public void setAge(int age) throws Exception {
32 | // if (age < 0) {
33 | // throw new Exception("bklol age -ve nhi hota hai");
34 | // }
35 | // this.age = age;
36 | //
37 | // }
38 |
39 | public String getName() {
40 | return name;
41 | }
42 |
43 | public void setName(String name) {
44 | this.name = name;
45 | }
46 |
47 | public int getAge() {
48 | return age;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Lec52/Minimum_Falling_Path_Sum_2.java:
--------------------------------------------------------------------------------
1 | package Lec52;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Minimum_Falling_Path_Sum_2 {
6 | public static void main(String[] args) {
7 | int[][] grid = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
8 | int[][] dp = new int[grid.length][grid[0].length];
9 | for (int[] a : dp) {
10 | Arrays.fill(a, -1000000);
11 | }
12 | int ans = Integer.MAX_VALUE;
13 | for (int i = 0; i < grid[0].length; i++) {
14 | ans = Math.min(ans, Falling_Path_Sum(grid, 0, i, dp));
15 | }
16 | System.out.println(ans);
17 | }
18 |
19 | public static int Falling_Path_Sum(int[][] grid, int cr, int cc, int[][] dp) {
20 | if (cr == grid.length - 1) {
21 | return grid[cr][cc];
22 | }
23 | if (dp[cr][cc] != -1000000) {
24 | return dp[cr][cc];
25 | }
26 | int ans = Integer.MAX_VALUE;
27 | for (int i = 0; i < grid[0].length; i++) {
28 | if (cc != i) {
29 | ans = Math.min(ans, Falling_Path_Sum(grid, cr + 1, i, dp));
30 | }
31 |
32 | }
33 | return dp[cr][cc]=ans + grid[cr][cc];
34 |
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Lec39/Diameter_of_Binary_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | public class Diameter_of_Binary_Tree {
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 diameterOfBinaryTree(TreeNode root) {
25 | return diameter(root);
26 | }
27 |
28 | public int diameter(TreeNode root) {
29 | if (root == null) {
30 | return 0;
31 | }
32 | int ld = diameter(root.left);
33 | int rd = diameter(root.right);
34 | int sd = ht(root.left) + ht(root.right) + 2;
35 | return Math.max(sd, Math.max(ld, rd));
36 | }
37 |
38 | public int ht(TreeNode root) {
39 | if (root == null) {
40 | return -1;
41 | }
42 | int lh = ht(root.left);
43 | int rh = ht(root.right);
44 | return Math.max(lh, rh) + 1;
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Lec39/Diameter_of_Binary_Tree_Opt.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | public class Diameter_of_Binary_Tree_Opt {
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 diameterOfBinaryTree(TreeNode root) {
25 | return diameter(root).dia;
26 | }
27 |
28 | public DiaPair diameter(TreeNode root) {
29 | if (root == null) {
30 | return new DiaPair();
31 | }
32 | DiaPair ldp = diameter(root.left);
33 | DiaPair rdp = diameter(root.right);
34 | DiaPair sdp = new DiaPair();
35 | sdp.ht = Math.max(ldp.ht, rdp.ht) + 1;
36 | int sd = ldp.ht + rdp.ht + 2;
37 | sdp.dia = Math.max(sd, Math.max(ldp.dia, rdp.dia));
38 | return sdp;
39 | }
40 |
41 | class DiaPair {
42 | int dia = 0;
43 | int ht = -1;
44 | }
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/Lec32/Histogram.java:
--------------------------------------------------------------------------------
1 | package Lec32;
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 |
14 | public static int Area(int[] arr) {
15 | int area = 0;
16 | Stack st = new Stack<>();
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 | area = Math.max(area, h * r);
23 | } else {
24 | int l = st.peek();
25 | area = Math.max(area, h * (r - l - 1));
26 | }
27 |
28 | }
29 | st.push(i);
30 |
31 | }
32 | int r = arr.length;
33 | while (!st.isEmpty()) {
34 | int h = arr[st.pop()];
35 |
36 | if (st.isEmpty()) {
37 | area = Math.max(area, h * r);
38 | } else {
39 | int l = st.peek();
40 | area = Math.max(area, h * (r - l - 1));
41 | }
42 |
43 | }
44 | return area;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/Doub_Class_Array/Circular_Array_Maxmum.java:
--------------------------------------------------------------------------------
1 | package Doub_Class_Array;
2 |
3 | public class Circular_Array_Maxmum {
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 | System.out.println(MaxmumSum(arr));
9 |
10 | }
11 |
12 | public static int MaxmumSum(int[] arr) {
13 | int linear_sum = Kadens_Algo(arr);
14 | int sum = 0;
15 | for (int i = 0; i < arr.length; i++) {
16 | sum = sum + arr[i];
17 | arr[i] = arr[i] * -1;
18 | }
19 | int mid = Kadens_Algo(arr);
20 | int Circular_sum = sum + mid;// circular sum
21 | if (Circular_sum == 0) {
22 | return linear_sum;
23 | }
24 | return Math.max(linear_sum, Circular_sum);
25 |
26 | }
27 |
28 | public static int Kadens_Algo(int[] arr) {
29 | // TODO Auto-generated method stub
30 | int ans = Integer.MIN_VALUE;
31 | int sum = 0;
32 | for (int i = 0; i < arr.length; i++) {
33 | sum += arr[i];
34 | ans = Math.max(ans, sum);
35 | if (sum < 0) {
36 | sum = 0;
37 | }
38 | }
39 | return ans;
40 |
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Lec39/Balanced_Binary_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | public class Balanced_Binary_Tree {
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 isBalanced(TreeNode root) {
25 | return isBalance(root).isbal;
26 | }
27 |
28 | public BalancePair isBalance(TreeNode root) {
29 | if (root == null) {
30 | return new BalancePair();
31 | }
32 | BalancePair lbp = isBalance(root.left);
33 | BalancePair rbp = isBalance(root.right);
34 | BalancePair sbp = new BalancePair();
35 | sbp.ht = Math.max(lbp.ht, rbp.ht) + 1;
36 | boolean sb = Math.abs(lbp.ht - rbp.ht) <= 1;
37 | sbp.isbal = lbp.isbal && rbp.isbal && sb;
38 | return sbp;
39 | }
40 |
41 | class BalancePair {
42 | boolean isbal = true;
43 | int ht = -1;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Lec14/Spiral_Print.java:
--------------------------------------------------------------------------------
1 | package Lec14;
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 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
8 | Print(arr);
9 | }
10 |
11 | public static void Print(int[][] arr) {
12 | int minr = 0;
13 | int minc = 0;
14 | int maxr = arr.length - 1;
15 | int maxc = arr[0].length - 1;
16 | int te = arr.length * arr[0].length;// total numner of element
17 | int c = 0;
18 | while (c < te) {
19 | for (int i = minc; i <= maxc && c < te; i++) {
20 | System.out.print(arr[minr][i] + " ");
21 | c++;
22 | }
23 | minr++;
24 | for (int i = minr; i <= maxr && c < te; i++) {
25 | System.out.print(arr[i][maxc] + " ");
26 | c++;
27 | }
28 | maxc--;
29 | for (int i = maxc; i >= minc && c < te; i--) {
30 | System.out.print(arr[maxr][i] + " ");
31 | c++;
32 | }
33 | maxr--;
34 | for (int i = maxr; i >= minr && c < te; i--) {
35 | System.out.print(arr[i][minc] + " ");
36 | c++;
37 | }
38 | minc++;
39 | }
40 |
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Lec48/DisJointSet.java:
--------------------------------------------------------------------------------
1 | package Lec48;
2 |
3 | import java.util.HashMap;
4 |
5 | public class DisJointSet {
6 | class Node {
7 | int rank;
8 | Node parent;
9 | int data;
10 | }
11 |
12 | private HashMap map = new HashMap<>();
13 |
14 | public void CreateSet(int v) {
15 | Node nn = new Node();
16 | nn.data = v;
17 | nn.rank = 0;
18 | nn.parent = nn;
19 | map.put(v, nn);
20 |
21 | }
22 |
23 | public int find(int v) {
24 | Node nn = map.get(v);
25 | return find(nn).data;
26 |
27 | }
28 |
29 | private Node find(Node nn) {
30 | if (nn.parent == nn) {
31 | return nn;
32 | }
33 | Node n = find(nn.parent);
34 | nn.parent = n;// path Compression
35 | return n;
36 |
37 | }
38 |
39 | public void union(int v1, int v2) {
40 | Node nn1 = map.get(v1);
41 | Node nn2 = map.get(v2);
42 | Node rn1 = find(nn1);
43 | Node rn2 = find(nn2);
44 | if (rn1.rank == rn2.rank) {
45 | rn1.parent = rn2;// rn1 ka parent rn2
46 | rn2.rank++;// rn2.rank = rn2.rank+1;
47 | } else if (rn1.rank > rn2.rank) {
48 | rn2.parent = rn1;
49 | } else {
50 | rn1.parent = rn2;
51 | }
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Lec21/Permutation.java:
--------------------------------------------------------------------------------
1 | package Lec21;
2 |
3 | public class Permutation {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | String ques = "abc";
8 | PrintPermuatation(ques, "");
9 | //System.out.println(CountPermuatation(ques, ""));
10 |
11 | }
12 |
13 | public static void PrintPermuatation(String ques, String ans) {
14 | if (ques.length() == 0) {
15 | System.out.println(ans);
16 | return;
17 | }
18 |
19 | for (int i = 0; i < ques.length(); i++) {
20 | char ch = ques.charAt(i);
21 | String s1 = ques.substring(0, i);
22 | String s2 = ques.substring(i + 1);
23 | PrintPermuatation(s1 + s2, ans + ch);
24 | }
25 |
26 | }
27 |
28 | public static int CountPermuatation(String ques, String ans) {
29 | if (ques.length() == 0) {
30 | System.out.println(ans);
31 | return 1;
32 | }
33 | int count = 0;
34 | for (int i = 0; i < ques.length(); i++) {
35 | char ch = ques.charAt(i);
36 | String s1 = ques.substring(0, i);
37 | String s2 = ques.substring(i + 1);
38 | count += CountPermuatation(s1 + s2, ans + ch);
39 | }
40 | return count;
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Lec33/Queue_Using_Stack.java:
--------------------------------------------------------------------------------
1 | package Lec33;
2 |
3 | import java.util.Stack;
4 |
5 | public class Queue_Using_Stack {
6 | private Stack st;
7 |
8 | public Queue_Using_Stack() {
9 | // TODO Auto-generated constructor stub
10 | st = new Stack<>();
11 | }
12 |
13 | public int size() {
14 | return st.size();
15 | }
16 |
17 | public boolean Isempty() {
18 | return st.isEmpty();
19 | }
20 |
21 | //O(N)
22 | public void Enqueue(int item) {
23 | Stack hlps = new Stack<>();
24 | while (!st.isEmpty()) {
25 | hlps.push(st.pop());
26 |
27 | }
28 | st.push(item);
29 | while (!hlps.isEmpty()) {
30 | st.push(hlps.pop());
31 |
32 | }
33 |
34 | }
35 |
36 | public int Dequeue() {
37 | return st.pop();
38 | }
39 |
40 | public int Getfront() {
41 | return st.peek();
42 | }
43 | public static void main(String[] args) {
44 | Queue_Using_Stack q = new Queue_Using_Stack();
45 | q.Enqueue(10);
46 | q.Enqueue(20);
47 | q.Enqueue(30);
48 | q.Enqueue(40);
49 | q.Enqueue(50);
50 | q.Enqueue(60);
51 | System.out.println(q.Dequeue());
52 | System.out.println(q.Getfront());
53 |
54 |
55 |
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/Lec20/Generate_Parentheses.java:
--------------------------------------------------------------------------------
1 | package Lec20;
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 ll = new ArrayList<>();
11 | GenerateParentheses(n, 0, 0, "", ll);
12 | System.out.println(ll);
13 | }
14 |
15 | public static void GenerateParentheses(int n, int open, int close, String ans, List ll) {
16 | if (open == n && close == n) {
17 | // System.out.print(ans + " ");
18 | ll.add(ans);
19 | return;
20 | }
21 | if (open < n) {
22 | GenerateParentheses(n, open + 1, close, ans + "(", ll);
23 | }
24 | if (close < open) {
25 | GenerateParentheses(n, open, close + 1, ans + ")", ll);
26 | }
27 | }
28 |
29 | public static void GenerateParentheses1(int n, int open, int close, String ans) {
30 | if (open == n && close == n) {
31 | System.out.print(ans + " ");
32 | return;
33 | }
34 | if (open > n || close > open) {
35 | return;
36 | }
37 | GenerateParentheses1(n, open + 1, close, ans + "(");
38 |
39 | GenerateParentheses1(n, open, close + 1, ans + ")");
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/Lec30/Celebrity_Problem.java:
--------------------------------------------------------------------------------
1 | package Lec30;
2 |
3 | import java.util.Stack;
4 |
5 | public class Celebrity_Problem {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[][] arr = { { 0, 1, 1, 1 }, { 1, 0, 1, 1 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 } };
10 | System.out.println(Celebrity(arr));
11 | }
12 |
13 | private static int Celebrity(int[][] arr) {
14 | Stack st = new Stack<>();
15 | for (int i = 0; i < arr.length; i++) {
16 | st.push(i);
17 |
18 | }
19 | while (st.size() > 1) {
20 | int a = st.pop();
21 | int b = st.pop();
22 | if (arr[a][b] == 1) {
23 | st.push(b);
24 | } else {
25 | st.push(a);
26 | }
27 | }
28 | int candidate = st.pop();
29 |
30 | for (int i = 0; i < arr.length; i++) {
31 | if (i == candidate) {
32 | continue;
33 | }
34 | if (arr[candidate][i] == 1 || arr[i][candidate] == 0) {
35 | return -1;
36 | }
37 | }
38 | // for (int i = 0; i < arr.length; i++) {
39 | // if (i == candidate) {
40 | // continue;
41 | // }
42 | // if (arr[i][candidate] == 0) {
43 | // return -1;
44 | // }
45 | // }
46 |
47 | return candidate;
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Lec44/Set_Demo.java:
--------------------------------------------------------------------------------
1 | package Lec44;
2 |
3 | import java.util.*;
4 |
5 | public class Set_Demo {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | HashSet set = new HashSet<>();
10 | set.add(20);
11 | set.add(2);
12 | set.add(201);
13 | set.add(1039);
14 | set.add(-20);
15 | set.add(5);
16 | System.out.println(set);
17 | // Contains
18 | // System.out.println(set.contains(2));
19 | // System.out.println(set.contains(25));
20 | // // remove
21 | // System.out.println(set.remove(2));
22 | // System.out.println(set);
23 | // for (int v : set) {
24 | // System.out.print(v + " ");
25 | // }
26 | TreeSet set1 = new TreeSet<>();
27 | set1.add(20);
28 | set1.add(2);
29 | set1.add(201);
30 | set1.add(1039);
31 | set1.add(-20);
32 | set1.add(5);
33 | System.out.println(set1);
34 |
35 | LinkedHashSet set2 = new LinkedHashSet<>();
36 | set2.add(20);
37 | set2.add(2);
38 | set2.add(201);
39 | set2.add(1039);
40 | set2.add(-20);
41 | set2.add(5);
42 | System.out.println(set2);
43 | HashSet> set4 = new HashSet<>();
44 | set4.add(new HashSet<>());
45 |
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Lec52/Minimum_Falling_Path_Sum.java:
--------------------------------------------------------------------------------
1 | package Lec52;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Minimum_Falling_Path_Sum {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[][] grid = { { 2, 1, 3 }, { 6, 5, 4 }, { 7, 8, 9 } };
10 | int[][] dp = new int[grid.length][grid[0].length];
11 | for (int[] a : dp) {
12 | Arrays.fill(a, -1000000);
13 | }
14 | int ans = Integer.MAX_VALUE;
15 | for (int i = 0; i < grid[0].length; i++) {
16 | ans = Math.min(ans, Falling_Path_Sum(grid, 0, i, dp));
17 | }
18 | System.out.println(ans);
19 |
20 | }
21 |
22 | public static int Falling_Path_Sum(int[][] grid, int cr, int cc, int[][] dp) {
23 | if (cc < 0 || cc >= grid[0].length) {
24 | return Integer.MAX_VALUE;
25 | }
26 | if (cr == grid.length - 1) {
27 | return grid[cr][cc];
28 | }
29 | if (dp[cr][cc] != -1000000) {
30 | return dp[cr][cc];
31 | }
32 | int dl = Falling_Path_Sum(grid, cr + 1, cc - 1, dp);
33 | int dr = Falling_Path_Sum(grid, cr + 1, cc + 1, dp);
34 | int dwon = Falling_Path_Sum(grid, cr + 1, cc, dp);
35 | return dp[cr][cc] = Math.min(dl, Math.min(dr, dwon)) + grid[cr][cc];
36 |
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Lec44/Intersection_of_Two_Arrays_II.java:
--------------------------------------------------------------------------------
1 | package Lec44;
2 |
3 | import java.util.*;
4 |
5 | public class Intersection_of_Two_Arrays_II {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr1 = { 4, 9, 5 };
10 | int[] arr2 = { 9, 4, 9, 8, 4 };
11 | int[] a = Intersection(arr1, arr2);
12 | for (int v : a) {
13 | System.out.print(v + " ");
14 | }
15 |
16 | }
17 |
18 | public static int[] Intersection(int[] arr1, int[] arr2) {
19 | HashMap map = new HashMap<>();
20 | for (int i = 0; i < arr1.length; i++) {
21 | if (map.containsKey(arr1[i])) {
22 | // int v = map.get(arr1[i]);
23 | map.put(arr1[i], map.get(arr1[i]) + 1);
24 |
25 | } else {
26 | map.put(arr1[i], 1);
27 | }
28 |
29 | }
30 | List ll = new ArrayList<>();
31 | for (int i = 0; i < arr2.length; i++) {
32 | if (map.containsKey(arr2[i]) && map.get(arr2[i]) > 0) {
33 | ll.add(arr2[i]);
34 | map.put(arr2[i], map.get(arr2[i]) - 1);
35 | }
36 |
37 | }
38 | int[] ans = new int[ll.size()];
39 | for (int i = 0; i < ans.length; i++) {
40 | ans[i] = ll.get(i);
41 | }
42 | return ans;
43 |
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Lec40/Validate_Binary_Search_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec40;
2 |
3 | public class Validate_Binary_Search_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 boolean isValidBST(TreeNode root) {
26 | return ValidBST(root).isbst;
27 | }
28 |
29 | public BstPair ValidBST(TreeNode root) {
30 | if (root == null) {
31 | return new BstPair();
32 | }
33 |
34 | BstPair lbp = ValidBST(root.left);
35 | BstPair rbp = ValidBST(root.right);
36 | BstPair sbp = new BstPair();
37 | sbp.max = Math.max(lbp.max, Math.max(rbp.max, root.val));
38 | sbp.min = Math.min(lbp.min, Math.min(rbp.min, root.val));
39 | sbp.isbst = lbp.isbst && rbp.isbst && lbp.max < root.val && rbp.min > root.val;
40 | return sbp;
41 |
42 | }
43 |
44 | class BstPair {
45 | long min = Long.MAX_VALUE;
46 | long max = Long.MIN_VALUE;
47 | boolean isbst = true;
48 | }
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Lec13/Book_Allocation.java:
--------------------------------------------------------------------------------
1 | package Lec13;
2 |
3 | public class Book_Allocation {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] page = { 10, 20, 30, 40 };
8 | int nos = 2;// number of student
9 | System.out.println(MinPages(page, nos));
10 |
11 | }
12 |
13 | public static int MinPages(int[] page, int nos) {
14 | int lo = 0;
15 | int hi = 0;
16 | for (int i = 0; i < page.length; i++) {
17 | hi += page[i];
18 | }
19 |
20 | int ans = 0;
21 | while (lo <= hi) {
22 | int mid = (lo + hi) / 2;
23 | if (isitpossible(page, nos, mid) == true) {
24 | ans = mid;
25 | hi = mid - 1;
26 |
27 | } else {
28 | lo = mid + 1;
29 | }
30 | }
31 | return ans;
32 | }
33 |
34 | public static boolean isitpossible(int[] page, int nos, int mid) {
35 | // TODO Auto-generated method stub
36 | int student = 1;
37 | int readpage = 0;
38 | for (int i = 0; i < page.length;) {
39 | if (readpage + page[i] <= mid) {
40 | readpage += page[i];
41 | i++;
42 | } else {
43 | readpage = 0;
44 | student++;
45 | }
46 | if (student > nos) {
47 | return false;
48 | }
49 |
50 | }
51 | return true;
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Lec27/Randomized_quick_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec27;
2 |
3 | import java.util.Random;
4 |
5 | public class Randomized_quick_Sort {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] arr = { 5, 7, 2, 1, 8, 3, 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 idx = Partition(arr, si, ei);
22 | Sort(arr, si, idx - 1);
23 | Sort(arr, idx + 1, ei);
24 |
25 | }
26 |
27 | public static int Partition(int[] arr, int si, int ei) {
28 | // TODO Auto-generated method stub
29 | Random rn = new Random();
30 | int p = rn.nextInt(ei - si + 1) + si;
31 | int ii = arr[p];
32 | arr[p] = arr[ei];
33 | arr[ei] = ii;
34 | int item = arr[ei];
35 | int idx = si;
36 | for (int i = si; i < ei; i++) {
37 | if (arr[i] < item) {
38 | int temp = arr[i];
39 | arr[i] = arr[idx];
40 | arr[idx] = temp;
41 | idx++;
42 | }
43 |
44 | }
45 | int temp = arr[ei];
46 | arr[ei] = arr[idx];
47 | arr[idx] = temp;
48 | return idx;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/Lec45/Group_Anagrams.java:
--------------------------------------------------------------------------------
1 | package Lec45;
2 |
3 | import java.util.*;
4 | import java.util.HashMap;
5 |
6 | public class Group_Anagrams {
7 |
8 | public static void main(String[] args) {
9 | // TODO Auto-generated method stub
10 | String[] strs = { "eat", "tea", "tan", "ate", "nat", "bat" };
11 | System.out.println(Anagrams(strs));
12 | }
13 |
14 | public static List> Anagrams(String[] strs) {
15 | HashMap> map = new HashMap<>();
16 | for (int i = 0; i < strs.length; i++) {
17 | String key = Key(strs[i]);
18 | if (!map.containsKey(key)) {
19 | map.put(key, new ArrayList<>());
20 | }
21 | map.get(key).add(strs[i]);
22 |
23 | }
24 | List> ans = new ArrayList<>();
25 | for (String key : map.keySet()) {
26 | ans.add(map.get(key));
27 | }
28 | return ans;
29 | }
30 |
31 | public static String Key(String s) {
32 | int[] freq = new int[26];
33 | for (int i = 0; i < s.length(); i++) {
34 | int idx = s.charAt(i) - 'a';
35 | freq[idx] = freq[idx] + 1;
36 | }
37 | StringBuilder sb = new StringBuilder();
38 | for (int i = 0; i < freq.length; i++) {
39 | sb.append(freq[i]+" ");
40 | }
41 | return sb.toString();
42 |
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Lec43/Merge_k_Sorted_Lists.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | import java.util.Comparator;
4 | import java.util.PriorityQueue;
5 |
6 | public class Merge_k_Sorted_Lists {
7 | public class ListNode {
8 | int val;
9 | ListNode next;
10 |
11 | ListNode() {
12 | }
13 |
14 | ListNode(int val) {
15 | this.val = val;
16 | }
17 |
18 | ListNode(int val, ListNode next) {
19 | this.val = val;
20 | this.next = next;
21 | }
22 | }
23 |
24 | class Solution {
25 | public ListNode mergeKLists(ListNode[] lists) {
26 | PriorityQueue pq = new PriorityQueue<>(new Comparator() {
27 |
28 | @Override
29 | public int compare(ListNode o1, ListNode o2) {
30 | // TODO Auto-generated method stub
31 | return o1.val - o2.val;
32 | }
33 | });
34 | for (int i = 0; i < lists.length; i++) {
35 | if (lists[i] != null) {
36 | pq.add(lists[i]);
37 | }
38 |
39 | }
40 | ListNode Dummy = new ListNode();
41 | ListNode temp = Dummy;
42 | while (!pq.isEmpty()) {
43 | ListNode rv = pq.poll();
44 | Dummy.next = rv;
45 | Dummy = Dummy.next;
46 | if (rv.next != null) {
47 | pq.add(rv.next);
48 | }
49 | }
50 |
51 | return temp.next;
52 |
53 | }
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Lec23/Palindrome_Partitioning.java:
--------------------------------------------------------------------------------
1 | package Lec23;
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 ques = "nitin";
11 | List ll = new ArrayList<>();
12 | List> ans = new ArrayList<>();
13 | Partitioning(ques, ll, ans);
14 | System.out.println(ans);
15 |
16 | }
17 |
18 | public static void Partitioning(String ques, List ll, List> ans) {
19 | if (ques.length() == 0) {
20 | // System.out.println(ll);
21 | ans.add(new ArrayList<>(ll));
22 | return;
23 | }
24 | for (int i = 1; i <= ques.length(); i++) {
25 | String s = ques.substring(0, i);
26 | if (isPalindrome(s)) {
27 | ll.add(s);
28 | Partitioning(ques.substring(i), ll, ans);
29 | ll.remove(ll.size() - 1);
30 | }
31 |
32 | }
33 |
34 | }
35 |
36 | public static boolean isPalindrome(String s) {
37 | // TODO Auto-generated method stub
38 | int i = 0;
39 | int j = s.length() - 1;
40 | while (i < j) {
41 | if (s.charAt(i) != s.charAt(j)) {
42 | return false;
43 | }
44 | i++;
45 | j--;
46 |
47 | }
48 | return true;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Lec47/Vaild_Tree.java:
--------------------------------------------------------------------------------
1 | package Lec47;
2 |
3 | import java.util.*;
4 |
5 | public class Vaild_Tree {
6 | class Solution {
7 | public boolean validTree(int n, int[][] edges) {
8 | HashMap> map = new HashMap<>();
9 | for (int i = 0; i < n; i++) {
10 | map.put(i, new ArrayList<>());
11 | }
12 | for (int i = 0; i < edges.length; i++) {
13 | int v1 = edges[i][0];
14 | int v2 = edges[i][1];
15 | map.get(v1).add(v2);
16 | map.get(v2).add(v1);
17 | }
18 | Stack s = new Stack<>();
19 | HashSet visited = new HashSet<>();
20 | int c = 0;
21 | for (int key : map.keySet()) {
22 | if (visited.contains(key)) {
23 | continue;
24 | }
25 | c++;
26 | s.push(key);
27 | while (!s.isEmpty()) {
28 | // 1. remove
29 | int rv = s.pop();
30 |
31 | // 2. Ignore if Already Visited
32 | if (visited.contains(rv)) {
33 | return false;
34 | }
35 |
36 | // 3. add visited
37 | visited.add(rv);
38 | // 5. Add unvisited nbrs
39 | for (int nbrs : map.get(rv)) {
40 | if (!visited.contains(nbrs)) {
41 | s.push(nbrs);
42 | }
43 | }
44 | }
45 |
46 | }
47 | return c == 1;
48 |
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Lec26/Merge_Sort.java:
--------------------------------------------------------------------------------
1 | package Lec26;
2 |
3 | public class Merge_Sort {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr = { 7, 5, 1, 4, 3 };
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[] fa = Sort(arr, si, mid);
23 | int[] sa = Sort(arr, mid + 1, ei);
24 | return Merge(fa, sa);
25 | }
26 |
27 | public static int[] Merge(int[] arr1, int[] arr2) {
28 | int n = arr1.length;
29 | int m = arr2.length;
30 | int[] ans = new int[n + m];
31 | int i = 0, j = 0, k = 0;
32 | while (i < n && j < m) {
33 | if (arr1[i] < arr2[j]) {
34 | ans[k] = arr1[i];
35 | i++;
36 | k++;
37 | } else {
38 | ans[k] = arr2[j];
39 | j++;
40 | k++;
41 | }
42 | }
43 | while (i < n) {
44 | ans[k] = arr1[i];
45 | i++;
46 | k++;
47 | }
48 | while (j < m) {
49 | ans[k] = arr2[j];
50 | j++;
51 | k++;
52 | }
53 | return ans;
54 |
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/Lec42/BUSYMAN.java:
--------------------------------------------------------------------------------
1 | package Lec42;
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 st = sc.nextInt();
18 | int et = sc.nextInt();
19 | arr[i] = new Pair(st, et);
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.et - o2.et;
27 | }
28 | });
29 | int activities = 1;
30 | int end = arr[0].et;
31 | for (int i = 1; i < arr.length; i++) {
32 | if (end <= arr[i].st) {
33 | end = arr[i].et;
34 | activities++;
35 | }
36 | }
37 | System.out.println(activities);
38 |
39 | }
40 |
41 | }
42 |
43 | static class Pair {
44 | int st;
45 | int et;
46 |
47 | public Pair(int st, int et) {
48 | // TODO Auto-generated constructor stub
49 | this.st = st;
50 | this.et = et;
51 | }
52 |
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/Lec40/Delete_Node_in_a_BST.java:
--------------------------------------------------------------------------------
1 | package Lec40;
2 |
3 | public class Delete_Node_in_a_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 null;
28 | }
29 | if (root.val < key) {
30 | root.right = deleteNode(root.right, key);
31 | } else if (root.val > key) {
32 | root.left = deleteNode(root.left, key);
33 | } else {
34 | // 1 or 0 child
35 | if (root.left == null) {
36 | return root.right;
37 | } else if (root.right == null) {
38 | return root.left;
39 | } else {
40 | int min = min(root.right);
41 | root.right = deleteNode(root.right, min);
42 | root.val = min;
43 | }
44 | }
45 | return root;
46 |
47 | }
48 |
49 | public int min(TreeNode root) {
50 | if (root == null) {
51 | return Integer.MAX_VALUE;
52 | }
53 | int l = min(root.left);
54 | return Math.min(l, root.val);
55 | }
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/Doub_Class_Array/Maximum_Sum_Path.java:
--------------------------------------------------------------------------------
1 | package Doub_Class_Array;
2 |
3 | public class Maximum_Sum_Path {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[] arr1 = { 2, 3, 7, 10, 12, 15, 30, 34 };
8 | int[] arr2 = { 1, 5, 7, 8, 10, 15, 16, 19 };
9 | System.out.println(Maximum_Sum(arr1, arr2));
10 |
11 | }
12 |
13 | public static int Maximum_Sum(int[] arr1, int[] arr2) {
14 | int i = 0;// arr1
15 | int s1 = 0;// arr1 ka start
16 | int j = 0;// arr2
17 | int s2 = 0;// arr2 ka start
18 | int ans = 0;
19 | while (i < arr1.length && j < arr2.length) {
20 | if (arr1[i] < arr2[j]) {
21 | i++;
22 | } else if (arr1[i] > arr2[j]) {
23 | j++;
24 | } else {
25 | int sum1 = 0;
26 | for (int k = s1; k <= i; k++) {
27 | sum1 += arr1[k];
28 | }
29 | int sum2 = 0;
30 | for (int k = s2; k <= j; k++) {
31 | sum2 += arr2[k];
32 | }
33 | ans = ans + Math.max(sum2, sum1);
34 | i++;
35 | j++;
36 | s1 = i;
37 | s2 = j;
38 | }
39 |
40 | }
41 | int sum1 = 0;
42 | for (int k = s1; k < arr1.length; k++) {
43 | sum1 += arr1[k];
44 | }
45 | int sum2 = 0;
46 |
47 | for (int k = s2; k < arr2.length; k++) {
48 | sum2 += arr2[k];
49 | }
50 | ans = ans + Math.max(sum2, sum1);
51 | return ans;
52 |
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/Lec24/Word_Search.java:
--------------------------------------------------------------------------------
1 | package Lec24;
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 | for (int i = 0; i < board.length; i++) {
10 | for (int j = 0; j < board[0].length; j++) {
11 | if (board[i][j] == word.charAt(0)) {
12 | boolean ans = Search(board, i, j, word, 0);
13 | if (ans == true) {
14 | System.out.println(ans);
15 | return;
16 | }
17 |
18 | }
19 |
20 | }
21 | }
22 | System.out.println(false);
23 | }
24 |
25 | public static boolean Search(char[][] board, int cr, int cc, String word, int idx) {
26 | // TODO Auto-generated method stub
27 | if (idx == word.length()) {
28 | return true;
29 | }
30 | if (cc < 0 || cr < 0 || cc >= board[0].length || cr >= board.length || word.charAt(idx) != board[cr][cc]) {
31 | return false;
32 | }
33 | board[cr][cc] = '*';
34 | int[] r = { -1, 0, 1, 0 };
35 | int[] c = { 0, -1, 0, 1 };
36 | for (int k = 0; k < c.length; k++) {
37 | boolean ans = Search(board, cr + r[k], cc + c[k], word, idx + 1);
38 | if (ans == true) {
39 | return true;
40 | }
41 | }
42 | board[cr][cc] = word.charAt(idx);
43 | return false;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Lec29/Stack.java:
--------------------------------------------------------------------------------
1 | package Lec29;
2 |
3 | public class Stack {
4 | protected int[] arr;
5 | private int idx = -1;
6 |
7 | public Stack() {
8 | // TODO Auto-generated constructor stub
9 | arr = new int[5];
10 | }
11 |
12 | public Stack(int n) {
13 | // TODO Auto-generated constructor stub
14 | arr = new int[n];
15 | }
16 |
17 | // O(1)
18 | public boolean Isempty() {
19 | return idx == -1;
20 | }
21 |
22 | // O(1)
23 | public void push(int item) throws Exception {
24 | if(isfull()) {
25 | throw new Exception("Bklol stack full hai ");
26 | }
27 | idx++;
28 | arr[idx] = item;
29 | }
30 |
31 | // O(1)
32 | public int size() {
33 | return idx + 1;
34 | }
35 |
36 | // O(1)
37 | public boolean isfull() {
38 | return idx == arr.length - 1;
39 | }
40 |
41 | // O(1)
42 | public int pop() throws Exception {
43 | if(Isempty()) {
44 | throw new Exception("Bklol stack Empty hai ");
45 | }
46 | int rv = arr[idx];
47 | idx--;
48 | return rv;
49 | }
50 |
51 | // O(1)
52 | public int peek() throws Exception {
53 | if(Isempty()) {
54 | throw new Exception("Bklol stack Empty hai ");
55 | }
56 | int rv = arr[idx];
57 | return rv;
58 | }
59 |
60 | public void Display() {
61 | for (int i = 0; i <= idx; i++) {
62 | System.out.print(arr[i] + " ");
63 | }
64 | System.out.println();
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/Lec41/Construct_Binary_Tree_from_Preorder_and_Inorder.java:
--------------------------------------------------------------------------------
1 | package Lec41;
2 |
3 | public class Construct_Binary_Tree_from_Preorder_and_Inorder {
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 buildTree(int[] preorder, int[] inorder) {
25 | return Cereate(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
26 |
27 | }
28 |
29 | public TreeNode Cereate(int[] pre, int plo, int phi, int[] in, int ilo, int ihi) {
30 | if (ilo > ihi || plo > phi) {
31 | return null;
32 | }
33 | TreeNode node = new TreeNode(pre[plo]);
34 | int idx = Search(in, ilo, ihi, pre[plo]);// 5
35 | int ne = idx - ilo;
36 | node.left = Cereate(pre, plo + 1, plo + ne, in, ilo, idx - 1);
37 | node.right = Cereate(pre, plo + ne + 1, phi, in, idx + 1, ihi);
38 |
39 | return node;
40 |
41 | }
42 |
43 | public int Search(int[] in, int si, int ei, int item) {
44 | for (int i = si; i <= ei; i++) {
45 | if (in[i] == item) {
46 | return i;
47 | }
48 | }
49 | return 0;
50 |
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Lec53/Wine_Problem.java:
--------------------------------------------------------------------------------
1 | package Lec53;
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 | int[][] dp = new int[wine.length][wine.length];
9 | System.out.println(maximum_profit(wine, 0, wine.length - 1, 1, dp));
10 | System.out.println(maximum_profitBU(wine));
11 | }
12 |
13 | public static int maximum_profit(int[] wine, int i, int j, int year, int[][] dp) {
14 | if (i > j) {
15 | return 0;
16 | }
17 | if (dp[i][j] != 0) {
18 | return dp[i][j];
19 | }
20 | int left = wine[i] * year + maximum_profit(wine, i + 1, j, year + 1, dp);
21 | int right = wine[j] * year + maximum_profit(wine, i, j - 1, year + 1, dp);
22 | return dp[i][j] = Math.max(left, right);
23 | }
24 |
25 | public static int maximum_profitBU(int[] wine) {
26 | int[][] dp = new int[wine.length][wine.length];
27 | for (int i = 0; i < dp.length; i++) {
28 | dp[i][i] = wine[i] * wine.length;
29 | }
30 | int year = wine.length - 1;
31 | for (int d = 1; d < wine.length; d++) {
32 | for (int j = d; j < dp.length; j++) {
33 | int i = j - d;
34 | int left = wine[i] * year + dp[i + 1][j];
35 | int right = wine[j] * year + dp[i][j - 1];
36 | dp[i][j] = Math.max(left, right);
37 | }
38 | year--;
39 |
40 | }
41 |
42 | return dp[0][dp[0].length - 1];
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Lec55/Tries_Contacts.java:
--------------------------------------------------------------------------------
1 | package Lec55;
2 |
3 | import java.util.HashMap;
4 |
5 | public class Tries_Contacts {
6 |
7 | class Node {
8 | char data;
9 | boolean isTerminal;
10 | HashMap child = new HashMap<>();
11 | int count = 1;
12 | }
13 |
14 | private Node root;
15 |
16 | public Tries_Contacts() {
17 | // TODO Auto-generated constructor stub
18 | Node nn = new Node();
19 | nn.data = '*';
20 | root = nn;
21 | }
22 |
23 | public void insert(String word) {
24 | Node curr = root;
25 | for (int i = 0; i < word.length(); i++) {
26 | char ch = word.charAt(i);
27 | if (curr.child.containsKey(ch)) {
28 | curr = curr.child.get(ch);
29 | curr.count++;
30 |
31 | } else {
32 | Node n = new Node();
33 | n.data = ch;
34 | curr.child.put(ch, n);
35 | curr = n;
36 | }
37 | }
38 | curr.isTerminal = true;
39 |
40 | }
41 |
42 | public int find(String word) {
43 | Node curr = root;
44 | for (int i = 0; i < word.length(); i++) {
45 | char ch = word.charAt(i);
46 | if (curr.child.containsKey(ch)) {
47 | curr = curr.child.get(ch);
48 |
49 | } else {
50 | return 0;
51 | }
52 | }
53 | return curr.count;
54 | }
55 |
56 | public static void main(String[] args) {
57 | Tries_Contacts t = new Tries_Contacts();
58 | t.insert("hacker");
59 | t.insert("hackerrrr");
60 | System.out.println(t.find("hac"));
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/Lec47/Is_Bipartite.java:
--------------------------------------------------------------------------------
1 | package Lec47;
2 |
3 | import java.util.HashMap;
4 | import java.util.LinkedList;
5 | import java.util.Queue;
6 |
7 | public class Is_Bipartite {
8 | class Solution {
9 | public boolean isBipartite(int[][] graph) {
10 | Queue q = new LinkedList<>();
11 | HashMap visited = new HashMap<>();
12 | for (int key = 0; key < graph.length; key++) {
13 | if (visited.containsKey(key)) {
14 | continue;
15 | }
16 | q.add(new BipartitePair(key, 0));
17 | while (!q.isEmpty()) {
18 | // 1. Remove
19 | BipartitePair rp = q.poll();
20 | // 2. Ignore if Already Visited
21 | if (visited.containsKey(rp.vtx)) {
22 | if (visited.get(rp.vtx) != rp.dis) {
23 | return false;
24 | }
25 | continue;
26 |
27 | }
28 | // 3. Marked visisted
29 | visited.put(rp.vtx, rp.dis);
30 | // 4. Self Work (Nothing here)
31 | // 5. Add unvisited nbrs
32 | for (int nbrs : graph[rp.vtx]) {
33 | if (!visited.containsKey(nbrs)) {
34 | q.add(new BipartitePair(nbrs, rp.dis + 1));
35 | }
36 | }
37 |
38 | }
39 | }
40 | return true;
41 |
42 | }
43 |
44 | class BipartitePair {
45 | int vtx;
46 | int dis;
47 |
48 | public BipartitePair(int vtx, int dis) {
49 | // TODO Auto-generated constructor stub
50 | this.vtx = vtx;
51 | this.dis = dis;
52 | }
53 | }
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Lec33/Stack_Using_Queue.java:
--------------------------------------------------------------------------------
1 | package Lec33;
2 |
3 | public class Stack_Using_Queue {
4 | private Dynamic_Queue Q;
5 |
6 | public Stack_Using_Queue() {
7 | // TODO Auto-generated constructor stub
8 | Q = new Dynamic_Queue();
9 | }
10 |
11 | public int size() {
12 | return Q.size();
13 | }
14 |
15 | public boolean Isempty() {
16 | return Q.Isempty();
17 | }
18 |
19 | //O(1)
20 | public void push(int item) throws Exception {
21 | Q.Enqueue(item);
22 | }
23 |
24 | //O(N)
25 | public int pop() throws Exception {
26 | Dynamic_Queue hlpQ = new Dynamic_Queue();
27 | while (Q.size() > 1) {
28 | hlpQ.Enqueue(Q.Dequeue());
29 | }
30 | int rv = Q.Dequeue();
31 | while (hlpQ.size() > 0) {
32 | Q.Enqueue(hlpQ.Dequeue());
33 | }
34 | return rv;
35 |
36 | }
37 |
38 | public int peek() throws Exception {
39 | Dynamic_Queue hlpQ = new Dynamic_Queue();
40 | while (Q.size() > 1) {
41 | hlpQ.Enqueue(Q.Dequeue());
42 | }
43 | int rv = Q.Dequeue();
44 | hlpQ.Enqueue(rv);
45 | while (hlpQ.size() > 0) {
46 | Q.Enqueue(hlpQ.Dequeue());
47 | }
48 | return rv;
49 |
50 | }
51 |
52 | public static void main(String[] args) throws Exception {
53 | Stack_Using_Queue st = new Stack_Using_Queue();
54 | st.push(10);
55 | st.push(20);
56 | st.push(30);
57 | st.push(40);
58 | st.push(50);
59 | st.push(60);
60 | System.out.println(st.pop());
61 | System.out.println(st.peek());
62 |
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/Lec29/Queue.java:
--------------------------------------------------------------------------------
1 | package Lec29;
2 |
3 | public class Queue {
4 | protected int[] arr;
5 | protected int front = 0;
6 | private 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 void Enqueue(int item) throws Exception {
23 | if (isfull()) {
24 | throw new Exception("bklol Queue full hai");
25 | }
26 | int idx = (front + size) % arr.length;
27 | arr[idx] = item;
28 | size++;
29 |
30 | }
31 |
32 | public int Dequeue() throws Exception {
33 | if (Isempty()) {
34 | throw new Exception("bklol Queue empty hai");
35 | }
36 | int rv = arr[front];
37 | front = (front + 1) % arr.length;
38 | size--;
39 | return rv;
40 |
41 | }
42 |
43 | public int getfront() throws Exception {
44 | if (Isempty()) {
45 | throw new Exception("bklol Queue empty hai");
46 | }
47 |
48 | int rv = arr[front];
49 | return rv;
50 | }
51 |
52 | public boolean isfull() {
53 | return size == arr.length;
54 | }
55 |
56 | public int size() {
57 | return size;
58 | }
59 |
60 | public void Display() {
61 | for (int i = 0; i < size; i++) {
62 | int idx = (front + i) % arr.length;
63 | System.out.print(arr[idx] + " ");
64 |
65 | }
66 | System.out.println();
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Lec55/Trie.java:
--------------------------------------------------------------------------------
1 | package Lec55;
2 |
3 | import java.util.HashMap;
4 |
5 | public class Trie {
6 |
7 | class Node {
8 | char data;
9 | boolean isTerminal;
10 | HashMap child = new HashMap<>();
11 | }
12 |
13 | private Node root;
14 |
15 | public Trie() {
16 | // TODO Auto-generated constructor stub
17 | Node nn = new Node();
18 | nn.data = '*';
19 | root = nn;
20 | }
21 |
22 | public void insert(String word) {
23 | Node curr = root;
24 | for (int i = 0; i < word.length(); i++) {
25 | char ch = word.charAt(i);
26 | if (curr.child.containsKey(ch)) {
27 | curr = curr.child.get(ch);
28 |
29 | } else {
30 | Node n = new Node();
31 | n.data = ch;
32 | curr.child.put(ch, n);
33 | curr = n;
34 | }
35 | }
36 | curr.isTerminal = true;
37 |
38 | }
39 |
40 | public boolean search(String word) {
41 | Node curr = root;
42 | for (int i = 0; i < word.length(); i++) {
43 | char ch = word.charAt(i);
44 | if (curr.child.containsKey(ch)) {
45 | curr = curr.child.get(ch);
46 |
47 | } else {
48 | return false;
49 | }
50 | }
51 | return curr.isTerminal;
52 |
53 | }
54 |
55 | public boolean startsWith(String prefix) {
56 | Node curr = root;
57 | for (int i = 0; i < prefix.length(); i++) {
58 | char ch = prefix.charAt(i);
59 | if (curr.child.containsKey(ch)) {
60 | curr = curr.child.get(ch);
61 |
62 | } else {
63 | return false;
64 | }
65 | }
66 | return true;
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Lec43/Heap.java:
--------------------------------------------------------------------------------
1 | package Lec43;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class Heap {
6 | private ArrayList ll = new ArrayList<>();
7 |
8 | public void Add(int item) {
9 | ll.add(item);
10 | upheapify(ll.size() - 1);
11 |
12 | }
13 |
14 | private void upheapify(int ci) {
15 | // TODO Auto-generated method stub
16 | int pi = (ci - 1) / 2;
17 | if (ll.get(pi) > ll.get(ci)) {
18 | Swap(pi, ci);
19 | upheapify(pi);
20 |
21 | }
22 | }
23 |
24 | private void Swap(int pi, int ci) {
25 | // TODO Auto-generated method stub
26 | int pith = ll.get(pi);
27 | int cith = ll.get(ci);
28 | ll.set(pi, cith);
29 | ll.set(ci, pith);
30 |
31 | }
32 |
33 | public int remove() {
34 | Swap(0, ll.size() - 1);
35 | int rv = ll.remove(ll.size() - 1);
36 | downheapify(0);
37 | return rv;
38 |
39 | }
40 |
41 | private void downheapify(int pi) {
42 | // TODO Auto-generated method stub
43 | int lci = 2 * pi + 1;
44 | int rci = 2 * pi + 2;
45 | int mini = pi;
46 | if (lci < ll.size() && ll.get(lci) < ll.get(mini)) {
47 | mini = lci;
48 |
49 | }
50 | if (rci < ll.size() && ll.get(rci) < ll.get(mini)) {
51 | mini = rci;
52 |
53 | }
54 | if (mini != pi) {
55 | Swap(pi, mini);
56 | downheapify(mini);
57 | }
58 |
59 | }
60 |
61 | public int get() {
62 | return ll.get(0);
63 | }
64 |
65 | public int size() {
66 | return ll.size();
67 | }
68 |
69 | public void Display() {
70 | System.out.println(ll);
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/Lec13/Aggressive_cows.java:
--------------------------------------------------------------------------------
1 | package Lec13;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Aggressive_cows {
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 n = sc.nextInt();// number of stall
14 | int c = sc.nextInt();// number of cows
15 | int[] stall = new int[n];
16 | for (int i = 0; i < stall.length; i++) {
17 | stall[i] = sc.nextInt();// stall ki position wali input
18 | }
19 | Arrays.sort(stall);// sort kr diya Array ko
20 | System.out.println(largest_mini_dis(stall, c));
21 | t--;
22 | }
23 |
24 | }
25 |
26 | public static int largest_mini_dis(int[] stall, int c) {
27 | int lo = 0;
28 | int hi = stall[stall.length - 1] - stall[0];
29 | int ans = 0;
30 | while (lo <= hi) {
31 | int mid = (lo + hi) / 2;
32 | if (isitpossible(stall, c, mid) == true) {
33 | ans = mid;
34 | lo = mid + 1;
35 | } else {
36 | hi = mid - 1;
37 | }
38 |
39 | }
40 | return ans;
41 | }
42 |
43 | public static boolean isitpossible(int[] stall, int c, int mid) {
44 | // TODO Auto-generated method stub
45 | int cow = 1;
46 | int pos = stall[0];
47 | for (int i = 1; i < stall.length; i++) {
48 | if (stall[i] - pos >= mid) {
49 | cow++;
50 | pos = stall[i];
51 | }
52 | if (cow == c) {
53 | return true;
54 | }
55 | }
56 |
57 | return false;
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/Lec42/Cras_Client.java:
--------------------------------------------------------------------------------
1 | package Lec42;
2 |
3 | import java.util.*;
4 |
5 | public class Cras_Client {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | // int[] arr = new int[5];
10 | Cars[] ar = new Cars[5];
11 | ar[0] = new Cars(200, 10, "White");
12 | ar[1] = new Cars(1000, 20, "Black");
13 | ar[2] = new Cars(345, 3, "Yellow");
14 | ar[3] = new Cars(34, 89, "Grey");
15 | ar[4] = new Cars(8907, 6, "Red");
16 | // Sort(ar);
17 | // Arrays.sort(ar, new Comparator() {
18 | //
19 | // @Override
20 | // public int compare(Cars o1, Cars o2) {
21 | // // TODO Auto-generated method stub
22 | // return o1.price - o2.price;
23 | // }
24 | //
25 | // });
26 | Arrays.sort(ar, new Comparator() {
27 |
28 | @Override
29 | public int compare(Cars o1, Cars o2) {
30 | // TODO Auto-generated method stub
31 | return o2.speed - o1.speed;
32 | }
33 |
34 | });
35 |
36 | Display(ar);
37 |
38 | }
39 |
40 | public static void Display(Cars[] ar) {
41 | for (int i = 0; i < ar.length; i++) {
42 | System.out.println(ar[i] + " ");
43 | }
44 | }
45 |
46 | public static > void Sort(T[] arr) {
47 |
48 | // pass
49 | for (int turn = 1; turn < arr.length; turn++) {
50 |
51 | for (int i = 0; i < arr.length - turn; i++) {// 6-1
52 | if (arr[i].compareTo(arr[i + 1]) > 0) {
53 | T t = arr[i];
54 | arr[i] = arr[i + 1];
55 | arr[i + 1] = t;
56 | }
57 |
58 | }
59 |
60 | }
61 |
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/Doub_Class_Array/ArrayList_Demo.java:
--------------------------------------------------------------------------------
1 | package Doub_Class_Array;
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<>();
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 | list.add(1, -9);// pehle index phir element and index ka range 0 to szie
17 | System.out.println(list);
18 |
19 | // get
20 | System.out.println(list.get(3));// index ka 0 to szie-1
21 |
22 | // remove
23 | System.out.println(list.remove(2));// index ka 0 to szie-1
24 | System.out.println(list);
25 | list.add(201);
26 | list.add(3);
27 | list.add(21);
28 | list.add(33);
29 | System.out.println(list);
30 | // upadate
31 | list.set(4, -11);
32 | System.out.println(list);
33 | // size
34 | System.out.println(list.size());
35 | // sort
36 | Collections.sort(list);
37 | System.out.println(list);
38 | ArrayList ll = new ArrayList<>();
39 | ll.add("Ankit");
40 | ll.add("Raj");
41 | ll.add("Pooja");
42 | ll.add("Puneet");
43 | ll.add("Ankita");
44 | ll.add("Nora");
45 | ll.add("Krishana");
46 | System.out.println(ll);
47 | for (int i = 0; i < ll.size(); i++) {
48 | System.out.print(ll.get(i) + " ");
49 |
50 | }
51 | System.out.println();
52 | for (int i = 0; i < list.size(); i++) {
53 | System.out.print(list.get(i) + " ");
54 |
55 | }
56 | System.out.println();
57 | for (int val : list) {
58 | System.out.print(val + " ");
59 | }
60 |
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/Lec16/Finding_Cb_Numebr.java:
--------------------------------------------------------------------------------
1 | package Lec16;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Finding_Cb_Numebr {
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 | String str = sc.next();
12 | Substring(str);
13 | }
14 |
15 | public static void Substring(String str) {
16 | int count = 0;
17 | boolean[] visited = new boolean[str.length()];
18 | for (int len = 1; len <= str.length(); len++) {
19 | for (int j = len; j <= str.length(); j++) {
20 | int i = j - len;
21 | String s = str.substring(i, j);// string number
22 | if (IscbNumber(Long.parseLong(s)) == true && Isvisited(visited, i, j - 1) == true) {
23 | count++;
24 | for (int k = i; k < j; k++) {
25 | visited[k] = true;// marked
26 | }
27 | }
28 |
29 | }
30 |
31 | }
32 | System.out.println(count);
33 |
34 | }
35 |
36 | public static boolean Isvisited(boolean[] visited, int i, int j) {
37 | // TODO Auto-generated method stub
38 | for (int k = i; k <= j; k++) {
39 | if (visited[k] == true) {
40 | return false;
41 | }
42 | }
43 | return true;
44 | }
45 |
46 | public static boolean IscbNumber(long num) {
47 | if (num == 0 || num == 1) {
48 | return false;
49 | }
50 | int[] arr = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
51 | for (int i = 0; i < arr.length; i++) {
52 | if (num == arr[i]) {
53 | return true;
54 | }
55 | }
56 | for (int i = 0; i < arr.length; i++) {
57 | if (num % arr[i] == 0) {
58 | return false;
59 | }
60 | }
61 | return true;
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Lec36/Sort.java:
--------------------------------------------------------------------------------
1 | package Lec36;
2 |
3 | import Lec35.Middle_of_LinkedList.ListNode;
4 |
5 | public class Sort {
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 | class Solution {
24 | public ListNode sortList(ListNode head) {
25 | if (head == null || head.next == null) {
26 | return head;
27 | }
28 | ListNode mid = middleNode(head);
29 | ListNode midnext = mid.next;
30 | mid.next = null;
31 | ListNode A = sortList(head);
32 | ListNode B = sortList(midnext);
33 | return mergeTwoLists(A, B);
34 |
35 | }
36 |
37 | public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
38 | ListNode Dummy = new ListNode();
39 | ListNode temp = Dummy;
40 | while (list1 != null && list2 != null) {
41 | if (list1.val < list2.val) {
42 | Dummy.next = list1;
43 | list1 = list1.next;
44 | Dummy = Dummy.next;
45 | } else {
46 | Dummy.next = list2;
47 | list2 = list2.next;
48 | Dummy = Dummy.next;
49 | }
50 | }
51 | if (list1 == null) {
52 | Dummy.next = list2;
53 | }
54 | if (list2 == null) {
55 | Dummy.next = list1;
56 | }
57 | return temp.next;
58 |
59 | }
60 |
61 | public ListNode middleNode(ListNode head) {
62 |
63 | ListNode slow = head;
64 | ListNode fast = head;
65 | while (fast.next != null && fast.next.next != null) {
66 | slow = slow.next;
67 | fast = fast.next.next;
68 | }
69 | return slow;
70 |
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Lec25/N_Queen.java:
--------------------------------------------------------------------------------
1 | package Lec25;
2 |
3 | public class N_Queen {
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 | QueenProblem(board, n, 0);
10 |
11 | }
12 |
13 | public static void QueenProblem(boolean[][] board, int tq, int row) {
14 | if (tq == 0) {
15 | Display(board);
16 | System.out.println();
17 | return;
18 | }
19 | for (int col = 0; col < board[0].length; col++) {
20 | if (Isitsafe(board, row, col) == true) {
21 | board[row][col] = true;
22 | QueenProblem(board, tq - 1, row + 1);
23 | board[row][col] = false;
24 |
25 | }
26 | }
27 | }
28 |
29 | public static boolean Isitsafe(boolean[][] board, int row, int col) {
30 | // TODO Auto-generated method stub
31 | // up
32 | int r = row;
33 | while (r >= 0) {
34 | if (board[r][col] == true) {
35 | return false;
36 | }
37 | r--;
38 |
39 | }
40 | // left diagonal
41 | r = row;
42 | int c = col;
43 | while (r >= 0 && c >= 0) {
44 | if (board[r][c] == true) {
45 | return false;
46 | }
47 | r--;
48 | c--;
49 | }
50 | // right diagonal
51 | r = row;
52 | c = col;
53 | while (r >= 0 && c < board[0].length) {
54 | if (board[r][c] == true) {
55 | return false;
56 | }
57 | r--;
58 | c++;
59 | }
60 | return true;
61 | }
62 |
63 | public static void Display(boolean[][] board) {
64 | // TODO Auto-generated method stub
65 | for (int i = 0; i < board.length; i++) {
66 | for (int j = 0; j < board[0].length; j++) {
67 | System.out.print(board[i][j] + " ");
68 | }
69 | System.out.println();
70 | }
71 |
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/Lec24/Rat_Chases_its_cheese.java:
--------------------------------------------------------------------------------
1 | package Lec24;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Rat_Chases_its_cheese {
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 m = sc.nextInt();
12 | char[][] maze = new char[n][m];
13 | for (int i = 0; i < maze.length; i++) {
14 | String s = sc.next();
15 | for (int j = 0; j < s.length(); j++) {
16 | maze[i][j] = s.charAt(j);
17 | }
18 | }
19 | int[][] ans = new int[n][m];
20 | MazePath(maze, 0, 0, ans);
21 | if (val == false) {
22 | System.out.println("NO PATH FOUND");
23 | }
24 |
25 | }
26 |
27 | static boolean val = false;
28 |
29 | public static void MazePath(char[][] maze, int cr, int cc, int[][] ans) {
30 |
31 | if (cc < 0 || cr < 0 || cc >= maze[0].length || cr >= maze.length || maze[cr][cc] == 'X') {
32 | return;
33 | }
34 | if (cr == maze.length - 1 && cc == maze[0].length - 1) {
35 | ans[cr][cc] = 1;
36 | val = true;
37 | Display(ans);
38 | }
39 | maze[cr][cc] = 'X';
40 | ans[cr][cc] = 1;
41 | int[] r = { -1, 0, 1, 0 };
42 | int[] c = { 0, -1, 0, 1 };
43 | for (int i = 0; i < c.length; i++) {
44 | MazePath(maze, cr + r[i], cc + c[i], ans);
45 | }
46 | // MazePath(maze, cr - 1, cc, ans);// up
47 | // MazePath(maze, cr, cc - 1, ans);// left
48 | // MazePath(maze, cr + 1, cc, ans);// down
49 | // MazePath(maze, cr, cc + 1, ans);// right
50 |
51 | maze[cr][cc] = 'O';
52 | ans[cr][cc] = 0;
53 |
54 | }
55 |
56 | public static void Display(int[][] ans) {
57 | // TODO Auto-generated method stub
58 | for (int i = 0; i < ans.length; i++) {
59 | for (int j = 0; j < ans[0].length; j++) {
60 | System.out.print(ans[i][j] + " ");
61 | }
62 | System.out.println();
63 | }
64 |
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/Lec50/House_Robber.java:
--------------------------------------------------------------------------------
1 | package Lec50;
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 | // for (int i = 0; i < dp.length; i++) {
13 | // dp[i] = -1;
14 | // }
15 | System.out.println(Robber(arr, 0));
16 | // System.out.println(RobberTD(arr, 0, dp));
17 | System.out.println(RobberTD2(arr, arr.length - 1, dp));
18 | }
19 |
20 | public static int RobberBU(int[] arr) {
21 | if (arr.length == 1) {
22 | return arr[0];
23 | }
24 | int[] dp = new int[arr.length];
25 | dp[0] = arr[0];
26 | dp[1] = Math.max(arr[0], arr[1]);
27 | for (int i = 2; i < dp.length; i++) {
28 | int rob = arr[i] + dp[i - 2];
29 | int Dont_rob = dp[i - 1];
30 | dp[i] = Math.max(rob, Dont_rob);
31 | }
32 | return dp[dp.length - 1];
33 | }
34 |
35 | public static int RobberTD2(int[] arr, int i, int[] dp) {
36 | if (i < 0) {
37 | return 0;
38 | }
39 | if (dp[i] != -1) {
40 | return dp[i];
41 | }
42 | int rob = arr[i] + RobberTD2(arr, i - 2, dp);
43 | int Dont_rob = RobberTD2(arr, i - 1, dp);
44 | return dp[i] = Math.max(rob, Dont_rob);
45 | }
46 |
47 | public static int RobberTD(int[] arr, int i, int[] dp) {
48 | if (i >= arr.length) {
49 | return 0;
50 | }
51 | if (dp[i] != -1) {
52 | return dp[i];
53 | }
54 | int rob = arr[i] + RobberTD(arr, i + 2, dp);
55 | int Dont_rob = RobberTD(arr, i + 1, dp);
56 | return dp[i] = Math.max(rob, Dont_rob);
57 | }
58 |
59 | public static int Robber(int[] arr, int i) {
60 | if (i >= arr.length) {
61 | return 0;
62 | }
63 |
64 | int rob = arr[i] + Robber(arr, i + 2);
65 | int Dont_rob = Robber(arr, i + 1);
66 | return Math.max(rob, Dont_rob);
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Lec39/Create_Tree_Using_Level_Order.java:
--------------------------------------------------------------------------------
1 | package Lec39;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 | import java.util.Scanner;
6 |
7 | public class Create_Tree_Using_Level_Order {
8 |
9 | class Node {
10 | int val;
11 | Node left;
12 | Node right;
13 |
14 | public Node(int val) {
15 | // TODO Auto-generated constructor stub
16 | this.val = val;
17 | }
18 | }
19 |
20 | private Node root;
21 |
22 | public Create_Tree_Using_Level_Order() {
23 | // TODO Auto-generated constructor stub
24 | createTree();
25 | }
26 |
27 | private void createTree() {
28 | // TODO Auto-generated method stub
29 | Scanner sc = new Scanner(System.in);
30 | int e = sc.nextInt();
31 | Node node = new Node(e);
32 | root = node;
33 | Queue q = new LinkedList<>();
34 | q.add(node);
35 | while (!q.isEmpty()) {
36 | Node rv = q.poll();// remove first
37 | int c1 = sc.nextInt();
38 | int c2 = sc.nextInt();
39 | if (c1 != -1) {
40 | Node nn = new Node(c1);
41 | rv.left = nn;
42 | q.add(nn);
43 |
44 | }
45 | if (c2 != -1) {
46 | Node nn = new Node(c2);
47 | rv.right = nn;
48 | q.add(nn);
49 | }
50 |
51 | }
52 | }
53 |
54 | public void Display() {
55 | Display(root);
56 | }
57 |
58 | private void Display(Node nn) {
59 | if (nn == null) {
60 | return;
61 | }
62 | String s = "<--" + nn.val + "-->";
63 | if (nn.left != null) {
64 | s = nn.left.val + s;
65 | } else {
66 | s = "." + s;
67 | }
68 | if (nn.right != null) {
69 | s = s + nn.right.val;
70 | } else {
71 | s = s + ".";
72 | }
73 | System.out.println(s);
74 | Display(nn.left);
75 | Display(nn.right);
76 |
77 | }
78 |
79 | public static void main(String[] args) {
80 | Create_Tree_Using_Level_Order cl = new Create_Tree_Using_Level_Order();
81 | cl.Display();
82 | // 10 20 30 40 50 -1 70 -1 -1 60 -1 -1 -1 -1 -1
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/Lec51/Coin_Change_II.java:
--------------------------------------------------------------------------------
1 | package Lec51;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Coin_Change_II {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | int[] coins = { 1, 2, 5 };
10 | int amount = 5;
11 | int[][] dp = new int[amount + 1][coins.length];
12 | for (int[] a : dp) {
13 | Arrays.fill(a, -1);
14 | }
15 | System.out.println(Coin_Change(coins, amount, 0));
16 | System.out.println(Coin_ChangeTD(coins, amount, 0, dp));
17 |
18 | }
19 |
20 | public static int Coin_ChangeTD(int[] coin, int amount, int idx, int[][] dp) {
21 | if (amount == 0) {
22 | return 1;
23 | }
24 | if (idx == coin.length) {
25 | return 0;
26 | }
27 | if (dp[amount][idx] != -1) {
28 | return dp[amount][idx];
29 | }
30 | int inc = 0, exc = 0;
31 | if (coin[idx] <= amount) {
32 | inc = Coin_ChangeTD(coin, amount - coin[idx], idx, dp);
33 | }
34 | exc = Coin_ChangeTD(coin, amount, idx + 1, dp);
35 | return dp[amount][idx] = inc + exc;
36 |
37 | }
38 |
39 | public static int Coin_ChangeBU(int[] coins, int amount) {
40 | int[][] dp = new int[amount + 1][coins.length + 1];
41 | for (int i = 0; i < dp[0].length; i++) {
42 | dp[0][i] = 1;
43 | }
44 | for (int am = 1; am < dp.length; am++) {// amount
45 | for (int i = 1; i < dp[0].length; i++) {// coin
46 | int inc = 0, exc = 0;
47 | if (coins[i - 1] <= am) {
48 | inc = dp[am - coins[i - 1]][i];
49 | }
50 | exc = dp[am][i - 1];
51 | dp[am][i] = inc + exc;
52 | }
53 |
54 | }
55 | return dp[dp.length - 1][dp[0].length - 1];
56 | }
57 |
58 | public static int Coin_Change(int[] coin, int amount, int idx) {
59 | if (amount == 0) {
60 | return 1;
61 | }
62 | if (idx == coin.length) {
63 | return 0;
64 | }
65 | int inc = 0, exc = 0;
66 | if (coin[idx] <= amount) {
67 | inc = Coin_Change(coin, amount - coin[idx], idx);
68 | }
69 | exc = Coin_Change(coin, amount, idx + 1);
70 | return inc + exc;
71 |
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/Lec25/Sudoku_Solver.java:
--------------------------------------------------------------------------------
1 | package Lec25;
2 |
3 | public class Sudoku_Solver {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | int[][] grid = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 8, 7, 0, 0, 0, 0, 3, 1 },
8 | { 0, 0, 3, 0, 1, 0, 0, 8, 0 }, { 9, 0, 0, 8, 6, 3, 0, 0, 5 }, { 0, 5, 0, 0, 9, 0, 6, 0, 0 },
9 | { 1, 3, 0, 0, 0, 0, 2, 5, 0 }, { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, { 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
10 | SudokuSolver(grid, 0, 0);
11 | }
12 |
13 | public static void SudokuSolver(int[][] grid, int row, int col) {
14 | // TODO Auto-generated method stub
15 | if (col == 9) {
16 | col = 0;
17 | row++;
18 | }
19 | if (row == 9) {
20 | Display(grid);
21 | return;
22 | }
23 |
24 | if (grid[row][col] != 0) {
25 | SudokuSolver(grid, row, col + 1);
26 | } else {
27 | for (int val = 1; val <= 9; val++) {
28 | if (isItsafe(grid, row, col, val) == true) {
29 | grid[row][col] = val;
30 | SudokuSolver(grid, row, col + 1);
31 | grid[row][col] = 0;
32 |
33 | }
34 |
35 | }
36 | }
37 | }
38 |
39 | public static boolean isItsafe(int[][] grid, int row, int col, int val) {
40 | // TODO Auto-generated method stub
41 |
42 | // col
43 | for (int i = 0; i < 9; i++) {
44 | if (grid[i][col] == val) {
45 | return false;
46 | }
47 | }
48 | // row
49 | for (int i = 0; i < 9; i++) {
50 | if (grid[row][i] == val) {
51 | return false;
52 | }
53 | }
54 | // 3*3 Matrix
55 | int r = row - row % 3;
56 | int c = col - col % 3;
57 |
58 | for (int i = r; i < r + 3; i++) {
59 | for (int j = c; j < c + 3; j++) {
60 | if (grid[i][j] == val) {
61 | return false;
62 | }
63 | }
64 |
65 | }
66 | return true;
67 |
68 | }
69 |
70 | public static void Display(int[][] grid) {
71 | // TODO Auto-generated method stub
72 | for (int i = 0; i < grid.length; i++) {
73 | for (int j = 0; j < grid[0].length; j++) {
74 | System.out.print(grid[i][j] + " ");
75 | }
76 | System.out.println();
77 | }
78 |
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/Lec51/Longest_Common_Subsequence.java:
--------------------------------------------------------------------------------
1 | package Lec51;
2 |
3 | import java.util.Arrays;
4 |
5 | public class Longest_Common_Subsequence {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | String s1 = "abcde";
10 | String s2 = "ace";
11 | int[][] dp = new int[s1.length()][s2.length()];
12 | // for (int i = 0; i < dp.length; i++) {
13 | // for (int j = 0; j < dp[0].length; j++) {
14 | // dp[i][j] = -1;
15 | // }
16 | // }
17 | for (int[] a : dp) {
18 | Arrays.fill(a, -1);
19 | }
20 | System.out.println(lcs(s1, s2, 0, 0));
21 | System.out.println(lcsTD(s1, s2, 0, 0, dp));
22 |
23 | }
24 |
25 | // i-->s1 j-->s2
26 | public static int lcs(String s1, String s2, int i, int j) {
27 | if (i == s1.length() || j == s2.length()) {
28 | return 0;
29 | }
30 | int ans = 0;
31 | if (s1.charAt(i) == s2.charAt(j)) {
32 | ans = 1 + lcs(s1, s2, i + 1, j + 1);
33 | } else {
34 | int f = lcs(s1, s2, i + 1, j);
35 | int s = lcs(s1, s2, i, j + 1);
36 | ans = Math.max(f, s);
37 | }
38 | return ans;
39 |
40 | }
41 |
42 | public static int lcsTD(String s1, String s2, int i, int j, int[][] dp) {
43 | if (i == s1.length() || j == s2.length()) {
44 | return 0;
45 | }
46 | if (dp[i][j] != -1) {
47 | return dp[i][j];
48 | }
49 | int ans = 0;
50 | if (s1.charAt(i) == s2.charAt(j)) {
51 | ans = 1 + lcsTD(s1, s2, i + 1, j + 1, dp);
52 | } else {
53 | int f = lcsTD(s1, s2, i + 1, j, dp);
54 | int s = lcsTD(s1, s2, i, j + 1, dp);
55 | ans = Math.max(f, s);
56 | }
57 | return dp[i][j] = ans;
58 |
59 | }
60 |
61 | public static int lcsBU(String s1, String s2) {
62 | int[][] dp = new int[s1.length() + 1][s2.length() + 1];
63 | for (int i = 1; i < dp.length; i++) {
64 | for (int j = 1; j < dp[0].length; j++) {
65 | if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
66 | dp[i][j] = 1 + dp[i - 1][j - 1];
67 | } else {
68 | int f = dp[i - 1][j];
69 | int s = dp[i][j - 1];
70 | dp[i][j] = Math.max(f, s);
71 | }
72 | }
73 | }
74 | return dp[dp.length - 1][dp[0].length - 1];
75 |
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------