├── ArraysRandom.java ├── AshishCHAllenge2.java ├── AshishChallenge.java ├── Dynamic_Programming └── Q1_fibonacci_number.java ├── MazeProblemOfRatSelf.java ├── RarMazeProblem.java ├── RatInMazeProblem.java ├── Recursion_By_Anuj_bhaiya ├── NQueens.java └── permutationsOfString.java ├── Recursion_By_KK ├── Lecture10 │ ├── MazeProblem.java │ ├── MazeProblem_printPath_inMatrixAnd_String_also.java │ ├── MazeProblem_returnList.java │ ├── Maze_With_Obstacle.java │ ├── Printing_All_The_paths.java │ └── printPathIn_Matrix.java ├── Lecture11 │ ├── K_knights_Problem │ │ ├── K_knights.java │ │ └── N_knights_returnCount.java │ ├── N_Queens_Problem │ │ ├── N_queens.java │ │ ├── N_queens_return_list.java │ │ ├── Nqueens_by_self.java │ │ ├── Return2D_list_nquens.java │ │ ├── nqueensReturnList2.java │ │ └── printOnlylOneBoard.java │ └── SudokuSolver.java ├── Lecture2 │ ├── CountZeroes.java │ ├── Palindrome.java │ ├── PrintNunm.java │ ├── ReverseNumber.java │ ├── SumOfDigits.java │ ├── factorialOfNum.java │ ├── number_of_steps_to_reduce_number_to_zero.java │ └── sumOfNum.java ├── Lecture3 │ ├── IsSortedOrNot.java │ ├── LinearSearch.java │ ├── RotatedBinarySearch.java │ └── findAllOccurence.java ├── Lecture4 │ ├── BubbleSort.java │ ├── SelectionSort.java │ └── TrianglePattern.java ├── Lecture5 │ ├── MergeSort.java │ ├── MergeSort_ApnaCollege.java │ ├── MergeSort_CWH.java │ ├── MergeSort_Final.java │ ├── MergeSort_love_babbar.java │ ├── Merge_Sort_Algorithm.java │ └── Merge_Sort_Inplace.java ├── Lecture6 │ ├── QuickSort_GFG.java │ ├── QuickSort_final.java │ ├── QuickSort_final_loveBabbar.java │ ├── Quick_Sort_KK.java │ ├── Quick_Sort_KK2.java │ ├── Quick_Sort_cwh.java │ └── Quick_sort_love_babbar.java ├── Lecture7 │ ├── R1_remove_a_from_string.java │ ├── R2_Skip_String.java │ ├── R3_skipAppNotApple.java │ ├── R4_String_Subsequence.java │ ├── R5_iterative_program_to_print_subsets.java │ ├── Return_Subsequence_In_2D_List_leetcode.java │ ├── leetcode_subseq.java │ └── subStringDemno.java ├── Lecture8 │ └── Print_Permutations.java └── Lecture9 │ ├── Dice_throw_for_targetSum.java │ ├── Dice_throw_leetCode.java │ ├── Phone_Pad_question.java │ ├── phonePadQuestionRevision.java │ └── phone_pas_leetcode.java ├── Recursion_By_Striver ├── Backtracking │ ├── DS_01_print_till_n.java │ └── DS_02_print_Num_Till_0.java ├── Lecture2 │ ├── DS_01_print_name_five_times.java │ ├── DS_02_print_Num_Till_N.java │ └── DS_03_print_Num_Till_0.java ├── Lecture3 │ ├── DS_01_sum_of_num.java │ └── DS_02_factorial_of_n.java ├── Lecture4 │ ├── DS_01_reverseArray.java │ └── DS_02_palindrome_String.java ├── Lecture5 │ └── DS_01_fibonacciSeries.java ├── Lecture6 │ ├── DS_01_subsequence.java │ └── DS_02_subsequence2.java └── Lecture7 │ ├── DS_01_print_All_Subsequence_Having_Sum_Equals_K.java │ ├── DS_02_print_Subsequence.java │ ├── DS_03_print_only_one_Subsequence.java │ └── DS_04_print_count_of_Subsequence.java └── Recursion_by_ApnaCollege └── Class2_Questions ├── Q1_tower_of_hanoi.java ├── Q2_printReverseString.java ├── Q3_first_and_last_occurence.java ├── Q4_check_if_array_isSorted.java ├── Q5_move_char_at_end.java ├── Q6_removeDuplicates.java ├── Q7_printSubsequence.java ├── Q8_unique_subsequences.java └── Q9_phoneKeyPad.java /ArraysRandom.java: -------------------------------------------------------------------------------- 1 | import javax.imageio.metadata.IIOMetadataFormatImpl; 2 | import java.util.Arrays; 3 | import java.util.Random; 4 | 5 | public class ArraysRandom { 6 | public static void main(String[] args) { 7 | String S = "abacc"; 8 | canPalindrome(5,S); 9 | } 10 | static void canPalindrome(int n, String s) { 11 | int[] arr = new int[26]; 12 | for(int i = 0 ; i < s.length() ; i++){ 13 | int ind = s.charAt(i) - 'a'; 14 | arr[ind] = arr[ind] + 1; 15 | } 16 | System.out.println(Arrays.toString(arr)); 17 | int count = 0; 18 | for (int i = 0; i < arr.length; i++) { 19 | if (arr[i] % 2 == 0 ){ 20 | count++; 21 | } 22 | } 23 | System.out.println(count); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /AshishCHAllenge2.java: -------------------------------------------------------------------------------- 1 | public class AshishCHAllenge2 { 2 | public static void main(String[] args) { 3 | for (int i = 0; i < 5; i++) { 4 | if (i < 4){ // spaces 5 | for (int j = 5 - 1; j >= i; j--) { 6 | System.out.print(" "); 7 | } 8 | 9 | // left triangle 10 | for (int j = i; j >= 0; j--) { 11 | if (j == i) System.out.print("1"); 12 | else System.out.print("#"); 13 | } 14 | 15 | // right triangle 16 | for (int j = 1; j <= i; j++) { 17 | if (j == i) System.out.print(i + 1); 18 | else System.out.print("#"); 19 | } 20 | } 21 | 22 | // last line 23 | if (i==4){ 24 | int count = 1; 25 | System.out.print(" "); 26 | for (int j = 0; j <= 2*i; j++) { 27 | if (j%2==0) 28 | System.out.print(count++); 29 | else System.out.print("#"); 30 | } 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /AshishChallenge.java: -------------------------------------------------------------------------------- 1 | public class AshishChallenge { 2 | public static void main(String[] args) { 3 | for (int i = 1; i <= 5; i++) { 4 | for (int j = 0; j <= 5 - i; j++) { 5 | System.out.print(" "); 6 | } 7 | int end = i * 2 - 1; 8 | for (int j = 0; j < end; j++) { 9 | int count = 1; 10 | if (j == 0) 11 | System.out.print(j + 1); 12 | System.out.print("#"); 13 | if (j == end - 1) 14 | System.out.print(i); 15 | if (i == 5 && j % 2 == 0) { 16 | System.out.print(count++); 17 | } 18 | } 19 | System.out.println(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Dynamic_Programming/Q1_fibonacci_number.java: -------------------------------------------------------------------------------- 1 | package Dynamic_Programming; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Q1_fibonacci_number { 7 | public static void main(String[] args) { 8 | // 0 1 1 2 3 5 8 13 21 34 55 89 9 | // Scanner sc = new Scanner(System.in); 10 | // int n = sc.nextInt(); 11 | // int[] dp = new int[n+1]; 12 | // Arrays.fill(dp, -1); 13 | // int ans = fibo(n,dp); 14 | // System.out.println(); 15 | // System.out.println(ans); 16 | //// System.out.println(fibo1(50)); 17 | System.out.println(fibo2(40)); 18 | System.out.println(fibo3(41)); 19 | } 20 | 21 | // fibonacci using memoize method of dynamic programming 22 | static int fibo(int n, int[] dp) { 23 | if (n <= 1) return n; 24 | if (dp[n] != -1) return dp[n]; 25 | System.out.print(n + " "); 26 | return fibo(n - 2, dp) + fibo(n - 1, dp); 27 | } 28 | 29 | // simple recursion program to find fibonacci number 30 | static int fibo1(int n) { 31 | if (n <= 1) return n; 32 | return fibo1(n - 2) + fibo1(n - 1); 33 | } 34 | 35 | //tabulation method of dynamic programming 36 | 37 | static int fibo2(int n) { 38 | int[] dp = new int[n + 1]; 39 | Arrays.fill(dp, -1); 40 | dp[0] = 0; 41 | dp[1] = 1; 42 | for (int i = 2; i <= n; i++) { 43 | dp[i] = dp[i - 1] + dp[i - 2]; 44 | } 45 | return dp[n]; 46 | } 47 | 48 | // simple function to find nth fibonacci number 49 | 50 | static int fibo3(int n) { 51 | if (n == 0) return 0; 52 | if (n == 1) return 1; 53 | int a = 0, b = 1; 54 | 55 | // 0 1 1 2 3 5 8 13 21 34 55 89 56 | for (int i = 2; i <= n; i++) { 57 | int sum = a + b; 58 | b = a; 59 | a = sum; 60 | } 61 | return a; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /MazeProblemOfRatSelf.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class MazeProblemOfRatSelf { 4 | public static void main(String[] args) { 5 | int[][] arr = {{1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 1}}; 6 | System.out.println(findPath(arr)); 7 | 8 | } 9 | 10 | static ArrayList findPath(int[][] arr) { 11 | int n = arr.length; 12 | ArrayList ans = new ArrayList<>(); 13 | if (arr[0][0] == 0) return ans; 14 | int[][] isVisited = new int[n][n]; 15 | 16 | solve(0, 0, arr, isVisited, n, ans, ""); 17 | 18 | return ans; 19 | } 20 | 21 | static void solve(int x, int y, int[][] arr, int[][] isVis, int n, ArrayList ans, String path) { 22 | if (x == n - 1 && y == n - 1) { 23 | ans.add(path); 24 | return; 25 | } 26 | 27 | isVis[x][y] = 1; 28 | // right 29 | if (isSafe(x, y + 1, arr, isVis, n)) { 30 | solve(x, y + 1, arr, isVis, n, ans, path + 'R'); 31 | } 32 | // left 33 | if (isSafe(x, y - 1, arr, isVis, n)) { 34 | solve(x, y - 1, arr, isVis, n, ans, path + 'L'); 35 | } 36 | // up 37 | if (isSafe(x - 1, y, arr, isVis, n)) { 38 | solve(x - 1, y, arr, isVis, n, ans, path + 'U'); 39 | } 40 | // down 41 | if (isSafe(x + 1, y, arr, isVis, n)) { 42 | solve(x + 1, y, arr, isVis, n, ans, path + 'D'); 43 | } 44 | 45 | isVis[x][y] = 0; 46 | } 47 | 48 | static boolean isSafe(int x, int y, int[][] arr, int[][] isVis, int n) { 49 | return (x >= 0 && x < n) && (y >= 0 && y < n) && arr[x][y] == 1 && isVis[x][y] == 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /RarMazeProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RarMazeProblem { 4 | public static void main(String[] args) { 5 | int[][] arr = {{1, 0, 0, 0}, 6 | {1, 1, 0, 1}, 7 | {1, 1, 0, 0}, 8 | {0, 1, 1, 1},}; 9 | System.out.println(findPath(arr)); 10 | } 11 | 12 | public static ArrayList findPath(int[][] arr) { 13 | ArrayList ans = new ArrayList<>(); 14 | if (arr[0][0] == 0) return ans; 15 | int n = arr.length; 16 | int vis[][] = new int[n][n]; 17 | solve(0, 0, arr, vis, n, ans, ""); 18 | return ans; 19 | } 20 | 21 | static boolean isSafe(int x, int y, int[][] arr, int[][] visited, int n) { 22 | return ((x >= 0 && x < n) && (y >= 0 && y < n) && arr[x][y] == 1 && visited[x][y] == 0); 23 | } 24 | 25 | static void solve(int i, int j, int arr[][], int vis[][], int n, ArrayList ans, String move) { 26 | if (i == n - 1 && j == n - 1) { 27 | ans.add(move); 28 | return; 29 | } 30 | vis[i][j] = 1; 31 | // down 32 | if (isSafe(i + 1, j, arr, vis, n)) { 33 | solve(i + 1, j, arr, vis, n, ans, move + 'D'); 34 | } 35 | // left 36 | if (isSafe(i, j - 1, arr, vis, n)) { 37 | solve(i, j - 1, arr, vis, n, ans, move + 'L'); 38 | } 39 | 40 | if (isSafe(i, j + 1, arr, vis, n)) { 41 | solve(i, j + 1, arr, vis, n, ans, move + 'R'); 42 | } 43 | // upward 44 | if (isSafe(i - 1, j, arr, vis, n)) { 45 | solve(i - 1, j, arr, vis, n, ans, move + 'U'); 46 | } 47 | vis[i][j] = 0; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /RatInMazeProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RatInMazeProblem { 4 | public static void main(String[] args) { 5 | 6 | } 7 | 8 | public static ArrayList findPath(int[][] arr, int n) { 9 | ArrayList ans = new ArrayList<>(); 10 | if (arr[0][0] == 0) return ans; 11 | int srcx = 0; 12 | int srcy = 0; 13 | int[][] visited = new int[n][n]; 14 | String path = ""; 15 | solve(arr, n, ans, srcx, srcy, visited, path); 16 | Collections.sort(ans); 17 | return ans; 18 | } 19 | 20 | static void solve(int[][] arr, int n, ArrayList ans, int x, int y, int[][] visited, String path) { 21 | if (x == n - 1 && y == n - 1) { 22 | ans.add(path); 23 | return; 24 | } 25 | visited[x][y] = 1; 26 | 27 | int newx = x + 1; 28 | int newy = y; 29 | if (isSafe(newx, newy, n, visited, arr)) { 30 | path += 'D'; 31 | } 32 | 33 | 34 | visited[x][y] = 0; 35 | 36 | } 37 | 38 | private static boolean isSafe(int x, int y, int n, int[][] visited, int[][] arr) { 39 | if ((x >= 0 && x < n) && (y >= 0 && y < n) && arr[x][y] == 1 && visited[x][y] == 0) { 40 | return true; 41 | } else { 42 | return false; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Recursion_By_Anuj_bhaiya/NQueens.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Anuj_bhaiya; 2 | 3 | import java.util.Arrays; 4 | 5 | public class NQueens { 6 | public static void main(String[] args) { 7 | int n = 4; 8 | boolean[][] board = { 9 | {false, false, false, false}, 10 | {false, false, false, false}, 11 | {false, false, false, false}, 12 | {false, false, false, false} 13 | }; 14 | 15 | System.out.println(nQueens(board, 0)); 16 | System.out.println(Arrays.deepToString(board)); 17 | } 18 | 19 | static int nQueens(boolean[][] board, int row) { 20 | if (row == board.length) { 21 | display(board); 22 | return 1; 23 | } 24 | int count = 0; 25 | for (int col = 0; col < board.length; col++) { 26 | if (isSafe(board, row, col)) { 27 | board[row][col] = true; 28 | count += nQueens(board, row + 1); 29 | board[row][col] = false; 30 | 31 | } 32 | } 33 | return count; 34 | } 35 | 36 | private static void display(boolean[][] board) { 37 | for (boolean[] row : board) { 38 | for (boolean element : row) { 39 | if (element) System.out.print("Q "); 40 | else System.out.print("X "); 41 | } 42 | System.out.println(); 43 | } 44 | System.out.println(); 45 | } 46 | 47 | static boolean isSafe(boolean[][] board, int row, int col) { 48 | 49 | for (int i = 0; i < row; i++) { 50 | if (board[i][col]) 51 | return false; 52 | } 53 | int maxLeft = Math.min(row, col); 54 | for (int i = 1; i <= maxLeft; i++) { 55 | if (board[row - i][col - i]) 56 | return false; 57 | } 58 | 59 | int maxRight = Math.min(row, board.length - col - 1); 60 | for (int i = 1; i <= maxRight; i++) { 61 | if (board[row - i][col + i]) 62 | return false; 63 | } 64 | return true; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Recursion_By_Anuj_bhaiya/permutationsOfString.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Anuj_bhaiya; 2 | 3 | public class permutationsOfString { 4 | public static void main(String[] args) { 5 | String str = "Ayush"; 6 | printPermutn("abc",""); 7 | } 8 | 9 | static void printPermutn(String str, String ans) { 10 | if (str.length() == 0) { 11 | System.out.print(ans + " "); 12 | return; 13 | } 14 | for (int i = 0; i < str.length(); i++) { 15 | char ch = str.charAt(i); 16 | String ros = str.substring(0, i) + str.substring(i + 1); 17 | printPermutn(ros, ans + ch); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/MazeProblem.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | public class MazeProblem { 4 | public static void main(String[] args) { 5 | // System.out.println(pathCount(3, 3)); 6 | // System.out.println(count(4,4)); 7 | printPath(3, 3, ""); 8 | // diagonalPath(3,3,""); 9 | } 10 | 11 | static int pathCount(int r, int c) { 12 | if (r == 1 || c == 1) return 1; 13 | int left = pathCount(r - 1, c); 14 | int right = pathCount(r, c - 1); 15 | return left + right; 16 | } 17 | 18 | static int count(int r, int c) { 19 | if (r == 1 || c == 1) return 1; 20 | return count(r - 1, c) + count(r, c - 1); 21 | } 22 | 23 | static void printPath(int r, int c, String p) { 24 | if (r == 1 && c == 1) { 25 | System.out.println(p); 26 | return; 27 | } 28 | if (r > 1) 29 | printPath(r - 1, c, p + 'D'); 30 | if (c > 1) 31 | printPath(r, c - 1, p + 'R'); 32 | } 33 | 34 | static void diagonalPath(int r, int c, String p) { 35 | if (r == 1 && c == 1) { 36 | System.out.println(p); 37 | return; 38 | } 39 | if (r > 1 && c > 1) 40 | diagonalPath(r - 1, c - 1, p + 'D'); 41 | if (r > 1) 42 | diagonalPath(r - 1, c, p + 'V'); 43 | if (c > 1) 44 | diagonalPath(r, c - 1, p + 'H'); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/MazeProblem_printPath_inMatrixAnd_String_also.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | import java.util.Arrays; 4 | 5 | public class MazeProblem_printPath_inMatrixAnd_String_also { 6 | public static void main(String[] args) { 7 | boolean[][] maze = { 8 | {true, true, true}, 9 | {true, true, true}, 10 | {true, true, true}, 11 | }; 12 | int[][] arr = new int[maze.length][maze[0].length]; 13 | mazeProb(0, 0, "", 1, maze, arr); 14 | } 15 | 16 | static void mazeProb(int r, int c, String p, int steps, boolean[][] maze, int[][] arr) { 17 | if (r == maze.length - 1 && c == maze[0].length - 1) { 18 | System.out.println(p); 19 | arr[r][c] = steps; 20 | 21 | for (int[] temp : arr) { 22 | System.out.println(Arrays.toString(temp)); 23 | } 24 | System.out.println(); 25 | } 26 | 27 | if (!maze[r][c]) return; 28 | maze[r][c] = false; 29 | arr[r][c] = steps; 30 | if (r > 0) 31 | mazeProb(r - 1, c, p + 'U', steps + 1, maze, arr); 32 | if (r < maze.length - 1) 33 | mazeProb(r + 1, c, p + 'D', steps + 1, maze, arr); 34 | if (c > 0) 35 | mazeProb(r, c - 1, p + 'L', steps + 1, maze, arr); 36 | if (c < maze[0].length - 1) 37 | mazeProb(r, c + 1, p + 'R', steps + 1, maze, arr); 38 | 39 | maze[r][c] = true; 40 | arr[r][c] = 0; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/MazeProblem_returnList.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class MazeProblem_returnList { 6 | public static void main(String[] args) { 7 | // System.out.println(path(4, 4, "")); 8 | System.out.println(pathDiagonal(3,3,"")); 9 | } 10 | 11 | static ArrayList path(int r, int c, String p) { 12 | if (r == 1 && c == 1) { 13 | ArrayList ans = new ArrayList<>(); 14 | ans.add(p); 15 | return ans; 16 | } 17 | ArrayList list = new ArrayList<>(); 18 | if (r > 1) 19 | list.addAll(path(r - 1, c, p + 'D')); 20 | if (c > 1) 21 | list.addAll(path(r, c - 1, p + 'R')); 22 | return list; 23 | 24 | } 25 | 26 | static ArrayList pathDiagonal(int r, int c, String p) { 27 | if (r == 1 && c == 1) { 28 | ArrayList ans = new ArrayList<>(); 29 | ans.add(p); 30 | return ans; 31 | } 32 | ArrayList list = new ArrayList<>(); 33 | if (r > 1 && c > 1) 34 | list.addAll(pathDiagonal(r - 1, c - 1, p + 'C')); 35 | if (r > 1) 36 | list.addAll(pathDiagonal(r - 1, c, p + 'D')); 37 | if (c > 1) 38 | list.addAll(pathDiagonal(r, c - 1, p + 'R')); 39 | return list; 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/Maze_With_Obstacle.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | public class Maze_With_Obstacle { 4 | public static void main(String[] args) { 5 | boolean[][] maze = { 6 | {true, true, true}, 7 | {true, false, true}, 8 | {true, true, true}, 9 | }; 10 | mazePath(maze, 0, 0, ""); 11 | } 12 | 13 | static void mazePath(boolean[][] maze, int r, int c, String p) { 14 | if (r == maze.length - 1 && c == maze[0].length - 1) { 15 | System.out.println(p); 16 | return; 17 | } 18 | if (!maze[r][c]) 19 | return; 20 | if (r < maze.length - 1) 21 | mazePath(maze, r + 1, c, p + 'D'); 22 | if (c < maze[0].length - 1) 23 | mazePath(maze, r, c + 1, p + 'R'); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/Printing_All_The_paths.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | public class Printing_All_The_paths { 4 | public static void main(String[] args) { 5 | boolean[][] arr = { 6 | {true, true, true}, 7 | {true, true, true}, 8 | {true, true, true}, 9 | }; 10 | maze(arr, 0, 0, ""); 11 | } 12 | 13 | static void maze(boolean[][] maze, int r, int c, String p) { 14 | if (r == maze.length - 1 && c == maze[0].length - 1) { 15 | System.out.println(p); 16 | return; 17 | } 18 | if (!maze[r][c]) return; 19 | maze[r][c] = false; 20 | // going downward 21 | if (r < maze.length - 1) 22 | maze(maze, r + 1, c, 'D' + p); 23 | // going upward 24 | if (r > 0) 25 | maze(maze, r - 1, c, 'U' + p); 26 | // going towards right 27 | if (c < maze[0].length - 1) 28 | maze(maze, r, c + 1, 'R' + p); 29 | // going towards left 30 | if (c > 0) 31 | maze(maze, r, c - 1, 'L' + p); 32 | maze[r][c] = true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture10/printPathIn_Matrix.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture10; 2 | 3 | import java.util.Arrays; 4 | 5 | public class printPathIn_Matrix { 6 | public static void main(String[] args) { 7 | int[][] arr = { 8 | {0, 0, 0}, 9 | {0, 0, 0}, 10 | {0, 0, 0}, 11 | 12 | }; 13 | maze(arr, 0, 0, 1); 14 | } 15 | 16 | static void printArr(int[][] arr) { 17 | for (int i = 0; i < arr.length; i++) { 18 | for (int j = 0; j < arr[0].length; j++) { 19 | System.out.print(arr[i][j] + " "); 20 | } 21 | System.out.println(); 22 | } 23 | System.out.println(); 24 | } 25 | 26 | static void maze(int[][] maze, int r, int c, int i) { 27 | if (r == maze.length - 1 && c == maze[0].length - 1) { 28 | maze[r][c] = i; 29 | printArr(maze); 30 | return; 31 | } 32 | if (maze[r][c] != 0) return; 33 | maze[r][c] = i; 34 | // going downward 35 | if (r < maze.length - 1) 36 | maze(maze, r + 1, c, i + 1); 37 | // going upward 38 | if (r > 0) 39 | maze(maze, r - 1, c, i + 1); 40 | // going towards right 41 | if (c < maze[0].length - 1) 42 | maze(maze, r, c + 1, i + 1); 43 | // going towards left 44 | if (c > 0) 45 | maze(maze, r, c - 1, i + 1); 46 | maze[r][c] = 0; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/K_knights_Problem/K_knights.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.K_knights_Problem; 2 | 3 | public class K_knights { 4 | public static void main(String[] args) { 5 | int n = 4; 6 | boolean[][] board = new boolean[n][n]; 7 | nKnights(board, 0, 0, n); 8 | } 9 | 10 | static void nKnights(boolean[][] board, int row, int col, int knights) { 11 | if (knights == 0) { 12 | display(board); 13 | return; 14 | } 15 | if (row == board.length - 1 && col == board.length) return; 16 | if (col == board.length) { 17 | nKnights(board, row + 1, 0, knights); 18 | return; 19 | } 20 | 21 | if (isSafe(board, row, col)) { 22 | board[row][col] = true; 23 | nKnights(board, row, col + 1, knights - 1); 24 | board[row][col] = false; 25 | } 26 | nKnights(board, row, col + 1, knights); 27 | } 28 | 29 | private static boolean isSafe(boolean[][] board, int row, int col) { 30 | if (isValid(board, row - 2, col - 1)) { 31 | if (board[row - 2][col - 1]) return false; 32 | } 33 | if (isValid(board, row - 1, col - 2)) { 34 | if (board[row - 1][col - 2]) return false; 35 | } 36 | if (isValid(board, row - 2, col + 1)) { 37 | if (board[row - 2][col + 1]) return false; 38 | } 39 | if (isValid(board, row - 1, col + 2)) { 40 | return !board[row - 1][col + 2]; 41 | } 42 | return true; 43 | } 44 | 45 | static boolean isValid(boolean[][] board, int row, int col) { 46 | if (row >= 0 && row < board.length && col >= 0 && col < board.length) return true; 47 | return false; 48 | } 49 | 50 | static void display(boolean[][] arr) { 51 | for (int i = 0; i < arr.length; i++) { 52 | for (int j = 0; j < arr[0].length; j++) { 53 | if (arr[i][j]) System.out.print("K "); 54 | else System.out.print(". "); 55 | } 56 | System.out.println(); 57 | } 58 | System.out.println("------------------------------------------------------"); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/K_knights_Problem/N_knights_returnCount.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.K_knights_Problem; 2 | 3 | public class N_knights_returnCount { 4 | public static void main(String[] args) { 5 | int n = 4; 6 | boolean[][] board = new boolean[n][n]; 7 | nKnights(board, 0, 0, n,0); 8 | } 9 | 10 | static void nKnights(boolean[][] board, int row, int col, int knights, int count) { 11 | if (knights == 0) { 12 | display(board); 13 | 14 | return; 15 | } 16 | if (row == board.length - 1 && col == board.length) return; 17 | if (col == board.length) { 18 | nKnights(board, row + 1, 0, knights,count); 19 | return; 20 | } 21 | 22 | if (isSafe(board, row, col)) { 23 | board[row][col] = true; 24 | nKnights(board, row, col + 1, knights - 1,count+1); 25 | board[row][col] = false; 26 | } 27 | nKnights(board, row, col + 1, knights,count); 28 | } 29 | 30 | private static boolean isSafe(boolean[][] board, int row, int col) { 31 | if (isValid(board, row - 2, col - 1)) { 32 | if (board[row - 2][col - 1]) return false; 33 | } 34 | if (isValid(board, row - 1, col - 2)) { 35 | if (board[row - 1][col - 2]) return false; 36 | } 37 | if (isValid(board, row - 2, col + 1)) { 38 | if (board[row - 2][col + 1]) return false; 39 | } 40 | if (isValid(board, row - 1, col + 2)) { 41 | return !board[row - 1][col + 2]; 42 | } 43 | return true; 44 | } 45 | 46 | static boolean isValid(boolean[][] board, int row, int col) { 47 | if (row >= 0 && row < board.length && col >= 0 && col < board.length) return true; 48 | return false; 49 | } 50 | 51 | static void display(boolean[][] arr) { 52 | for (int i = 0; i < arr.length; i++) { 53 | for (int j = 0; j < arr[0].length; j++) { 54 | if (arr[i][j]) System.out.print("K "); 55 | else System.out.print(". "); 56 | } 57 | System.out.println(); 58 | } 59 | System.out.println("------------------------------------------------------"); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/N_queens.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | import java.util.Arrays; 4 | 5 | public class N_queens { 6 | public static void main(String[] args) { 7 | int n = 8; 8 | boolean[][] board = new boolean[n][n]; 9 | // System.out.println(nQueens(board, 0)); 10 | nQueens(board, 0); 11 | } 12 | 13 | static int nQueens(boolean[][] board, int row) { 14 | if (row == board.length) { 15 | display(board); 16 | return 1; 17 | } 18 | int count = 0; 19 | for (int col = 0; col < board.length; col++) { 20 | if (isSafe(board, row, col)) { 21 | board[row][col] = true; 22 | count += nQueens(board, row + 1); 23 | board[row][col] = false; 24 | } 25 | } 26 | return count; 27 | } 28 | 29 | static void display(boolean[][] board) { 30 | for (boolean[] row : board) { 31 | for (boolean element : row) { 32 | if (element) System.out.print("@ "); 33 | else System.out.print(". "); 34 | } 35 | System.out.println(); 36 | } 37 | System.out.println(); 38 | } 39 | 40 | static boolean isSafe(boolean[][] board, int row, int col) { 41 | for (int i = 0; i < row; i++) { 42 | if (board[i][col]) return false; 43 | } 44 | int maxLeft = Math.min(row, col); 45 | for (int i = 1; i <= maxLeft; i++) { 46 | if (board[row - i][col - i]) return false; 47 | } 48 | int maxRight = Math.min(row, board.length - col - 1); 49 | for (int i = 1; i <= maxRight; i++) { 50 | if (board[row - i][col + i]) return false; 51 | } 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/N_queens_return_list.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | import java.util.*; 4 | 5 | public class N_queens_return_list { 6 | public static void main(String[] args) { 7 | List> ans = solveNQueens(4); 8 | System.out.println(ans); 9 | } 10 | 11 | public static List> outer = new ArrayList<>(); 12 | public static List> solveNQueens(int n) { 13 | boolean[][] board = new boolean[n][n]; 14 | solve(board, 0); 15 | return outer; 16 | } 17 | 18 | 19 | static void solve(boolean[][] board, int row) { 20 | if (row == board.length) { 21 | display(board); 22 | return; 23 | } 24 | for (int col = 0; col < board.length; col++) { 25 | if (isSafe(board, row, col)) { 26 | board[row][col] = true; 27 | solve(board, row + 1); 28 | board[row][col] = false; 29 | } 30 | } 31 | } 32 | static boolean isSafe(boolean[][] board, int row, int col) { 33 | for (int i = 0; i < row; i++) { 34 | if (board[i][col]) 35 | return false; 36 | } 37 | int maxLeft = Math.min(row, col); 38 | for (int i = 1; i <= maxLeft; i++) { 39 | if (board[row - i][col - 1]) 40 | return false; 41 | } 42 | int maxRight = Math.min(row, board.length - col - 1); 43 | for (int i = 1; i <= maxRight; i++) { 44 | if (board[row - i][col + i]) 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | public static void display(boolean[][] board) { 51 | // List> outer = new ArrayList<>(); 52 | List internal = new ArrayList<>(); 53 | for(boolean[] row : board) { 54 | String temp = ""; 55 | for(boolean element : row) { 56 | if(element) { 57 | temp += "Q"; 58 | } else { 59 | temp += "."; 60 | } 61 | } 62 | internal.add(temp); 63 | } 64 | outer.add(internal); 65 | } 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | static void display1(boolean[][] arr) { 74 | for (int i = 0; i < arr.length; i++) { 75 | for (int j = 0; j < arr[0].length; j++) { 76 | if (arr[i][j]) System.out.print("@ "); 77 | else System.out.print(". "); 78 | } 79 | System.out.println(); 80 | } 81 | System.out.println("------------------------------------------------------"); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/Nqueens_by_self.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | public class Nqueens_by_self { 4 | public static void main(String[] args) { 5 | int n = 5; 6 | boolean[][] board = new boolean[n][n]; 7 | System.out.println(); 8 | System.out.println(); 9 | System.out.println(); 10 | System.out.println(queens(board, 0)); 11 | } 12 | 13 | static void display(boolean[][] arr) { 14 | for (int i = 0; i < arr.length; i++) { 15 | for (int j = 0; j < arr[0].length; j++) { 16 | if (arr[i][j]) System.out.print("@ "); 17 | else System.out.print(". "); 18 | } 19 | System.out.println(); 20 | } 21 | System.out.println("------------------------------------------------------"); 22 | } 23 | 24 | static int queens(boolean[][] board, int row) { 25 | if (row == board.length ) { 26 | display(board); 27 | return 1; 28 | } 29 | int count = 0; 30 | for (int col = 0; col < board.length; col++) { 31 | if (isSafe(board, row, col)) { 32 | board[row][col] = true; 33 | count += queens(board, row + 1); 34 | board[row][col] = false; 35 | } 36 | } 37 | return count; 38 | } 39 | 40 | static boolean isSafe(boolean[][] board, int row, int col) { 41 | for (int i = 0; i < row; i++) { 42 | if (board[i][col]) return false; 43 | } 44 | int maxLeft = Math.min(row, col); 45 | for (int i = 0; i <= maxLeft; i++) { 46 | if (board[row - i][col - i]) return false; 47 | } 48 | int maxRight = Math.min(row, board.length - col - 1); 49 | for (int i = 0; i <= maxRight; i++) { 50 | if (board[row - i][col + i]) return false; 51 | } 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/Return2D_list_nquens.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | import java.util.*; 4 | 5 | public class Return2D_list_nquens { 6 | public static void main(String[] args) { 7 | 8 | } 9 | 10 | static List> solveNQueens(int n) { 11 | boolean[][] board = new boolean[n][n]; 12 | List> list = new ArrayList<>(queen("", board, 0)); 13 | return list; 14 | } 15 | static List> queen(String p, boolean[][] board, int row) { 16 | if (row == board.length) { 17 | List> list = new ArrayList<>(); 18 | list.add(addQ(board)); 19 | return list; 20 | } 21 | List> list = new ArrayList<>(); 22 | for (int col = 0; col < board.length; col++) { 23 | if (isSafe(board, row, col)) { 24 | board[row][col] = true; 25 | list.addAll(queen(p, board, row + 1)); 26 | board[row][col] = false; 27 | } 28 | } 29 | return list; 30 | } 31 | static List addQ(boolean[][] board) { 32 | List list = new ArrayList<>(); 33 | for (boolean[] arr : board) { 34 | StringBuilder s = new StringBuilder(); 35 | for (boolean element : arr) { 36 | if (element) 37 | s = s.append('Q'); 38 | else 39 | s = s.append('.'); 40 | } 41 | list.add(s.toString()); 42 | } 43 | return list; 44 | } 45 | static boolean isSafe(boolean[][] board, int row, int col) { 46 | //vertical checkUP 47 | for (int i = 0; i < row; i++) { 48 | if (board[i][col]) 49 | return false; 50 | } 51 | //check Diagonal left 52 | int maxLeft = Math.min(row, col); 53 | for (int i = 0; i <= maxLeft; i++) { 54 | if (board[row - i][col - i]) 55 | return false; 56 | } 57 | //check Diagonal Right 58 | int maxRight = Math.min(row, board.length - 1 - col); 59 | for (int i = 0; i <= maxRight; i++) { 60 | if (board[row - i][col + i]) 61 | return false; 62 | } 63 | return true; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/nqueensReturnList2.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | import java.util.*; 4 | 5 | public class nqueensReturnList2 { 6 | public static void main(String[] args) { 7 | List> ans = solveNQueens(4); 8 | System.out.println(ans); 9 | } 10 | 11 | static List> solveNQueens(int n) { 12 | char[][] board = new char[n][n]; 13 | for (int i = 0; i < board.length; i++) { 14 | for (int j = 0; j < board.length; j++) { 15 | board[i][j] = '.'; 16 | } 17 | } 18 | List> result = new ArrayList<>(); 19 | solve(board, 0, result); 20 | 21 | return result; 22 | } 23 | 24 | static void solve(char[][] board, int row, List> ans) { 25 | if (row == board.length) { 26 | List newList = new ArrayList<>(); 27 | for (int i = 0; i < board.length; i++) { 28 | String str = new String(board[i]); 29 | newList.add(str); 30 | } 31 | ans.add(newList); 32 | return; 33 | } 34 | for (int col = 0; col < board.length; col++) { 35 | if (isSafe(board, row, col)) { 36 | board[row][col] = 'Q'; 37 | solve(board, row + 1, ans); 38 | board[row][col] = '.'; 39 | } 40 | } 41 | } 42 | 43 | static boolean isSafe(char[][] board, int row, int col) { 44 | for (int i = 0; i < row; i++) { 45 | if (board[i][col] == 'Q') 46 | return false; 47 | } 48 | int maxLeft = Math.min(row, col); 49 | for (int i = 1; i <= maxLeft; i++) { 50 | if (board[row - i][col - 1] == 'Q') 51 | return false; 52 | } 53 | int maxRight = Math.min(row, board.length - col - 1); 54 | for (int i = 1; i <= maxRight; i++) { 55 | if (board[row - i][col + i] == 'Q') 56 | return false; 57 | } 58 | return true; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/N_Queens_Problem/printOnlylOneBoard.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11.N_Queens_Problem; 2 | 3 | public class printOnlylOneBoard { 4 | public static void main(String[] args) { 5 | int n = 8; 6 | boolean[][] board =new boolean[n][n]; 7 | if (!nQueens(board,0)) System.out.println("Solution does not exists"); 8 | else display(board); 9 | } 10 | 11 | static boolean nQueens(boolean[][] board, int row) { 12 | if (row == board.length) { 13 | // display(board); 14 | return true; 15 | } 16 | int count = 0; 17 | for (int col = 0; col < board.length; col++) { 18 | if (isSafe(board, row, col)) { 19 | board[row][col] = true; 20 | if(nQueens(board, row + 1))return true; 21 | board[row][col] = false; 22 | } 23 | } 24 | return false; 25 | } 26 | 27 | static void display(boolean[][] board) { 28 | for (boolean[] row : board) { 29 | for (boolean element : row) { 30 | if (element) System.out.print("+ "); 31 | else System.out.print("- "); 32 | } 33 | System.out.println(); 34 | } 35 | System.out.println(); 36 | } 37 | 38 | static boolean isSafe(boolean[][] board, int row, int col) { 39 | for (int i = 0; i < row; i++) { 40 | if (board[i][col]) 41 | return false; 42 | } 43 | int maxLeft = Math.min(row, col); 44 | for (int i = 1; i <= maxLeft; i++) { 45 | if (board[row - i][col - i]) 46 | return false; 47 | } 48 | int maxRight = Math.min(row, board.length - col - 1); 49 | for (int i = 1; i <= maxRight; i++) { 50 | if (board[row - i][col + i]) 51 | return false; 52 | } 53 | return true; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture11/SudokuSolver.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture11; 2 | 3 | public class SudokuSolver { 4 | public static void main(String[] args) { 5 | int[][] board = { 6 | { 8, 0, 0, 0, 0, 0, 0, 0, 0 }, 7 | { 0, 0, 3, 6, 0, 0, 0, 0, 0 }, 8 | { 0, 7, 0, 0, 9, 0, 2, 0, 0 }, 9 | { 0, 5, 0, 0, 0, 7, 0, 0, 0 }, 10 | { 0, 0, 0, 0, 4, 5, 7, 0, 0 }, 11 | { 0, 0, 0, 1, 0, 0, 0, 3, 0 }, 12 | { 0, 0, 1, 0, 0, 0, 0, 6, 8 }, 13 | { 0, 0, 8, 5, 0, 0, 0, 1, 0 }, 14 | { 0, 9, 0, 0, 0, 0, 4, 0, 0 } 15 | }; 16 | int[][] arr = new int[9][9]; 17 | 18 | if (solve(arr)) { 19 | display(arr); 20 | System.out.println("Bravo ! Your sudoku is solved "); 21 | } 22 | else 23 | System.out.println("Cannot solve"); 24 | } 25 | 26 | static boolean solve(int[][] board) { 27 | int n = board.length; 28 | int row = -1; 29 | int col = -1; 30 | 31 | boolean emptySpace = true; 32 | for (int i = 0; i < n; i++) { 33 | for (int j = 0; j < n; j++) { 34 | if (board[i][j] == 0) { 35 | row = i; 36 | col = j; 37 | emptySpace = false; 38 | break; 39 | } 40 | } 41 | if (!emptySpace) break; 42 | } 43 | if (emptySpace) return true; 44 | for (int number = 1; number <= 9; number++) { 45 | if (isSafe(board, row, col, number)) { 46 | board[row][col] = number; 47 | if (solve(board)) return true; 48 | else board[row][col] = 0; 49 | } 50 | } 51 | return false; 52 | } 53 | 54 | static boolean isSafe(int[][] board, int row, int col, int number) { 55 | for (int i = 0; i < board.length; i++) { 56 | if (board[row][i] == number) return false; 57 | } 58 | 59 | for (int[] nums : board) { 60 | if (nums[col] == number) return false; 61 | } 62 | 63 | int sqrt = (int) Math.sqrt(board.length); 64 | int rowStart = row - row % sqrt; 65 | int colStart = col - col % sqrt; 66 | 67 | for (int r = rowStart; r < rowStart + sqrt; r++) { 68 | for (int c = colStart; c < colStart + sqrt; c++) { 69 | if (board[r][c] == number) return false; 70 | } 71 | } 72 | return true; 73 | } 74 | 75 | static void display(int[][] arr) { 76 | for (int i = 0; i < arr.length; i++) { 77 | for (int j = 0; j < arr[0].length; j++) { 78 | if (j==3||j==6) 79 | System.out.print("| "); 80 | System.out.print(arr[i][j] + " "); 81 | } 82 | System.out.println(); 83 | if(i==2||i==5) 84 | System.out.println("-----------------------------"); 85 | 86 | } 87 | System.out.println(); 88 | } 89 | 90 | // 8, 0, 0, 0, 0, 0, 0, 0, 0 91 | // 0, 0, 3, 6, 0, 0, 0, 0, 0 92 | // 0, 7, 0, 0, 9, 0, 2, 0, 0 93 | // 0, 5, 0, 0, 0, 7, 0, 0, 0 94 | // 0, 0, 0, 0, 4, 5, 7, 0, 0 95 | // 0, 0, 0, 1, 0, 0, 0, 3, 0 96 | // 0, 0, 1, 0, 0, 0, 0, 6, 8 97 | // 0, 0, 8, 5, 0, 0, 0, 1, 0 98 | // 0, 9, 0, 0, 0, 0, 4, 0, 0 99 | 100 | } 101 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/CountZeroes.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class CountZeroes { 4 | public static void main(String[] args) { 5 | System.out.println(count(30001230)); 6 | System.out.println(helper(30001230,0)); 7 | } 8 | 9 | static int count(int n) { 10 | return helper(n, 0); 11 | } 12 | 13 | private static int helper(int n, int cnt) { 14 | if (n == 0) return cnt; 15 | if (n % 10 == 0) 16 | return helper(n / 10, ++cnt); 17 | return helper(n / 10, cnt); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/Palindrome.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class Palindrome { 4 | 5 | 6 | public static void main(String[] args) { 7 | System.out.println(isPalindrome(1234321)); 8 | 9 | } 10 | 11 | static boolean isPalindrome(int n) { 12 | return n == rev2(n); 13 | } 14 | 15 | static int rev2(int n) { 16 | int digits = (int) Math.log10(n) + 1; 17 | return helper(n, digits); 18 | } 19 | 20 | private static int helper(int n, int digits) { 21 | if (n % 10 == n) return n; 22 | int rem = n % 10; 23 | return rem * (int) Math.pow(10, digits - 1) + rev2(n / 10); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/PrintNunm.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class PrintNunm { 4 | public static void main(String[] args) { 5 | print(5); 6 | print1(5); 7 | } 8 | 9 | static void print(int n) { 10 | if (n == 0) 11 | return; 12 | print(n - 1); 13 | System.out.println(n); 14 | } 15 | 16 | static void print1(int n) { 17 | if (n == 0) 18 | return; 19 | System.out.println(n); 20 | print1(n - 1); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/ReverseNumber.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class ReverseNumber { 4 | static int sum = 0; 5 | 6 | public static void main(String[] args) { 7 | // System.out.println(reverse(1234,1)); 8 | reverse(1234); 9 | System.out.println(sum); 10 | System.out.println(rev2(38974)); 11 | 12 | } 13 | 14 | static void reverse(int n) { 15 | if (n == 0) return; 16 | sum = sum * 10 + n % 10; 17 | reverse(n / 10); 18 | } 19 | 20 | static int rev2(int n) { 21 | int digits = (int) Math.log10(n) + 1; 22 | return helper(n, digits); 23 | } 24 | 25 | private static int helper(int n, int digits) { 26 | if (n%10 == n)return n; 27 | int rem = n %10; 28 | return rem*(int)Math.pow(10,digits-1) + rev2(n/10); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/SumOfDigits.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class SumOfDigits { 4 | public static void main(String[] args) { 5 | System.out.println(sum(1234)); 6 | } 7 | 8 | static int sum(int n){ 9 | if (n <= 0) return 0; 10 | return n%10 + sum(n/10); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/factorialOfNum.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class factorialOfNum { 4 | public static void main(String[] args) { 5 | System.out.println(fact(5)); 6 | } 7 | 8 | static int fact(int n){ 9 | if (n <= 1)return 1; 10 | return n * fact(n-1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/number_of_steps_to_reduce_number_to_zero.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class number_of_steps_to_reduce_number_to_zero { 4 | public static void main(String[] args) { 5 | System.out.println(steps(8)); 6 | } 7 | 8 | static int steps(int n){ 9 | return helper(n,0); 10 | } 11 | 12 | private static int helper(int n, int cnt) { 13 | if (n==0) return cnt; 14 | if ((n&1)==0) 15 | return helper(n/2,cnt+1); 16 | else 17 | return helper(n-1,cnt+1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture2/sumOfNum.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture2; 2 | 3 | public class sumOfNum { 4 | public static void main(String[] args) { 5 | System.out.println(sum(10)); 6 | } 7 | 8 | static int sum(int n) { 9 | if (n <= 1) 10 | return n; 11 | return n + sum(n - 1); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture3/IsSortedOrNot.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture3; 2 | 3 | public class IsSortedOrNot { 4 | public static void main(String[] args) { 5 | int[] arr = {1, 2, 3, 3, 3, 3, 4, 4, 4, 5}; 6 | System.out.println(isSorted(arr, 0)); 7 | } 8 | 9 | static boolean isSorted(int[] arr, int i) { 10 | if (i == arr.length - 1) return true; 11 | return arr[i] <= arr[i + 1] && isSorted(arr, i + 1); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture3/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture3; 2 | 3 | public class LinearSearch { 4 | public static void main(String[] args) { 5 | int[] arr = {9, 12, 34, 32, 65, 78, 90, 32, 54, 4}; 6 | System.out.println(find(arr, 32, 0)); 7 | System.out.println(findlast(arr, 32, arr.length - 1)); 8 | System.out.println(search(arr, 165, 0)); 9 | } 10 | 11 | static int find(int[] arr, int key, int index) { 12 | if (index == arr.length - 1) return -1; 13 | if (arr[index] == key) return index; 14 | return find(arr, key, index + 1); 15 | } 16 | 17 | static int findlast(int[] arr, int key, int index) { 18 | if (index == -1) return -1; 19 | if (arr[index] == key) return index; 20 | return findlast(arr, key, index - 1); 21 | } 22 | 23 | static boolean search(int[] arr, int key, int index) { 24 | if (index == arr.length - 1) return false; 25 | return arr[index] == key || search(arr, key, index + 1); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture3/RotatedBinarySearch.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture3; 2 | 3 | public class RotatedBinarySearch { 4 | public static void main(String[] args) { 5 | int[] arr = {5, 6, 7, 8, 9, 1, 2, 3}; 6 | System.out.println(search(arr, 5, 0, arr.length - 1)); 7 | System.out.println(search(arr, 6, 0, arr.length - 1)); 8 | System.out.println(search(arr, 7, 0, arr.length - 1)); 9 | System.out.println(search(arr, 8, 0, arr.length - 1)); 10 | System.out.println(search(arr, 9, 0, arr.length - 1)); 11 | System.out.println(search(arr, 1, 0, arr.length - 1)); 12 | System.out.println(search(arr, 2, 0, arr.length - 1)); 13 | System.out.println(search(arr, 12, 0, arr.length - 1)); 14 | System.out.println(search(arr, 245, 0, arr.length - 1)); 15 | System.out.println(search2(arr, 5, 0, arr.length - 1)); 16 | System.out.println(search2(arr, 6, 0, arr.length - 1)); 17 | System.out.println(search2(arr, 7, 0, arr.length - 1)); 18 | System.out.println(search2(arr, 8, 0, arr.length - 1)); 19 | System.out.println(search2(arr, 9, 0, arr.length - 1)); 20 | System.out.println(search2(arr, 1, 0, arr.length - 1)); 21 | System.out.println(search2(arr, 2, 0, arr.length - 1)); 22 | System.out.println(search2(arr, 12, 0, arr.length - 1)); 23 | System.out.println(search2(arr, 245, 0, arr.length - 1)); 24 | } 25 | 26 | static int search(int[] arr, int key, int low, int high) { 27 | if (low > high) return -1; 28 | int mid = low + (high - low) / 2; 29 | if (arr[mid] == key) return mid; 30 | if (arr[low] < arr[mid]) { 31 | if (key >= arr[low] && key < arr[mid]) 32 | return search(arr, key, low, mid - 1); 33 | else 34 | return search(arr, key, mid + 1, high); 35 | } else { 36 | if (key > arr[mid] && key <= arr[high]) 37 | return search(arr, key, mid + 1, high); 38 | else 39 | return search(arr, key, low, mid - 1); 40 | } 41 | } 42 | 43 | static int search2(int[] arr, int key, int low, int high) { 44 | 45 | if (low > high) return -1; 46 | 47 | int mid = low + (high - low) / 2; 48 | 49 | if (arr[mid] == key) return mid; 50 | 51 | if (arr[low] <= arr[mid]) { 52 | if (key >= arr[low] && key <= arr[mid]) 53 | return search2(arr, key, low, mid - 1); 54 | else 55 | return search2(arr, key, mid + 1, high); 56 | } 57 | 58 | if (key >= arr[mid] && key <= arr[high]) 59 | return search2(arr, key, mid + 1, high); 60 | 61 | return search2(arr, key, low, mid - 1); 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture3/findAllOccurence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture3; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class findAllOccurence { 6 | static ArrayList ans = new ArrayList<>(); 7 | 8 | public static void main(String[] args) { 9 | int[] arr = {9, 8, 6, 9, 3, 8, 5, 9, 0, 7, 6, 5, 9}; 10 | search(arr, 9, 0); 11 | System.out.println(ans); 12 | // ArrayList 13 | // searchAll(arr, 9, 0, new ArrayList<>()); 14 | System.out.println(findAll(arr,5,0)); 15 | } 16 | 17 | static void search(int[] arr, int key, int i) { 18 | if (i == arr.length) return; 19 | if (arr[i] == key) ans.add(i); 20 | search(arr, key, i + 1); 21 | } 22 | 23 | static void searchAll(int[] arr, int key, int i, ArrayList ans) { 24 | if (i == arr.length) { 25 | System.out.println(ans); 26 | return; 27 | } 28 | if (arr[i] == key) ans.add(i); 29 | searchAll(arr, key, i + 1, ans); 30 | } 31 | 32 | static ArrayList findAll(int[] arr, int key, int i) { 33 | ArrayList list = new ArrayList<>(); 34 | if (i == arr.length) return list; 35 | if (arr[i] == key) list.add(i); 36 | ArrayList ans = findAll(arr, key, i + 1); 37 | list.addAll(ans); 38 | return list; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture4/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture4; 2 | 3 | public class BubbleSort { 4 | public static void main(String[] args) { 5 | int[] arr = {9, 8, 69, 3, 5, 6, 3, 0, 4, 5}; 6 | printArr(arr); 7 | // sort(arr,arr.length-1,0); 8 | bubbleSort(arr, 0, arr.length - 1); 9 | printArr(arr); 10 | } 11 | 12 | static void printArr(int[] arr) { 13 | for (int n : arr) { 14 | System.out.print(n + " "); 15 | } 16 | System.out.println(); 17 | } 18 | 19 | static void bubbleSort(int[] arr, int i, int l) { 20 | if (l == 0) return; 21 | if (l > i) { 22 | if (arr[i] > arr[i + 1]) { 23 | int temp = arr[i]; 24 | arr[i] = arr[i + 1]; 25 | arr[i + 1] = temp; 26 | } 27 | bubbleSort(arr, i + 1, l); 28 | } else { 29 | bubbleSort(arr, 0, l - 1); 30 | } 31 | } 32 | 33 | 34 | static void sort(int[] arr, int l, int i) { 35 | if (l == 0) return; 36 | if (l > i) { 37 | if (arr[i] > arr[i + 1]) { 38 | int temp = arr[i]; 39 | arr[i] = arr[i + 1]; 40 | arr[i + 1] = temp; 41 | } 42 | sort(arr, l, i + 1); 43 | } else { 44 | sort(arr, l - 1, 0); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture4/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture4; 2 | 3 | import java.util.Arrays; 4 | 5 | public class SelectionSort { 6 | public static void main(String[] args) { 7 | int[] arr = {0, 3, 8, 9, 4, 7, 5, 6}; 8 | System.out.println(Arrays.toString(arr)); 9 | sort(arr, 0, arr.length , 0); 10 | System.out.println(Arrays.toString(arr)); 11 | } 12 | 13 | private static void sort(int[] arr, int i, int l, int max) { 14 | if (l == 0) return; 15 | 16 | if (l > i) { 17 | if (arr[i] > arr[max]) 18 | sort(arr, i + 1, l, i); 19 | else 20 | sort(arr, i + 1, l, max); 21 | } else { 22 | int temp = arr[l - 1]; 23 | arr[l - 1] = arr[max]; 24 | arr[max] = temp; 25 | sort(arr, 0, l - 1, 0); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture4/TrianglePattern.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture4; 2 | 3 | public class TrianglePattern { 4 | public static void main(String[] args) { 5 | // pattern(4); 6 | print3(4, 4, 4); 7 | } 8 | 9 | 10 | static void pattern(int n) { 11 | print(n, 0); 12 | print2(n, 0); 13 | } 14 | 15 | static void print(int row, int col) { 16 | if (row == 0) 17 | return; 18 | if (row > col) { 19 | System.out.print("* "); 20 | print(row, col + 1); 21 | } else { 22 | System.out.println(); 23 | print(row - 1, 0); 24 | } 25 | } 26 | 27 | static void print2(int row, int col) { 28 | if (row == 0) 29 | return; 30 | if (row > col) { 31 | print2(row, col + 1); 32 | System.out.print("* "); 33 | } else { 34 | print2(row - 1, 0); 35 | System.out.println(); 36 | } 37 | } 38 | 39 | static void print3(int r, int c, int s) { 40 | if (r == 0) return; 41 | if (c > 0) { 42 | print3(r, c - 1, s); 43 | System.out.print("* "); 44 | 45 | } else { 46 | print3(r - 1, s, s); 47 | System.out.println(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/MergeSort.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import static Recursion_By_KK.Lecture5.Merge_Sort_Algorithm.isSorted; 7 | 8 | public class MergeSort { 9 | public static void main(String[] args) { 10 | Random rand = new Random(); 11 | int[] arr = new int[10]; 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = rand.nextInt(20); 14 | } 15 | System.out.println(Arrays.toString(arr)); 16 | mergeSort(arr, 0, arr.length - 1); 17 | System.out.println(Arrays.toString(arr)); 18 | System.out.println(isSorted(arr)); 19 | } 20 | 21 | static void mergeSort(int[] arr, int low, int high) { 22 | if (low >= high) return; 23 | int mid = low + (high - low) / 2; 24 | mergeSort(arr, low, mid); 25 | mergeSort(arr, mid + 1, high); 26 | merge(arr, low, high, mid); 27 | } 28 | 29 | static void merge(int[] arr, int low, int high, int mid) { 30 | int i = low; 31 | int j = mid + 1; 32 | int k = low; 33 | int[] ans = new int[high + 1]; 34 | 35 | while (i <= mid && j <= high) { 36 | if (arr[i] < arr[j]) ans[k++] = arr[i++]; 37 | else ans[k++] = arr[j++]; 38 | } 39 | while (i <= mid) ans[k++] = arr[i++]; 40 | while (j <= high) ans[k++] = arr[j++]; 41 | 42 | for (int l = low; l <= high; l++) { 43 | arr[l] = ans[l]; 44 | } 45 | // for (int l = 0, si = low; l < ans.length-1; l++,si++) { 46 | // arr[si] = ans[l]; 47 | // } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/MergeSort_ApnaCollege.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | 5 | public class MergeSort_ApnaCollege { 6 | public static void main(String[] args) { 7 | int[] arr = {3, 2, 1, 5, 4, 5, 7, 9, 6, 5}; 8 | System.out.println(Arrays.toString(arr)); 9 | mergeSort(arr, 0, arr.length - 1); 10 | System.out.println(Arrays.toString(arr)); 11 | } 12 | 13 | static void mergeSort(int[] arr, int low, int high) { 14 | if (low >= high) return; 15 | int mid = low + (high - low) / 2; 16 | mergeSort(arr, low, mid); 17 | mergeSort(arr, mid + 1, high); 18 | merge(arr, low, mid, high); 19 | 20 | } 21 | 22 | static void merge(int[] arr, int low, int mid, int high) { 23 | 24 | int i = low; 25 | int j = mid + 1; 26 | int k = 0; 27 | int[] ans = new int[high - low + 1]; 28 | while (i <= mid && j <= high) { 29 | if (arr[i] <= arr[j]) { 30 | ans[k++] = arr[i++]; 31 | } else { 32 | ans[k++] = arr[j++]; 33 | } 34 | } 35 | while (i <= mid) 36 | ans[k++] = arr[i++]; 37 | while (j <= high) 38 | ans[k++] = arr[j++]; 39 | 40 | for (int l = 0, si = low; l < ans.length; l++,si++) { 41 | arr[si] = ans[l]; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/MergeSort_CWH.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import static Recursion_By_KK.Lecture5.Merge_Sort_Algorithm.isSorted; 7 | 8 | public class MergeSort_CWH { 9 | 10 | public static void main(String[] args) { 11 | int[] arr = {3, 2, 1, 5, 4, 5, 7, 9, 6, 5}; 12 | Random rand = new Random(); 13 | int[] arr1 = new int[100]; 14 | for (int i = 0; i < arr1.length; i++) { 15 | arr1[i] = rand.nextInt(500); 16 | } 17 | System.out.println(Arrays.toString(arr1)); 18 | mergeSort(arr1, 0, arr1.length - 1); 19 | System.out.println(Arrays.toString(arr1)); 20 | System.out.println(isSorted(arr1)); 21 | } 22 | 23 | private static void mergeSort(int[] arr, int low, int high) { 24 | if (low >= high) return; 25 | int mid = low + (high - low) / 2; 26 | mergeSort(arr, low, mid); 27 | mergeSort(arr, mid + 1, high); 28 | merge(arr, low, mid, high); 29 | } 30 | private static void merge(int[] arr, int low, int mid, int high) { 31 | int i = low; 32 | int j = mid + 1; 33 | int k = low; 34 | int[] ans = new int[high + 1]; 35 | while (i <= mid && j <= high) { 36 | if (arr[i] < arr[j]) ans[k++] = arr[i++]; 37 | else ans[k++] = arr[j++]; 38 | } 39 | while (i <= mid) ans[k++] = arr[i++]; 40 | while (j <= high) ans[k++] = arr[j++]; 41 | for (int l = low; l <= high; l++) { 42 | arr[l] = ans[l]; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/MergeSort_Final.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Random; 4 | 5 | public class MergeSort_Final { 6 | static void printArray(int[] arr) { 7 | for (int n : arr) 8 | System.out.print(n + " "); 9 | System.out.println(); 10 | } 11 | static boolean isSorted(int[] arr) { 12 | for (int i = 0; i < arr.length - 1; i++) 13 | if (arr[i] > arr[i + 1]) 14 | return false; 15 | return true; 16 | } 17 | public static void main(String[] args) { 18 | Random random = new Random(); 19 | int[] arr = new int[10]; 20 | for (int i = 0; i < arr.length; i++) 21 | arr[i] = random.nextInt(20); 22 | printArray(arr); 23 | sort(arr); 24 | printArray(arr); 25 | System.out.println(isSorted(arr)); 26 | } 27 | static void sort(int[] arr) { 28 | int low = 0; 29 | int high = arr.length - 1; 30 | mergeSort(arr, low, high); 31 | } 32 | static void mergeSort(int[] arr, int low, int high) { 33 | if (low >= high) return; 34 | int mid = low + (high - low) / 2; 35 | mergeSort(arr, low, mid); 36 | mergeSort(arr, mid + 1, high); 37 | merge(arr, low, high, mid); 38 | } 39 | static void merge(int[] arr, int low, int high, int mid) { 40 | int i = low; 41 | int j = mid + 1; 42 | int k = low; 43 | int[] ans = new int[high + 1]; 44 | while (i <= mid && j <= high) { 45 | if (arr[i] < arr[j]) ans[k++] = arr[i++]; 46 | else ans[k++] = arr[j++]; 47 | } 48 | while (i <= mid) ans[k++] = arr[i++]; 49 | while (j <= high) ans[k++] = arr[j++]; 50 | for (int index = low; index <= high; index++) { 51 | arr[index] = ans[index]; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/MergeSort_love_babbar.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | public class MergeSort_love_babbar { 7 | public static void main(String[] args) { 8 | Random rand = new Random(); 9 | int[] arr = new int[10]; 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = rand.nextInt(100); 12 | } 13 | System.out.println(Arrays.toString(arr)); 14 | mergeSort(arr,0,9); 15 | System.out.println(Arrays.toString(arr)); 16 | } 17 | 18 | private static void mergeSort(int[] arr, int s, int e) { 19 | if (s >= e) return; 20 | int mid = s + (e - s) / 2; 21 | mergeSort(arr, s, mid); 22 | mergeSort(arr, mid + 1, e); 23 | merge(arr, s, e); 24 | } 25 | 26 | private static void merge(int[] arr, int s, int e) { 27 | int mid = (s + e) / 2; 28 | 29 | int len1 = mid - s + 1; 30 | int len2 = e - mid; 31 | 32 | int[] first = new int[len1]; 33 | int[] second = new int[len2]; 34 | 35 | int k = s; 36 | for (int i = 0; i < len1; i++) { 37 | first[i] = arr[k++]; 38 | } 39 | // k = mid + 1; 40 | for (int i = 0; i < len2; i++) { 41 | second[i] = arr[k++]; 42 | } 43 | k = s; 44 | int index1 = 0; 45 | int index2 = 0; 46 | 47 | while (index1 < len1 && index2 < len2) { 48 | if (first[index1] < second[index2]) arr[k++] = first[index1++]; 49 | else arr[k++] = second[index2++]; 50 | } 51 | while (index1 < len1) arr[k++] = first[index1++]; 52 | while (index2 < len2) arr[k++] = second[index2++]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/Merge_Sort_Algorithm.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Merge_Sort_Algorithm { 6 | public static void main(String[] args) { 7 | int[] arr = {9, 8, 3, 5, 9, 0, 7, 8}; 8 | int[] ans = mergeSort(arr); 9 | System.out.println(); 10 | System.out.println(Arrays.toString(arr)); 11 | System.out.println(Arrays.toString(ans)); 12 | System.out.println(isSorted(ans)); 13 | } 14 | 15 | static int[] mergeSort(int[] arr) { 16 | if (arr.length == 1) return arr; 17 | int mid = arr.length / 2; 18 | int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid)); 19 | int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length)); 20 | return merge(left, right); 21 | } 22 | 23 | static int[] merge(int[] arr1, int[] arr2) { 24 | int[] ans = new int[arr1.length + arr2.length]; 25 | int i = 0, j = 0, k = 0; 26 | while (i < arr1.length && j < arr2.length) { 27 | if (arr1[i] < arr2[j]) ans[k++] = arr1[i++]; 28 | else ans[k++] = arr2[j++]; 29 | } 30 | while (i < arr1.length) ans[k++] = arr1[i++]; 31 | while (j < arr2.length) ans[k++] = arr2[j++]; 32 | return ans; 33 | } 34 | 35 | public static boolean isSorted(int[] arr) { 36 | for (int i = 0; i < arr.length - 1; i++) { 37 | if (arr[i] > arr[i + 1]) return false; 38 | } 39 | return true; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture5/Merge_Sort_Inplace.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture5; 2 | 3 | import java.util.Arrays; 4 | 5 | import static Recursion_By_KK.Lecture5.Merge_Sort_Algorithm.isSorted; 6 | 7 | public class Merge_Sort_Inplace { 8 | public static void main(String[] args) { 9 | int[] arr = {5, 6, 3, 7, 8, 9, 1, 5, 6, 8}; 10 | System.out.println(Arrays.toString(arr)); 11 | mergeSort(arr, 0, arr.length); 12 | System.out.println(Arrays.toString(arr)); 13 | System.out.println(isSorted(arr)); 14 | } 15 | 16 | static void mergeSort(int[] arr, int i, int l) { 17 | if (l - i == 1) return; 18 | int mid = i + (l - i) / 2; 19 | mergeSort(arr, i, mid); 20 | mergeSort(arr, mid , l); 21 | merge(arr, i, l, mid); 22 | } 23 | 24 | static void merge(int[] arr, int start, int end, int mid) { 25 | int[] ans = new int[end - start]; 26 | int i = start, j = mid, k = 0; 27 | while (i < mid && j < end) { 28 | if (arr[i] < arr[j]) ans[k++] = arr[i++]; 29 | else ans[k++] = arr[j++]; 30 | } 31 | while (i < mid) ans[k++] = arr[i++]; 32 | while (j < end) ans[k++] = arr[j++]; 33 | for (int l = 0; l < ans.length; l++) { 34 | arr[start+l] = ans[l]; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/QuickSort_GFG.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import static Recursion_By_KK.Lecture5.Merge_Sort_Algorithm.isSorted; 7 | 8 | public class QuickSort_GFG { 9 | public static void main(String[] args) { 10 | Random rand = new Random(); 11 | int[] arr = new int[10]; 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = rand.nextInt(20); 14 | } 15 | System.out.println(Arrays.toString(arr)); 16 | quickSort(arr,0,arr.length-1); 17 | System.out.println(Arrays.toString(arr)); 18 | System.out.println(isSorted(arr)); 19 | } 20 | 21 | static void quickSort(int[] arr, int low, int high) { 22 | if (low < high) { 23 | int pivotIndex = pivotInd(arr, low, high); 24 | quickSort(arr, low, pivotIndex - 1); 25 | quickSort(arr, pivotIndex + 1, high); 26 | } 27 | } 28 | 29 | private static int pivotInd(int[] arr, int low, int high) { 30 | int pivot = arr[high]; 31 | int i = (low - 1); 32 | for (int j = low; j < high; j++) { 33 | if (arr[j] < pivot) { 34 | i++; 35 | int temp = arr[i]; 36 | arr[i] = arr[j]; 37 | arr[j] = temp; 38 | } 39 | } 40 | int temp = arr[i + 1]; 41 | arr[i + 1] = arr[high]; 42 | arr[high] = temp; 43 | return i + 1; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/QuickSort_final.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Random; 4 | 5 | public class QuickSort_final { 6 | static void printArray(int[] arr) { 7 | for (int n : arr) 8 | System.out.print(n + " "); 9 | System.out.println(); 10 | } 11 | 12 | static boolean isSorted(int[] arr) { 13 | for (int i = 0; i < arr.length - 1; i++) 14 | if (arr[i] > arr[i + 1]) 15 | return false; 16 | return true; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Random random = new Random(); 21 | int[] arr = new int[10]; 22 | for (int i = 0; i < arr.length; i++) 23 | arr[i] = random.nextInt(20); 24 | printArray(arr); 25 | sort(arr); 26 | printArray(arr); 27 | System.out.println(isSorted(arr)); 28 | } 29 | 30 | static void sort(int[] arr) { 31 | int low = 0; 32 | int high = arr.length - 1; 33 | quickSort(arr, low, high); 34 | } 35 | 36 | static void quickSort(int[] arr, int low, int high) { 37 | if (low >= high) return; 38 | int i = low; 39 | int j = high; 40 | int pivot = arr[low]; 41 | while (i <= j) { 42 | while (arr[i] < pivot) i++; 43 | while (arr[j] > pivot) j--; 44 | if (i <= j) { 45 | int temp = arr[i]; 46 | arr[i] = arr[j]; 47 | arr[j] = temp; 48 | i++; 49 | j--; 50 | } 51 | } 52 | quickSort(arr, low, j); 53 | quickSort(arr, i, high); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/QuickSort_final_loveBabbar.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Random; 4 | 5 | public class QuickSort_final_loveBabbar { 6 | static void printArray(int[] arr) { 7 | for (int n : arr) 8 | System.out.print(n + " "); 9 | System.out.println(); 10 | } 11 | 12 | static boolean isSorted(int[] arr) { 13 | for (int i = 0; i < arr.length - 1; i++) 14 | if (arr[i] > arr[i + 1]) 15 | return false; 16 | return true; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Random random = new Random(); 21 | int[] arr = new int[10]; 22 | for (int i = 0; i < arr.length; i++) 23 | arr[i] = random.nextInt(20); 24 | printArray(arr); 25 | sort(arr); 26 | printArray(arr); 27 | System.out.println(isSorted(arr)); 28 | } 29 | 30 | static void sort(int[] arr) { 31 | int low = 0; 32 | int high = arr.length - 1; 33 | quickSort(arr, low, high); 34 | } 35 | 36 | static void quickSort(int[] arr, int low, int high) { 37 | if (low >= high) return; 38 | int pivotIndex = partition(arr, low, high); 39 | quickSort(arr, low, pivotIndex - 1); 40 | quickSort(arr, pivotIndex + 1, high); 41 | } 42 | 43 | static int partition(int[] arr, int low, int high) { 44 | int i = low; 45 | int j = high; 46 | int cnt = 0; 47 | int pivot = arr[low]; 48 | for (int k = low + 1; k <= high; k++) { 49 | if (arr[k] <= pivot) cnt++; 50 | } 51 | int pivotIndex = low + cnt; 52 | int temp = arr[low]; 53 | arr[low] = arr[pivotIndex]; 54 | arr[pivotIndex] = temp; 55 | while (i < pivotIndex && j > pivotIndex) { 56 | while (arr[i] <= pivot) i++; 57 | while (arr[j] > pivot) j--; 58 | if (i < pivotIndex && j > pivotIndex) { 59 | temp = arr[i]; 60 | arr[i] = arr[j]; 61 | arr[j] = temp; 62 | i++; 63 | j--; 64 | } 65 | } 66 | return pivotIndex; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/Quick_Sort_KK.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | public class Quick_Sort_KK { 7 | public static void main(String[] args) { 8 | Random ramd = new Random(); 9 | int[] arr = new int[10]; 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = ramd.nextInt(20); 12 | } 13 | int[] arr1 = {11, 3, 0, 7, 7, 15, 6, 3, 0, 1}; 14 | System.out.println(Arrays.toString(arr)); 15 | sort(arr, 0, arr.length - 1); 16 | System.out.println(Arrays.toString(arr)); 17 | } 18 | 19 | static void sort(int[] arr, int low, int high) { 20 | if (low >= high) return; 21 | int i = 0; 22 | int j = high - 1; 23 | // int mid = i+(j-i)/2; 24 | int pivot = arr[high]; 25 | 26 | while (i <= j) { 27 | while (arr[i] < pivot) i++; 28 | while (arr[j] > pivot) j--; 29 | if (i <= j) { 30 | int temp = arr[i]; 31 | arr[i] = arr[j]; 32 | arr[j] = temp; 33 | i++; 34 | j--; 35 | } 36 | } 37 | int temp = arr[i]; 38 | arr[i] = arr[high]; 39 | arr[high] = temp; 40 | 41 | sort(arr, low, j - 1); 42 | sort(arr, i + 1, high); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/Quick_Sort_KK2.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import static Recursion_By_KK.Lecture5.Merge_Sort_Algorithm.isSorted; 7 | 8 | public class Quick_Sort_KK2 { 9 | public static void main(String[] args) { 10 | Random ramd = new Random(); 11 | int[] arr = new int[30]; 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = ramd.nextInt(40); 14 | } 15 | int[] arr1 = {9,5,2,2,2,8,7,9}; 16 | System.out.println(Arrays.toString(arr)); 17 | sort(arr, 0, arr.length - 1); 18 | System.out.println(Arrays.toString(arr)); 19 | System.out.println(isSorted(arr)); 20 | } 21 | 22 | 23 | static void sort(int[] arr, int low, int hi) { 24 | if (low >= hi) return; 25 | int i = low; 26 | int j = hi; 27 | int pivot = arr[low]; 28 | while (i <= j) { 29 | while (arr[i] < pivot) i++; 30 | while (arr[j] > pivot) j--; 31 | if (i <= j) { 32 | int temp = arr[i]; 33 | arr[i] = arr[j]; 34 | arr[j] = temp; 35 | i++; 36 | j--; 37 | } 38 | } 39 | sort(arr, low, j); 40 | sort(arr, i, hi); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/Quick_Sort_cwh.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | public class Quick_Sort_cwh { 7 | public static void main(String[] args) { 8 | Random ramd = new Random(); 9 | int[] arr = new int[8]; 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = ramd.nextInt(10); 12 | } 13 | System.out.println(Arrays.toString(arr)); 14 | sort(arr, 0, arr.length - 1); 15 | System.out.println(Arrays.toString(arr)); 16 | } 17 | 18 | static void sort(int[] arr, int low, int high) { 19 | if (low < high) { 20 | int pivotInedx = partition(arr, low, high); 21 | sort(arr, low, pivotInedx - 1); 22 | sort(arr, pivotInedx + 1, high); 23 | } 24 | } 25 | 26 | private static int partition(int[] arr, int low, int high) { 27 | int pivot = arr[low]; 28 | int i = low; 29 | int j = high; 30 | while (i < j) { 31 | do { 32 | i++; 33 | } while (arr[i] <= pivot); 34 | do { 35 | j--; 36 | } while (arr[j] > pivot); 37 | if (i < j) { 38 | int temp = arr[i]; 39 | arr[i] = arr[j]; 40 | arr[j] = temp; 41 | 42 | 43 | } 44 | } 45 | int temp = arr[low]; 46 | arr[low] = arr[j]; 47 | arr[j] = temp; 48 | return j; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture6/Quick_sort_love_babbar.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture6; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | public class Quick_sort_love_babbar { 7 | public static void main(String[] args) { 8 | Random ramd = new Random(); 9 | int[] arr = new int[8]; 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = ramd.nextInt(10); 12 | } 13 | int[] arr1 = {9,5,2,2,2,8,7,9}; 14 | System.out.println(Arrays.toString(arr1)); 15 | quickSort(arr1, 0, arr1.length - 1); 16 | System.out.println(Arrays.toString(arr1)); 17 | } 18 | 19 | static void quickSort(int[] arr, int low, int high) { 20 | if (low >= high) return; 21 | int pivotIndex = partition(arr, low, high); 22 | quickSort(arr, low, pivotIndex - 1); 23 | quickSort(arr, pivotIndex + 1, high); 24 | } 25 | 26 | private static int partition(int[] arr, int low, int high) { 27 | int i = low; 28 | int j = high; 29 | int cnt = 0; 30 | int pivot = arr[low]; 31 | for (int k = low + 1; k <= high; k++) { 32 | if (arr[k] <= pivot) cnt++; 33 | } 34 | int pivotIndex = cnt + low; 35 | int temp = arr[pivotIndex]; 36 | arr[pivotIndex] = arr[low]; 37 | arr[low] = temp; 38 | 39 | while (i < pivotIndex && j > pivotIndex) { 40 | while (arr[i] <= pivot) i++; 41 | while (arr[j] > pivot) j--; 42 | if (i < pivotIndex && j > pivotIndex) { 43 | temp = arr[i]; 44 | arr[i] = arr[j]; 45 | arr[j] = temp; 46 | i++;j--; 47 | } 48 | } 49 | 50 | return pivotIndex; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/R1_remove_a_from_string.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | public class R1_remove_a_from_string { 4 | public static void main(String[] args) { 5 | String str = "Hello hii"; 6 | System.out.println(str); 7 | skip("", str, ' '); 8 | System.out.println(skip("HelloBro",'o')); 9 | } 10 | 11 | // approach 1 12 | static void skip(String p, String up, char k) { 13 | if (up.isEmpty()) { 14 | System.out.println(p); 15 | return; 16 | } 17 | char ch = up.charAt(0); 18 | if (ch == k) skip(p, up.substring(1), k); 19 | else skip(p + ch, up.substring(1), k); 20 | } 21 | 22 | //approach 2 23 | static String skip(String str, char ch) { 24 | if (str.isEmpty()) return ""; 25 | char c = str.charAt(0); 26 | // if (c == ch) return skip(str.substring(1), ch); 27 | // else return c + skip(str.substring(1), ch); 28 | return (c == ch) ? skip(str.substring(1), ch) : c + skip(str.substring(1), ch); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/R2_Skip_String.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | public class R2_Skip_String { 4 | public static void main(String[] args) { 5 | System.out.println(skip("hello bro hii bro", " bro")); 6 | skip("hello bro hii bro","", " bro"); 7 | } 8 | 9 | static String skip(String str, String r) { 10 | if (str.isEmpty()) return ""; 11 | if (str.startsWith(r)) return skip(str.substring(r.length()), r); 12 | else return str.charAt(0) + skip(str.substring(1), r); 13 | } 14 | 15 | static void skip(String str, String p, String rem) { 16 | if (str.isEmpty()) { 17 | System.out.println(p); 18 | return; 19 | } 20 | if (str.startsWith(rem)) skip(str.substring(rem.length()), p, rem); 21 | else skip(str.substring(1), p + str.charAt(0), rem); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/R3_skipAppNotApple.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | public class R3_skipAppNotApple { 4 | public static void main(String[] args) { 5 | System.out.println(skip("An Apple a day keeps doctor app away")); 6 | } 7 | 8 | static String skip(String str) { 9 | if (str.isEmpty()) return ""; 10 | if (str.startsWith("app") && !str.startsWith("apple")) { 11 | return skip(str.substring(4)); 12 | } else { 13 | return str.charAt(0) + skip(str.substring(1)); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/R4_String_Subsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class R4_String_Subsequence { 6 | public static void main(String[] args) { 7 | // subSeq("abc", ""); 8 | System.out.println(subSeqRet("abcd","")); 9 | } 10 | 11 | static void subSeq(String str, String p) { 12 | if (str.isEmpty()) { 13 | System.out.println(p); 14 | return; 15 | } 16 | subSeq(str.substring(1), p + str.charAt(0)); 17 | subSeq(str.substring(1), p); 18 | } 19 | 20 | static ArrayList subSeqRet(String str, String p) { 21 | if (str.isEmpty()) { 22 | ArrayList list = new ArrayList<>(); 23 | list.add(p); 24 | return list; 25 | } 26 | ArrayList list1 = subSeqRet(str.substring(1), p + str.charAt(0)); 27 | ArrayList list2 = subSeqRet(str.substring(1), p); 28 | list1.addAll(list2); 29 | return list1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/R5_iterative_program_to_print_subsets.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class R5_iterative_program_to_print_subsets { 8 | public static void main(String[] args) { 9 | int[] arr = {1, 2, 2, 3}; 10 | List> ans = subsetDuplicate(arr); 11 | System.out.println(ans); 12 | 13 | } 14 | 15 | 16 | static List> subset(int[] arr) { 17 | List> outer = new ArrayList<>(); 18 | outer.add(new ArrayList<>()); 19 | for (int num : arr) { 20 | int n = outer.size(); 21 | for (int i = 0; i < n; i++) { 22 | List internal = new ArrayList<>(outer.get(i)); 23 | internal.add(num); 24 | outer.add(internal); 25 | } 26 | } 27 | return outer; 28 | } 29 | 30 | static List> subsetDuplicate(int[] arr) { 31 | Arrays.sort(arr); 32 | List> outer = new ArrayList<>(); 33 | outer.add(new ArrayList<>()); 34 | int start = 0; 35 | int end = 0; 36 | for (int i = 0; i < arr.length; i++) { 37 | if (i > 0 && arr[i] == arr[i - 1]) { 38 | start = end + 1; 39 | } 40 | end = outer.size() - 1; 41 | int n = outer.size(); 42 | for (int j = start; j < n; j++) { 43 | List internal = new ArrayList<>(outer.get(j)); 44 | internal.add(arr[i]); 45 | outer.add(internal); 46 | } 47 | } 48 | return outer; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/Return_Subsequence_In_2D_List_leetcode.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | import java.util.*; 4 | 5 | public class Return_Subsequence_In_2D_List_leetcode { 6 | public static void main(String[] args) { 7 | int[] arr = {1,2,3}; 8 | List> ans = subsets(arr); 9 | System.out.println(ans); 10 | } 11 | 12 | public static List> subsets(int[] arr) { 13 | List> ans = new ArrayList<>(); 14 | solve(arr, 0, new ArrayList<>(), ans); 15 | return ans; 16 | } 17 | 18 | static void solve(int[] arr, int i, List output, List> ans) { 19 | 20 | if (i >= arr.length) { 21 | ans.add(new ArrayList<>(output)); 22 | return; 23 | } 24 | solve(arr, i + 1, output, ans); 25 | output.add(arr[i]); 26 | solve(arr, i + 1, output, ans); 27 | output.remove(output.size() - 1); 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/leetcode_subseq.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | import java.util.*; 4 | 5 | public class leetcode_subseq { 6 | 7 | public static void main(String[] args) { 8 | int[] arr = {1, 2, 3}; 9 | System.out.println(subsets(arr)); 10 | } 11 | 12 | public static List> subsets(int[] arr) { 13 | List> ans = new ArrayList<>(); 14 | List output = new ArrayList<>(); 15 | int i = 0; 16 | solve(arr, i, new ArrayList<>(), ans); 17 | return ans; 18 | } 19 | private static void solve(int[] arr, int i, List output, List> ans) { 20 | 21 | if (i >= arr.length) { 22 | ans.add(new ArrayList<>(output)); 23 | return; 24 | } 25 | solve(arr, i + 1, output, ans); 26 | output.add(arr[i]); 27 | solve(arr, i + 1, output, ans); 28 | output.remove(output.size()-1); 29 | 30 | } 31 | 32 | static void solve1(int[] arr, int i, List output, List> ans) { 33 | 34 | if (i >= arr.length) { 35 | ans.add(new ArrayList<>(output)); 36 | return ; 37 | } 38 | ArrayList aux = new ArrayList<>(); 39 | solve1(arr, i + 1, output, ans); 40 | output.add(arr[i]); 41 | solve1(arr, i + 1, output, ans); 42 | output.remove(output.size()-1); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture7/subStringDemno.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture7; 2 | 3 | public class subStringDemno { 4 | public static void main(String[] args) { 5 | String name = "Ayush Lalchand Soni"; 6 | System.out.println(name); 7 | System.out.println(name.substring(1)); 8 | System.out.println(name); 9 | char ch = 'a'; 10 | System.out.println(name+ch); 11 | System.out.println(name); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture8/Print_Permutations.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture8; 2 | 3 | public class Print_Permutations { 4 | public static void main(String[] args) { 5 | permutation("abc", ""); 6 | String str = "ayush"; 7 | 8 | } 9 | 10 | static void permutation(String str, String p) { 11 | if (str.isEmpty()) { 12 | // System.out.println(p); 13 | return; 14 | } 15 | char ch = str.charAt(0); 16 | for (int i = 0; i <= p.length(); i++) { 17 | String f = p.substring(0, i); 18 | // System.out.println("f" + i + "-> " + f); 19 | String s = p.substring(i); 20 | System.out.println("s" + i + "-> " + s +" "+"f" + i + "-> " + f); 21 | System.out.println("t" + i + "-> " + f + ch + s); 22 | permutation(str.substring(1), f + ch + s); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture9/Dice_throw_for_targetSum.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture9; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Dice_throw_for_targetSum { 7 | public static void main(String[] args) { 8 | // dice("", 5); 9 | ArrayList ans = diceThrow("", 4); 10 | System.out.println(ans); 11 | } 12 | 13 | static void dice(String p, int target) { 14 | if (target == 0) { 15 | System.out.println(p); 16 | return; 17 | } 18 | for (int i = 1; i <= 6 && i <= target; i++) { 19 | dice(p + i, target - i); 20 | } 21 | } 22 | 23 | static ArrayList diceThrow(String str, int target) { 24 | if (target == 0) { 25 | ArrayList ans = new ArrayList<>(); 26 | ans.add(str); 27 | return ans; 28 | } 29 | ArrayList list = new ArrayList<>(); 30 | for (int i = 1; i <= 6 && i <= target; i++) { 31 | list.addAll(diceThrow(str + i, target - i)); 32 | } 33 | return list; 34 | } 35 | 36 | 37 | static ArrayList diceThrowKsides(String str, int target, int k ) { 38 | if (target == 0) { 39 | ArrayList ans = new ArrayList<>(); 40 | ans.add(str); 41 | return ans; 42 | } 43 | ArrayList list = new ArrayList<>(); 44 | for (int i = 1; i <= k && i <= target; i++) { 45 | list.addAll(diceThrowKsides(str + i, target - i, k)); 46 | } 47 | return list; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture9/Dice_throw_leetCode.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture9; 2 | 3 | public class Dice_throw_leetCode { 4 | public static void main(String[] args) { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture9/Phone_Pad_question.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture9; 2 | 3 | import java.util.ArrayList; 4 | import java.util.SplittableRandom; 5 | 6 | public class Phone_Pad_question { 7 | public static void main(String[] args) { 8 | // print("12", ""); 9 | System.out.println(phonePad("12","")); 10 | } 11 | 12 | static void print(String str, String p) { 13 | if (str.isEmpty()) { 14 | System.out.println(p); 15 | return; 16 | } 17 | int digit = str.charAt(0) - '0'; 18 | for (int i = (digit - 1) * 3; i < digit * 3; i++) { 19 | char ch = (char) (i + 'a'); 20 | print(str.substring(1), p + ch); 21 | } 22 | } 23 | 24 | static ArrayList phonePad(String str, String p) { 25 | if (str.isEmpty()) { 26 | ArrayList list = new ArrayList<>(); 27 | list.add(p); 28 | return list; 29 | } 30 | int digit = str.charAt(0) - '0'; 31 | ArrayList ans = new ArrayList<>(); 32 | for (int i = (digit - 1) * 3; i < digit * 3; i++) { 33 | char ch = (char) (i + 'a'); 34 | ans.addAll(phonePad(str.substring(1), p + ch)); 35 | } 36 | return ans; 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture9/phonePadQuestionRevision.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture9; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class phonePadQuestionRevision { 6 | public static void main(String[] args) { 7 | phone("12", ""); 8 | System.out.println(phone1("23","")); 9 | } 10 | 11 | static void phone(String str, String p) { 12 | if (str.isEmpty()) { 13 | System.out.println(p); 14 | return; 15 | } 16 | int digit = str.charAt(0) - '0'; 17 | int start = (digit - 1) * 3; 18 | int end = digit * 3; 19 | for (int i = start; i < end; i++) { 20 | char ch = (char) (i + 'a'); 21 | phone(str.substring(1), p + ch); 22 | } 23 | } 24 | 25 | static ArrayList phone1(String str, String p) { 26 | if (str.isEmpty()) { 27 | ArrayList list = new ArrayList<>(); 28 | list.add(p); 29 | return list; 30 | } 31 | int digit = str.charAt(0) - '0'; 32 | int start = (digit - 1) * 3; 33 | int end = digit * 3; 34 | ArrayList ans = new ArrayList<>(); 35 | for (int i = start; i < end; i++) { 36 | char ch = (char) (i + 'a'); 37 | ans.addAll(phone1(str.substring(1), p + ch)); 38 | } 39 | return ans; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Recursion_By_KK/Lecture9/phone_pas_leetcode.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_KK.Lecture9; 2 | 3 | import java.util.*; 4 | 5 | public class phone_pas_leetcode { 6 | public static void main(String[] args) { 7 | System.out.println(phone("", "")); 8 | List ans = new ArrayList<>(); 9 | ans.clear(); 10 | } 11 | 12 | static List phone(String str, String p) { 13 | 14 | if (str.isEmpty()) { 15 | List ans = new ArrayList<>(); 16 | ans.add(p); 17 | return ans; 18 | } 19 | List list = new ArrayList<>(); 20 | int digit = str.charAt(0) - '0'; 21 | int start = (digit - 2) * 3; 22 | int end = (digit - 1) * 3; 23 | if (digit == 7){ 24 | end = ((digit - 1) * 3) + 1; 25 | } 26 | if (digit == 8) { 27 | start = ((digit - 2) * 3) + 1; 28 | end = ((digit - 1) * 3) + 1; 29 | } 30 | if (digit == 9) { 31 | start = ((digit - 2) * 3) + 1; 32 | end = ((digit - 1) * 3) + 2; 33 | } 34 | for (int i = start; i < end; i++) { 35 | char ch = (char) (i + 'a'); 36 | list.addAll(phone(str.substring(1), p + ch)); 37 | } 38 | return list; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Backtracking/DS_01_print_till_n.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Backtracking; 2 | 3 | public class DS_01_print_till_n { 4 | public static void main(String[] args) { 5 | printNum(9, 9); 6 | } 7 | 8 | static void printNum(int i ,int n){ 9 | if (i < 0) 10 | return; 11 | printNum(i-1,n); 12 | System.out.println(i); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Backtracking/DS_02_print_Num_Till_0.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Backtracking; 2 | 3 | public class DS_02_print_Num_Till_0 { 4 | public static void main(String[] args) { 5 | printTill0(0,9); 6 | } 7 | 8 | static void printTill0(int i , int n){ 9 | if (i > n) 10 | return; 11 | printTill0(i+1,n); 12 | System.out.println(i); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture2/DS_01_print_name_five_times.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture2; 2 | 3 | public class DS_01_print_name_five_times { 4 | public static void main(String[] args) { 5 | printName(5); 6 | } 7 | 8 | static void printName(int i) { 9 | if (i == 0) 10 | return; 11 | System.out.println("Ayush"); 12 | printName(i - 1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture2/DS_02_print_Num_Till_N.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture2; 2 | 3 | public class DS_02_print_Num_Till_N { 4 | public static void main(String[] args) { 5 | printNum(1,10); 6 | } 7 | 8 | static void printNum(int i ,int n){ 9 | if (i==n)return; 10 | System.out.println(i); 11 | printNum(i+1,n); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture2/DS_03_print_Num_Till_0.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture2; 2 | 3 | public class DS_03_print_Num_Till_0 { 4 | public static void main(String[] args) { 5 | printNum(1, 10); 6 | } 7 | 8 | static void printNum(int i, int n) { 9 | if (i == n) return; 10 | printNum(i + 1, n); 11 | System.out.println(i); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture3/DS_01_sum_of_num.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture3; 2 | 3 | public class DS_01_sum_of_num { 4 | public static void main(String[] args) { 5 | System.out.println(sumOfN1(20)); 6 | sumOfN2(20,0); 7 | } 8 | 9 | static int sumOfN1(int n) { 10 | if (n < 1) { 11 | return 0; 12 | } 13 | return n + sumOfN1(n - 1); 14 | } 15 | 16 | static void sumOfN2(int n, int sum) { 17 | if (n < 1) { 18 | System.out.println(sum); 19 | return; 20 | } 21 | sumOfN2(n-1,sum+n); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture3/DS_02_factorial_of_n.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture3; 2 | 3 | public class DS_02_factorial_of_n { 4 | public static void main(String[] args) { 5 | System.out.println(factorial(5)); 6 | } 7 | 8 | static int factorial(int n) { 9 | if (n == 1) return 1; 10 | return n * factorial(n - 1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture4/DS_01_reverseArray.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture4; 2 | 3 | public class DS_01_reverseArray { 4 | public static void main(String[] args) { 5 | int[] arr = {1,2,3,4,5,6,7,8}; 6 | printArray(arr); 7 | reverse(arr,0,arr.length-1); 8 | printArray(arr); 9 | reverse2(arr,0); 10 | printArray(arr); 11 | 12 | } 13 | static void printArray(int [] arr){ 14 | for (int i = 0; i < arr.length ; i++) { 15 | System.out.print(arr[i] + " "); 16 | } 17 | System.out.println(); 18 | } 19 | 20 | // using two pointers approach 21 | static void reverse(int[] arr, int i , int j){ 22 | if (i >= j) 23 | return; 24 | 25 | int temp = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = temp; 28 | 29 | reverse(arr,i+1,j-1); 30 | } 31 | 32 | // using single pointer 33 | static void reverse2(int[] arr, int i){ 34 | if (i > arr.length/2) return; 35 | int temp = arr[i]; 36 | arr[i] = arr[arr.length-i-1]; 37 | arr[arr.length-i-1] = temp; 38 | reverse2(arr,i+1); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture4/DS_02_palindrome_String.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture4; 2 | 3 | public class DS_02_palindrome_String { 4 | public static void main(String[] args) { 5 | String str = "ABACABA"; 6 | System.out.println(isPalindrome(str, 0)); 7 | } 8 | 9 | static boolean isPalindrome(String str, int i){ 10 | if (i > str.length()/2) return true; 11 | if (str.charAt(i) != str.charAt(str.length()-i-1)) return false; 12 | return isPalindrome(str,i+1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture5/DS_01_fibonacciSeries.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture5; 2 | 3 | public class DS_01_fibonacciSeries { 4 | public static void main(String[] args) { 5 | // 0 1 1 2 3 5 8 13 21 34 55 89 6 | System.out.println(fibo(10)); 7 | } 8 | 9 | static int fibo(int n){ 10 | if (n <= 1) return n; 11 | return fibo(n-1)+fibo(n-2); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture6/DS_01_subsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture6; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DS_01_subsequence { 6 | public static void main(String[] args) { 7 | int[] arr = {1, 2, 3, 4}; 8 | ArrayList arrayList = new ArrayList<>(); 9 | printf(0, arrayList, arr); 10 | System.out.println(); 11 | 12 | } 13 | 14 | static void printf(int ind, ArrayList arrayList, int[] arr) { 15 | 16 | if (ind == arr.length) { 17 | System.out.println(arrayList); 18 | return; 19 | } 20 | arrayList.add(arr[ind]); 21 | printf(ind + 1, arrayList, arr); 22 | arrayList.remove(arrayList.size() - 1); 23 | printf(ind + 1, arrayList, arr); 24 | } 25 | 26 | 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture6/DS_02_subsequence2.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture6; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DS_02_subsequence2 { 6 | public static void main(String[] args) { 7 | int[] arr = {1, 2, 3}; 8 | ArrayList ans = new ArrayList<>(); 9 | printF(arr, ans, 0); 10 | } 11 | 12 | static void printF(int[] arr, ArrayList ans, int i) { 13 | 14 | if (i == arr.length) { 15 | System.out.println(ans); 16 | return; 17 | } 18 | ans.add(arr[i]); 19 | printF(arr, ans, i + 1); 20 | ans.remove(ans.size() - 1); 21 | printF(arr, ans, i + 1); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture7/DS_01_print_All_Subsequence_Having_Sum_Equals_K.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture7; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DS_01_print_All_Subsequence_Having_Sum_Equals_K { 6 | public static void main(String[] args) { 7 | int[] arr = {1, 2, 3, 4}; 8 | ArrayList ans = new ArrayList<>(); 9 | printSubsequence(arr,ans,0,0,6); 10 | } 11 | 12 | static void printSubsequence(int[] arr,ArrayList ans,int ind, int s,int sum){ 13 | 14 | if (ind == arr.length){ 15 | if (sum == s) 16 | System.out.println(ans); 17 | return; 18 | } 19 | 20 | ans.add(arr[ind]); 21 | s+=arr[ind]; 22 | printSubsequence(arr,ans,ind+1, s, sum); 23 | ans.remove(ans.size() - 1); 24 | s-= arr[ind]; 25 | printSubsequence(arr,ans,ind+1, s, sum); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture7/DS_02_print_Subsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture7; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DS_02_print_Subsequence { 6 | public static void main(String[] args) { 7 | int[] arr = {1,2,3,4,5,6}; 8 | ArrayList ans = new ArrayList<>(); 9 | int i = 0,s = 0; 10 | int sum = 9; 11 | printS(arr,ans,i,s,sum); 12 | 13 | } 14 | 15 | 16 | 17 | static void printS(int[] arr, ArrayList ans, int i, int s, int sum) { 18 | if (i == arr.length){ 19 | if (s == sum){ 20 | System.out.println(ans); 21 | } 22 | return; 23 | } 24 | 25 | ans.add(arr[i]); 26 | s+=arr[i]; 27 | printS(arr,ans,i+1,s,sum); 28 | ans.remove(ans.size() - 1); 29 | s-=arr[i]; 30 | printS(arr,ans,i+1,s,sum); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture7/DS_03_print_only_one_Subsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture7; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class DS_03_print_only_one_Subsequence { 6 | public static void main(String[] args) { 7 | int[] arr = {1,2,3,4}; 8 | ArrayList ans = new ArrayList<>(); 9 | printS(arr,ans,0,0,6); 10 | } 11 | 12 | static boolean printS(int[] arr, ArrayList ans, int i, int s, int sum) { 13 | if (i == arr.length) { 14 | if (s == sum) { 15 | System.out.println(ans); 16 | return true; 17 | } else return false; 18 | } 19 | ans.add(arr[i]); 20 | s += arr[i]; 21 | if (printS(arr, ans, i + 1, s, sum)) return true; 22 | ans.remove(ans.size()-1); 23 | s -= arr[i]; 24 | return printS(arr, ans, i + 1, s, sum); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Recursion_By_Striver/Lecture7/DS_04_print_count_of_Subsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_By_Striver.Lecture7; 2 | 3 | public class DS_04_print_count_of_Subsequence { 4 | public static void main(String[] args) { 5 | int[] arr = {1,2,3,4,5,6}; 6 | System.out.println(printCount2(arr,0,0,9)); 7 | } 8 | 9 | static int printCount(int[] arr, int i , int s, int sum){ 10 | 11 | if (i == arr.length){ 12 | if (s == sum){ 13 | return 1; 14 | }else return 0; 15 | } 16 | s+=arr[i]; 17 | int l = printCount(arr,i+1,s,sum); 18 | s-=arr[i]; 19 | int r = printCount(arr,i+1,s,sum); 20 | 21 | return l + r; 22 | } 23 | static int printCount2(int[] arr, int i , int s, int sum){ 24 | 25 | if (i == arr.length){ 26 | if (s == sum){ 27 | return 1; 28 | }else return 0; 29 | } 30 | s+=arr[i]; 31 | int l = printCount2(arr,i+1,s,sum); 32 | s-=arr[i]; 33 | 34 | 35 | return l +printCount2(arr,i+1,s,sum) ; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q1_tower_of_hanoi.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q1_tower_of_hanoi { 4 | public static void main(String[] args) { 5 | System.out.println(cnt(3)); 6 | // towerOfHanoi(4, "S", "H", "D"); 7 | // System.out.println(towerRet(4, "S", "H", "D")); 8 | // int steps = counttoh(4, "S", "H", "D"); 9 | // System.out.println(steps); 10 | } 11 | 12 | static void towerOfHanoi(int n, String src, String helper, String des) { 13 | if (n == 1) { 14 | System.out.println("Transfer Disk " + n + " from " + src + " to " + des); 15 | return; 16 | } 17 | towerOfHanoi(n - 1, src, des, helper); 18 | System.out.println("Transfer Disk " + n + " from " + src + " to " + des); 19 | towerOfHanoi(n - 1, helper, src, des); 20 | } 21 | 22 | 23 | static int towerRet(int n, String src, String helper, String des) { 24 | if (n == 0) return 0; 25 | int count = 1; 26 | count += towerRet(n - 1, src, des, helper); 27 | count += towerRet(n - 1, helper, src, des); 28 | return count; 29 | } 30 | 31 | static int counttoh(int n, String T1, String T2, String T3) { 32 | int count = 1; 33 | if (n == 1) return count; 34 | count += counttoh(n - 1, T1, T3, T2); 35 | count += counttoh(n - 1, T3, T2, T1); 36 | return count; 37 | } 38 | 39 | static int cnt(int n){ 40 | if (n==0) return 0; 41 | int count = 1; 42 | count+= cnt(n-1); 43 | count+= cnt(n-1); 44 | return count; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q2_printReverseString.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | import java.sql.Array; 4 | import java.util.ArrayList; 5 | 6 | public class Q2_printReverseString { 7 | public static void main(String[] args) { 8 | String str = "Ayush"; 9 | reverse(str, str.length() - 1); 10 | ArrayList ans = reverseStr(str); 11 | 12 | for (char ch : ans) { 13 | System.out.print(ch); 14 | } 15 | reverse(str, ans); 16 | } 17 | 18 | static void reverse(String str, int ind) { 19 | if (ind == 0) { 20 | System.out.println(str.charAt(ind)); 21 | return; 22 | } 23 | System.out.print(str.charAt(ind)); 24 | reverse(str, ind - 1); 25 | } 26 | 27 | static void reverse(String str, ArrayList ans) { 28 | if (str.isEmpty()) { 29 | for (char ch : ans) { 30 | System.out.print(ch); 31 | } 32 | return; 33 | } 34 | ans.add(0, str.charAt(0)); 35 | reverse(str.substring(1), ans); 36 | } 37 | 38 | static ArrayList reverseStr(String str) { 39 | if (str.isEmpty()) { 40 | return new ArrayList<>(); 41 | } 42 | ArrayList res = 43 | reverseStr(str.substring(1)); 44 | res.add(str.charAt(0)); 45 | return res; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q3_first_and_last_occurence.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q3_first_and_last_occurence { 4 | static int first = -1; 5 | static int last = -1; 6 | 7 | 8 | static void occurence(String str, char ch, int ind) { 9 | if (ind == str.length()) { 10 | System.out.println(first + " " + last); 11 | return; 12 | } 13 | if (str.charAt(ind) == ch) { 14 | if (first == -1) 15 | first = ind; 16 | else 17 | last = ind; 18 | } 19 | occurence(str,ch,ind+1); 20 | 21 | } 22 | public static void main(String[] args) { 23 | String str = "abbaadab"; 24 | occurence(str,'a',0); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q4_check_if_array_isSorted.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q4_check_if_array_isSorted { 4 | public static void main(String[] args) { 5 | int[] arr = {1, 2, 3, 4, 5, 6, 7, 7, 8, 9}; 6 | System.out.println(isSorted(arr, 0)); 7 | } 8 | 9 | static boolean isSorted(int[] arr, int i) { 10 | if (i == arr.length - 1) { 11 | return true; 12 | } 13 | if (arr[i] > arr[i + 1]) 14 | return false; 15 | return isSorted(arr, i + 1); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q5_move_char_at_end.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q5_move_char_at_end { 4 | public static void main(String[] args) { 5 | String str = "axbcxxd"; 6 | move(str, "", 0, 0); 7 | } 8 | 9 | static void move(String str, String p, int ind, int count) { 10 | if (ind == str.length()) { 11 | for (int i = 0; i < count; i++) { 12 | p += 'x'; 13 | } 14 | System.out.print(p); 15 | return; 16 | } 17 | char ch = str.charAt(ind); 18 | if (ch != 'x') 19 | move(str, p + ch, ind + 1, count); 20 | else 21 | move(str, p, ind + 1, count + 1); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q6_removeDuplicates.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q6_removeDuplicates { 4 | public static void main(String[] args) { 5 | boolean[] map = new boolean[26]; 6 | String str = "abbbbbcccceeeefdfdfdfdfdcbadabcabda"; 7 | remove(str, "", 0, map); 8 | } 9 | 10 | static void remove(String str, String p, int ind, boolean[] map) { 11 | if (ind == str.length()) { 12 | System.out.println(p); 13 | return; 14 | } 15 | char ch = str.charAt(ind); 16 | if (map[ch - 'a']) { 17 | remove(str, p, ind + 1, map); 18 | } else { 19 | map[ch - 'a'] = true; 20 | remove(str, p + ch, ind + 1, map); 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q7_printSubsequence.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Q7_printSubsequence { 6 | public static void main(String[] args) { 7 | String str = "abc"; 8 | print(str, ""); 9 | } 10 | 11 | static void print(String str, String p) { 12 | if (str.isEmpty()) { 13 | System.out.println(p); 14 | return; 15 | } 16 | char ch = str.charAt(0); 17 | print(str.substring(1), p + ch); 18 | print(str.substring(1), p); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q8_unique_subsequences.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | import java.lang.reflect.Array; 4 | import java.util.ArrayList; 5 | import java.util.HashSet; 6 | import java.util.LinkedHashSet; 7 | 8 | public class Q8_unique_subsequences { 9 | public static void main(String[] args) { 10 | ArrayList ans = new ArrayList<>(); 11 | HashSet set = new LinkedHashSet<>(); 12 | String str = "aaaa"; 13 | // printSub(str, "", ans); 14 | printSub1(str,"",set); 15 | System.out.println(set); 16 | } 17 | 18 | static void printSub(String str, String p, ArrayList ans) { 19 | if (str.isEmpty()) { 20 | if (ans.contains(p)) 21 | return; 22 | else { 23 | System.out.println(p); 24 | ans.add(p); 25 | return; 26 | } 27 | } 28 | char ch = str.charAt(0); 29 | printSub(str.substring(1), p + ch, ans); 30 | printSub(str.substring(1), p, ans); 31 | } 32 | 33 | static void printSub1(String str, String p, HashSet ans) { 34 | if (str.isEmpty()) { 35 | ans.add(p); 36 | return; 37 | } 38 | char ch = str.charAt(0); 39 | printSub1(str.substring(1), p + ch, ans); 40 | printSub1(str.substring(1), p, ans); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Recursion_by_ApnaCollege/Class2_Questions/Q9_phoneKeyPad.java: -------------------------------------------------------------------------------- 1 | package Recursion_by_ApnaCollege.Class2_Questions; 2 | 3 | public class Q9_phoneKeyPad { 4 | public static void main(String[] args) { 5 | // String[] map = {".", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 6 | // phonePad("23", "", map); 7 | phone("29", ""); 8 | } 9 | 10 | static void phonePad(String str, String p, String[] map) { 11 | if (str.isEmpty()) { 12 | System.out.println(p); 13 | return; 14 | } 15 | char digit = str.charAt(0); 16 | String mapping = map[digit - '0']; 17 | for (int i = 0; i < mapping.length(); i++) { 18 | char ch = mapping.charAt(i); 19 | phonePad(str.substring(1), p + ch, map); 20 | } 21 | } 22 | 23 | static void phone(String str, String p) { 24 | if (str.isEmpty()) { 25 | System.out.println(p); 26 | return; 27 | } 28 | int digit = str.charAt(0) - '0'; 29 | int start = (digit - 1) * 3; 30 | int end = digit * 3; 31 | if (digit == 9) end = digit * 3 - 1; 32 | for (int i = start; i < end; i++) { 33 | char ch = (char) (i + 'a'); 34 | phone(str.substring(1), p + ch); 35 | } 36 | } 37 | } 38 | --------------------------------------------------------------------------------