diff --git a/Matrix.java b/Matrix.java new file mode 100644 index 00000000..4cddc93f --- /dev/null +++ b/Matrix.java @@ -0,0 +1,32 @@ +//Time Complexity - O(log(m*n)) where m is number of row and n is number of columns +//Space Complexity - O(1) Not using any extra space. + +// Implemented searchMatrix using Binary search. +// Assumed the 2d array into 1d array as it stated that the last element of each row is less than +// first element of next row. The row and column of the mid is found by dividing and mod of n. +public class Matrix { + public boolean searchMatrix(int[][] matrix, int target) { + if(matrix == null || matrix.length == 0) { + return false; + } + int m = matrix.length; + int n = matrix[0].length; + int low = 0; + int high = (m * n) - 1; + while(low <= high) { + int mid = low +(high - low) / 2; // prevent overflow + int row = mid / n; + int col = mid % n; + if(matrix[row][col] == target) { + return true; + } else if(target < matrix[row][col]) { + high = mid - 1; + + } else { + low = mid + 1; + } + + } + return false; + } +} diff --git a/RotatedArray.java b/RotatedArray.java new file mode 100644 index 00000000..c55cabc8 --- /dev/null +++ b/RotatedArray.java @@ -0,0 +1,40 @@ +//Time Complexity - O(log n) +//Space Complexity - O(1) Not using any extra space. + +// Implemented using Binary search on rotated sorted array. After caluculating the mid I have compared +// value at mid with the value at low and high to find the sorted side of the array. If I find it on the +// left side I will then compare the target with value at low and mid and move the low and high values accordingly. +public class RotatedArray { + public int search(int[] nums, int target) { + if(nums == null && nums.length == 0) { + return -1; + } + int n = nums.length; + int low = 0; + int high = n - 1; + while(low <= high) { + int mid = low + (high - low) / 2; //prevent overflow + if(nums[mid] == target) { + return mid; + } + if(nums[low] <= nums[mid]){ + if(target >= nums[low] && target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + + } else { + if(target > nums[mid] && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + + } + return -1; + + } +} diff --git a/SearchInUnkownSize.java b/SearchInUnkownSize.java new file mode 100644 index 00000000..7d9cdcb5 --- /dev/null +++ b/SearchInUnkownSize.java @@ -0,0 +1,47 @@ +//Time Complexity - O(log(n)) +//Space Complexity - O(1) Not using any extra space. + +// Implemented search in UnkownSize array using Binary search. Initially I have taken left=0 and right = 1 as size is unkown +// if the target is not in left and right I moved the right to right *2 and left to right then the low and high bound is founda nd performed binary search. +public class SearchInUnkownSize { + + public static class ArrayReader { + private int[] secret; + + public ArrayReader(int[] arr) { + this.secret = arr.clone(); + } + + public int get(int index) { + if(index < 0 || index >= secret.length){ + return Integer.MAX_VALUE; + } + return secret[index]; + } + } + public static int search(ArrayReader reader, int target) { + + if(reader.get(0) == target) { + return 0; + + } + int left = 0; + int right = 1; + while (reader.get(right) < target) { + left = right; + right *= 2; + } + while(left <= right) { + int pivot = left + (right - left) /2; + if(reader.get(pivot) == target) { + return pivot; + } else if(reader.get(pivot) > target) { + right = pivot - 1; + } else { + left = pivot + 1; + } + } + return -1; + + } +}