From 1f47abf913908410980d196fcd050f79011c054b Mon Sep 17 00:00:00 2001 From: GOUTHAM9955 Date: Tue, 7 Oct 2025 23:52:02 -0400 Subject: [PATCH] Binary-Search-1 Solution --- .idea/.gitignore | 5 +++ .idea/Binary-Search-1.iml | 11 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 7 +++ ArrayReader.java | 3 ++ RotatedSortedArray.java | 44 +++++++++++++++++++ Sample | 7 --- SearchIn2DMatrix.java | 62 +++++++++++++++++++++++++++ SearchInSortedArrayOfUnknownSize.java | 44 +++++++++++++++++++ 10 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Binary-Search-1.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 ArrayReader.java create mode 100644 RotatedSortedArray.java delete mode 100644 Sample create mode 100644 SearchIn2DMatrix.java create mode 100644 SearchInSortedArrayOfUnknownSize.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..a0ccf77b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml diff --git a/.idea/Binary-Search-1.iml b/.idea/Binary-Search-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Binary-Search-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..6f29fee2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..1642735e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..83067447 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ArrayReader.java b/ArrayReader.java new file mode 100644 index 00000000..d2d898ce --- /dev/null +++ b/ArrayReader.java @@ -0,0 +1,3 @@ +public interface ArrayReader { + int get(int high); +} diff --git a/RotatedSortedArray.java b/RotatedSortedArray.java new file mode 100644 index 00000000..3e2a86f1 --- /dev/null +++ b/RotatedSortedArray.java @@ -0,0 +1,44 @@ +/* +Time Complexity: O(log(n)) +Space Complexity : O(1) + +Approach: Implementing binary search to find the value as we can reduce the search size by half with this approach + */ +public class RotatedSortedArray { + // Declare instance variables + private int low; + private int high; + private int mid; + public int search(int[] nums, int target) { + // Initialize the instance variables declared above + low =0 ; + high = nums.length-1; + mid = 0; + + // Initialise the while loop with break condition low<=high to implement binary search + while(low <= high){ + //Calculate mid value + mid = low + (high-low) / 2; + // If value at index mix is target return mid + if(nums[mid] == target) return mid; + // Reducing the search by half + else{ + // If left side of mid is sorted + if(nums[low] <= nums[mid]){ + // And target lies in left side + if(nums[low] <= target && nums[mid] >= target) high = mid-1; + // Remove right side + else low = mid+1; + } + // Right side is sorted + else{ + //If target lies in right side remove left half + if(nums[mid] <= target && nums[high] >= target) low = mid+1; + // If not it should be left half so remove right half + else high = mid-1; + } + } + } + return -1; + } +} diff --git a/Sample b/Sample deleted file mode 100644 index ceae9f98..00000000 --- a/Sample +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach in three sentences only diff --git a/SearchIn2DMatrix.java b/SearchIn2DMatrix.java new file mode 100644 index 00000000..d357de6f --- /dev/null +++ b/SearchIn2DMatrix.java @@ -0,0 +1,62 @@ +/* +Time complexity: O(log(n) +Space Complexity: O(1) + +Approach: We a 2 dimentional array with 2 conditions + 1) Rows are sorted + 2) At any give row last column value at previous row is less than the 1st column value of current row + If we convert the 2D array into a 1D array it looks like a sorted array of length ((m*n)-1) on which we + can apply binary search + + To find the location this mid in 2 array we can use + row value = ((m*n)-1) / (length of column) + column value = ((m*n)-1) % (length of column) + + Reason: Visualise it in 2D, we will have 1st row values followed 2nd row values etc... until length/(total number of columns) + So, to find exact row value we have to divide by column value + + In the order of values in these rows will be in the order of columns so modulus of length will give the exact column location + + + + */ +public class SearchIn2DMatrix { + // Declaring instance variables + private int low; + private int high; + private int mid; + private int rows; + private int columns; + public boolean searchMatrix(int[][] matrix, int target) { + // Intializing instance variables based on arguments + low = 0; + rows = matrix.length; + columns = matrix[0].length; + high = rows*columns -1; + // Initialise the while loop with break condition low<=high to implement binary search + while(low <= high){ + // Calculating mid + mid = low + (high-low)/2; + // Based on mid calculating row and column values + int row = mid/columns; + int column = mid % columns; + // If target is equal to mid value return true + if(matrix[row][column] == target) return true; + + // Reducing the search by half + else{ + // If value lies in right half + if(matrix[row][column] < target){ + // remove left half + low = mid+1; + } + else + { // remove left half + high = mid -1; + } + } + } + // It the value isn't present in the array return false + return false; + } +} diff --git a/SearchInSortedArrayOfUnknownSize.java b/SearchInSortedArrayOfUnknownSize.java new file mode 100644 index 00000000..8011c479 --- /dev/null +++ b/SearchInSortedArrayOfUnknownSize.java @@ -0,0 +1,44 @@ +/* +Time complexity: O(log(n)) +Space complexity: O(1) + +Approach: + Since we don't know the upper bound, we can start with index 0 and 1 and increase the search space by twice + until we exceed the upper limit. + + Now do binary search on that limit + + ****Question I had: + Please correct me if my understanding is wrong*** + Why twice: We can increase if by any value but it won't have any effect. It will for sure reduce number of steps in finding + the range but the search space in that range increases + */ +public class SearchInSortedArrayOfUnknownSize { + public int search(ArrayReader reader, int target) { + // Initialising the local variables + int low = 0; + int high =1; + int mid; + + // While loop to find the search range + // Will stop if we find the range or if upper limit exceeds the length of the array. + while(target > reader.get(high)){ + low = high; + high = high*2; + } + + // Apply binary search on the range found above and do the search + while(low<= high){ + mid = low + (high-low)/2; + if (reader.get(mid) == target) return mid; + if(reader.get(mid)>=target){ + high = mid-1; + } + else{ + low = mid+1; + } + } + // Return -1 if the value isn't present + return -1; + } +}