Skip to content
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
5 changes: 5 additions & 0 deletions src/binarysearch/ArrayReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package binarysearch;

public interface ArrayReader {
int get(int index);
}
15 changes: 15 additions & 0 deletions src/binarysearch/MockArrayReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package binarysearch;

class MockArrayReader implements ArrayReader {
private int[] arr;

MockArrayReader(int[] arr) {
this.arr = arr;
}

@Override
public int get(int index) {
if (index < 0 || index >= arr.length) return Integer.MAX_VALUE;
return arr[index];
}
}
45 changes: 45 additions & 0 deletions src/binarysearch/RotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
In rotated sorted array at least one size will be sorted, we will check which is side is
sorted first and also check if the target lies in that sorted side, if yes we do binary search on that side
else we repeat the same thing on the opp side
Time Complexity (average): O(log n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : NO
Space Complexity:
O(1)
*/

package binarysearch;

public class RotatedSortedArray {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low)/2;
if (nums[mid] == target) return mid;
// if left side is sorted
if (nums[low] <= nums[mid]){
if( nums[low]<=target && nums[mid]>target){
high = mid - 1;
}
else {
low = mid + 1;
}
}
// right side is sorted
else if(nums[mid] < target){
if(nums[high]>target && nums[mid]<target){
low = mid + 1;
}
else {
high = mid - 1;
}
}
}
return -1;
}
}
36 changes: 36 additions & 0 deletions src/binarysearch/Search2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Treat the 2D matrix as a 1D sorted array using index mapping (mid / n, mid % n).
Apply binary search on this 1D view of the matrix.
Compare the target with the mid-element and adjust the low and high pointers accordingly.
Time Complexity : O(log m*n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : NO
Space Complexity:
O(1)
*/

package binarysearch;

public class Search2DMatrix {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length, n = matrix[0].length;
int low =0;
int high = m*n - 1;
while(low<=high){
int mid = low + (high-low)/2;
int i = mid / n;
int j = mid % n;
if(target == matrix[i][j]){
return true;
}
else if(matrix[i][j] < target){
low = mid + 1;
}
else if(matrix[i][j] > target){
high = mid - 1;
}
}

return false;
}
}
32 changes: 32 additions & 0 deletions src/binarysearch/TestBinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package binarysearch;

public class TestBinarySearch {
public static void main(String[] args) {
//Test for search in RotatedSortedArray
RotatedSortedArray sortedArray = new RotatedSortedArray();
int [] nums = new int[]{4,5,6,7,0,1,2};
System.out.println(sortedArray.search(nums, 0)); //expected 4
System.out.println(sortedArray.search(nums, 9)); //expected -1

//Test for search an element in unknownSortedArray
UnknownSizeSortedArray sol = new UnknownSizeSortedArray();

ArrayReader reader = new MockArrayReader(new int[]{-1, 0, 3, 5, 9, 12});

System.out.println(sol.search(reader, 9)); // expected 4
System.out.println(sol.search(reader, 2)); // expected -1
System.out.println(sol.search(reader, -1)); // expected 0
System.out.println(sol.search(reader, 12)); // expected 5
// Test for search in 2D matrix
Search2DMatrix matrix = new Search2DMatrix();
int[][] values = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};

System.out.println(matrix.searchMatrix(values, 3));// true
System.out.println(matrix.searchMatrix(values, 61)); //false
}
}

34 changes: 34 additions & 0 deletions src/binarysearch/UnknownSizeSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Double the high index until the target is within range.
Perform binary search between low and high.
Time Complexity : O(log n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : NO
Space Complexity:
O(1)
*/

package binarysearch;

public class UnknownSizeSortedArray {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;
while ( reader.get(high) <target ) {
low = high;
high = high *2;
}
while (low <= high) {
int mid = low + (high - low)/2;
int val = reader.get(mid);
if (val == target) return mid;
if (reader.get(mid) >= target) {
high = mid - 1;
}
else if (reader.get(mid) <= target) {
low = mid + 1;
}
}
return -1;
}
}