Skip to content

Done Binary-Search-1 #2327

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: master
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: 32 additions & 0 deletions Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Time Complexity - O(log(m*n)) where m is number of row and n is number of columns
//Space Complexity - O(1) Not using any extra space.

// Implemented searchMatrix using Binary search.
// Assumed the 2d array into 1d array as it stated that the last element of each row is less than
// first element of next row. The row and column of the mid is found by dividing and mod of n.
public class Matrix {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = (m * n) - 1;
while(low <= high) {
int mid = low +(high - low) / 2; // prevent overflow
int row = mid / n;
int col = mid % n;
if(matrix[row][col] == target) {
return true;
} else if(target < matrix[row][col]) {
high = mid - 1;

} else {
low = mid + 1;
}

}
return false;
}
}
40 changes: 40 additions & 0 deletions RotatedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Time Complexity - O(log n)
//Space Complexity - O(1) Not using any extra space.

// Implemented using Binary search on rotated sorted array. After caluculating the mid I have compared
// value at mid with the value at low and high to find the sorted side of the array. If I find it on the
// left side I will then compare the target with value at low and mid and move the low and high values accordingly.
public class RotatedArray {
public int search(int[] nums, int target) {
if(nums == null && nums.length == 0) {
return -1;
}
int n = nums.length;
int low = 0;
int high = n - 1;
while(low <= high) {
int mid = low + (high - low) / 2; //prevent overflow
if(nums[mid] == target) {
return mid;
}
if(nums[low] <= nums[mid]){
if(target >= nums[low] && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}

} else {
if(target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}


}
return -1;

}
}
47 changes: 47 additions & 0 deletions SearchInUnkownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//Time Complexity - O(log(n))
//Space Complexity - O(1) Not using any extra space.

// Implemented search in UnkownSize array using Binary search. Initially I have taken left=0 and right = 1 as size is unkown
// if the target is not in left and right I moved the right to right *2 and left to right then the low and high bound is founda nd performed binary search.
public class SearchInUnkownSize {

public static class ArrayReader {
private int[] secret;

public ArrayReader(int[] arr) {
this.secret = arr.clone();
}

public int get(int index) {
if(index < 0 || index >= secret.length){
return Integer.MAX_VALUE;
}
return secret[index];
}
}
public static int search(ArrayReader reader, int target) {

if(reader.get(0) == target) {
return 0;

}
int left = 0;
int right = 1;
while (reader.get(right) < target) {
left = right;
right *= 2;
}
while(left <= right) {
int pivot = left + (right - left) /2;
if(reader.get(pivot) == target) {
return pivot;
} else if(reader.get(pivot) > target) {
right = pivot - 1;
} else {
left = pivot + 1;
}
}
return -1;

}
}