Skip to content
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

Binary-Search-1 Completed #2038

Open
wants to merge 1 commit 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
49 changes: 49 additions & 0 deletions SearchIn2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
// Time Complexity : O(log(m*n)) = logm + logn
// Space Complexity : O(1)
// Approach : Identify the row where potential target can be found and apply Binary search on the row to check if it exists
int rowIdx = getSearchRow(matrix,target);
if (rowIdx == -1){
return false;
} else {
return binarySearch(matrix,rowIdx,target);
}

}
public int getSearchRow(int[][] matrix, int target){
int columnLen = matrix[0].length - 1;

// perform binary search on rows
int topRow = 0;
int bottomRow = matrix.length-1;
while(topRow<=bottomRow){
int midRow = bottomRow + (topRow-bottomRow)/2; // midRow
// check if traget exists in the midRow
if(matrix[midRow][0] <= target && matrix[midRow][columnLen] >= target){
return midRow;
} else if(matrix[midRow][0]>target){
bottomRow = midRow-1;
} else{
topRow = midRow+1;
}
}
return -1;
}

public boolean binarySearch(int[][] matrix, int row, int target){
int low = 0;
int high = matrix[row].length-1;
while (low<=high){
int mid = low + (high -low)/2;
if(matrix[row][mid] == target){
return true;
} else if (matrix[row][mid]> target){
high=mid-1;
} else{
low = mid+1;
}
}
return false;
}
}
32 changes: 32 additions & 0 deletions SearchInInfiniteSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Approach : When sorted Array is rotated then one portion of array is always sorted (left or right). Calculate mid if our target is at mid then simple return the mid. If our target is not at the mid then use the sorted part of the array to move left and right pointers. by checking if the target is lying in sorted part of the array.

class Solution {
public int search(int[] nums, int target) {
int low=0;
int high = nums.length - 1;
while(low<=high){
int mid = low + (high-low)/2;
if(nums[mid] == target){
return mid;
}
// left array is sorted
if(nums[low]<=nums[mid]){
if(nums[low]<= target && nums[mid]>=target){
high = mid -1;
} else{
low=mid+1;
}
} else{
// right array is sorted
if(nums[mid]<=target && nums[high]>=target){
low=mid+1;
} else{
high = mid-1;
}
}
}
return -1;
}
}
66 changes: 66 additions & 0 deletions SearchInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
// Time Complexity :O(logn)
// Space Complexity : O(1)
// Approach : We need to create a range utilizing reader in such a way that target is included in it or high not exceeding target and then perform Binary Search.

class Solution {
public int search(ArrayReader reader, int target) {
int low=0;
int high = 1;
while(reader.get(high)<target){
low=high;
high=2*high;
}
// now range is estabilished - perform binary search
while (low<=high){
int mid = low + (high -low)/2;
if (reader.get(mid)==target){
return mid;
} else if (reader.get(mid)> target){
high=mid-1;
} else{
low=mid+1;
}
}
return -1;

}
}





/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
// Time Complexity :O(logn)
// Space Complexity : O(1)
// Approach : Brute Force Approach by assuming high as Integer max Value and performing Binary Search.
class Solution {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = Integer.MAX_VALUE-1;
while(low<=high){
int mid = low + (high-low)/2;
if(reader.get(mid)==target){
return mid;
} else if(reader.get(mid)>target){
high=mid-1;
} else{
low=mid+1;
}
}
return -1;
}
}