diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..53a2d56e --- /dev/null +++ b/Problem1.java @@ -0,0 +1,34 @@ +// 74. Search in a 2D Matrix +// Time Complexity : O(log(m * n)) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes (Could not try since LC premium Question but ran on multiple example values) +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// 1. Row 1 is sorted and the next row begans after first ends and is sorted again +// 2. Row 1,2,3 can be flattened into one big single row of length, m*n +// 3. Index of row can be calculated by i/n and columns as i%n where n would be the column. +class Solution { + 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 midVal = matrix[mid / n][mid % n]; + + if (midVal == target) { + return true; + } else if (midVal < target) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return false; + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..842a1f98 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,44 @@ +// 33. Search in a Rotated 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; + + // Step 1: Identify which half is sorted + if (nums[low] <= nums[mid]) { + // Step 2: Check if target lies in left half + if (target >= nums[low] && target < nums[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } else { // Right half is sorted + // Step 3: Check if target lies in right half + if (target > nums[mid] && target <= nums[high]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + } + return -1; + } +} + + +// Time Complexity : O(Log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : I missed checking the value is pressent on the left side of mid and again checkin fthe righ side. + + +// Your code here along with comments explaining your approach in three sentences only +// Set Low and High to start and ending of the array. Figure the Mid +// Check which side is sorted from both, Low is less than mid then left side is shorter +// After Getting Shorter side choose if the target value lies on which side and do binary search on same. \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..671d04df --- /dev/null +++ b/Problem3.java @@ -0,0 +1,36 @@ +// 702. Search in a Sorted Array of Unknown Size +import java.util.*; + +class Problem3{ + public int search(ArrayReader reader, int target){ + int low=0; + int high=1;// To be able to multiply twice everytime + while(reader.get(high)target){ + high=mid-1; + } + else{ + low=mid+1; + } + } + return -1;// If Value is not Present + } +} + +// Time Complexity : O(N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes (Could not try since LC premium Question but ran on multiple example values) +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// 1. First I set the Low and HIgh pointer to 0 and 1 respectively and kep increasing the higher twice until its bigger than Mid. +// 2. After getting a range I could Apply the normal binary search \ No newline at end of file