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/SearchElementInUnknownSize.java b/SearchElementInUnknownSize.java new file mode 100644 index 00000000..a557939f --- /dev/null +++ b/SearchElementInUnknownSize.java @@ -0,0 +1,36 @@ +// Time Complexity : O(M + N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : YES +// Any problem you faced while coding this : NO + + +// Your code here along with comments explaining your approach in three sentences only + +// This code finds a target in an array of unknown size by first exponentially expanding the search range until the upper bound exceeds the target. +// Once the boundaries are established, it performs a standard Binary Search within that range to locate the specific index. + +class SearchElementInUnknownSize { + public int search(ArrayReader reader, int target) { + int low = 0; + int high = 1; + + while (reader.get(high) < target) { + low = high; + high = high * 2; + } + + while (low <= high) { + int mid = low + (high - low)/2; + + if (reader.get(mid) == target) { + return mid; + } else if (target > reader.get(mid)) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/SearchIn2DMatrix.java b/SearchIn2DMatrix.java new file mode 100644 index 00000000..871e2fd8 --- /dev/null +++ b/SearchIn2DMatrix.java @@ -0,0 +1,38 @@ +// Time Complexity : O(log m*n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : YES +// Any problem you faced while coding this : NO + + +// Your code here along with comments explaining your approach in three sentences only + +// This code performs a binary search on a 2D matrix by treating it as a single, sorted 1D array of length times n. +// It calculates the 2D coordinates using mid / n and mid % n to check the target, efficiently finding the value in O(log(m x n)) time. + +public class SearchIn2DMatrix { + public boolean searchMatrix(int[][] matrix, int target) { + int m = matrix.length; + int n = matrix[0].length; + + int low = 0, high = m*n-1; + + while(low <= high){ + int mid = low + (high - low)/2; + + int r = mid / n; + int c = mid % n; + + if(matrix[r][c] == target){ + return true; + }else if(matrix[r][c] > target){ + high = mid - 1; + }else{ + low = mid + 1; + } + + } + + return false; + + } +} \ No newline at end of file diff --git a/SearchInRotatedSortedArray.java b/SearchInRotatedSortedArray.java new file mode 100644 index 00000000..e550740c --- /dev/null +++ b/SearchInRotatedSortedArray.java @@ -0,0 +1,38 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : YES +// Any problem you faced while coding this : NO + + +// Your code here along with comments explaining your approach in three sentences only + +// This implementation performs a Binary Search by first determining which half of the array is sorted relative to the midpoint. +// By comparing the target against the boundaries of that sorted half, it can reliably discard half of the search space in each step and finds the target. + +class SearchInRotatedSortedArray { + 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 (target == nums[mid]) return mid; + if (nums[left] <= nums[mid]) { + if (target >= nums[left] && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + if (target > nums[mid] && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + } + + return -1; + } +} \ No newline at end of file