From c8124b155d4a7d0c811ce0f371c6fd46526676fc Mon Sep 17 00:00:00 2001 From: PrasiddhShah Date: Fri, 3 Oct 2025 15:09:36 -0700 Subject: [PATCH] Binary Search - 1 Completed --- search_in_2d_matrix.java | 37 +++++++++++++++++++ search_in_a_sorted_array_of_unknown_size.java | 25 +++++++++++++ search_in_rotated_sorted_array.java | 34 +++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 search_in_2d_matrix.java create mode 100644 search_in_a_sorted_array_of_unknown_size.java create mode 100644 search_in_rotated_sorted_array.java diff --git a/search_in_2d_matrix.java b/search_in_2d_matrix.java new file mode 100644 index 00000000..556c6f81 --- /dev/null +++ b/search_in_2d_matrix.java @@ -0,0 +1,37 @@ +// Time Complexity : log(mn) +// Space Complexity : O(mn) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : yes mid calcaution is a bit weird to wrap my head around as i ma seeing it for the first time + +/* + * we are treating 2D matrix as a 1d matrix as in the problem statment is it given that last number of row will be smaller then the + * first number of the next row + * + * we are calculating mid as if it was a 1d array and after after words we are converting that mid to row and column index using row size + * divide by row size for row postion and modlus by row size to get column postion + * + * we will use standard binary search + */ + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int left = 0; + int right = matrix[0].length * matrix.length - 1; + int n = matrix[0].length; + while (left <= right) { + int mid = left + (right - left) / 2; + int r = mid / n; + int c = mid % n; + if (matrix[r][c] == target) { + return true; + } else if (matrix[r][c] > target) { + right = mid - 1; + } else { + left = mid + 1; + } + } + + return false; + + } +} \ No newline at end of file diff --git a/search_in_a_sorted_array_of_unknown_size.java b/search_in_a_sorted_array_of_unknown_size.java new file mode 100644 index 00000000..d8ea7965 --- /dev/null +++ b/search_in_a_sorted_array_of_unknown_size.java @@ -0,0 +1,25 @@ +class Solution { + public int search(ArrayReader reader, int target) { + int low = 0; + int high = 1; + while (reader.get(high) <= target) { + if (reader.get(high) == target) { + return high; + } else { + low = high; + high = high * 2; + } + } + while (low <= high) { + int mid = low + (high - low) / 2; + if (reader.get(mid) == target) { + return mid; + } else if (reader.get(mid) > target) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return -1; + } +} \ No newline at end of file diff --git a/search_in_rotated_sorted_array.java b/search_in_rotated_sorted_array.java new file mode 100644 index 00000000..2d803f6d --- /dev/null +++ b/search_in_rotated_sorted_array.java @@ -0,0 +1,34 @@ +// Time Complexity : log n +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : keeping in mind that every time we will get one side as sorted and one side a not was deficult + +/* + * Approach + * standard binary search + * first we check check which side is sorted and then check if the target is in that side or not, if not check the other side + */ + +class Solution { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + 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[mid] < target && nums[right] >= target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return -1; + } +} \ No newline at end of file