From 2b8fe47f6756b2789af21b7a2a243f1759b72dda Mon Sep 17 00:00:00 2001 From: Indra Date: Thu, 23 Apr 2026 18:32:44 -0500 Subject: [PATCH] Binary search solutions Co-authored-by: Copilot --- Search2DMatrix.java | 30 ++++++++++++++++++++++++++++++ SearchUnknownSizeArray.java | 33 +++++++++++++++++++++++++++++++++ SearhRotatedArray.java | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 Search2DMatrix.java create mode 100644 SearchUnknownSizeArray.java create mode 100644 SearhRotatedArray.java diff --git a/Search2DMatrix.java b/Search2DMatrix.java new file mode 100644 index 00000000..0084cf06 --- /dev/null +++ b/Search2DMatrix.java @@ -0,0 +1,30 @@ +// #74. Search a 2D Matrix +// 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 + +public class Search2DMatrix { + public boolean searchMatrix(int[][] matrix, int target) { + 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; + int r = mid / n; + int c = mid % n; + + if(matrix[r][c] == target){ + return true; + } + else if (target > matrix[r][c]){ + low = mid+1; + }else { + high = mid -1; + } + } + return false; + } +} \ No newline at end of file diff --git a/SearchUnknownSizeArray.java b/SearchUnknownSizeArray.java new file mode 100644 index 00000000..bb457b94 --- /dev/null +++ b/SearchUnknownSizeArray.java @@ -0,0 +1,33 @@ +// #702. Search in a Sorted Array of Unknown Size +// Time Complexity : O(log(n)): n is the length of the array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// interface ArrayReader { +// public int get(int index) {} +// } +public class SearchUnknownSizeArray { + public int search(ArrayReader reader, int target) { + int low = 0; + int 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; + } else if( target < reader.get(mid)){ + high = mid-1; + } + else{ + low = mid+1; + } + } + return -1; + } +} \ No newline at end of file diff --git a/SearhRotatedArray.java b/SearhRotatedArray.java new file mode 100644 index 00000000..f5886b15 --- /dev/null +++ b/SearhRotatedArray.java @@ -0,0 +1,33 @@ +// #33. Search in Rotated Sorted Array +// Time Complexity : O(log(n)): n is the length of the array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +public class SearhRotatedArray { + public int search(int[] nums, int target) { + int l = 0; + int h = nums.length -1; + int m = 0; + while(l= nums[l] && target < nums[m]){ + h = m - 1; + }else{ + l = m + 1; + } + }else{ + if(target > nums[m] && target <= nums[h]){ + l = m + 1; + }else{ + h = m -1; + } + } + } + if(nums[l] == target) return l; + return -1; + } +}