Skip to content

Completed All Problems in BinarySearch1 #2331

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 3 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
40 changes: 40 additions & 0 deletions Search2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(log(m*n))
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Had to revise basic syntax


class Solution {
public boolean searchMatrix(int[][] matrix, int target) {

// Base Case
if (matrix == null || matrix.length == 0){
return false;
}
int m = matrix.length; // Number of rows
int n = matrix[0].length; // Number of columns

// Treating the 2D matrix as a virtual 1D array
int low = 0;
int high = m * n - 1;

while (low <= high){
int mid = low + (high - low) / 2; // To Prevent Integer Overflow

// Converting 1D index back to 2D coordinates
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;
}
}
45 changes: 45 additions & 0 deletions SearchInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Had to revise basic syntax

class Solution {
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; // To prevent Integer Overflow
if(nums[mid] == target){
return mid;
}

// Determining which side is sorted
if(nums[low] <= nums[mid]){
// Left side is sorted
if(target < nums[mid] && target >= nums[low]){
high = mid - 1; // Target lies in left sorted part
}
else{
low = mid + 1;
}
}
else{
// Right side is sorted
if(target > nums[mid] && target <= nums[high]){
low = mid + 1; // Target lies in right sorted part
}
else{
high = mid - 1;
}
}

}
return -1; // Target not found

}
}
48 changes: 48 additions & 0 deletions SearchInSortedArrayUnknownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Time complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Had to revise basic syntax

/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/

class Solution {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = 1;

// Increasing the range exponentially until reader.get(high) >= target
while (reader.get(high) < target){
low = high;
high = high * 2;
}
if (reader.get(high) == target){
return high;
}

// Using binary search within the found range
return binarySearch(low, high, reader, target);
}

private int binarySearch(int low, int high, ArrayReader reader, int target){
while (low <= high){
int mid = low + (high - low) / 2; // To prevent Integer Overflow
if(reader.get(mid) == target){
return mid;
}
else if(target > reader.get(mid)){
low = mid + 1;
}
else {
high = mid - 1;
}

}
return -1; // Target not found
}
}