Skip to content

sudoku solver rows isSafe updated #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lectures/10-binary search/code/src/com/kunal/InfiniteArray.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
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]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
// ans found
// Answer found
return mid;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions lectures/20-trees/code/AVL/.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</natures>
<filteredResources>
<filter>
<id>1679078569810</id>
<id>1736512177822</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down
4 changes: 2 additions & 2 deletions lectures/20-trees/code/Questions/.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</natures>
<filteredResources>
<filter>
<id>1679078569810</id>
<id>1736512177855</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down