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


class RotatedSortedArray {
public int search(int[] nums, int target) {

int left = 0, right = nums.length-1;

// Use binary search to find the target in the array and check whether the left half or right half is sorted.
// On the basic of of the outcome, we decide which half may contain the target and discard the other half
while(left <= right){

int mid = left + (right-left)/2;
if(nums[mid] == target) return mid;

else if(nums[left] <= nums[mid]){
if(nums[left] <= target && nums[mid] > target){
right = mid-1;
}else{
left = mid+1;
}
} else{
if(nums[right] >= target && nums[mid] < target){
left = mid+1;
}else{
right = mid-1;
}
}

}

return -1;

}
}
33 changes: 33 additions & 0 deletions SearchA2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 : No

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

int row = matrix.length;
int col = matrix[0].length;

int left =0;
int right = row*col-1;

// make it to 1D array and then convert the middle index into row and column using division and modulo
// and finally apply binary search to check whether the target exists in the matrix.
while(left <= right){
int mid = left+(right-left)/2;
int r = mid/col;
int c = mid%col;

if(matrix[r][c] == target) return true;

else if(matrix[r][c] < target){
left = mid + 1;
}else{
right = mid - 1;
}
}

return false;
}
}
47 changes: 47 additions & 0 deletions SearchInASortedArrayOfUnknownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class SearchInASortedArrayOfUnknownSize{
public int search(ArrayReader reader, int target) {


// So, we expand the search range until the right boundary is greater than or equal to the target.
// Then apply binary search inside that range because the array is sorted in increasing order.
// If the target is found, return its index; otherwise, return -1.

if(reader.get(0) == target) return 0;

int left =0, right = 1;

while(reader.get(right) < target){
left = right;
right = right*2;
}

while(left <= right){

int mid = left + (right-left)/2;

if(reader.get(mid) == target ) return mid;

if(reader.get(left) <= reader.get(mid)){
if(reader.get(mid) > target && reader.get(left) <= target){
right = mid - 1;
}else{
left = mid + 1;
}
}else{
if(reader.get(mid) < target && reader.get(right) >=target){
left = mid+1;
}else{
right = mid -1;
}
}
}

return -1;
}
}