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
34 changes: 34 additions & 0 deletions Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Search {

/*
// Time Complexity : O(2log n) = O(log n)
// Space Complexity : O(1)
To serach in an array of unknown size, we implement binary search but not starting with first and last
index of the array as it is unknown. We start with the first two indices of the array and check if the value at high index
is less than the target, then we increment the serch size of array by high * 2. We do this as long as we get a high index which is
less than the target value. Once we get the high index range, we do a regular binary search on the array to find index of the target element.
*/

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;
if (reader.get(mid) == target) {
return mid;
} else if (reader.get(mid) > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}
}
30 changes: 30 additions & 0 deletions SearchMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class SearchMatrix {

/*
// Time Complexity : O(log n + log m)
// Space Complexity : O(1)
To find a target element in a 2-D matrix which has elements sorted in ascending order, we are treating this 2D matrix as
an array which will make it possible to find element using binary search. To find the mid element in the matrix between
low and high indexes, we get the row index by dividing mid by column legth and column index by modulo operation with column length.
Then we compare if the value we are looking for is found return true. If its greater than mid element, we move the low pointer to mid + 1 index
else we move the high pointer to mid - 1. If we do not find the element, return false.
*/


public boolean searchMatrix(int[][] matrix, int target) {
int row = matrix.length;;
int col = matrix[0].length;
int low = 0;
int high = row * col - 1;
while (low <= high) {
int mid = low + (high - low)/2;
int r = mid/col;
int c = mid % col;
if (matrix[r][c] == target) return true;
else if (matrix[r][c] < target) low = mid + 1;
else high = mid - 1;
}

return false;
}
}
38 changes: 38 additions & 0 deletions SearchRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class SearchRotatedSortedArray {
/*
Time complexity - O(log n)
Space complexity = O(1)
to search in a rotated sorted array using binary search, we need to determine in which
part of the array the target lies in. If mid element >= low element, it means the array
is in sorted order. Check if mid element > target and low element <= target, if so we
search in the left side lese we search in the right side of the array. When searching in
right side if mid element < target and high ele is >= target then search in the right side of mid
else search on the left side. If mid element equals target we return index. Else we return -1.
*/
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;
}
if (nums[mid] >= nums[low]) {
if (nums[mid] > target && nums[low] <= target) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (nums[mid] < target && nums[high] >= target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}

return -1;
}
}