From 649cc4bd8d92f95ffc68b0b2f3c6b55e0992ce44 Mon Sep 17 00:00:00 2001 From: Ishan Tharwal Date: Sun, 22 Jun 2025 03:33:23 -0700 Subject: [PATCH 1/3] Done BinarySearch1 --- Search2DMatrix.java | 36 +++++++++++++++++++++++++++++++++ SearchInRotatedSortedArray.java | 11 ++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Search2DMatrix.java create mode 100644 SearchInRotatedSortedArray.java diff --git a/Search2DMatrix.java b/Search2DMatrix.java new file mode 100644 index 00000000..e87c6455 --- /dev/null +++ b/Search2DMatrix.java @@ -0,0 +1,36 @@ +// Time Complexity : O(log(m*n)) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + + // Base Case + if (matrix == null || matrix.length == 0){ + return false; + } + 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; // To Prevent Integer Overflow + int row = mid / n; + int col = mid % n; + + if(matrix[row][col] == target){ + return true; + } + else if(target < matrix[row][col]){ + high = mid - 1; + } + else{ + low = mid + 1; + } + } + return false; + } +} \ No newline at end of file diff --git a/SearchInRotatedSortedArray.java b/SearchInRotatedSortedArray.java new file mode 100644 index 00000000..55b03e27 --- /dev/null +++ b/SearchInRotatedSortedArray.java @@ -0,0 +1,11 @@ +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Had to revise basic syntax + +class Solution { + public int search(int[] nums, int target) { + + + } +} \ No newline at end of file From 2c9f8524adf39cbc1b1dd71159f0964db8a9f580 Mon Sep 17 00:00:00 2001 From: Ishan Tharwal Date: Sun, 22 Jun 2025 04:47:37 -0700 Subject: [PATCH 2/3] Done BinarySearch1 --- Search2DMatrix.java | 4 ++-- SearchInRotatedSortedArray.java | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Search2DMatrix.java b/Search2DMatrix.java index e87c6455..8bf20d47 100644 --- a/Search2DMatrix.java +++ b/Search2DMatrix.java @@ -1,7 +1,7 @@ // Time Complexity : O(log(m*n)) // Space Complexity : O(1) -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Had to revise basic syntax class Solution { diff --git a/SearchInRotatedSortedArray.java b/SearchInRotatedSortedArray.java index 55b03e27..7cfdcc55 100644 --- a/SearchInRotatedSortedArray.java +++ b/SearchInRotatedSortedArray.java @@ -5,7 +5,37 @@ class Solution { public int search(int[] nums, int target) { + if (nums == null || nums.length == 0){ + return -1; + } + int n = nums.length; + int low = 0; + int high = n - 1; + + while(low <= high){ + int mid = low + (high - low) / 2; // To prevent Integer Overflow + if(nums[mid] == target){ + return mid; + } + if(nums[low] <= nums[mid]){ + if(target < nums[mid] && target >= nums[low]){ + 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 From 2d7c692adc7b7c0cd678f83a0b86c7c4036cff33 Mon Sep 17 00:00:00 2001 From: Ishan Tharwal Date: Tue, 24 Jun 2025 01:32:40 -0700 Subject: [PATCH 3/3] Done All Problems --- Search2DMatrix.java | 8 +++-- SearchInRotatedSortedArray.java | 10 ++++-- SearchInSortedArrayUnknownSize.java | 48 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 SearchInSortedArrayUnknownSize.java diff --git a/Search2DMatrix.java b/Search2DMatrix.java index 8bf20d47..7a53d925 100644 --- a/Search2DMatrix.java +++ b/Search2DMatrix.java @@ -11,13 +11,17 @@ public boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0){ return false; } - int m = matrix.length; - int n = matrix[0].length; + int m = matrix.length; // Number of rows + int n = matrix[0].length; // Number of columns + + // Treating the 2D matrix as a virtual 1D array int low = 0; int high = m * n - 1; while (low <= high){ int mid = low + (high - low) / 2; // To Prevent Integer Overflow + + // Converting 1D index back to 2D coordinates int row = mid / n; int col = mid % n; diff --git a/SearchInRotatedSortedArray.java b/SearchInRotatedSortedArray.java index 7cfdcc55..8924ab8c 100644 --- a/SearchInRotatedSortedArray.java +++ b/SearchInRotatedSortedArray.java @@ -17,17 +17,21 @@ public int search(int[] nums, int target) { if(nums[mid] == target){ return mid; } + + // Determining which side is sorted if(nums[low] <= nums[mid]){ + // Left side is sorted if(target < nums[mid] && target >= nums[low]){ - high = mid - 1; + high = mid - 1; // Target lies in left sorted part } else{ low = mid + 1; } } else{ + // Right side is sorted if(target > nums[mid] && target <= nums[high]){ - low = mid + 1; + low = mid + 1; // Target lies in right sorted part } else{ high = mid - 1; @@ -35,7 +39,7 @@ public int search(int[] nums, int target) { } } - return -1; + return -1; // Target not found } } \ No newline at end of file diff --git a/SearchInSortedArrayUnknownSize.java b/SearchInSortedArrayUnknownSize.java new file mode 100644 index 00000000..9fe0db8d --- /dev/null +++ b/SearchInSortedArrayUnknownSize.java @@ -0,0 +1,48 @@ +// Time complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Had to revise basic syntax + +/** + * // This is ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public int get(int index) {} + * } + */ + +class Solution { + public int search(ArrayReader reader, int target) { + int low = 0; + int high = 1; + + // Increasing the range exponentially until reader.get(high) >= target + while (reader.get(high) < target){ + low = high; + high = high * 2; + } + if (reader.get(high) == target){ + return high; + } + + // Using binary search within the found range + return binarySearch(low, high, reader, target); + } + + private int binarySearch(int low, int high, ArrayReader reader, int target){ + while (low <= high){ + int mid = low + (high - low) / 2; // To prevent Integer Overflow + if(reader.get(mid) == target){ + return mid; + } + else if(target > reader.get(mid)){ + low = mid + 1; + } + else { + high = mid - 1; + } + + } + return -1; // Target not found + } +} \ No newline at end of file