diff --git a/SearchIn2DMatrix.java b/SearchIn2DMatrix.java new file mode 100644 index 00000000..cdc97409 --- /dev/null +++ b/SearchIn2DMatrix.java @@ -0,0 +1,49 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + // Time Complexity : O(log(m*n)) = logm + logn + // Space Complexity : O(1) + // Approach : Identify the row where potential target can be found and apply Binary search on the row to check if it exists + int rowIdx = getSearchRow(matrix,target); + if (rowIdx == -1){ + return false; + } else { + return binarySearch(matrix,rowIdx,target); + } + + } + public int getSearchRow(int[][] matrix, int target){ + int columnLen = matrix[0].length - 1; + + // perform binary search on rows + int topRow = 0; + int bottomRow = matrix.length-1; + while(topRow<=bottomRow){ + int midRow = bottomRow + (topRow-bottomRow)/2; // midRow + // check if traget exists in the midRow + if(matrix[midRow][0] <= target && matrix[midRow][columnLen] >= target){ + return midRow; + } else if(matrix[midRow][0]>target){ + bottomRow = midRow-1; + } else{ + topRow = midRow+1; + } + } + return -1; + } + + public boolean binarySearch(int[][] matrix, int row, int target){ + int low = 0; + int high = matrix[row].length-1; + while (low<=high){ + int mid = low + (high -low)/2; + if(matrix[row][mid] == target){ + return true; + } else if (matrix[row][mid]> target){ + high=mid-1; + } else{ + low = mid+1; + } + } + return false; + } +} \ No newline at end of file diff --git a/SearchInInfiniteSortedArray.java b/SearchInInfiniteSortedArray.java new file mode 100644 index 00000000..13cd0b7c --- /dev/null +++ b/SearchInInfiniteSortedArray.java @@ -0,0 +1,32 @@ +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Approach : When sorted Array is rotated then one portion of array is always sorted (left or right). Calculate mid if our target is at mid then simple return the mid. If our target is not at the mid then use the sorted part of the array to move left and right pointers. by checking if the target is lying in sorted part of the array. + +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; + if(nums[mid] == target){ + return mid; + } + // left array is sorted + if(nums[low]<=nums[mid]){ + if(nums[low]<= target && nums[mid]>=target){ + high = mid -1; + } else{ + low=mid+1; + } + } else{ + // right array is sorted + if(nums[mid]<=target && nums[high]>=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..e3c4f2fe --- /dev/null +++ b/SearchInRotatedSortedArray.java @@ -0,0 +1,66 @@ +/** + * // This is ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public int get(int index) {} + * } + */ +// Time Complexity :O(logn) +// Space Complexity : O(1) +// Approach : We need to create a range utilizing reader in such a way that target is included in it or high not exceeding target and then perform Binary Search. + +class Solution { + public int search(ArrayReader reader, int target) { + int low=0; + int high = 1; + while(reader.get(high) target){ + high=mid-1; + } else{ + low=mid+1; + } + } + return -1; + + } +} + + + + + +/** + * // This is ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * interface ArrayReader { + * public int get(int index) {} + * } + */ +// Time Complexity :O(logn) +// Space Complexity : O(1) +// Approach : Brute Force Approach by assuming high as Integer max Value and performing Binary Search. +class Solution { + public int search(ArrayReader reader, int target) { + int low = 0; + int high = Integer.MAX_VALUE-1; + 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