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

completed Binary-Search-1 #2174

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
35 changes: 35 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This solution uses binary search on the rows to dtermine whihc row our target might fall under
// and after getting required row again binary search is applied on the columns to check if the target is present in our
//row or not and returns true or false based on availability
// Time Complexity: O(log m) for binary search rows and O(log n) for binary search on the row for target
// => O(log m + log n)=> O(log(m*n))

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int low = 0;
int high = matrix.length - 1;
int mid;
int columns = matrix[low].length - 1;
while (low != high) {
mid = low + (high - low) / 2;
if (target <= matrix[mid][columns]) {
high = mid;
} else
low = mid + 1;
}
int[] reqRow = matrix[high];
low = 0;
high = reqRow.length - 1;
while (low < high) {
mid = low + (high - low) / 2;
if (reqRow[mid] == target)
return true;
if (target < reqRow[mid]) {
high = mid;
} else
low = mid + 1;
}
boolean result = reqRow[high] == target ? true : false;
return result;
}
}
33 changes: 33 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class SearchInRotatedSortedArray {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
if (nums.length == 0)
return -1;

while (low < high) {
int midIndex = low + (high - low) / 2;
if (nums[midIndex] == target) {
return midIndex;
}
if (nums[low] <= nums[midIndex]) {
if ((target < nums[midIndex]) && (target >= nums[low])) {
high = midIndex - 1;
} else {
low = midIndex + 1;
}
} else if (nums[midIndex] < nums[high]) {

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

}

}
int index = (nums[high] == target) ? high : -1;
return index;
}
}
28 changes: 28 additions & 0 deletions Problem-3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This solution first calculates the upper bound and then applies binary search using the calculated upperbound
//Time Complexity: O(log n + log n)=> O(2logN) => O(log n)

class SearchInASortedArrayOfUnknownSize {
public int search(ArrayReader reader, int target) {
int upperBound = 1;
int low = 0;
int mid;
while (reader.get(upperBound) <= target) {
upperBound = upperBound * 2;
}

while (low < upperBound) {
mid = low + (upperBound - low) / 2;
int currMid = reader.get(mid);
if (currMid == target)
return mid;
if (target > currMid) {
low = mid + 1;
} else
upperBound = mid - 1;
}
if (reader.get(upperBound) == target)
return upperBound;
return -1;

}
}