diff --git a/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java b/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java index 65bbfd630..b3787704a 100644 --- a/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java +++ b/lectures/10-binary search/code/src/com/kunal/InfiniteArray.java @@ -1,33 +1,35 @@ package com.kunal; -// https://www.geeksforgeeks.org/find-position-element-sorted-array-infinite-numbers/ + public class InfiniteArray { public static void main(String[] args) { - int[] arr = {3, 5, 7, 9, 10, 90, - 100, 130, 140, 160, 170}; + int[] arr = {3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170}; int target = 10; System.out.println(ans(arr, target)); } + static int ans(int[] arr, int target) { - // first find the range - // first start with a box of size 2 + // First, find the range int start = 0; int end = 1; - // condition for the target to lie in the range - while (target > arr[end]) { - int temp = end + 1; // this is my new start - // double the box value - // end = previous end + sizeofbox*2 + // Condition for the target to lie in the range + while (end < arr.length && target > arr[end]) { + int temp = end + 1; // This is the new start + // Double the box value end = end + (end - start + 1) * 2; + // Adjust `end` if it exceeds the array length + if (end >= arr.length) { + end = arr.length - 1; + } start = temp; } - return binarySearch(arr, target, start, end); + return binarySearch(arr, target, start, end); } + static int binarySearch(int[] arr, int target, int start, int end) { - while(start <= end) { - // find the middle element -// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java + while (start <= end) { + // Find the middle element int mid = start + (end - start) / 2; if (target < arr[mid]) { @@ -35,7 +37,7 @@ static int binarySearch(int[] arr, int target, int start, int end) { } else if (target > arr[mid]) { start = mid + 1; } else { - // ans found + // Answer found return mid; } } diff --git a/lectures/14-recursion/code/src/com/kunal/backtracking/SudokuSolver.java b/lectures/14-recursion/code/src/com/kunal/backtracking/SudokuSolver.java index 577ecb81d..f94825e17 100644 --- a/lectures/14-recursion/code/src/com/kunal/backtracking/SudokuSolver.java +++ b/lectures/14-recursion/code/src/com/kunal/backtracking/SudokuSolver.java @@ -2,16 +2,16 @@ public class SudokuSolver { public static void main(String[] args) { - int[][] board = new int[][]{ - {3, 0, 6, 5, 0, 8, 4, 0, 0}, - {5, 2, 0, 0, 0, 0, 0, 0, 0}, - {0, 8, 7, 0, 0, 0, 0, 3, 1}, - {0, 0, 3, 0, 1, 0, 0, 8, 0}, - {9, 0, 0, 8, 6, 3, 0, 0, 5}, - {0, 5, 0, 0, 9, 0, 6, 0, 0}, - {1, 3, 0, 0, 0, 0, 2, 5, 0}, - {0, 0, 0, 0, 0, 0, 0, 7, 4}, - {0, 0, 5, 2, 0, 6, 3, 0, 0} + int[][] board = new int[][] { + { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, + { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 8, 7, 0, 0, 0, 0, 3, 1 }, + { 0, 0, 3, 0, 1, 0, 0, 8, 0 }, + { 9, 0, 0, 8, 6, 3, 0, 0, 5 }, + { 0, 5, 0, 0, 9, 0, 6, 0, 0 }, + { 1, 3, 0, 0, 0, 0, 2, 5, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, + { 0, 0, 5, 2, 0, 6, 3, 0, 0 } }; if (solve(board)) { @@ -67,15 +67,14 @@ static boolean solve(int[][] board) { } private static void display(int[][] board) { - for(int[] row : board) { - for(int num : row) { + for (int[] row : board) { + for (int num : row) { System.out.print(num + " "); } System.out.println(); } } - static boolean isSafe(int[][] board, int row, int col, int num) { // check the row for (int i = 0; i < board.length; i++) { @@ -85,15 +84,14 @@ static boolean isSafe(int[][] board, int row, int col, int num) { } } - // check the col - for (int[] nums : board) { - // check if the number is in the col - if (nums[col] == num) { - return false; + // check the column + for (int i = 0; i < board.length; i++) { + if (board[i][col] == num) { // Notice 'i' is now used for rows, and 'col' is fixed + return false; // Number already exists in this column } } - int sqrt = (int)(Math.sqrt(board.length)); + int sqrt = (int) (Math.sqrt(board.length)); int rowStart = row - row % sqrt; int colStart = col - col % sqrt; diff --git a/lectures/20-trees/code/AVL/.project b/lectures/20-trees/code/AVL/.project index a2380fe36..8dde750af 100644 --- a/lectures/20-trees/code/AVL/.project +++ b/lectures/20-trees/code/AVL/.project @@ -22,12 +22,12 @@ - 1679078569810 + 1736512177822 30 org.eclipse.core.resources.regexFilterMatcher - node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ diff --git a/lectures/20-trees/code/Questions/.project b/lectures/20-trees/code/Questions/.project index a2380fe36..4b1cea0e1 100644 --- a/lectures/20-trees/code/Questions/.project +++ b/lectures/20-trees/code/Questions/.project @@ -22,12 +22,12 @@ - 1679078569810 + 1736512177855 30 org.eclipse.core.resources.regexFilterMatcher - node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__