diff --git a/RotatedSortedArray.java b/RotatedSortedArray.java new file mode 100644 index 00000000..27ee665d --- /dev/null +++ b/RotatedSortedArray.java @@ -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; + + } +} \ No newline at end of file diff --git a/SearchA2DMatrix.java b/SearchA2DMatrix.java new file mode 100644 index 00000000..72481b49 --- /dev/null +++ b/SearchA2DMatrix.java @@ -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; + } +} \ No newline at end of file diff --git a/SearchInASortedArrayOfUnknownSize.java b/SearchInASortedArrayOfUnknownSize.java new file mode 100644 index 00000000..19e74c5b --- /dev/null +++ b/SearchInASortedArrayOfUnknownSize.java @@ -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; + } +}