diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..b127f3af --- /dev/null +++ b/Problem1.java @@ -0,0 +1,39 @@ +// 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 + + +//Search in rotated sorted array +// - At each step, determines whether the left half or right half is sorted, then checks if the target lies within that sorted portion. +// - Narrows the search range accordingly and returns the target's index if found; otherwise returns -1. + + +class Problem2 { + 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; + } + if(nums[low] < nums[mid]) { + if(target >= nums[low] && target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } else { + if(target > nums[mid] && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + } + + return -1; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..8fc9f61a --- /dev/null +++ b/Problem2.java @@ -0,0 +1,34 @@ +// 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 + +// Search in a sorted array of unknown size +// - Finds a valid search range by exponentially expanding the upper bound until it is greater than or equal to the target +// - Performs binary search within the identified range to locate the target. + +class Problem3 { + public int search(ArrayReader reader, int target) { + int low = 0, high = 1; + + while(target > reader.get(high)) { + low = high; + high = high * 2; + } + + while(low <= high) { + int mid = low + (high - low) / 2; + + if(reader.get(mid) == target) { + return mid; + } + if(target < reader.get(mid)) { + high = mid - 1; + } else { + low = mid + 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..536c6fae --- /dev/null +++ b/Problem3.java @@ -0,0 +1,38 @@ +// 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 + + +//Search in 2D matrix +// - Treats the 2D matrix as a single sorted 1D array and applies binary search. +// - Converts the 1D middle index back to row and column coordinates using division and modulo operations. +// - Returns true if the target is found; otherwise returns false after the search space is exhausted. + + + +class Problem1 { + public boolean searchMatrix(int[][] matrix, int target) { + int rows = matrix.length; + int cols = matrix[0].length; + int low = 0, high = rows * cols - 1; + + while(low <= high) { + int mid = low + (high - low) / 2; + + int midRow = mid / cols; + int midColumn = mid % cols; + + if(matrix[midRow][midColumn] == target) { + return true; + } + if(target < matrix[midRow][midColumn]) { + high = mid - 1; + } else { + low = mid + 1; + } + } + + return false; + } +} \ No newline at end of file