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 #2176

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
34 changes: 34 additions & 0 deletions BinarySearchinrotatedsortedarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach in three sentences only: I followed the binary search approach where I calculate the middle index (mid). If mid matches the target, I return true. If not, I compare mid with the target: if mid > target, it indicates that the target may lie in the left part of the array. Otherwise,
// the target may be in the right part. This process is repeated, continuously reducing the search space by halfclass 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;
}else if(nums[low] <= nums[mid]){
if(nums[low] <= target && target < nums[mid] ){
high=mid-1;
}else{
low=mid+1;
}
}else{
if(nums[mid] < target && target <= nums[high]){
low=mid+1;
}
else{
high=mid-1;
}

}
}
return -1;
}
}
33 changes: 33 additions & 0 deletions Search-a-2d-matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity :O(logm*n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach in three sentences only: I followed the binary search approach by treating the matrix as a 1D array of length m*n-1. I calculate the middle index (mid),
// then use it to determine the corresponding row and column indices. Using these indices, I access the value at mid. If mid matches the target, I return true. If not, I compare mid with the target: if mid is greater than the target, the target may lie in the left part of the matrix. Otherwise, the target is likely in the right part.
// This process continues while(low<=high), reducing the search space by half at each step

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n=matrix[0].length;
int low=0;int high=m*n-1;
while(low <= high){
int mid=low+(high-low)/2;
int r=mid/n;
int c=mid%n;

int midvalue=matrix[r][c];
if(midvalue == target){
return true;
}else if(midvalue > target){
high=mid-1;
}else{
low=mid+1;
}

}
return false;
}
}
37 changes: 37 additions & 0 deletions search-in-a-sorted-array-of-unknown-size.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity :O(logm+logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :i did not run on it leetcode,
// because i do not have premium subcription.
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach in three sentences only:
//Since the end of the array is unknown, I first determined the search range for binary search.
// Instead of performing binary search from 0 to Integer.MAX_VALUE,
// which would be inefficient, I optimized the process by expanding the search range exponentially.
// I started with low = 0 and high = 1, then doubled high (high = 2 * high)
// until reader.get(high) > target. Once the range was identified,
// I applied a standard binary search within that range to efficiently locate the target.

public int searchMatrix(ArrayReader reader, int target) {
System.out.println("Hello World!");

int low=0; int high=1;
while(reader.get(high) < target){//logm
low=high;
high=2*high;
}
while(low<=high){//logn
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;
}