diff --git a/Searchin2DArray.java b/Searchin2DArray.java new file mode 100644 index 00000000..7f8517ef --- /dev/null +++ b/Searchin2DArray.java @@ -0,0 +1,41 @@ +/*There are multiple ways to solve this problem + +Approach 1: Check if the elements belongs to that row, if it does perform BS on that row +Approach 2: Start from either top right or bottom left corner because from there we will have clear idea about where to go +Approach 3: Imagine the array as a 1D array and perform BS on that +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +*/ + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + + int m = matrix.length; //number of rows + int n = matrix[0].length; // number of columns + + int start = 0; + int end = m * n - 1; // total number of elements minus one + + while(start <= end){ + int mid = start + (end - start) / 2; + + //translating 1D array mid to a 2D array mid + // number of rows is dependent on number of column hence using n as the denominator + // division will help with chunking the rows, modulo will help with finding the element + int r = mid / n; + int c = mid % n; + + if(matrix[r][c] == target){ + return true; + } + else if(matrix[r][c] > target){ + end = mid - 1; + } + else + start = mid + 1; + } + return false; + } +} diff --git a/SearchinInfiniteArray.java b/SearchinInfiniteArray.java new file mode 100644 index 00000000..602d7021 --- /dev/null +++ b/SearchinInfiniteArray.java @@ -0,0 +1,41 @@ +/** + * // This is ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public int get(int index) {} + * } + */ + + /* +Approach: In a rotated sorted array one side of the mid will always be sorted! +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +*/ + +class Solution { + public int search(ArrayReader reader, int target) { + int low = 0; + int high = 1; + + // loop through until you find and element greater than target + while(reader.get(high) < target){ + low = high; + high = high * 2; // to have balanced operations + } + + // binary search to find target + while(low <= high){ + int mid = low + (high - low) / 2; + if(reader.get(mid) == target) + return mid; + else if(reader.get(mid) < target){ + low = mid + 1; + } + else + high = mid - 1; + } + return - 1; + } +} \ No newline at end of file diff --git a/SearchinRotatedSortedArray.java b/SearchinRotatedSortedArray.java new file mode 100644 index 00000000..f8de4db4 --- /dev/null +++ b/SearchinRotatedSortedArray.java @@ -0,0 +1,40 @@ +/* +Approach: In a rotated sorted array one side of the mid will always be sorted! +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +*/ +class Solution { + public int search(int[] nums, int target) { + int low = 0; + int high = nums.length - 1; + + while(low <= high){ + int mid = low + (high - low) / 2; + //check which side of the array is sorted + + if(nums[mid] == target) + return mid; + + //check if left half is sorted + if(nums[low] <= nums[mid]){ + // condition to validate where to go + if(nums[low] <= target && nums[mid] > target) + high = mid - 1; + else + low = mid + 1; + } + + //check if right half is sorted + else{ + if(nums[high] >= target && nums[mid] < target) + low = mid + 1; + else + high = mid - 1; + } + } + return -1; + } +} \ No newline at end of file