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 done #2011

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
29 changes: 29 additions & 0 deletions InfiniteSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
class solution{
public int search(ArrayReader reader, int target){
int low =0,high=0;
if(reader.get(0)==Integer.MAX_VALUE) return -1;
while(reader.get(high)<target){
low=high+1;
high=low*2;
}

while(low<=high){
int mid = low +(high-low)/2;
if(reader(mid)==target) return mid;
if(reader(mid)>target){
high = mid-1;
}
else{
low = mid+1;
}
}
return -1;
}
}
26 changes: 26 additions & 0 deletions Search2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(log(m+n))
// Space Complexity : O(1) no extra space used
// 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
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length;
int n= matrix[0].length;
int i=0, j=m*n-1;
while(i<=j){
int mid = i+(j-i)/2;
if(matrix[mid/n][mid%n]==target){
return true;
}
else if(matrix[mid/n][mid%n]>target){
j=mid-1;
}
else i=mid+1;
}
return false;

}
}
37 changes: 37 additions & 0 deletions SearchRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : O(log n)
// Space Complexity : O(1) // since we are not using additional we created apart from the original nums array given to us
// 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
class Solution {
public int search(int[] nums, int target) {
int low=0,high=nums.length-1;

while(low<=high){
int mid = low + (high - low)/2;
if(nums[mid]==target) return mid;
else{
if(nums[mid]<=nums[high]){ //if right part is sorted or not
if(target>nums[mid] && target<=nums[high]){ // checking if the target is there in the sorted portion of the array or not
low=mid+1;
}
else{
high=mid-1;
}
}
else{
if(target>=nums[low] && target<nums[mid]){
high=mid-1;
}
else{
low=mid+1;
}
}
}

}
return -1;
}
}