From 18a874980eca01d91e9b1419215d55b2ee72e752 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Thu, 9 Apr 2026 22:32:02 -0700 Subject: [PATCH 1/2] BinarySearch 1 --- SearchUnkownSize.java | 57 +++++++++++++++++++++++++++++++++ SearchinMatrix.java | 47 +++++++++++++++++++++++++++ SearchinRotatedSortedArray.java | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 SearchUnkownSize.java create mode 100644 SearchinMatrix.java create mode 100644 SearchinRotatedSortedArray.java diff --git a/SearchUnkownSize.java b/SearchUnkownSize.java new file mode 100644 index 00000000..7e9f800c --- /dev/null +++ b/SearchUnkownSize.java @@ -0,0 +1,57 @@ +// Time Complexity :OLog(M)+OLog(N) +// Space Complexity :o(1) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :Got confused about the out of range condition, Need to think deeper on the question 2^31-1. + + +// Your code here along with comments explaining your approach in three sentences only +// I made use of the solution discussed in class. +// We are not given the size of the array. +// We can start with small values low=0 and high=1. +// To keep the N value low we have many conditions, we start high by 1, we increment it by 2*x and move low as well. +//Once range it fixed we follow the same binary search approach as we do in normal sorted array. +// We need to check the out of range condition as well, if the value is out of range we can return -1. + +public class SearchUnkownSize { +/** + * // This is ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public int get(int index) {} + * } + */ +interface ArrayReader { + public default int get(int index) { + return index;} + } + +class Solution { + public int search(ArrayReader reader, int target) { + + int low=0,high=1; + while(reader.get(high)target) + { + high=mid-1; + } + else + { + low=mid+1; + } + } + //right side is sorted + else + { + if(target > nums[mid] && nums[high]>=target) + { + low=mid+1; + } + else + { + high=mid-1; + } + } + + } + return -1; + + } +} \ No newline at end of file From 4f14454347a63faac1536e40c84a07828cbbbee1 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Thu, 9 Apr 2026 22:34:47 -0700 Subject: [PATCH 2/2] BinarySearch 1 --- SearchinMatrix.java | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/SearchinMatrix.java b/SearchinMatrix.java index 7b0b1e7d..94d6d147 100644 --- a/SearchinMatrix.java +++ b/SearchinMatrix.java @@ -12,36 +12,29 @@ // We can then compare the mid value with the target and adjust // the low and high pointers accordingly until we find the target or the search space is empty. -public class SearchinMatrix { - class Solution { +class Solution { public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; - int rows=matrix.length; - int cols=matrix[0].length; + int left = 0; + int right = m * n - 1; - int low=0; - int high=rows*cols-1; + while (left <= right) { + int mid = left + (right - left) / 2; - while(low<=high) - { - int mid=low+(high-low)/2; - int midval=matrix[mid/cols][mid%cols]; //we get index convert it back to row*column + int row = mid / n; + int col = mid % n; - if(midval==target) - { + if (matrix[row][col] == target) { return true; - } - if(midval