├── .gitattributes ├── .gitignore ├── Lecture 03 └── src │ ├── DataTypes.java │ ├── HelloWorld.java │ └── Main.java ├── Lecture 04_Java_Input └── src │ ├── Main.java │ ├── ReadChar.java │ ├── SimpleInterest.java │ ├── SumOfTwoNumbers.java │ └── TakingInput.java ├── Lecture 05_Operators └── src │ ├── ArithmeticOperators.java │ ├── AssignmentOperators.java │ ├── BitwiseOperators.java │ ├── LogicalOperators.java │ ├── Main.java │ ├── RelationalOperators.java │ └── UnaryOperators.java ├── Lecture 06_Conditionals └── src │ ├── AgeGroup.java │ ├── ConditionalOperators.java │ ├── Day.java │ ├── Main.java │ └── OddEven.java ├── Lecture 07_Loops └── src │ ├── ContinueKeyword.java │ ├── Main.java │ ├── MultipleOf5And7.java │ ├── StreamOfNums.java │ ├── SumForLoop.java │ ├── WhileLoop.java │ └── doWhile.java ├── Lecture 08_Loops_1 └── src │ ├── CountDigits.java │ ├── Factorial.java │ ├── Main.java │ ├── raisePower.java │ ├── reverseDigits.java │ └── sumSeries.java ├── Lecture 09_Loops_Patterns └── src │ ├── Main.java │ ├── NumericalRectangularPattern.java │ ├── NumericalTriangularPattern.java │ ├── PyramidPattern.java │ ├── RectangularPattern.java │ └── TriangularPattern.java ├── Lecture 10_Number_System └── src │ ├── Main.java │ ├── binaryToDecimal.java │ ├── decimalToBinary.java │ └── myPackage │ └── MyClass.java ├── Lecture 11 OOPs └── src │ ├── Main.java │ ├── Person.java │ └── pw │ └── skills │ └── SkillsClass.java ├── Lecture 12 Methods Live ├── Lecture 12 Methods │ └── src │ │ ├── Main.java │ │ ├── OutsidePWSkills.java │ │ └── pw │ │ └── skills │ │ └── App.java └── src │ ├── Main.java │ ├── StandardLibraryDemo.java │ └── pw │ └── skills │ └── App.java ├── Lecture 13 Scope └── src │ └── Main.java ├── Lecture 14_Arrays_1 └── src │ └── Main.java ├── Lecture 15 Arrays └── src │ ├── App.java │ ├── Main.java │ └── Questions.java ├── Lecture 16 Arrays Problem Solving └── src │ ├── FindUnique.java │ ├── Main.java │ ├── PairSum.java │ ├── PresentQuery.java │ ├── RepeatingNumber.java │ ├── Rotate.java │ ├── SecondLargest.java │ └── TripletSum.java ├── Lecture 17 Arrays - Miscenalleous Problems └── src │ ├── MiscProblems.java │ ├── PresentQuery.java │ └── RotateArray.java ├── Lecture 18 - Two Pointers └── src │ └── SortArray.java ├── Lecture 19 - Prefix Sum └── src │ ├── PrefixSum.java │ └── RangeQuery.java ├── Lecture 20 - 2D Arrays └── src │ ├── Matrix.java │ └── MultiDimensionalArray.java ├── Lecture 21 - 2D Arrays └── src │ ├── PascalTriangle.java │ ├── RotateMatrix.java │ └── TransposeMatrix.java ├── Lecture 22 └── src │ ├── GenerateSpiral.java │ └── PrintSpiral.java ├── Lecture 23 └── src │ └── RectangleSum.java ├── Lecture 24 - Arraylist └── src │ ├── Main.java │ └── Problems.java ├── Lecture 25 - Time and Space Complexity └── src │ └── Main.java ├── Lecture 27 └── src │ └── Main.java ├── Lecture 28 - Recursion PS1 └── src │ ├── Factorial.java │ └── Fibonacci.java ├── Lecture 29 - Recursion PS2 └── src │ ├── Power.java │ └── SOD.java ├── Lecture 30 - Recursion PS3 └── src │ ├── Multiples.java │ └── SeriesSum.java ├── Lecture 31 - Recursion PS4 GCD └── src │ └── GCD.java ├── Lecture 32 - Recursion PS5 - Arrays └── src │ └── ArrayRecursion.java ├── Lecture 33 - Recursion PS6 - Arrays └── src │ ├── Main.java │ ├── Search.java │ └── findAllIndices.java ├── Lecture 34 - Recursion PS7 - Strings └── src │ ├── Main.java │ ├── Palindrome.java │ ├── RemoveOccurrences.java │ └── Reverse.java ├── Lecture 35 - Recursion PS8 - Subsets Subsequences └── src │ ├── Main.java │ ├── PrintSSQ.java │ ├── ReturnSSQ.java │ └── SubsetSum.java ├── Lecture 36 - Recursion 9 └── src │ ├── FrogJump.java │ ├── KeypadCombinations.java │ └── Main.java ├── Lecture 37 - Bubble Sort └── src │ └── Main.java ├── Lecture 38 - Selection Sort └── src │ └── Main.java ├── Lecture 39 - Insertion Sort └── src │ └── Main.java ├── Lecture 40 └── src │ ├── Main.java │ └── moveZeroes.java ├── Lecture 41 - Merge Sort └── src │ └── Main.java ├── Lecture 42 Quick Sort └── src │ └── Main.java ├── Lecture 43 Non-Comparison Sorting └── src │ ├── BucketSort.java │ ├── CountSort.java │ ├── Main.java │ └── RadixSort.java ├── Lecture 44 Sorting Problems └── src │ ├── Main.java │ ├── Sort012.java │ ├── SortArr.java │ └── SortPosNeg.java ├── Lecture 45 - Binary Search └── src │ ├── BinarySearch.java │ ├── FindSquareRoot.java │ ├── FirstOccurence.java │ └── Main.java └── Lecture 46 Rotated Binary Search └── src ├── RotatedSortedArray.java └── findMin.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.idea 3 | *.class 4 | out/ 5 | .editorconfig -------------------------------------------------------------------------------- /Lecture 03/src/DataTypes.java: -------------------------------------------------------------------------------- 1 | public class DataTypes { 2 | public static void main(String[] args) { 3 | boolean flag = true; 4 | int a = 12345; 5 | short s = 1200; 6 | byte b = 120; 7 | long l = 832786310; 8 | float f = 800.7692f; 9 | double d = 7878.8765; 10 | char ch = 'p'; 11 | 12 | System.out.println("The boolean variable is " + flag); 13 | System.out.println("The integer variable is " + a); 14 | System.out.println("The short variable is " + s); 15 | System.out.println("The byte variable is " + b); 16 | System.out.println("The long variable is " + l); 17 | System.out.println("The float variable is " + f); 18 | System.out.println("The double variable is " + d); 19 | System.out.println("The char variable is " + ch); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture 03/src/HelloWorld.java: -------------------------------------------------------------------------------- 1 | class HelloWorld { 2 | public static void main(String[] args) { 3 | int money = 2000; 4 | System.out.println(money); 5 | money = 5000; 6 | System.out.println(money); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Lecture 03/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello world! - Physics Wallah"); 4 | } 5 | } -------------------------------------------------------------------------------- /Lecture 04_Java_Input/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Hello world!"); 6 | Scanner sc = new Scanner(System.in); 7 | String p = sc.next(); 8 | int q = sc.nextInt(); 9 | System.out.print(p + " " + q); 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Lecture 04_Java_Input/src/ReadChar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ReadChar { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.println("Enter a character"); 8 | char ch = sc.next().charAt(0); 9 | System.out.println("Character is: " + ch); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Lecture 04_Java_Input/src/SimpleInterest.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SimpleInterest { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.println("Enter principle"); 8 | float p = sc.nextFloat(); 9 | 10 | System.out.println("Enter rate of interest"); 11 | float r = sc.nextFloat(); 12 | 13 | System.out.println("Enter time"); 14 | float t = sc.nextFloat(); 15 | 16 | float si = (p * r * t)/100; 17 | 18 | System.out.println("Principle: " + p); 19 | System.out.println("Rate: " + r); 20 | System.out.println("Time: " + t); 21 | 22 | System.out.println("Simple Interest: " + si); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lecture 04_Java_Input/src/SumOfTwoNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumOfTwoNumbers { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.println("Enter first number"); 8 | int num_1 = sc.nextInt(); 9 | 10 | System.out.println("Enter second number"); 11 | int num_2 = sc.nextInt(); 12 | 13 | int sum = num_1 + num_2; 14 | 15 | System.out.println("Sum: " + sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 04_Java_Input/src/TakingInput.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TakingInput { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.println("Enter your name"); 8 | String name = sc.nextLine(); 9 | System.out.println("Name is: " + name); 10 | 11 | // System.out.println("Enter your lucky number"); 12 | // int num_1 = sc.nextInt(); 13 | // 14 | // System.out.println("Lucky Number is: " + num_1); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/ArithmeticOperators.java: -------------------------------------------------------------------------------- 1 | public class ArithmeticOperators { 2 | public static void main(String[] args) { 3 | int p = 20, q = 10; 4 | 5 | System.out.println(p+q); 6 | System.out.println(p-q); 7 | System.out.println(p*q); 8 | System.out.println(p/q); 9 | System.out.println(p%q); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/AssignmentOperators.java: -------------------------------------------------------------------------------- 1 | public class AssignmentOperators { 2 | public static void main(String[] args) { 3 | int p = 10; 4 | int q; 5 | 6 | // = 7 | q = p; 8 | System.out.println(q); //10 9 | 10 | // += 11 | p += q; // p = p + q, p = 10 + 10 = 20 12 | System.out.println(p); //20 13 | 14 | p -= q; // p = p - q , p = 20 - 10 = 10 15 | System.out.println(p); //10 16 | 17 | p *= q; // p = p * q 18 | System.out.println(p); //100 19 | 20 | p /= q; // p = p/q , p = 100/10 = 10 21 | System.out.println(p); //10 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/BitwiseOperators.java: -------------------------------------------------------------------------------- 1 | public class BitwiseOperators { 2 | public static void main(String[] args) { 3 | int p = 5, q = 3; // p = 101, q = 011 4 | 5 | System.out.println(p & q); // 1 6 | System.out.println(p | q); // 7 7 | System.out.println(p ^ q); // 6 8 | 9 | System.out.println(p << 1); //10 10 | System.out.println(p << 2); //20 11 | 12 | int r = 11; 13 | System.out.println(r >> 1); // 5 14 | System.out.println(r >> 2); // 2 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/LogicalOperators.java: -------------------------------------------------------------------------------- 1 | public class LogicalOperators { 2 | public static void main(String[] args) { 3 | int p = 15, q = 10, r = 5; 4 | 5 | // && operator 6 | System.out.println((p > q) && (p > r)); //true 7 | System.out.println((p > q) && (p < r)); //false 8 | 9 | // || operator 10 | System.out.println((r < q) || (p < q)); //true 11 | System.out.println((p > q) || (q > r)); //true 12 | System.out.println((p < q) || (q < r)); //false 13 | 14 | // ! operator 15 | System.out.println(!(p == q)); //true 16 | System.out.println(!(p > q)); //false 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int p = 4, q = 3, r = 2; 4 | System.out.println(p-++r-++q); 5 | System.out.println(6 + "pqr"); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 05_Operators/src/RelationalOperators.java: -------------------------------------------------------------------------------- 1 | public class RelationalOperators { 2 | public static void main(String[] args) { 3 | int p = 10, q = 15; 4 | 5 | System.out.println(p == q); //false 6 | System.out.println(p != q); //true 7 | System.out.println(p <= q); //true 8 | System.out.println(p >= q); //false 9 | System.out.println(p < q); //true 10 | System.out.println(p > q); //false 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Lecture 05_Operators/src/UnaryOperators.java: -------------------------------------------------------------------------------- 1 | public class UnaryOperators { 2 | public static void main(String[] args) { 3 | int p = 5, q = 5; 4 | 5 | System.out.println(p++); //5 6 | System.out.println(p); // 6 7 | 8 | System.out.println(++q); // 6 9 | System.out.println(q); // 6 10 | 11 | int x = p++; 12 | int y = ++q; 13 | 14 | System.out.println(x); //6 15 | System.out.println(y); // 7 16 | 17 | System.out.println(p); // 7 18 | System.out.println(q); // 7 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture 06_Conditionals/src/AgeGroup.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class AgeGroup { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int age = sc.nextInt(); 7 | 8 | if(age <= 12){ 9 | System.out.println("child"); 10 | } else if(age > 12 && age < 18){ 11 | System.out.println("teenager"); 12 | } else { 13 | System.out.println("adult"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture 06_Conditionals/src/ConditionalOperators.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ConditionalOperators { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int value = sc.nextInt(); 7 | 8 | if(value % 5 == 0 || value % 3 == 0){ 9 | System.out.println("Found ans - " + value); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Lecture 06_Conditionals/src/Day.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int day = sc.nextInt(); 7 | 8 | switch (day){ 9 | case 1: 10 | System.out.println("MONDAY"); 11 | break; 12 | case 2: 13 | System.out.println("TUESDAY"); 14 | break; 15 | case 3: 16 | System.out.println("WEDNESDAY"); 17 | break; 18 | case 4: 19 | System.out.println("THURSDAY"); 20 | break; 21 | case 5: 22 | System.out.println("FRIDAY"); 23 | break; 24 | case 6: 25 | System.out.println("SATURDAY"); 26 | break; 27 | case 7: 28 | System.out.println("SUNDAY"); 29 | break; 30 | default: 31 | System.out.println("Invalid Day Number"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lecture 06_Conditionals/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int x = 60, y = 90; 4 | if (y % x == 0) 5 | System.out.println("Good"); 6 | else 7 | System.out.println("Bad"); 8 | } 9 | } -------------------------------------------------------------------------------- /Lecture 06_Conditionals/src/OddEven.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class OddEven { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | // if(num % 2 == 0){ 9 | // System.out.println("even"); 10 | // } else { 11 | // System.out.println("odd"); 12 | // } 13 | 14 | String ans; 15 | ans = (num % 2 == 0) ? "even" : "odd"; 16 | System.out.println(ans); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/ContinueKeyword.java: -------------------------------------------------------------------------------- 1 | public class ContinueKeyword { 2 | public static void main(String[] args) { 3 | 4 | // outer: for(int i = 0; i < 10; i++){ 5 | // inner: for(int j = 0; j < 5; j++){ 6 | // break outer; 7 | // } 8 | // } 9 | 10 | // myloop: for(int num = 1; num <= 50; num++){ 11 | // if(num % 3 == 0){ 12 | // continue myloop; 13 | // } 14 | // System.out.println(num); 15 | // } 16 | 17 | // int num = 1; 18 | // 19 | // while(num <= 50){ 20 | // if(num % 3 == 0){ 21 | // num++; 22 | // continue; 23 | // } 24 | // System.out.println(num); 25 | // num++; 26 | // } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int num = 1; 6 | while(true){ 7 | if(num % 5 == 0 && num % 7 == 0){ 8 | System.out.println(num); 9 | break; 10 | } 11 | num++; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Lecture 07_Loops/src/MultipleOf5And7.java: -------------------------------------------------------------------------------- 1 | public class MultipleOf5And7 { 2 | public static void main(String[] args) { 3 | 4 | int num = 1; 5 | 6 | while(true){ 7 | if((num % 5 == 0) && (num % 7 == 0)){ 8 | System.out.println("Found ans " + num); 9 | break; 10 | } 11 | num++; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/StreamOfNums.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class StreamOfNums { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | int sum = 0; 8 | 9 | do{ 10 | sum += num; 11 | num = sc.nextInt(); 12 | } while (num != -1); 13 | 14 | // while(num != -1){ 15 | // sum += num; 16 | // num = sc.nextInt(); 17 | // } 18 | System.out.println(sum); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/SumForLoop.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumForLoop { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | int num = n; 8 | for(; num >= 1;){ // num -= 2, num = num - 2 9 | System.out.println(num); 10 | num -= 2; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/WhileLoop.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //print 1 to n 4 | public class WhileLoop { 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | int sum = 0; 9 | 10 | int num = 1; 11 | 12 | while (num <= n){ 13 | // System.out.println(num); 14 | sum = sum + num; 15 | num++; // increment operator ++ , num = num + 1 16 | } 17 | 18 | System.out.println(sum); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture 07_Loops/src/doWhile.java: -------------------------------------------------------------------------------- 1 | public class doWhile { 2 | public static void main(String[] args) { 3 | int num = 1; 4 | do{ 5 | System.out.println(num); 6 | num++; 7 | } while(num <= 10); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/CountDigits.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class CountDigits { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | int sumOfDigits = 0; 8 | int original_n = n; 9 | 10 | while(n > 0) { 11 | sumOfDigits += n % 10; 12 | n = n / 10; 13 | } 14 | 15 | System.out.println("Sum of digits in " + original_n + " = " + sumOfDigits); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Factorial { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int fact = 1; 9 | 10 | for(int i = 1; i <= n; i++){ 11 | fact = fact * i; 12 | System.out.println("Factorial of " + i + ": " + fact); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello world!"); 4 | for(int i = 1; i <= 4; i++){ 5 | for(int j = 1; j <= 4-i; j++){ 6 | System.out.print(" "); 7 | } 8 | for(int k = 1; k <= 2 * i - 1; k++){ 9 | System.out.print("*"); 10 | } 11 | System.out.println(); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/raisePower.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class raisePower { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int a = sc.nextInt(), b = sc.nextInt(); 7 | 8 | int ans = 1; 9 | for(int i = 1; i <= b; i++){ 10 | ans *= a; 11 | } 12 | System.out.println(ans); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/reverseDigits.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class reverseDigits { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int ans = 0; 9 | 10 | while(n > 0){ 11 | ans = ans * 10 + n % 10; 12 | n /= 10; 13 | } 14 | 15 | System.out.println(ans); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 08_Loops_1/src/sumSeries.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class sumSeries { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int ans = 0; 9 | for(int i = 1; i <= n; i++){ 10 | if(i % 2 == 0){ 11 | ans -= i; 12 | } else { 13 | ans += i; 14 | } 15 | } 16 | 17 | System.out.println(ans); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | float p = 34.5f; 4 | float r = 2.2f; 5 | float t = 3.5f; 6 | 7 | float si = ((p*r*t)/100); 8 | System.out.println(si); 9 | 10 | 11 | // int a[] = { 1, 4, 7, 9 }; 12 | // 13 | // int n = a.length; 14 | // int b[] = a; 15 | // 16 | //// b = a; 17 | // 18 | // b[0] = 5; 19 | // 20 | // System.out.println("Original array "); 21 | // for (int i = 0; i < n; i++) 22 | // System.out.print(a[i] + " "); 23 | // 24 | // System.out.println("\nReferenced Array "); 25 | // for (int i = 0; i < n; i++) 26 | // System.out.print(b[i] + " "); 27 | 28 | // double[] wts = new double[3]; 29 | // float[] heights = {5.5f, 5.0f, 6.5f}; 30 | // 31 | // int marks[] = new int[3]; 32 | // String[] names = {"Meeta", "Shivangi", "Meghna"}; 33 | // 34 | // wts[0] = 56.82; 35 | // wts[1] = 45.9; 36 | // 37 | // marks[0] = 2; 38 | // marks[1] = 3; 39 | // 40 | // System.out.println("Names: "); 41 | // for(int i = 0; i < 3; i++){ //names.length 42 | // System.out.println(names[i]); 43 | // } 44 | // 45 | // System.out.println("Weights: "); 46 | // for(int i = 0; i < 3; i++){ //weights.length 47 | // System.out.println(wts[i]); 48 | // } 49 | // 50 | // System.out.println("Marks: "); 51 | // for(int i = 0; i < 3; i++){ 52 | // System.out.println(marks[i]); 53 | // } 54 | // 55 | // System.out.println("Heights: "); 56 | // for(int i = 0; i < 3; i++){ 57 | // System.out.println(heights[i]); 58 | // } 59 | // 60 | // for(int i = 0; i < 3; i++){ 61 | //// System.out.println(names[i] + ": " + wts[i] + ", " + heights[i] + ", " + marks[i]); 62 | // System.out.printf("Name: %s, Weight: %f, Height: %f, Marks: %d \n", names[i], wts[i], heights[i], marks[i]); 63 | // } 64 | 65 | 66 | // System.out.println(marks[-2]); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/NumericalRectangularPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class NumericalRectangularPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int r = sc.nextInt(); 7 | 8 | for(int i = 1; i <= r; i++){ 9 | for(int j = i; j <= r; j++){ 10 | System.out.print(j); 11 | } 12 | for(int k = 1; k <= i-1; k++){ 13 | System.out.print(k); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/NumericalTriangularPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class NumericalTriangularPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int r = sc.nextInt(); 7 | 8 | for(int i = 1; i <= r; i++){ 9 | for(int j = 1; j <= i; j++){ 10 | System.out.print(j); 11 | } 12 | System.out.println(); 13 | } 14 | 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/PyramidPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PyramidPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int r = sc.nextInt(); 7 | 8 | 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/RectangularPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RectangularPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int r = sc.nextInt(); 7 | int c = sc.nextInt(); 8 | 9 | for(int i = 1; i <= r; i++){ 10 | 11 | for(int j = 1; j <= c; j++){ 12 | if((i + j) % 2 == 0){ 13 | System.out.print(1); 14 | } else { 15 | System.out.print(2); 16 | } 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture 09_Loops_Patterns/src/TriangularPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TriangularPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int r = sc.nextInt(); 7 | 8 | for(int i = r; i >=1 ; i--){ 9 | for(int j = 1; j <= i; j++) { 10 | System.out.print("*"); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture 10_Number_System/src/Main.java: -------------------------------------------------------------------------------- 1 | import myPackage.MyClass; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | MyClass obj = new MyClass(); 6 | obj.myPrint(); 7 | } 8 | } -------------------------------------------------------------------------------- /Lecture 10_Number_System/src/binaryToDecimal.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class binaryToDecimal { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int binary_num = sc.nextInt(); 7 | 8 | int ans = 0; //converted decimal number 9 | int pw = 1; //2 ^ 0 = 1 power of 2 10 | 11 | while(binary_num > 0){ 12 | int unit_digit = binary_num % 10; 13 | ans += (unit_digit * pw); 14 | binary_num /= 10; 15 | pw *= 2; 16 | } 17 | System.out.println(ans); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 10_Number_System/src/decimalToBinary.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class decimalToBinary { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int decimal_num = sc.nextInt(); 7 | int ans = 0; //binary number 8 | 9 | int pw = 1; // powers of 10 10 | 11 | while(decimal_num > 0){ 12 | int parity = decimal_num % 2; 13 | ans += (parity * pw); 14 | pw *= 10; 15 | decimal_num /= 2; 16 | } 17 | System.out.println(ans); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 10_Number_System/src/myPackage/MyClass.java: -------------------------------------------------------------------------------- 1 | package myPackage; 2 | 3 | public class MyClass { 4 | public void myPrint(){ 5 | System.out.println("This is my print from my package"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Lecture 11 OOPs/src/Main.java: -------------------------------------------------------------------------------- 1 | class Student { 2 | int rollNo; 3 | String studentName; 4 | } 5 | 6 | public class Main{ 7 | public static void main(String[] args) { 8 | Student obj1 = new Student(); 9 | obj1.rollNo = 1; 10 | obj1.studentName = "Rohan"; 11 | System.out.println(obj1.rollNo); 12 | System.out.println(obj1.studentName); 13 | 14 | Student obj2 = new Student(); 15 | obj2.rollNo = 2; 16 | obj2.studentName = "Rahul"; 17 | System.out.println(obj2.rollNo); 18 | System.out.println(obj2.studentName); 19 | } 20 | } -------------------------------------------------------------------------------- /Lecture 11 OOPs/src/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | int age = 20; 3 | 4 | public static void main(String[] args) { 5 | Person Rohan = new Person(); 6 | System.out.println(Rohan.age); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Lecture 11 OOPs/src/pw/skills/SkillsClass.java: -------------------------------------------------------------------------------- 1 | package pw.skills; 2 | 3 | public class SkillsClass { 4 | } 5 | -------------------------------------------------------------------------------- /Lecture 12 Methods Live/Lecture 12 Methods/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Algebra{ 4 | int a; 5 | int b; 6 | Algebra(int x, int y){ 7 | System.out.println("Constructor of Algebra class called"); 8 | a = x; 9 | b = y; 10 | } 11 | int add(){ 12 | int ans = a + b; 13 | return ans; 14 | } 15 | 16 | int sub(){ 17 | return a - b; 18 | } 19 | 20 | int mul(){ 21 | return a * b; 22 | } 23 | 24 | } 25 | 26 | public class Main { 27 | public static void main(String[] args) { 28 | Algebra obj = new Algebra(5, 7); 29 | // Scanner sc = new Scanner(System.in); 30 | // int x = sc.nextInt(); 31 | // int y = sc.nextInt(); 32 | 33 | int ans = obj.add(); 34 | System.out.println(ans); 35 | System.out.println(obj.sub()); 36 | System.out.println(obj.mul()); 37 | 38 | Algebra obj2 = new Algebra(10, 7); 39 | System.out.println(obj2.add()); 40 | System.out.println(obj2.sub()); 41 | System.out.println(obj2.mul()); 42 | 43 | // System.out.println(Math.sqrt(24)); 44 | // System.out.println(Math.sqrt(16)); 45 | // 46 | // System.out.println(Math.floor(5.3)); 47 | // System.out.println(Math.floor(5.8)); 48 | // 49 | // System.out.println(Math.ceil(5.3)); 50 | // System.out.println(Math.ceil(5.8)); 51 | // 52 | // return; 53 | } 54 | } -------------------------------------------------------------------------------- /Lecture 12 Methods Live/Lecture 12 Methods/src/OutsidePWSkills.java: -------------------------------------------------------------------------------- 1 | import pw.skills.App; 2 | 3 | public class OutsidePWSkills { 4 | public static void main(String[] args) { 5 | App obj = new App(); 6 | // System.out.println("Outside Package, non-child class: " + obj.str_1); 7 | 8 | App3 obj3 = new App3(); 9 | obj3.printFromChildClass(); 10 | } 11 | } 12 | //child class //parent class 13 | class App3 extends App{ 14 | void printFromChildClass(){ 15 | App3 obj3 = new App3(); 16 | // System.out.println("Child Class: " + obj3.str_1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture 12 Methods Live/Lecture 12 Methods/src/pw/skills/App.java: -------------------------------------------------------------------------------- 1 | package pw.skills; 2 | 3 | public class App { 4 | private String str_1 = "I am a private member"; 5 | void printFromClass(){ 6 | System.out.println("Within class: " + str_1); 7 | } 8 | 9 | public static void main(String[] args) { 10 | App obj = new App(); 11 | obj.printFromClass(); 12 | System.out.println("Within class: " + obj.str_1); 13 | App2 obj2 = new App2(); 14 | obj2.printFromOutsideClass(); 15 | } 16 | } 17 | 18 | class App2{ 19 | void printFromOutsideClass(){ 20 | App obj = new App(); 21 | // System.out.println("Within package, outside class: " + obj.str_1); 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 12 Methods Live/src/Main.java: -------------------------------------------------------------------------------- 1 | import pw.skills.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | App obj = new App(); 6 | System.out.println(obj.a); 7 | // System.out.println(obj.b); 8 | // System.out.println(obj.c); 9 | // System.out.println(obj.d); 10 | } 11 | } 12 | 13 | //app2 -> child class, app -> parent class 14 | class App2 extends App{ 15 | void printFromApp2(){ 16 | App2 obj = new App2(); 17 | System.out.println(obj.a); //public 18 | System.out.println(obj.b); //protected 19 | // System.out.println(obj.c); 20 | // System.out.println(obj.d); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 12 Methods Live/src/StandardLibraryDemo.java: -------------------------------------------------------------------------------- 1 | public class StandardLibraryDemo { 2 | public static void main(String[] args) { 3 | int num = 16; 4 | System.out.println(Math.sqrt(num)); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Lecture 12 Methods Live/src/pw/skills/App.java: -------------------------------------------------------------------------------- 1 | package pw.skills; 2 | 3 | public class App { 4 | public int a = 1; 5 | protected int b = 2; 6 | int c = 3; 7 | private int d = 4; 8 | 9 | void printFromApp(){ 10 | System.out.println("Printing within class"); 11 | System.out.println(a); 12 | System.out.println(b); 13 | System.out.println(c); 14 | System.out.println(d); 15 | } 16 | 17 | public static void main(String[] args) { 18 | App obj = new App(); 19 | obj.printFromApp(); 20 | A obj2 = new A(); 21 | obj2.printFromA(); 22 | } 23 | } 24 | 25 | class A{ 26 | void printFromA(){ 27 | System.out.println("Printing from same package but different class"); 28 | App obj = new App(); 29 | System.out.println(obj.a); 30 | System.out.println(obj.b); 31 | System.out.println(obj.c); 32 | // System.out.println(obj.d); 33 | } 34 | } -------------------------------------------------------------------------------- /Lecture 13 Scope/src/Main.java: -------------------------------------------------------------------------------- 1 | class Algebra{ 2 | // int a = 10; //member variables 3 | // int b = 5; 4 | 5 | int add(){ 6 | int p = 100; 7 | int q = 20; 8 | return p + q; 9 | } 10 | 11 | int sub(){ 12 | int p = 67; 13 | int q = 20; 14 | return p - q; 15 | } 16 | 17 | void demo(){ 18 | 19 | { 20 | int b = 5; 21 | } 22 | 23 | { 24 | int b = 100; 25 | } 26 | 27 | for(int i = 0; i < 5; i++) 28 | { 29 | System.out.println(i); 30 | } 31 | 32 | for(int i = 0; i < 2; i++) 33 | { 34 | System.out.println(i); 35 | } 36 | 37 | 38 | } 39 | } 40 | 41 | public class Main { 42 | 43 | static void changeValue(int a){ 44 | a *= 100; 45 | 46 | System.out.println("Inside change value " + a); 47 | } 48 | 49 | public static void main(String[] args) { 50 | // Algebra obj = new Algebra(); 51 | // obj.demo(); 52 | 53 | int a = 10; 54 | System.out.println("Before changing value " + a); 55 | changeValue(a); 56 | System.out.println("After changing value " + a); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Lecture 14_Arrays_1/src/Main.java: -------------------------------------------------------------------------------- 1 | class ArraysExample{ 2 | 3 | void cloneArray2(){ 4 | int ai[] = {1, 2, 3, 4, 5}; 5 | 6 | /* copying the reference ai to aic, 7 | * after following assignment, aic 8 | * will point to the same array ai points. 9 | */ 10 | int aic[] = ai; 11 | 12 | aic[2] = -9; 13 | 14 | /* both print statements will print 15 | * the same result because ai, and aic 16 | * are the reference of same array, then no matter 17 | * which reference is being used to update array values. 18 | */ 19 | System.out.println(aic[2]); 20 | System.out.println(ai[2]); 21 | 22 | /* Now illustrating clone(). 23 | * In below assignment ai.clone() creates 24 | * a separate copy of the array in memory 25 | * and then assigns it to aic. Now, both 26 | * ai and aic point to two different arrays 27 | * so changes made to one will not impact the other 28 | */ 29 | System.out.println("---"); //separator 30 | aic = ai.clone(); 31 | aic[1] = -15; 32 | 33 | /* both print statements will print 34 | * the value stored at 1st index in the array 35 | * they point to. 36 | */ 37 | System.out.println(aic[1]); 38 | System.out.println(ai[1]); 39 | } 40 | void cloneArray(){ 41 | int a[] = { 1, 4, 7, 9 }; 42 | 43 | int n = a.length; 44 | int b[] = a.clone(); 45 | 46 | b[0] = 5; 47 | 48 | System.out.println("Original array "); 49 | for (int i = 0; i < n; i++) 50 | System.out.print(a[i] + " "); 51 | 52 | System.out.println("\nCloned Array "); 53 | for (int i = 0; i < b.length; i++) 54 | System.out.print(b[i] + " "); 55 | 56 | } 57 | void searchInArray(){ 58 | //Linear Search7 59 | int[] arr = {10, 5, 3, 6, 2, 8, 4}; 60 | int x = 3; 61 | 62 | int ans = -1; 63 | 64 | for(int i = 0; i < arr.length; i++){ 65 | if(arr[i] == x){ 66 | ans = i; 67 | } 68 | } 69 | if(ans == -1){ 70 | System.out.println("Not Found"); 71 | } else { 72 | System.out.println("Found " + x + " at index " + ans); 73 | } 74 | } 75 | 76 | void maxOfArray(){ 77 | int[] arr = {10, 5, 3, 6, 2, 8, 4}; //ans = 8 78 | 79 | int ans = 0; 80 | 81 | for(int i = 0; i < arr.length; i++){ 82 | if(arr[i] > ans){ 83 | ans = arr[i]; 84 | } 85 | } 86 | 87 | System.out.println("Max " + ans); 88 | 89 | } 90 | void sumOfArray(){ 91 | int[] arr = {1, 4, 5, 6, 1}; 92 | int sum = 0; 93 | 94 | for(int i = 0; i < arr.length; i++){ 95 | sum = sum + arr[i]; 96 | } 97 | 98 | System.out.println(sum); 99 | } 100 | 101 | void multiArrays(){ 102 | int[][] arr = {{56, 43, 6}, {34, 7}}; 103 | 104 | for(int i = 0; i < arr.length; i++){ 105 | //arr[0] and arr[1] 106 | for(int j = 0; j < arr[i].length; j++){ 107 | System.out.println(arr[i][j]); 108 | } 109 | } 110 | 111 | } 112 | void demoArrays(){ 113 | int[] ages = new int[3]; // ages[0], ages[1], ages[2] 114 | float[] weights = new float[3]; 115 | String[] names = {"rahul", "raj", "aradhya", "vaishali"}; 116 | 117 | ages[0] = 34; 118 | ages[1] = 12; 119 | ages[2] = 45; 120 | 121 | for(int i = 0; i < ages.length; i++){ 122 | System.out.println(ages[i]); 123 | } 124 | 125 | for(int age : ages){ 126 | System.out.println(age); 127 | } 128 | 129 | int i = 0; 130 | while(i < ages.length){ 131 | System.out.println(ages[i]); 132 | ++i; 133 | } 134 | } 135 | } 136 | 137 | public class Main { 138 | public static void main(String[] args) { 139 | ArraysExample obj = new ArraysExample(); 140 | // obj.demoArrays(); 141 | // obj.multiArrays(); 142 | // obj.sumOfArray(); 143 | // obj.maxOfArray(); 144 | // obj.searchInArray(); 145 | // obj.cloneArray(); 146 | obj.cloneArray2(); 147 | 148 | } 149 | 150 | 151 | 152 | 153 | } -------------------------------------------------------------------------------- /Lecture 15 Arrays/src/App.java: -------------------------------------------------------------------------------- 1 | public class App { 2 | 3 | static void printArray(int[] arr){ 4 | for(int i = 0; i < arr.length; i++){ 5 | System.out.print(arr[i] + " "); 6 | } 7 | System.out.println(); 8 | } 9 | static void changeArray(int[] arr){ 10 | for(int i = 0; i < arr.length; i++){ 11 | arr[i] = 0; 12 | } 13 | } 14 | 15 | static void changeVal(int a){ 16 | a = 1004; 17 | } 18 | 19 | public static void main(String[] args) { 20 | int a = 5; 21 | changeVal(a); 22 | System.out.println(a); // 5 23 | 24 | int[] arr = new int[3]; 25 | arr[0] = 5; 26 | arr[1] = 8; 27 | arr[2] = 7; 28 | 29 | changeArray(arr); 30 | printArray(arr); 31 | 32 | boolean ans = true; 33 | for (int i = 1; i < arr.length; i++){ 34 | if(arr[i] < arr[i-1]){ 35 | ans = false; 36 | } 37 | } 38 | System.out.println(ans); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Lecture 15 Arrays/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | static void printArray(int[] arr){ 6 | for(int i = 0; i < arr.length; i++){ 7 | System.out.print(arr[i] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | 15 | // System.out.println("Enter size of array"); 16 | // int n = sc.nextInt(); 17 | int[] arr = new int[5]; 18 | arr[0] = 5; 19 | arr[1] = 6; 20 | arr[2] = 7; 21 | arr[3] = 8; 22 | arr[4] = 9; 23 | 24 | // System.out.println("Enter " + n + " elements"); 25 | // for(int i = 0; i < arr.length; i++){ 26 | // arr[i] = sc.nextInt(); 27 | // } 28 | 29 | System.out.println("Original arr"); 30 | printArray(arr); 31 | 32 | //trying to copy arr to arr_2 33 | // [ ) 34 | int[] arr_2 = Arrays.copyOfRange(arr, 1, 4); 35 | System.out.println("Copied arr_2"); 36 | printArray(arr_2); 37 | 38 | //changing some values of arr_2 39 | // arr_2[0] = 0; 40 | // arr_2[1] = 0; 41 | // 42 | // System.out.println("original array after changing arr_2"); 43 | // printArray(arr); 44 | // 45 | // System.out.println("copied arr_2 after changing arr_2"); 46 | // printArray(arr_2); 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Lecture 15 Arrays/src/Questions.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Questions { 5 | static int[] smallestAndLargestElement(int[] arr){ //1, 2, 3, 4, 5 6 | Arrays.sort(arr); 7 | int[] ans = {arr[0], arr[arr.length-1]}; 8 | return ans; 9 | } 10 | 11 | static boolean isSorted(int[] arr){ 12 | boolean check = true; 13 | for(int i = 1; i < arr.length; i++){ 14 | if(arr[i] < arr[i-1]){ 15 | //not sorted 16 | check = false; 17 | break; 18 | } 19 | } 20 | return check; 21 | } 22 | 23 | static int lastOccurrence(int[] arr, int x){ 24 | int lastIndex = -1; 25 | for(int i = 0; i < arr.length; i++){ 26 | if(arr[i] == x){ 27 | lastIndex = i; 28 | } 29 | } 30 | return lastIndex; 31 | } 32 | static int countOccurrences(int[] arr, int x){ 33 | int count = 0; 34 | for(int i = 0; i < arr.length; i++){ 35 | if(arr[i] == x){ 36 | count++; 37 | } 38 | } 39 | return count; 40 | } 41 | 42 | static int a; 43 | 44 | public static void main(String[] args) { 45 | System.out.println(a); 46 | Scanner sc = new Scanner(System.in); 47 | System.out.println("Enter size of array: "); 48 | int n = sc.nextInt(); 49 | 50 | int[] arr = new int[n]; 51 | System.out.println("Enter " + n + " elements: "); 52 | for(int i = 0; i < arr.length; i++){ 53 | arr[i] = sc.nextInt(); 54 | } 55 | 56 | // System.out.println("Enter x"); 57 | // int x = sc.nextInt(); 58 | 59 | // System.out.println("COUNT OF X: " + countOccurrences(arr, x)); 60 | // System.out.println("LAST OCCURRENCE OF X: " + lastOccurrence(arr, x)); 61 | // System.out.println("Is Sorted: " + isSorted(arr)); 62 | int[] ans = smallestAndLargestElement(arr); 63 | System.out.println("Smallest: " + ans[0]); 64 | System.out.println("Largest: " + ans[1]); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/FindUnique.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class FindUnique { 4 | static int findUnique(int[] arr){ 5 | int n = arr.length; 6 | for(int i = 0; i < n; i++){ 7 | for(int j = i + 1; j < n; j++){ 8 | if(arr[i] == arr[j]){ 9 | arr[i] = -1; 10 | arr[j] = -1; 11 | } 12 | } 13 | } 14 | 15 | int ans = -1; 16 | for(int i = 0; i < n; i++){ 17 | if(arr[i] > 0){ 18 | ans = arr[i]; 19 | } 20 | } 21 | return ans; 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner sc = new Scanner(System.in); 26 | System.out.print("Enter array size "); 27 | int n = sc.nextInt(); 28 | int[] arr = new int[n]; 29 | 30 | System.out.println("Enter " + n + " elements"); 31 | for(int i = 0; i < n; i++){ 32 | arr[i] = sc.nextInt(); 33 | } 34 | System.out.println("Unique element: " + findUnique(arr)); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello world!"); 4 | } 5 | } -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/PairSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PairSum { 4 | static int pairSum(int[] arr, int target){ 5 | int n = arr.length; 6 | int ans = 0; 7 | 8 | for(int i = 0; i < n; i++){ //first number 9 | for(int j = i + 1; j < n; j++){ //second number 10 | if(arr[i] + arr[j] == target){ 11 | ans++; 12 | } 13 | } 14 | } 15 | 16 | return ans; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | System.out.print("Enter array size "); 22 | int n = sc.nextInt(); 23 | int[] arr = new int[n]; 24 | 25 | System.out.println("Enter " + n + " elements"); 26 | for(int i = 0; i < n; i++){ 27 | arr[i] = sc.nextInt(); 28 | } 29 | 30 | System.out.print("Enter target sum "); 31 | int target = sc.nextInt(); 32 | 33 | System.out.println(pairSum(arr, target)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/PresentQuery.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PresentQuery { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.print("Enter array size "); 8 | int n = sc.nextInt(); 9 | int[] arr = new int[n]; 10 | 11 | System.out.println("Enter " + n + " elements"); 12 | for(int i = 0; i < n; i++){ 13 | arr[i] = sc.nextInt(); 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/RepeatingNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RepeatingNumber { 4 | static int firstRepeatingNumber(int[] arr){ 5 | int n = arr.length; 6 | 7 | for(int i = 0; i < n; i++){ //first number 8 | for(int j = i + 1; j < n; j++){ //second number 9 | if(arr[i] == arr[j]){ //found answer 10 | return arr[i]; 11 | } 12 | } 13 | } 14 | return -1; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | System.out.print("Enter array size "); 20 | int n = sc.nextInt(); 21 | int[] arr = new int[n]; 22 | 23 | System.out.println("Enter " + n + " elements"); 24 | for(int i = 0; i < n; i++){ 25 | arr[i] = sc.nextInt(); 26 | } 27 | 28 | System.out.println("First Repeating Number: " + firstRepeatingNumber(arr)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/Rotate.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Rotate { 4 | 5 | static void rotateByK(){ 6 | 7 | } 8 | static void rotateByK(int[] arr, int k){ 9 | 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | System.out.print("Enter array size "); 15 | int n = sc.nextInt(); 16 | int[] arr = new int[n]; 17 | 18 | System.out.println("Enter " + n + " elements"); 19 | for(int i = 0; i < n; i++){ 20 | arr[i] = sc.nextInt(); 21 | } 22 | 23 | System.out.print("Enter rotation steps k"); 24 | int k = sc.nextInt(); 25 | 26 | rotateByK(arr, k); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/SecondLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SecondLargest { 4 | 5 | static int findMax(int[] arr){ 6 | int mx = Integer.MIN_VALUE; 7 | for(int i = 0; i < arr.length; i++){ 8 | if(arr[i] > mx){ 9 | mx = arr[i]; 10 | } 11 | } 12 | return mx; 13 | } 14 | static int findSecondMax(int[] arr){ 15 | int mx = findMax(arr); 16 | 17 | for(int i = 0; i < arr.length; i++){ 18 | if(arr[i] == mx){ 19 | arr[i] = Integer.MIN_VALUE; 20 | } 21 | } 22 | 23 | int secondMax = findMax(arr); 24 | 25 | return secondMax; 26 | 27 | } 28 | 29 | public static void main(String[] args) { 30 | Scanner sc = new Scanner(System.in); 31 | System.out.print("Enter array size "); 32 | int n = sc.nextInt(); 33 | int[] arr = new int[n]; 34 | 35 | System.out.println("Enter " + n + " elements"); 36 | for(int i = 0; i < n; i++){ 37 | arr[i] = sc.nextInt(); 38 | } 39 | 40 | System.out.println("Second Maximum Element: " + findSecondMax(arr)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lecture 16 Arrays Problem Solving/src/TripletSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TripletSum { 4 | static int tripletSum(int[] arr, int target){ 5 | int ans = 0; 6 | int n = arr.length; 7 | 8 | for(int i = 0; i < n; i++){ //first number 9 | for(int j = i+1; j < n; j++){ //second number 10 | for(int k = j+1; k < n; k++){ //third number 11 | if(arr[i] + arr[j] + arr[k] == target){ 12 | ans++; 13 | } 14 | } 15 | } 16 | } 17 | return ans; 18 | 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner sc = new Scanner(System.in); 23 | System.out.print("Enter array size "); 24 | int n = sc.nextInt(); 25 | int[] arr = new int[n]; 26 | 27 | System.out.println("Enter " + n + " elements"); 28 | for(int i = 0; i < n; i++){ 29 | arr[i] = sc.nextInt(); 30 | } 31 | 32 | System.out.print("Enter target sum "); 33 | int target = sc.nextInt(); 34 | 35 | System.out.println(tripletSum(arr, target)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture 17 Arrays - Miscenalleous Problems/src/MiscProblems.java: -------------------------------------------------------------------------------- 1 | public class MiscProblems { 2 | 3 | 4 | static void swapWithoutTemp(int a, int b){ 5 | System.out.println("Original values before swap"); 6 | System.out.println("a: " + a); 7 | System.out.println("b: " + b); 8 | 9 | a = a + b; 10 | b = a- b; 11 | a = a - b; 12 | 13 | 14 | System.out.println("Values after swap"); 15 | System.out.println("a: " + a); 16 | System.out.println("b: " + b); 17 | } 18 | 19 | // a = 9, b = 3 20 | static void swap(int a, int b){ 21 | System.out.println("Original values before swap"); 22 | System.out.println("a: " + a); 23 | System.out.println("b: " + b); 24 | 25 | int temp = a; // temp = 9 26 | a = b; // a = 3 27 | b = temp; // b = 9 28 | 29 | System.out.println("Values after swap"); 30 | System.out.println("a: " + a); 31 | System.out.println("b: " + b); 32 | 33 | //a = 3, b = 9 34 | } 35 | 36 | static void printArray(int[] arr){ 37 | for(int i = 0; i < arr.length; i++){ 38 | System.out.print(arr[i] + " "); 39 | } 40 | System.out.println(); 41 | } 42 | 43 | // static int[] reverseArray(int[] arr){ 44 | // int n = arr.length; 45 | // int[] ans = new int[n]; 46 | // //traverse original array in reverse direction 47 | //// for(int i = n-1; i >= 0; i--){ 48 | //// ans[j++] = arr[i]; 49 | //// } 50 | // 51 | // int i = n-1, j = 0; 52 | // while(i >= 0){ 53 | // ans[j++] = arr[i--]; 54 | // } 55 | // return ans; 56 | // } 57 | 58 | static void swapInArray(int[] arr, int i, int j){ 59 | int temp = arr[i]; 60 | arr[i] = arr[j]; 61 | arr[j] = temp; 62 | } 63 | static void reverseArray(int[] arr){ 64 | int i = 0, j = arr.length-1; 65 | 66 | while(i < j){ 67 | swapInArray(arr, i, j); 68 | i++; 69 | j--; 70 | } 71 | } 72 | 73 | 74 | 75 | public static void main(String[] args) { 76 | int a = 9; 77 | int b = 3; 78 | int[] arr = {1, 2, 3, 4, 5}; 79 | reverseArray(arr); 80 | printArray(arr); 81 | 82 | // swap(a, b); 83 | // swapWithoutTemp(a, b); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /Lecture 17 Arrays - Miscenalleous Problems/src/PresentQuery.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PresentQuery { 4 | static int[] makeFrequencyArray(int[] arr){ 5 | int[] freq = new int[100005]; 6 | for(int i = 0; i < arr.length; i++){ 7 | freq[arr[i]]++; 8 | } 9 | return freq; 10 | } 11 | 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | System.out.print("Enter array size "); 16 | int n = sc.nextInt(); 17 | int[] arr = new int[n]; 18 | 19 | System.out.println("Enter " + n + " elements"); 20 | for(int i = 0; i < n; i++){ 21 | arr[i] = sc.nextInt(); 22 | } 23 | 24 | int[] freq = makeFrequencyArray(arr); 25 | 26 | System.out.print("Enter number of queries "); 27 | int q = sc.nextInt(); 28 | 29 | while(q > 0){ 30 | System.out.print("Enter number to be searched "); 31 | int x = sc.nextInt(); 32 | if(freq[x] > 0){ 33 | System.out.println("YES"); 34 | } else { 35 | System.out.println("NO"); 36 | } 37 | q--; 38 | } 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /Lecture 17 Arrays - Miscenalleous Problems/src/RotateArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RotateArray { 4 | static void printArray(int[] arr){ 5 | for(int i = 0; i < arr.length; i++){ 6 | System.out.print(arr[i] + " "); 7 | } 8 | System.out.println(); 9 | } 10 | static void swap(int[] arr, int i, int j){ 11 | int temp = arr[i]; 12 | arr[i] = arr[j]; 13 | arr[j] = temp; 14 | } 15 | 16 | static void reverse(int[] arr, int i, int j){ 17 | while (i < j){ 18 | swap(arr, i, j); 19 | i++; 20 | j--; 21 | } 22 | } 23 | 24 | static void rotateInPlace(int[] arr, int k){ 25 | int n = arr.length; 26 | k = k % n; 27 | reverse(arr, 0, n-k-1); 28 | reverse(arr, n-k, n-1); 29 | reverse(arr, 0, n-1); 30 | } 31 | 32 | static int[] rotate(int[] arr, int k){ 33 | int n = arr.length; 34 | k = k % n; 35 | int[] ans = new int[n]; 36 | int j = 0; 37 | 38 | for(int i = n-k; i < n; i++){ 39 | ans[j++] = arr[i]; 40 | } 41 | 42 | for(int i = 0; i < n-k; i++){ 43 | ans[j++] = arr[i]; 44 | } 45 | return ans; 46 | } 47 | 48 | 49 | 50 | public static void main(String[] args) { 51 | Scanner sc = new Scanner(System.in); 52 | System.out.print("Enter array size "); 53 | int n = sc.nextInt(); 54 | int[] arr = new int[n]; 55 | 56 | System.out.println("Enter " + n + " elements"); 57 | for(int i = 0; i < n; i++){ 58 | arr[i] = sc.nextInt(); 59 | } 60 | 61 | System.out.print("Enter k: "); 62 | int k = sc.nextInt(); 63 | 64 | System.out.println("Original array"); 65 | printArray(arr); 66 | rotateInPlace(arr, k); 67 | // int[] ans = rotate(arr, k); 68 | System.out.println("Array after rotation"); 69 | printArray(arr); 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Lecture 18 - Two Pointers/src/SortArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SortArray { 4 | static void printArray(int[] arr){ 5 | int n = arr.length; 6 | for(int i = 0; i < n; i++){ 7 | System.out.print(arr[i] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | 12 | static void swap(int[] arr, int i, int j){ 13 | int temp = arr[i]; 14 | arr[i] = arr[j]; 15 | arr[j] = temp; 16 | } 17 | 18 | static void sortArrayByParity(int[] arr){ 19 | int n = arr.length; 20 | 21 | int left = 0, right = n-1; 22 | 23 | while (left < right){ 24 | if(arr[left] % 2 == 1 && arr[right] % 2 == 0){ 25 | swap(arr, left, right); 26 | left++; 27 | right--; 28 | } 29 | 30 | if(arr[left] % 2 == 0){ 31 | left++; 32 | } 33 | 34 | if(arr[right] % 2 == 1){ 35 | right--; 36 | } 37 | } 38 | } 39 | 40 | static void sortZeroesAndOnes(int[] arr){ 41 | int n = arr.length; 42 | 43 | int left = 0, right = n-1; 44 | 45 | while(left < right){ 46 | if(arr[left] == 1 && arr[right] == 0){ 47 | swap(arr, left, right); 48 | left++; 49 | right--; 50 | } 51 | 52 | if(arr[left] == 0){ 53 | left++; 54 | } 55 | 56 | if(arr[right] == 1){ 57 | right--; 58 | } 59 | } 60 | } 61 | 62 | // static void sortZeroesAndOnes(int[] arr){ 63 | // int n = arr.length; 64 | // int zeroes = 0; 65 | // 66 | // //count number of zeroes 67 | // for(int i = 0; i < n; i++){ 68 | // if(arr[i] == 0){ 69 | // zeroes++; 70 | // } 71 | // } 72 | // 73 | // //0 to zeroes-1 : 0, zeroes to n-1 : 1 74 | // for(int i = 0; i < n; i++){ 75 | // if(i < zeroes){ 76 | // arr[i] = 0; 77 | // } else { 78 | // arr[i] = 1; 79 | // } 80 | // } 81 | // } 82 | 83 | static void reverse(int[] arr){ 84 | int i = 0, j = arr.length-1; 85 | while (i < j){ 86 | swap(arr, i, j); 87 | i++; 88 | j--; 89 | } 90 | } 91 | 92 | static int[] sortSquares(int[] arr){ 93 | int n = arr.length; 94 | int left = 0, right = n-1; 95 | int[] ans = new int[n]; 96 | int k = n-1; 97 | 98 | while(left <= right){ 99 | if(Math.abs(arr[left]) > Math.abs(arr[right])){ 100 | ans[k--] = arr[left] * arr[left]; 101 | left++; 102 | } else { 103 | ans[k--] = arr[right] * arr[right]; 104 | right--; 105 | } 106 | } 107 | // reverse(ans); 108 | return ans; 109 | } 110 | 111 | 112 | public static void main(String[] args) { 113 | Scanner sc = new Scanner(System.in); 114 | System.out.print("Enter array size "); 115 | int n = sc.nextInt(); 116 | int[] arr = new int[n]; 117 | 118 | System.out.println("Enter " + n + " elements"); 119 | for(int i = 0; i < n; i++){ 120 | arr[i] = sc.nextInt(); 121 | } 122 | 123 | System.out.println("Original array: "); 124 | printArray(arr); 125 | int[] ans = sortSquares(arr); 126 | // sortArrayByParity(arr); 127 | // sortZeroesAndOnes(arr); 128 | System.out.println("Sorted array: "); 129 | printArray(ans); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /Lecture 19 - Prefix Sum/src/PrefixSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PrefixSum { 4 | static void printArray(int[] arr){ 5 | for(int i = 0; i < arr.length; i++){ 6 | System.out.print(arr[i] + " "); 7 | } 8 | System.out.println(); 9 | } 10 | 11 | static int[] makePrefixSumArray(int[] arr){ 12 | int n = arr.length; 13 | for(int i = 1; i < n; i++){ 14 | arr[i] = arr[i-1] + arr[i]; 15 | } 16 | return arr; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | System.out.print("Enter array size "); 22 | int n = sc.nextInt(); 23 | int[] arr = new int[n]; 24 | 25 | System.out.println("Enter " + n + " elements"); 26 | for(int i = 0; i < n; i++){ 27 | arr[i] = sc.nextInt(); 28 | } 29 | System.out.println("Input Array"); 30 | printArray(arr); 31 | 32 | int[] pref = makePrefixSumArray(arr); 33 | printArray(pref); 34 | 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /Lecture 19 - Prefix Sum/src/RangeQuery.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RangeQuery { 4 | static int[] makePrefixSumArray(int[] arr){ 5 | for(int i = 1; i < arr.length; i++){ 6 | arr[i] += arr[i-1]; 7 | } 8 | return arr; 9 | } 10 | 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | System.out.print("Enter array size "); 14 | int n = sc.nextInt(); 15 | int[] arr = new int[n+1]; 16 | 17 | System.out.println("Enter " + n + " elements"); 18 | for(int i = 1; i <= n; i++){ 19 | arr[i] = sc.nextInt(); 20 | } 21 | 22 | int[] prefSum = makePrefixSumArray(arr); 23 | 24 | System.out.print("Enter number of queries "); 25 | int q = sc.nextInt(); 26 | 27 | while (q-- > 0){ 28 | System.out.println("Enter range"); 29 | int l = sc.nextInt(); 30 | int r = sc.nextInt(); 31 | 32 | int ans = prefSum[r] - prefSum[l-1]; 33 | 34 | System.out.println("Sum " + ans); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture 20 - 2D Arrays/src/Matrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Matrix { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | 13 | static void multiply(int[][] a, int r1, int c1, int[][] b, int r2, int c2){ 14 | if(c1 != r2){ 15 | System.out.println("Multiplication not possible - wrong dimension"); 16 | return; 17 | } 18 | 19 | int[][] mul = new int[r1][c2]; 20 | 21 | for(int i = 0; i < r1; i++){ // row number 22 | for(int j = 0; j < c2; j++){ //column number 23 | for(int k = 0; k < c1; k++){ 24 | /* 25 | i = 1, j = 0 26 | mul[i][j] = ith row of a * jth col of b 27 | */ 28 | mul[i][j] += (a[i][k] * b[k][j]); 29 | } 30 | } 31 | } 32 | 33 | System.out.println("Multiplication of 2 matrices"); 34 | printMatrix(mul); 35 | } 36 | 37 | static void add(int[][] a, int r1, int c1, int[][] b, int r2, int c2){ 38 | if(r1 != r2 || c1 != c2){ 39 | System.out.println("Wrong Input - Addition not possible"); 40 | return; 41 | } 42 | 43 | int[][] sum = new int[r1][c1]; 44 | 45 | for(int i = 0; i < r1; i++){ //row number 46 | for(int j = 0; j < c1; j++){ //column number 47 | sum[i][j] = a[i][j] + b[i][j]; 48 | } 49 | } 50 | System.out.println("Sum of matrix 1 and matrix 2"); 51 | printMatrix(sum); 52 | } 53 | 54 | public static void main(String[] args) { 55 | Scanner sc = new Scanner(System.in); 56 | System.out.println("Enter number of rows and columns of matrix 1"); 57 | int r1 = sc.nextInt(); 58 | int c1 = sc.nextInt(); 59 | int[][] a = new int[r1][c1]; 60 | System.out.println("Enter matrix values"); 61 | for (int i = 0; i < r1; i++){ 62 | for(int j = 0; j < c1; j++){ 63 | a[i][j] = sc.nextInt(); 64 | } 65 | } 66 | 67 | System.out.println("Enter number of rows and columns of matrix 2"); 68 | int r2 = sc.nextInt(); 69 | int c2 = sc.nextInt(); 70 | int[][] b = new int[r2][c2]; 71 | System.out.println("Enter matrix values"); 72 | for (int i = 0; i < r2; i++){ 73 | for(int j = 0; j < c2; j++){ 74 | b[i][j] = sc.nextInt(); 75 | } 76 | } 77 | 78 | System.out.println("Matrix 1"); 79 | printMatrix(a); 80 | System.out.println("Matrix 2"); 81 | printMatrix(b); 82 | 83 | // add(a, r1, c1, b, r2, c2); 84 | multiply(a, r1, c1, b, r2, c2); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Lecture 20 - 2D Arrays/src/MultiDimensionalArray.java: -------------------------------------------------------------------------------- 1 | import java.sql.SQLOutput; 2 | import java.util.Scanner; 3 | 4 | public class MultiDimensionalArray { 5 | 6 | static void printArray(int[][] arr){ 7 | 8 | for(int i = 0; i < arr.length; i++){ //row 9 | // System.out.println(arr[i]); 10 | for(int j = 0; j < arr[i].length; j++){ //column 11 | System.out.print(arr[i][j] + " "); 12 | } 13 | System.out.println(); 14 | } 15 | } 16 | 17 | public static void main(String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | System.out.println("Enter number of rows"); 20 | int r = sc.nextInt(); 21 | System.out.println("Enter number of columns"); 22 | int c = sc.nextInt(); 23 | 24 | int[][] arr = new int[r][c]; // total = r*c 25 | 26 | System.out.println("Enter " + r*c + " elements"); 27 | for(int i = 0; i < r; i++) { // row 28 | for(int j = 0; j < c; j++){ 29 | arr[i][j] = sc.nextInt(); 30 | } 31 | } 32 | 33 | printArray(arr); 34 | 35 | 36 | // int[][] arr2 = { 37 | // {1, 5, 6}, 38 | // {7, 9 , 11}, 39 | // {8, 1, 1} 40 | // }; 41 | 42 | // printArray(arr2); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Lecture 21 - 2D Arrays/src/PascalTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PascalTriangle { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | 13 | static int[][] pascal(int n){ 14 | int[][] ans = new int[n][]; 15 | for(int i = 0; i < n; i++){ 16 | //ith row has i+1 columns 17 | ans[i] = new int[i+1]; 18 | //1st and last element of every row is 1 19 | ans[i][0] = ans[i][i] = 1; 20 | 21 | for(int j = 1; j < i; j++){ 22 | ans[i][j] = ans[i-1][j] + ans[i-1][j-1]; 23 | } 24 | } 25 | return ans; 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner sc = new Scanner(System.in); 30 | System.out.println("Enter n "); 31 | int n = sc.nextInt(); 32 | int[][] ans = pascal(n); 33 | printMatrix(ans); 34 | } 35 | } -------------------------------------------------------------------------------- /Lecture 21 - 2D Arrays/src/RotateMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RotateMatrix { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | static void transposeInPlace(int[][] matrix, int r, int c){ 13 | for (int i = 0; i < c; i++) { 14 | for(int j = i; j < r; j++){ 15 | int temp = matrix[i][j]; 16 | matrix[i][j] = matrix[j][i]; 17 | matrix[j][i] = temp; 18 | } 19 | } 20 | } 21 | 22 | static void reverseArray(int[] arr){ 23 | int i = 0, j = arr.length-1; 24 | 25 | while (i < j){ 26 | int temp = arr[i]; 27 | arr[i] = arr[j]; 28 | arr[j] = temp; 29 | i++; 30 | j--; 31 | } 32 | } 33 | static void rotate(int[][] matrix, int n){ 34 | // transpose 35 | transposeInPlace(matrix, n, n); 36 | 37 | // reverse each row of transposed matrix 38 | for(int i = 0; i < n; i++){ 39 | reverseArray(matrix[i]); 40 | } 41 | 42 | } 43 | 44 | public static void main(String[] args) { 45 | Scanner sc = new Scanner(System.in); 46 | System.out.println("Enter number of rows and columns of matrix"); 47 | int r = sc.nextInt(); 48 | int c = sc.nextInt(); 49 | int[][] matrix = new int[r][c]; 50 | int totalElements = r * c; 51 | System.out.println("Enter " + totalElements + " values"); 52 | for (int i = 0; i < r; i++) { 53 | for (int j = 0; j < c; j++) { 54 | matrix[i][j] = sc.nextInt(); 55 | } 56 | } 57 | System.out.println("Input Matrix"); 58 | printMatrix(matrix); 59 | 60 | rotate(matrix, r); 61 | 62 | System.out.println("Rotation of matrix"); 63 | printMatrix(matrix); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Lecture 21 - 2D Arrays/src/TransposeMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class TransposeMatrix { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | 13 | static int[][] findTranspose(int[][] matrix, int r, int c){ 14 | int[][] ans = new int[c][r]; 15 | 16 | for(int i = 0; i < c; i++){ 17 | for(int j = 0; j < r; j++){ 18 | ans[i][j] = matrix[j][i]; 19 | } 20 | } 21 | return ans; 22 | } 23 | 24 | static void transposeInPlace(int[][] matrix, int r, int c){ 25 | 26 | for(int i = 0; i < c; i++){ 27 | for(int j = i; j < r; j++){ 28 | // swap matrix[i][j], matrix[j][i] 29 | int temp = matrix[i][j]; 30 | matrix[i][j] = matrix[j][i]; 31 | matrix[j][i] = temp; 32 | } 33 | } 34 | } 35 | 36 | public static void main(String[] args) { 37 | Scanner sc = new Scanner(System.in); 38 | System.out.println("Enter number of rows and columns of matrix"); 39 | int r = sc.nextInt(); 40 | int c = sc.nextInt(); 41 | int[][] matrix = new int[r][c]; 42 | int totalElements = r * c; 43 | System.out.println("Enter " + totalElements + " values"); 44 | for (int i = 0; i < r; i++){ 45 | for(int j = 0; j < c; j++){ 46 | matrix[i][j] = sc.nextInt(); 47 | } 48 | } 49 | System.out.println("Input Matrix"); 50 | printMatrix(matrix); 51 | 52 | System.out.println("Transpose of matrix"); 53 | transposeInPlace(matrix, r, c); 54 | printMatrix(matrix); 55 | // int[][] ans = findTranspose(matrix, r, c); 56 | // printMatrix(ans); 57 | 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Lecture 22/src/GenerateSpiral.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GenerateSpiral { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | static int[][] generateSpiralMatrix(int n){ 13 | int[][] matrix = new int[n][n]; 14 | int topRow = 0, bottomRow = n-1, leftCol = 0, rightCol = n-1; 15 | int curr = 1; 16 | while(curr <= n * n){ 17 | //top row -> leftCol to RightCol 18 | for(int j = leftCol; j <= rightCol && curr <= n*n; j++){ 19 | matrix[topRow][j] = curr++; 20 | } 21 | topRow++; 22 | 23 | //right Col -> topRow to bottomRow 24 | for(int i = topRow; i <= bottomRow && curr < n*n; i++){ 25 | matrix[i][rightCol] = curr++; 26 | } 27 | rightCol--; 28 | 29 | //bottomRow -> rightCol to leftCol 30 | for(int j = rightCol; j >= leftCol && curr < n*n; j--){ 31 | matrix[bottomRow][j] = curr++; 32 | } 33 | bottomRow--; 34 | 35 | //left Col -> bottomRow to topRow 36 | for(int i = bottomRow; i >= topRow && curr < n*n; i--){ 37 | matrix[i][leftCol] = curr++; 38 | } 39 | leftCol++; 40 | } 41 | return matrix; 42 | } 43 | 44 | 45 | public static void main(String[] args) { 46 | Scanner sc = new Scanner(System.in); 47 | System.out.println("Enter n"); 48 | int n = sc.nextInt(); 49 | 50 | int[][] matrix = generateSpiralMatrix(n); 51 | printMatrix(matrix); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 22/src/PrintSpiral.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PrintSpiral { 4 | static void printMatrix(int[][] matrix){ 5 | for(int i = 0; i < matrix.length; i++){ 6 | for(int j = 0; j < matrix[i].length; j++){ 7 | System.out.print(matrix[i][j] + " "); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | static void printSpiralOrder(int[][] matrix, int r, int c){ 13 | int topRow = 0, bottomRow = r-1, leftCol = 0, rightCol = c-1; 14 | int totalElements = 0; 15 | 16 | while(totalElements < r * c){ 17 | // topRow -> leftCol to rightCol 18 | for(int j = leftCol; j <= rightCol && totalElements < r*c; j++){ 19 | System.out.print(matrix[topRow][j] + " "); 20 | totalElements++; 21 | } 22 | topRow++; 23 | 24 | //rightCol -> topRow to BottomRow 25 | for(int i = topRow; i <= bottomRow && totalElements < r*c; i++){ 26 | System.out.print(matrix[i][rightCol] + " "); 27 | totalElements++; 28 | } 29 | rightCol--; 30 | 31 | //bottomRow -> rightCol to leftCol 32 | for(int j = rightCol; j >= leftCol && totalElements < r*c; j--){ 33 | System.out.print(matrix[bottomRow][j] + " "); 34 | totalElements++; 35 | } 36 | bottomRow--; 37 | 38 | //leftCol -> bottomRow to topRow 39 | for(int i = bottomRow; i >= topRow && totalElements < r*c; i--){ 40 | System.out.print(matrix[i][leftCol] + " "); 41 | totalElements++; 42 | } 43 | leftCol++; 44 | } 45 | } 46 | 47 | public static void main(String[] args) { 48 | Scanner sc = new Scanner(System.in); 49 | System.out.println("Enter number of rows and columns of matrix"); 50 | int r = sc.nextInt(); 51 | int c = sc.nextInt(); 52 | int[][] matrix = new int[r][c]; 53 | int total = r * c; 54 | System.out.println("Enter " + total + " values"); 55 | for (int i = 0; i < r; i++){ 56 | for(int j = 0; j < c; j++){ 57 | matrix[i][j] = sc.nextInt(); 58 | } 59 | } 60 | System.out.println("Input Matrix"); 61 | printMatrix(matrix); 62 | 63 | System.out.println("Spiral Order"); 64 | printSpiralOrder(matrix, r, c); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Lecture 23/src/RectangleSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RectangleSum { 4 | static int findSum(int[][] matrix, int l1, int r1, int l2, int r2) { 5 | int sum = 0; 6 | for(int i = l1; i <= l2; i++){ 7 | for(int j = r1; j <= r2; j++){ 8 | sum += matrix[i][j]; 9 | } 10 | } 11 | return sum; 12 | } 13 | 14 | //calculate row-wise and column wise sum 15 | // matrix[i][j] = sumRectangle( (0,0) (i,j)) 16 | static void findPrefixSumMatrix(int[][] matrix){ 17 | int r = matrix.length; 18 | int c = matrix[0].length; 19 | // traverse horizontally to calculate row-wise prefix sum 20 | for(int i = 0; i < r; i++){ 21 | for(int j = 1; j < c; j++){ 22 | matrix[i][j] += matrix[i][j-1]; 23 | } 24 | } 25 | 26 | //traverse vertically to calculate column-wise sum 27 | for(int j = 0; j < c; j++){ // fixing column 28 | for(int i = 1; i < r; i++){ 29 | matrix[i][j] += matrix[i-1][j]; 30 | } 31 | } 32 | } 33 | 34 | //only row-wise prefix sum method 35 | static int findSum2(int[][] matrix, int l1, int r1, int l2, int r2) { 36 | int sum = 0; 37 | findPrefixSumMatrix(matrix); 38 | for(int i = l1; i <= l2; i++){ 39 | // r1 to r2 sum for row i 40 | if(r1 >= 1) 41 | sum += matrix[i][r2] - matrix[i][r1-1]; 42 | else 43 | sum += matrix[i][r2]; 44 | } 45 | return sum; 46 | } 47 | 48 | //both row-wise and column-wise prefix sum - Best Approach 49 | static int findSum3(int[][] matrix, int l1, int r1, int l2, int r2) { 50 | int ans = 0, sum = 0, up = 0, left = 0, leftUp = 0; 51 | findPrefixSumMatrix(matrix); 52 | 53 | sum = matrix[l2][r2]; 54 | if(r1 >= 1) { 55 | left = matrix[l2][r1 - 1]; 56 | } 57 | if(l1 >= 1) { 58 | up = matrix[l1 - 1][r2]; 59 | } 60 | if(l1 >=1 && r1 >= 1) { 61 | leftUp = matrix[l1 - 1][r1 - 1]; 62 | } 63 | ans = sum - up - left + leftUp; 64 | return ans; 65 | } 66 | 67 | public static void main(String[] args) { 68 | Scanner sc = new Scanner(System.in); 69 | System.out.println("Enter number of rows and columns of matrix"); 70 | int r = sc.nextInt(); 71 | int c = sc.nextInt(); 72 | int[][] matrix = new int[r][c]; 73 | int totalElements = r * c; 74 | System.out.println("Enter " + totalElements + " values"); 75 | for (int i = 0; i < r; i++) { 76 | for (int j = 0; j < c; j++) { 77 | matrix[i][j] = sc.nextInt(); 78 | } 79 | } 80 | 81 | System.out.println("Enter rectangle boundaries l1, r1, l2, r2"); 82 | int l1 = sc.nextInt(); 83 | int r1 = sc.nextInt(); 84 | int l2 = sc.nextInt(); 85 | int r2 = sc.nextInt(); 86 | 87 | System.out.println("Rectangle Sum " + findSum(matrix, l1, r1, l2, r2)); 88 | System.out.println("Rectangle Sum " + findSum3(matrix, l1, r1, l2, r2)); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Lecture 24 - Arraylist/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | //wrapper classes 6 | // Integer in = Integer.valueOf(4); 7 | // System.out.println(in); 8 | // Float f = Float.valueOf(4.5f); 9 | // System.out.println(f); 10 | 11 | ArrayList l1 = new ArrayList<>(); 12 | 13 | // add new element 14 | 15 | l1.add(5); // 5 16 | l1.add(6); // 5 6 17 | l1.add(7); // 5 6 7 18 | l1.add(8); // 5 6 7 8 19 | 20 | // get an element at index i 21 | // System.out.println(l1.get(1)); // 6 22 | 23 | 24 | // // print with for loop 25 | // for(int i = 0; i < l1.size(); i++){ 26 | // System.out.println(l1.get(i)); // 5, 6, 7, 8 27 | // } 28 | 29 | //printing the array list directly 30 | System.out.println(l1); // [5, 6, 7, 8] 31 | 32 | //adding element at some index i 33 | l1.add(1, 100); 34 | System.out.println(l1); // [5 100 6 7 8] 35 | 36 | 37 | //modifying element at index i 38 | l1.set(1, 10); 39 | System.out.println(l1); // [5, 10, 6, 7, 8] 40 | 41 | 42 | //removing an element at index i 43 | l1.remove(1); 44 | System.out.println(l1); // 5, 6, 7, 8 45 | 46 | 47 | //removing an element e 48 | l1.remove(Integer.valueOf(7)); 49 | System.out.println(l1); // 5, 6, 8 50 | 51 | //checking if an element exists 52 | boolean ans = l1.contains(Integer.valueOf(60)); 53 | System.out.println(ans); 54 | 55 | // if you don't specify class, you can put anything inside l 56 | ArrayList l = new ArrayList(); 57 | l.add("pqres"); 58 | l.add(1); 59 | l.add(true); 60 | System.out.println(l); 61 | 62 | 63 | } 64 | } -------------------------------------------------------------------------------- /Lecture 24 - Arraylist/src/Problems.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class Problems { 5 | static void reverseList(ArrayList list){ 6 | int i = 0, j = list.size() - 1; 7 | 8 | while(i < j){ 9 | /* 10 | int temp = a; 11 | a = b; 12 | b = temp; 13 | */ 14 | Integer temp = Integer.valueOf(list.get(i)); 15 | list.set(i, list.get(j)); 16 | list.set(j, temp); 17 | i++; 18 | j--; 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | ArrayList list = new ArrayList<>(); 24 | list.add(0); 25 | list.add(10); 26 | list.add(3); 27 | list.add(5); 28 | list.add(22); 29 | list.add(10); 30 | System.out.println("Original List " + list); 31 | Collections.reverse(list); 32 | System.out.println("Reversed List " + list); 33 | Collections.sort(list); 34 | System.out.println("Ascending order " + list); 35 | Collections.sort(list, Collections.reverseOrder()); 36 | System.out.println("Desc order " + list); 37 | 38 | ArrayList l1 = new ArrayList<>(); 39 | l1.add("Welcome"); 40 | l1.add("To"); 41 | l1.add("Physics"); 42 | l1.add("Wallah"); 43 | System.out.println("OG " + l1); 44 | Collections.sort(l1, Collections.reverseOrder()); 45 | System.out.println("Sorted " + l1); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Lecture 25 - Time and Space Complexity/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | void f1(int[] arr){ 3 | int n = arr.length; 4 | for(int i = 0; i < n; i++){ 5 | System.out.println(arr[i]); 6 | } 7 | } 8 | 9 | void f2(int[] arr1, int arr2[]){ 10 | int n = arr1.length; 11 | int m = arr2.length; 12 | 13 | for(int i = 0; i < n; i++){ 14 | System.out.println(arr1[i]); 15 | } 16 | 17 | for(int i = 0; i < m; i++){ 18 | System.out.println(arr2[i]); 19 | } 20 | } 21 | 22 | void f3(int n){ 23 | for(int i = 0; i < n; i++){ 24 | for(int j = 0; j < n; j++){ 25 | System.out.println("Hello"); 26 | } 27 | } 28 | } 29 | 30 | void f4(int n){ 31 | for(int i = 0; i < n; i++){ 32 | for(int j = 0; j < i; j++){ 33 | System.out.println("Hello"); 34 | } 35 | } 36 | } 37 | 38 | void f5(int n){ 39 | for(int i = 0; i < n; i++){ 40 | for(int j = 0; j < Math.sqrt(n); j++){ 41 | System.out.println("Hello"); 42 | } 43 | } 44 | } 45 | public static void main(String[] args) { 46 | } 47 | } -------------------------------------------------------------------------------- /Lecture 27/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | static void printDecreasing(int n){ // 5 4 3 2 1 5 | //base case 6 | if(n == 1){ 7 | System.out.println(1); 8 | return; 9 | } 10 | 11 | // self work 12 | System.out.println(n); // n 13 | 14 | // recursive work 15 | printDecreasing(n-1); // n-1, n-2,.....1 16 | 17 | } 18 | 19 | static void printIncreasing(int n){ // 1, 2, ..... n-1, n 20 | //base case 21 | if(n == 1){ 22 | System.out.println(n); 23 | return; 24 | } 25 | 26 | //recursive work 27 | printIncreasing(n-1); // 1, 2, .... n-1 28 | 29 | //self work 30 | System.out.println(n); 31 | } 32 | 33 | public static void main(String[] args) { 34 | Scanner sc = new Scanner(System.in); 35 | int n = sc.nextInt(); 36 | // printIncreasing(n); 37 | printDecreasing(n); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture 28 - Recursion PS1/src/Factorial.java: -------------------------------------------------------------------------------- 1 | public class Factorial { 2 | 3 | static int factorial(int n){ // n = 5 4 | //base case 5 | if(n == 0) return 1; 6 | //smaller problem - recursive work 7 | // big problem - self work 8 | return n * factorial(n-1); 9 | } 10 | 11 | public static void main(String[] args) { 12 | System.out.println(factorial(5)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lecture 28 - Recursion PS1/src/Fibonacci.java: -------------------------------------------------------------------------------- 1 | public class Fibonacci { 2 | static int fib(int n){ 3 | //base case 4 | if(n == 0 || n == 1){ 5 | return n; 6 | } 7 | return fib(n-1) + fib(n-2); 8 | // //subproblems - recursive work 9 | // int prev = fib(n-1); 10 | // int prevPrev = fib(n-2); 11 | // 12 | // //self work 13 | // return prev + prevPrev; 14 | } 15 | public static void main(String[] args) { 16 | for(int i = 0; i <= 10; i++) { 17 | System.out.println(fib(i)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 29 - Recursion PS2/src/Power.java: -------------------------------------------------------------------------------- 1 | public class Power { 2 | // static int pow(int p, int q){ // p^q -> TC - O(q) 3 | // if(q == 0) return 1; 4 | // return pow(p, q-1) * p; // p^q-1 * p = p^q 5 | // } 6 | 7 | static int pow(int p, int q){ 8 | if(q == 0) return 1; 9 | int smallPow = pow(p, q/2); 10 | if(q % 2 == 0){ // even 11 | return smallPow * smallPow; 12 | } 13 | return p * smallPow * smallPow; 14 | } 15 | 16 | public static void main(String[] args) { 17 | System.out.println(pow(2, 5)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 29 - Recursion PS2/src/SOD.java: -------------------------------------------------------------------------------- 1 | public class SOD { 2 | static int sod(int n){ 3 | if(n >= 0 && n <= 9) return n; 4 | return sod(n/10) + n%10; 5 | } 6 | 7 | public static void main(String[] args) { 8 | System.out.println(sod(12345)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Lecture 30 - Recursion PS3/src/Multiples.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Multiples { 4 | static void printMultiples(int n, int k){ // 5, 4 5 | //base case 6 | if(k == 0){ 7 | return; 8 | } 9 | //recursive work 10 | printMultiples(n, k-1); // 5, 10, 15 11 | //self work 12 | System.out.println(n * k); // 20 13 | } 14 | 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | int n = sc.nextInt(); 18 | int k = sc.nextInt(); 19 | printMultiples(n, k); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture 30 - Recursion PS3/src/SeriesSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SeriesSum { 4 | // main question - alternate sums 5 | static int seriesSum(int n){ 6 | if(n == 0) return 0; 7 | if(n % 2 == 0) { // even 8 | return seriesSum(n - 1) - n; 9 | } else { // odd 10 | return seriesSum(n - 1) + n; 11 | } 12 | } 13 | static int seriesSum1(int n){ 14 | if(n == 0){ 15 | return 0; 16 | } 17 | return seriesSum1(n-1) + n; 18 | } 19 | 20 | public static void main(String[] args){ 21 | Scanner sc = new Scanner(System.in); 22 | int n = sc.nextInt(); 23 | System.out.println(seriesSum(n)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 31 - Recursion PS4 GCD/src/GCD.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GCD { 4 | static int gcd(int x, int y){ 5 | if(y == 0) return x; 6 | return gcd(y, x % y); 7 | } 8 | 9 | static int iGCD(int x, int y){ 10 | while (x % y != 0){ 11 | int rem = x % y; 12 | x = y; 13 | y = rem; 14 | } 15 | return y; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | int x = sc.nextInt(); 21 | int y = sc.nextInt(); 22 | System.out.println(iGCD(x, y)); 23 | System.out.println(gcd(x, y)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 32 - Recursion PS5 - Arrays/src/ArrayRecursion.java: -------------------------------------------------------------------------------- 1 | public class ArrayRecursion { 2 | static void printArray(int[] arr, int idx){ 3 | // base case 4 | if(idx == arr.length){ 5 | return; 6 | } 7 | // self work 8 | System.out.println(arr[idx]); // 5 9 | // recursive work - sub problem 10 | printArray(arr, idx+1); // 6, 7, 8 11 | } 12 | 13 | static int maxInArray(int[] arr, int idx){ 14 | //base case 15 | if(idx == arr.length-1){ 16 | return arr[idx]; 17 | } 18 | // small problem -> idx+1, end of the array -> max -> recursive 19 | int smallAns = maxInArray(arr, idx+1); 20 | // self work and final ans 21 | return Math.max(arr[idx], smallAns); 22 | } 23 | 24 | static int sumOfArray(int[] arr, int idx){ 25 | // base case 26 | if(idx == arr.length){ 27 | return 0; 28 | } 29 | //recursive work - subproblem 30 | int smallAns = sumOfArray(arr, idx+1); 31 | // self work 32 | return smallAns + arr[idx]; 33 | } 34 | 35 | public static void main(String[] args) { 36 | int [] arr = {}; 37 | // printArray(arr, 0); 38 | // System.out.println(maxInArray(arr, 0)); 39 | System.out.println(sumOfArray(arr, 0)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Lecture 33 - Recursion PS6 - Arrays/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class Main { 3 | static void allIndices2(int[] a, int n, int x, int idx){ 4 | if(idx >= n) return; 5 | if(a[idx] == x) System.out.println(idx); 6 | allIndices2(a, n, x, idx+1); 7 | } 8 | static ArrayList allIndices(int[] a, int n, int x, int idx){ 9 | if(idx >= n){ 10 | return new ArrayList(); 11 | } 12 | ArrayList ans = new ArrayList<>(); 13 | if(a[idx] == x){ 14 | ans.add(idx); 15 | } 16 | ArrayList smallAns = allIndices(a, n, x, idx+1); 17 | ans.addAll(smallAns); 18 | return ans; 19 | } 20 | static int firstIndex(int[] a, int n, int target, int idx){ 21 | if(idx == n) return -1; 22 | if(a[idx] == target) return idx; 23 | return firstIndex(a, n, target, idx+1); 24 | } 25 | static int lastIndex(int[] a, int n, int target, int idx){ 26 | if(idx == -1) return -1; 27 | if(a[idx] == target) return idx; 28 | return lastIndex(a, n, target, idx-1); 29 | } 30 | static boolean search(int[] a, int n, int target, int idx){ 31 | if(idx >= n) return false; 32 | if(a[idx] == target) return true; 33 | return search(a, n, target, idx+1); 34 | } 35 | 36 | public static void main(String[] args) { 37 | int[] a = {}; 38 | int target = 4; 39 | int n = a.length; 40 | System.out.println(firstIndex(a, n, target, 0)); 41 | System.out.println(lastIndex(a, n, target, n-1)); 42 | allIndices2(a, n, target, 0); 43 | ArrayList ans = allIndices(a, n, target, 0); 44 | for(Integer i : ans){ 45 | System.out.print(i + ","); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Lecture 33 - Recursion PS6 - Arrays/src/Search.java: -------------------------------------------------------------------------------- 1 | public class Search { 2 | // 1, 2, 3, 2, 2, 5 3 | static void findAllIndices(int[] a, int n, int target, int idx){ 4 | // base case 5 | if(idx >= n){ 6 | return; 7 | } 8 | //self work 9 | if(a[idx] == target){ 10 | System.out.println(idx); 11 | } 12 | //recursive work 13 | findAllIndices(a, n, target, idx+1); // 1, 3, 4 14 | } 15 | 16 | 17 | 18 | // findFirstIndex -> return index of target if target present in array, otherwise return -1 19 | static int findIndex(int[] a, int n, int target, int idx){ 20 | // base case 21 | if(idx >= n) return -1; 22 | //self work 23 | if(a[idx] == target) return idx; 24 | //recursive work 25 | return findIndex(a, n, target, idx+1); 26 | } 27 | 28 | // true, false -> based on existence 29 | static boolean search(int[] a, int n, int target, int idx){ 30 | // base case 31 | if(idx >= n) return false; 32 | //self work 33 | if(a[idx] == target) return true; 34 | //recursive work 35 | return search(a, n, target, idx+1); 36 | } 37 | 38 | public static void main(String[] args) { 39 | int[] a = {1, 2, 4, 4, 4, 5, 6}; 40 | int target = 4; 41 | int n = a.length; 42 | findAllIndices(a, n, target, 0); 43 | 44 | // System.out.println(findIndex(a, n, target, 0)); 45 | 46 | // if(search(a, a.length, target, 0)){ 47 | // System.out.println("Yes"); 48 | // } else { 49 | // System.out.println("No"); 50 | // } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Lecture 33 - Recursion PS6 - Arrays/src/findAllIndices.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class findAllIndices { 4 | /* 5 | arr -> 4, 4, 3, 4 6 | target -> 4 7 | ans(arraylist) -> {0, 1, 3} 8 | */ 9 | static ArrayList allIndices(int[] a, int n, int target, int idx){ 10 | ArrayList ans = new ArrayList<>(); 11 | // base case 12 | if(idx >= n){ 13 | return ans; // return empty arraylist 14 | } 15 | // self work 16 | if(a[idx] == target){ // ans -> {0} 17 | ans.add(idx); 18 | } 19 | // recursive work 20 | ArrayList smallAns = allIndices(a, n, target, idx+1); // smallAns = {1, 3} 21 | 22 | ans.addAll(smallAns); // {0, 1, 3} 23 | return ans; 24 | } 25 | 26 | 27 | public static void main(String[] args) { 28 | int[] a = {1, 2, 4, 4, 5, 4}; 29 | int target = 4; 30 | int n = a.length; 31 | ArrayList ans = allIndices(a, n, target, 0); // ans -> all indices 32 | for (Integer i: ans) { 33 | System.out.println(i); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Lecture 34 - Recursion PS7 - Strings/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | public static void main(String[] args) { 4 | Scanner sc = new Scanner(System.in); 5 | String s = sc.nextLine(); // college 6 | System.out.println(s.substring(7)); // 1, 2, 3, 4, 5...n-1 7 | } 8 | } -------------------------------------------------------------------------------- /Lecture 34 - Recursion PS7 - Strings/src/Palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Palindrome { 4 | static boolean isPalindrome(String s, int l, int r){ 5 | if(l >= r) return true; 6 | return (s.charAt(l) == s.charAt(r) && isPalindrome(s, l+1, r-1)) ; 7 | } 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | String s = sc.nextLine(); 11 | System.out.println(isPalindrome(s, 0, s.length()-1)); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lecture 34 - Recursion PS7 - Strings/src/RemoveOccurrences.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class RemoveOccurrences { 3 | static String removeA2(String s){ 4 | if(s.length() == 0) return ""; 5 | String smallAns = removeA2(s.substring(1)); 6 | char currChar = s.charAt(0); 7 | if(currChar != 'a'){ 8 | return currChar + smallAns; 9 | } else { 10 | return smallAns; 11 | } 12 | } 13 | static String removeA(String s, int idx){ 14 | if(idx == s.length()) return ""; 15 | String smallAns = removeA(s, idx+1); 16 | char currChar = s.charAt(idx); 17 | if(currChar != 'a'){ 18 | return currChar + smallAns; 19 | } else { 20 | return smallAns; 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner sc = new Scanner(System.in); 26 | String s = sc.nextLine(); 27 | System.out.println(removeA(s, 0)); 28 | System.out.println(removeA2(s)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Lecture 34 - Recursion PS7 - Strings/src/Reverse.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Reverse { 4 | static String reverse(String s, int idx){ 5 | if(idx == s.length()) return ""; 6 | String smallAns = reverse(s, idx+1); 7 | return smallAns + s.charAt(idx); 8 | } 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | String s = sc.nextLine(); 12 | String rev = reverse(s,0); 13 | if(rev.equals(s)){ 14 | System.out.printf("%s is Palindrome", s); 15 | } else { 16 | System.out.printf("%s is Not palindrome", s); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 35 - Recursion PS8 - Subsets Subsequences/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Main { 4 | static void printSSQ(String s, String currAns){ 5 | if(s.length() == 0){ 6 | System.out.println(currAns); 7 | return; 8 | } 9 | char currChar = s.charAt(0); 10 | String remString = s.substring(1); 11 | printSSQ(remString, currAns + currChar); // Add curr char 12 | printSSQ(remString, currAns); // Do not add curr char 13 | } 14 | static void sumSubset(int[] a , int n , int idx , int sum){ 15 | if(idx >= n){ 16 | System.out.println(sum); 17 | return; 18 | } 19 | sumSubset(a, n, idx + 1, sum); 20 | sumSubset(a, n, idx + 1, sum + a[idx]); 21 | } 22 | static ArrayList getSSQ(String s){ 23 | ArrayList ans = new ArrayList<>(); 24 | if(s.length() == 0){ 25 | ans.add(""); 26 | return ans; 27 | } 28 | char curr = s.charAt(0); 29 | ArrayList smallAns = getSSQ(s.substring(1)); 30 | for(String ss: smallAns){ 31 | ans.add(ss); 32 | ans.add(curr + ss); 33 | } 34 | return ans; 35 | } 36 | 37 | 38 | 39 | public static void main(String[] args) { 40 | int[] a = {2, 4, 5}; 41 | sumSubset(a, a.length, 0, 0); 42 | } 43 | } -------------------------------------------------------------------------------- /Lecture 35 - Recursion PS8 - Subsets Subsequences/src/PrintSSQ.java: -------------------------------------------------------------------------------- 1 | public class PrintSSQ { 2 | static void printSSQ(String s, String currAns){ 3 | if(s.length() == 0){ 4 | System.out.println(currAns); 5 | return; 6 | } 7 | char curr = s.charAt(0); 8 | String remString = s.substring(1); 9 | printSSQ(remString, currAns + curr); // add curr 10 | printSSQ(remString, currAns); // do not add curr 11 | } 12 | 13 | public static void main(String[] args) { 14 | printSSQ("abc", ""); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture 35 - Recursion PS8 - Subsets Subsequences/src/ReturnSSQ.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class ReturnSSQ { 4 | /* 5 | s = "abc" 6 | */ 7 | static ArrayList getSSQ(String s){ 8 | ArrayList ans = new ArrayList<>(); 9 | // base case 10 | if(s.length() == 0){ 11 | ans.add(""); 12 | return ans; 13 | } 14 | char curr = s.charAt(0); // a 15 | ArrayList smallAns = getSSQ(s.substring(1)); // ["bc", "b", "c", ""] 16 | //smallAns = ["bc", "b", "c", ""] 17 | // ans = ["bc", "abc", "b", "ab", "c", "ac", "", a] 18 | for(String ss: smallAns){ 19 | ans.add(ss); // "" 20 | ans.add(curr + ss); // a 21 | } 22 | return ans; 23 | } 24 | 25 | public static void main(String[] args) { 26 | ArrayList ans = getSSQ("abc"); 27 | for(String ss : ans){ 28 | System.out.println(ss); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Lecture 35 - Recursion PS8 - Subsets Subsequences/src/SubsetSum.java: -------------------------------------------------------------------------------- 1 | public class SubsetSum { 2 | static void subsetSum(int[] a, int n, int idx, int sum){ 3 | if(idx >= n){ 4 | System.out.println(sum); 5 | return; 6 | } 7 | subsetSum(a, n, idx+1, sum + a[idx]); // include 8 | subsetSum(a, n, idx+1, sum); //exclude 9 | } 10 | 11 | public static void main(String[] args) { 12 | int[] a = {2, 4, 5}; 13 | subsetSum(a, a.length, 0, 0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture 36 - Recursion 9/src/FrogJump.java: -------------------------------------------------------------------------------- 1 | public class FrogJump { 2 | static int best(int[] h, int n, int idx){ 3 | if(idx == n-1) return 0; 4 | int op1 = Math.abs(h[idx] - h[idx+1]) + best(h, n, idx+1); 5 | if(idx == n-2) return op1; 6 | int op2 = Math.abs(h[idx] - h[idx+2]) + best(h, n, idx+2); 7 | return Math.min(op1, op2); 8 | } 9 | 10 | public static void main(String[] args) { 11 | int[] h = {10, 30, 40, 20}; 12 | System.out.println(best(h, h.length, 0)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lecture 36 - Recursion 9/src/KeypadCombinations.java: -------------------------------------------------------------------------------- 1 | public class KeypadCombinations { 2 | static void combination(String dig, String[] kp, String res){ // "253" -> kp[2] 3 | if(dig.length() == 0){ 4 | System.out.print(res + " "); 5 | return; 6 | } 7 | int currNum = dig.charAt(0) - '0'; // 2 8 | String currChoices = kp[currNum]; // "abc" 9 | for(int i = 0; i < currChoices.length(); i++){ // "abc" 10 | combination(dig.substring(1), kp, res + currChoices.charAt(i)); 11 | } 12 | } 13 | public static void main(String[] args) { 14 | String dig = "253"; 15 | // kp[7] = "pqrs" 16 | String[] kp = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 17 | // 0 1 2 3 4 5 6 7 8 9 18 | combination(dig, kp, ""); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 36 - Recursion 9/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | static void combination(String dig, String[] kp, String res) { 5 | if (dig.length() == 0) { 6 | System.out.print(res + " "); 7 | return; 8 | } 9 | int currNum = dig.charAt(0) - '0'; 10 | String currChoices = kp[currNum]; 11 | for (int j = 0; j < currChoices.length(); j++) { 12 | combination(dig.substring(1), kp, res + currChoices.charAt(j)); 13 | } 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in); 18 | String s = sc.nextLine(); 19 | String[] keypad = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqr", "stuv", "wxyz"}; 20 | combination(s, keypad, ""); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lecture 37 - Bubble Sort/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void bubbleSort(int[] a){ 3 | int n = a.length; 4 | // n-1 iterations/passes 5 | for(int i = 0; i < n-1; i++){ 6 | boolean flag = false; // has any swapping happened 7 | for(int j = 0; j < n-i-1; j++){ 8 | /* 9 | last i elements are already at correct sorted positions, 10 | so no need to check them 11 | */ 12 | if(a[j] > a[j+1]){ 13 | // swap - a[j], a[j+1] 14 | int temp = a[j]; 15 | a[j] = a[j+1]; 16 | a[j+1] = temp; 17 | flag = true; // some swap has happened 18 | } 19 | } 20 | if(!flag){ // have any swaps happened? 21 | return; 22 | } 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | int[] a = {5, 4, 1, 2, 3, 6, 0}; 28 | bubbleSort(a); 29 | for (int i : a) { 30 | System.out.print(i + " "); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Lecture 38 - Selection Sort/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void selectionSort(int[] arr){ 3 | int n = arr.length; 4 | for(int i = 0; i < n-1; i++){ // i represent the current index 5 | //Find the minimum element in unsorted part of array 6 | int min_index = i; 7 | for(int j = i+1; j < n; j++){ 8 | if(arr[j] < arr[min_index]){ 9 | min_index = j; 10 | } 11 | } 12 | //swap current element and minimum element -> current index i will have correct element 13 | // a[min_index], a[i] 14 | int temp = arr[i]; 15 | arr[i] = arr[min_index]; 16 | arr[min_index] = temp; 17 | } 18 | } 19 | public static void main(String[] args) { 20 | int[] arr = {7, 4, 1, 2, 100, 90}; 21 | selectionSort(arr); 22 | for(int i = 0; i < arr.length; i++){ 23 | System.out.print(arr[i] + " "); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 39 - Insertion Sort/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void insertionSort(int[] a){ 3 | int n = a.length; 4 | for(int i = 1; i < n; i++){ 5 | int j = i; 6 | while(j > 0 && a[j] < a[j-1]){ 7 | int temp = a[j]; 8 | a[j] = a[j-1]; 9 | a[j-1] = temp; 10 | j--; 11 | } 12 | } 13 | } 14 | 15 | public static void main(String[] args) { 16 | int[] a = {8, 3, 6, 5, 4, 2}; 17 | insertionSort(a); 18 | for(int val : a){ 19 | System.out.print(val + " "); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture 40/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void sortFruits(String[] fruits){ 3 | int n = fruits.length; 4 | for(int i = 0; i < n-1; i++){ 5 | int min_index = i; 6 | for(int j = i+1; j < n; j++){ 7 | if(fruits[j].compareTo(fruits[min_index]) < 0){ 8 | min_index = j; 9 | } 10 | } 11 | // swap fruits[min_index], fruits[i] 12 | String temp = fruits[i]; 13 | fruits[i] = fruits[min_index]; 14 | fruits[min_index] = temp; 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | String[] fruits = {"kiwi", "apple", "papaya", "mango"}; 20 | sortFruits(fruits); 21 | for(String val : fruits){ 22 | System.out.print(val + " "); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 40/src/moveZeroes.java: -------------------------------------------------------------------------------- 1 | public class moveZeroes { 2 | public static void main(String[] args) { 3 | int[] arr = {0, 0, 0, 5, 0, 0}; 4 | int n = arr.length; 5 | for(int i = 0; i < n-1; i++){ 6 | for(int j = 0; j < n-i-1; j++){ 7 | if(arr[j] == 0 && arr[j+1] != 0){ 8 | // swap arr[j], arr[j+1] 9 | int temp = arr[j]; 10 | arr[j] = arr[j+1]; 11 | arr[j+1] = temp; 12 | } 13 | } 14 | } 15 | for(int val : arr){ 16 | System.out.print(val + " "); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Lecture 41 - Merge Sort/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void displayArr(int[] arr){ 3 | for(int val : arr){ 4 | System.out.print(val + " "); 5 | } 6 | } 7 | static void merge(int[] arr, int l, int mid, int r){ 8 | int n1 = mid-l+1; 9 | int n2 = r-mid; 10 | int[] left = new int[n1]; 11 | int[] right = new int[n2]; 12 | int i, j, k; 13 | for(i = 0; i < n1; i++) left[i] = arr[l+i]; 14 | for(j = 0; j < n2; j++) right[j] = arr[mid+1+j]; 15 | i = 0; 16 | j = 0; 17 | k = l; 18 | while(i < n1 && j < n2){ 19 | if(left[i] < right[j]) 20 | arr[k++] = left[i++]; 21 | else 22 | arr[k++] = right[j++]; 23 | } 24 | while(i < n1) 25 | arr[k++] = left[i++]; 26 | while (j < n2) 27 | arr[k++] = right[j++]; 28 | } 29 | static void mergeSort(int[] arr, int l, int r){ 30 | if(l >= r) return; 31 | int mid = (l+r)/2; 32 | mergeSort(arr, l, mid); 33 | mergeSort(arr, mid+1, r); 34 | merge(arr, l, mid, r); 35 | } 36 | 37 | public static void main(String[] args) { 38 | int[] arr = {4, 1, 3, 5, 2}; 39 | int n = arr.length; 40 | System.out.println("Array before sorting"); 41 | displayArr(arr); // 4 1 3 5 2 42 | mergeSort(arr, 0, n-1); 43 | System.out.println(); 44 | System.out.println("Array after sorting"); 45 | displayArr(arr); // 1 2 3 4 5 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Lecture 42 Quick Sort/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void displayArr(int[] arr){ 3 | for(int val : arr){ 4 | System.out.print(val + " "); 5 | } 6 | } 7 | static void swap(int[] arr, int x, int y){ 8 | int temp = arr[x]; 9 | arr[x] = arr[y]; 10 | arr[y] = temp; 11 | } 12 | static int partition(int[] arr, int st, int end){ 13 | int pivot = arr[st]; 14 | int cnt = 0; 15 | for(int i = st+1; i <= end; i++){ 16 | if(arr[i] <= pivot) cnt++; 17 | } 18 | int pivotIdx = st + cnt; 19 | swap(arr, st, pivotIdx); 20 | int i = st, j = end; 21 | while(i < pivotIdx && j > pivotIdx){ 22 | while (arr[i] <= pivot) i++; 23 | while (arr[j] > pivot) j--; 24 | if(i < pivotIdx && j > pivotIdx){ 25 | swap(arr, i, j); 26 | i++; 27 | j--; 28 | } 29 | } 30 | return pivotIdx; 31 | } 32 | static void quickSort(int[] arr, int st, int end){ 33 | if(st >= end) return; 34 | int pi = partition(arr, st, end); 35 | quickSort(arr, st, pi-1); 36 | quickSort(arr, pi+1, end); 37 | } 38 | 39 | public static void main(String[] args) { 40 | int[] arr = {6, 6, 3, 1, 5, 5, 4}; 41 | System.out.println("Array before sorting"); 42 | displayArr(arr); 43 | System.out.println(); 44 | quickSort(arr, 0, arr.length-1); 45 | System.out.println("Array after sorting"); // 1 3 4 5 6 46 | displayArr(arr); 47 | } 48 | } -------------------------------------------------------------------------------- /Lecture 43 Non-Comparison Sorting/src/BucketSort.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | 4 | public class BucketSort { 5 | static void bucketSort(float[] arr){ 6 | int n = arr.length; 7 | // Buckets 8 | ArrayList[] buckets = new ArrayList[n]; 9 | // Create empty buckets 10 | for(int i = 0; i < n; i++){ 11 | buckets[i] = new ArrayList(); 12 | } 13 | // Add elements into our buckets 14 | for(int i = 0; i < n; i++){ 15 | int bucketIndex = (int) arr[i] * n; 16 | buckets[bucketIndex].add(arr[i]); 17 | } 18 | // Sort each bucket individually 19 | for(int i = 0; i < buckets.length; i++){ 20 | Collections.sort(buckets[i]); 21 | } 22 | // Merge all buckets to get final sorted array 23 | int index = 0; 24 | for(int i = 0; i < buckets.length; i++){ 25 | ArrayList currBucket = buckets[i]; 26 | for(int j = 0; j < currBucket.size(); j++){ 27 | arr[index++] = currBucket.get(j); 28 | } 29 | } 30 | } 31 | 32 | public static void main(String[] args) { 33 | float[] arr = {0.5f, 0.4f, 0.3f, 0.2f, 0.1f}; 34 | bucketSort(arr); 35 | for(float val : arr){ 36 | System.out.print(val + " "); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture 43 Non-Comparison Sorting/src/CountSort.java: -------------------------------------------------------------------------------- 1 | public class CountSort { 2 | static int findMax(int[] arr){ 3 | int mx = Integer.MIN_VALUE; 4 | for(int i = 0; i < arr.length; i++){ 5 | if(arr[i] > mx){ 6 | mx = arr[i]; 7 | } 8 | } 9 | return mx; 10 | } 11 | static void basicCountSort(int[] arr){ 12 | int max = findMax(arr); // Find the largest element of the arr 13 | int[] count = new int[max+1]; 14 | for(int i = 0; i < arr.length; i++){ 15 | count[arr[i]]++; 16 | } 17 | int k = 0; 18 | for(int i = 0; i < count.length; i++){ 19 | for(int j = 0; j < count[i]; j++){ 20 | arr[k++] = i; 21 | } 22 | } 23 | } 24 | static void display(int[] a){ 25 | for(int val : a){ 26 | System.out.print(val + " "); 27 | } 28 | System.out.println(); 29 | } 30 | static void countSort(int[] arr){ 31 | int n = arr.length; 32 | int[] output = new int[n]; 33 | int max = findMax(arr); // Find the largest element of the arr 34 | int[] count = new int[max+1]; 35 | for(int i = 0; i < arr.length; i++){ // Make frequency array -> TC -> n 36 | count[arr[i]]++; 37 | } 38 | // Make prefix sum array of count array 39 | for(int i = 1; i < count.length; i++){ // TC -> max 40 | count[i] += count[i-1]; 41 | } 42 | // Find the index of each element in the original array and put it in output array 43 | for(int i = n-1; i >= 0; i--){ // TC -> n 44 | int idx = count[arr[i]] - 1; 45 | output[idx] = arr[i]; 46 | count[arr[i]]--; 47 | } 48 | // copy all elements of output to arr 49 | for(int i = 0; i < n; i++){ // TC -> n 50 | arr[i] = output[i]; 51 | } 52 | } 53 | 54 | public static void main(String[] args) { 55 | int[] arr = {1, 4, 5, 2, 2, 5}; 56 | countSort(arr); 57 | // basicCountSort(arr); 58 | display(arr); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Lecture 43 Non-Comparison Sorting/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void bbucketSort(float[] arr) { 5 | //Let's create 10 buckets 6 | ArrayList> buckets = new ArrayList<>(); 7 | // Initialize each bucket 8 | for (int i = 0; i < 10; i++) { 9 | buckets.add(new ArrayList<>()); 10 | } 11 | // Dividing arr into buckets 12 | for (int i = 0; i < arr.length; i++) { 13 | int index; 14 | if (arr[i] >= 1) { 15 | index = ((int) arr[i]) % 10; 16 | } else { 17 | index = (int) (arr[i] * 10); 18 | } 19 | buckets.get(index).add(arr[i]); 20 | } 21 | // Sorting each bucket 22 | for (int i = 0; i < 10; i++) { 23 | Collections.sort(buckets.get(i)); 24 | } 25 | // Merging buckets together 26 | int index = 0; 27 | for (int bucket = 0; bucket < 10; bucket++) { 28 | ArrayList currBucket = buckets.get(bucket); 29 | for (int i = 0; i < currBucket.size(); i++) { 30 | arr[index++] = currBucket.get(i); 31 | } 32 | } 33 | } 34 | static int findMax(int[] arr) { 35 | int max = arr[0]; 36 | for (int i = 1; i < arr.length; i++) { 37 | if (arr[i] > max) max = arr[i]; 38 | } 39 | return max; 40 | } 41 | static void countSort(int[] arr, int place) { 42 | int n = arr.length; 43 | int[] output = new int[n]; 44 | int[] count = new int[10]; 45 | for (int i = 0; i < n; i++) { // Store the count of each element 46 | count[(arr[i] / place) % 10]++; 47 | } 48 | for (int i = 1; i < count.length; i++) { // prefix sum 49 | count[i] += count[i - 1]; 50 | } 51 | // Find the index of each element of the original arr in count arr, and place the elements in output arr 52 | for (int i = n - 1; i >= 0; i--) { 53 | int idx = count[(arr[i] / place) % 10] - 1; 54 | output[idx] = arr[i]; 55 | count[(arr[i] / place) % 10]--; 56 | } 57 | for (int i = 0; i < n; i++) { 58 | arr[i] = output[i]; 59 | } 60 | } 61 | static void display(int[] arr) { 62 | for (int val : arr) { 63 | System.out.print(val + " "); 64 | } 65 | System.out.println(); 66 | } 67 | static void display(float[] arr) { 68 | for (float val : arr) { 69 | System.out.print(val + " "); 70 | } 71 | System.out.println(); 72 | } 73 | static void radixSort(int arr[]) { 74 | // Get maximum element 75 | int max = findMax(arr); 76 | // Apply counting sort to sort elements based on place value. 77 | for (int place = 1; max / place > 0; place *= 10) 78 | countSort(arr, place); 79 | } 80 | static void bucketSort(float[] arr) { 81 | int n = arr.length; 82 | ArrayList[] bucket = new ArrayList[n]; 83 | // Create empty buckets 84 | for (int i = 0; i < n; i++) 85 | bucket[i] = new ArrayList(); 86 | // Add elements into the buckets 87 | for (int i = 0; i < n; i++) { 88 | int bucketIndex = (int) arr[i] * n; 89 | bucket[bucketIndex].add(arr[i]); 90 | } 91 | // Sort the elements of each bucket 92 | for (int i = 0; i < bucket.length; i++) { 93 | Collections.sort((bucket[i])); 94 | } 95 | // Merge all buckets to get sorted array 96 | int index = 0; 97 | for (int i = 0; i < bucket.length; i++) { 98 | ArrayList currBucket = bucket[i]; 99 | for (int j = 0; j < currBucket.size(); j++) { 100 | arr[index++] = currBucket.get(j); 101 | } 102 | } 103 | } 104 | 105 | public static void main(String[] args) { 106 | // int[] arr = {0, 1236, 41, 19, 242, 0, 6, 5, 232}; 107 | // countSort(arr); 108 | // radixSort(arr); 109 | // display(arr); 110 | // float[] a1 = {5, 4, 3, 2, 1}; 111 | float[] a2 = {0.5f, 0.4f, 0.3f, 0.2f, 0.1f}; 112 | //bucketSort(a1); 113 | bucketSort(a2); 114 | // display(a1); 115 | display(a2); 116 | } 117 | } -------------------------------------------------------------------------------- /Lecture 43 Non-Comparison Sorting/src/RadixSort.java: -------------------------------------------------------------------------------- 1 | public class RadixSort { 2 | static int findMax(int[] arr){ 3 | int mx = Integer.MIN_VALUE; 4 | for(int i = 0; i < arr.length; i++){ 5 | if(arr[i] > mx){ 6 | mx = arr[i]; 7 | } 8 | } 9 | return mx; 10 | } 11 | static void countSort(int[] arr, int place){ 12 | int n = arr.length; 13 | int[] output = new int[n]; 14 | int[] count = new int[10]; 15 | for(int i = 0; i < arr.length; i++){ // Make frequency array 16 | count[(arr[i]/place)%10]++; 17 | } 18 | // Make prefix sum array of count array 19 | for(int i = 1; i < count.length; i++){ 20 | count[i] += count[i-1]; 21 | } 22 | for(int i = n-1; i >= 0; i--){ 23 | int idx = count[(arr[i]/place)%10] - 1; 24 | output[idx] = arr[i]; 25 | count[(arr[i]/place)%10]--; 26 | } 27 | // copy all elements of output to arr 28 | for(int i = 0; i < n; i++){ 29 | arr[i] = output[i]; 30 | } 31 | } 32 | 33 | static void radixSort(int[] arr){ 34 | int max = findMax(arr); // get maximum element 35 | // apply counting sort to sort elements based on place value 36 | for(int place = 1; max/place > 0; place *= 10){ 37 | countSort(arr, place); 38 | } 39 | } 40 | 41 | public static void main(String[] args) { 42 | int[] arr = {43, 453, 626, 894, 0, 3}; 43 | radixSort(arr); 44 | for(int val : arr){ 45 | System.out.print(val + " "); 46 | } 47 | System.out.println(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Lecture 44 Sorting Problems/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static void displayArr(int[] num){ 3 | for(int val : num){ 4 | System.out.print(val + " "); 5 | } 6 | System.out.println(); 7 | } 8 | static void sort012_(int[] num){ 9 | int count_0 = 0, count_1 = 0, count_2 = 0; 10 | for (int j : num) { 11 | if (j == 0) { 12 | count_0++; 13 | } else if (j == 1) { 14 | count_1++; 15 | } else { 16 | count_2++; 17 | } 18 | } 19 | int k = 0; 20 | while(count_0 > 0){ 21 | num[k++] = 0; 22 | count_0--; 23 | } 24 | while(count_1 > 0){ 25 | num[k++] = 1; 26 | count_1--; 27 | } 28 | while(count_2 > 0){ 29 | num[k++] = 2; 30 | count_2--; 31 | } 32 | } 33 | public static void main(String[] args) { 34 | int[] num2_ = {2, 2, 1, 2, 2, 0, 1, 0, 2, 0}; 35 | sort012_(num2_); 36 | displayArr(num2_); 37 | } 38 | } -------------------------------------------------------------------------------- /Lecture 44 Sorting Problems/src/Sort012.java: -------------------------------------------------------------------------------- 1 | public class Sort012 { 2 | static void display(int[] num){ 3 | for(int val : num){ 4 | System.out.print(val + " "); 5 | } 6 | System.out.println(); 7 | } 8 | static void swap(int[] a, int x, int y){ 9 | int temp = a[x]; 10 | a[x] = a[y]; 11 | a[y] = temp; 12 | } 13 | static void sort012(int[] a){ 14 | int lo = 0, mid = 0, hi = a.length-1; 15 | //explore the unknown region 16 | while(mid <= hi){ 17 | if(a[mid] == 0){ 18 | swap(a, mid, lo); 19 | mid++; 20 | lo++; 21 | } else if(a[mid] == 1){ 22 | mid++; 23 | } else { 24 | swap(a, mid, hi); 25 | hi--; 26 | } 27 | } 28 | } 29 | 30 | public static void main(String[] args) { 31 | int[] num = {2, 2, 0, 0, 1, 1, 2, 0, 1, 0}; 32 | sort012(num); 33 | display(num); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture 44 Sorting Problems/src/SortArr.java: -------------------------------------------------------------------------------- 1 | public class SortArr { 2 | static void display(int[] num){ 3 | for(int val : num){ 4 | System.out.print(val + " "); 5 | } 6 | System.out.println(); 7 | } 8 | static void sortArr(int[] num){ 9 | int n = num.length; 10 | int x = -1, y = -1; 11 | if(n <= 1){ // corner case, edge case 12 | return; 13 | } 14 | //process all adjacent elements 15 | for(int i = 1; i < n; i++){ 16 | if(num[i-1] > num[i]){ 17 | if(x == -1){ // first conflict 18 | x = i-1; 19 | y = i; 20 | } else { // 2nd conflict 21 | y = i; 22 | } 23 | } 24 | } 25 | // swap x, y in num 26 | int temp = num[x]; 27 | num[x] = num[y]; 28 | num[y] = temp; 29 | } 30 | 31 | public static void main(String[] args) { 32 | int[] num = {3}; 33 | sortArr(num); 34 | display(num); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Lecture 44 Sorting Problems/src/SortPosNeg.java: -------------------------------------------------------------------------------- 1 | public class SortPosNeg { 2 | static void display(int[] num){ 3 | for(int val : num){ 4 | System.out.print(val + " "); 5 | } 6 | System.out.println(); 7 | } 8 | static void partition(int[] num){ 9 | int l = 0, r = num.length-1; 10 | while(l < r){ 11 | while(num[l] < 0) l++; 12 | while (num[r] >= 0) r--; 13 | if(l < r){ 14 | int temp = num[l]; 15 | num[l] = num[r]; 16 | num[r] = temp; 17 | l++; 18 | r--; 19 | } 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | int[] num = {-13, 20, 7, 0, -4, -13, 11, -5, -13}; 25 | partition(num); 26 | display(num); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Lecture 45 - Binary Search/src/BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | static boolean binarySearch(int[] a, int target){ 3 | int n = a.length; 4 | int st = 0, end = n-1; // 0 based indexing 5 | while(st <= end){ 6 | int mid = (st + end)/2; 7 | if(target == a[mid]){ 8 | return true; 9 | } else if(target < a[mid]){ 10 | end = mid-1; 11 | } else { 12 | st = mid+1; 13 | } 14 | } 15 | return false; 16 | } 17 | static boolean recBinarySearch(int[] a, int st, int end, int target){ 18 | if(st > end) return false; // base case 19 | int mid = (st + end)/2; 20 | if(target == a[mid]){ 21 | return true; 22 | } else if (target < a[mid]){ 23 | return recBinarySearch(a, st, mid-1, target); // subproblems 24 | } else { 25 | return recBinarySearch(a, mid+1, end, target); // subproblems 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | int[] a = {1, 2, 3, 4, 5}; 31 | int target = 0; 32 | while(target != 10) { 33 | System.out.printf("%d exists in arr: %b \n", target, recBinarySearch(a, 0, a.length-1, target)); 34 | System.out.printf("%d exists in arr: %b \n", target, binarySearch(a, target)); 35 | System.out.println(); 36 | target++; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture 45 - Binary Search/src/FindSquareRoot.java: -------------------------------------------------------------------------------- 1 | public class FindSquareRoot { 2 | static int sqrt(int x){ 3 | int st = 0, end = x; 4 | int ans = -1; 5 | while (st <=end){ 6 | int mid = st + (end-st)/2; 7 | int val = mid * mid; // use long here to avoid overflow 8 | if(val == x){ 9 | return mid; 10 | } else if(val < x){ 11 | ans = mid; 12 | st = mid+1; 13 | } else { 14 | end = mid-1; 15 | } 16 | } 17 | return ans; 18 | } 19 | 20 | public static void main(String[] args) { 21 | int x = 25; 22 | System.out.println(sqrt(x)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lecture 45 - Binary Search/src/FirstOccurence.java: -------------------------------------------------------------------------------- 1 | public class FirstOccurence { 2 | static int firstOcc(int[] a, int x){ 3 | int n = a.length; 4 | int st = 0, end = n-1; 5 | int fo = -1; 6 | while(st <= end){ 7 | int mid = st + (end-st)/2; 8 | if(a[mid] == x){ 9 | fo = mid; 10 | end = mid-1; 11 | } else if(x < a[mid]){ 12 | end = mid-1; 13 | } else { 14 | st = mid+1; 15 | } 16 | } 17 | return fo; 18 | } 19 | 20 | public static void main(String[] args) { 21 | int[] a = {5, 5, 5, 5, 6, 2, 4}; 22 | int x = 15; 23 | System.out.println(firstOcc(a, x)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 45 - Binary Search/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static int findSquareRoot(int x) { 3 | int st = 0, end = x, ans = -1; 4 | while (st <= end) { 5 | int mid = st + (end - st) / 2; 6 | int val = mid * mid; // you can use long to avoid overflow error 7 | if (val == x) 8 | return mid; 9 | else if (val < x) { 10 | ans = mid; 11 | st = mid + 1; 12 | } else 13 | end = mid - 1; 14 | } 15 | return ans; 16 | } 17 | static int firstOcc(int[] a, int val){ 18 | int st = 0, end = a.length-1; 19 | int fo = -1; 20 | while(st <= end){ 21 | int mid = st + (end-st)/2; 22 | if(val == a[mid]){ 23 | fo = mid; 24 | end = mid-1; 25 | } else if(val < a[mid]){ 26 | end = mid-1; 27 | } else { 28 | st = mid+1; 29 | } 30 | } 31 | return fo; 32 | } 33 | static boolean recBinarySearch(int[] a, int st, int end, int val) { 34 | if (st > end) return false; 35 | int mid = (st + end) / 2; // find middle element of the array 36 | if (a[mid] == val) 37 | return true; // value found 38 | else if (val < a[mid]) 39 | return recBinarySearch(a, st, mid - 1, val); 40 | else 41 | return recBinarySearch(a, mid + 1, end, val); 42 | } 43 | 44 | static boolean binarySearch(int[] a, int val) { 45 | int n = a.length; 46 | int st = 0, end = n - 1; // 0 based indexing 47 | while (st <= end) { 48 | int mid = (st + end) / 2; // find middle element of the array 49 | if (a[mid] == val) 50 | return true; // value found 51 | else if (val < a[mid]) 52 | end = mid - 1; 53 | else 54 | st = mid + 1; 55 | } 56 | return false; // value not found in the array 57 | } 58 | 59 | public static void main(String[] args) { 60 | int[] a = {1, 2, 3, 4, 5}; 61 | int val = 16; 62 | while(val != 30){ 63 | System.out.printf("sqrt of %d is %d\n", val, findSquareRoot(val)); 64 | int fo = firstOcc(a, val); 65 | // System.out.printf("First occurrence of %d is %d\n", val, fo); 66 | // System.out.println(binarySearch(a, val)); 67 | // System.out.println(recBinarySearch(a, 0, a.length-1, val)); 68 | val++; 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /Lecture 46 Rotated Binary Search/src/RotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | public class RotatedSortedArray { 2 | static int search(int[] a, int target){ 3 | int st = 0, end = a.length-1; 4 | while(st <= end){ 5 | int mid = st + (end-st)/2; 6 | if(a[mid] == target){ 7 | return mid; 8 | } 9 | else if (a[mid] < a[end]){ // mid to end is sorted 10 | if(target > a[mid] && target <= a[end]){ 11 | st = mid+1; 12 | } else { 13 | end = mid-1; 14 | } 15 | } else { // st to mid is sorted 16 | if(target >= a[st] && target < a[mid]){ 17 | end = mid-1; 18 | } else { 19 | st = mid+1; 20 | } 21 | } 22 | } 23 | return -1; 24 | } 25 | // duplicate elements 26 | static int search_(int[] a, int target){ 27 | int st = 0, end = a.length-1; 28 | while(st <= end){ 29 | int mid = st + (end-st)/2; 30 | if(a[mid] == target){ 31 | return mid; 32 | } 33 | else if(a[st] == a[mid] && a[end] == mid){ 34 | st++; 35 | end--; 36 | } 37 | else if (a[mid] <= a[end]){ // mid to end is sorted 38 | if(target > a[mid] && target <= a[end]){ 39 | st = mid+1; 40 | } else { 41 | end = mid-1; 42 | } 43 | } else { // st to mid is sorted 44 | if(target >= a[st] && target < a[mid]){ 45 | end = mid-1; 46 | } else { 47 | st = mid+1; 48 | } 49 | } 50 | } 51 | return -1; 52 | } 53 | 54 | public static void main(String[] args) { 55 | int[] a = {1, 1, 1, 2, 2, 3, 1}; 56 | int target = 10; 57 | // System.out.println(search(a, target)); 58 | System.out.println(search_(a, target)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Lecture 46 Rotated Binary Search/src/findMin.java: -------------------------------------------------------------------------------- 1 | public class findMin{ 2 | static int findMinimum(int[] a){ 3 | int n = a.length; 4 | int st = 0, end = n-1; 5 | int ans = -1; 6 | while(st <= end){ 7 | int mid = st + (end-st)/2; 8 | if(a[mid] <= a[n-1]){ 9 | ans = mid; 10 | end = mid-1; 11 | } else { 12 | st = mid+1; 13 | } 14 | } 15 | return ans; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int[] a = {3, 4, 5, 6, 1, 2}; 20 | System.out.println(findMinimum(a)); 21 | } 22 | } 23 | --------------------------------------------------------------------------------