From 19cf8b3e0d0b07b55b09fff1c5fe4ea3356a08e5 Mon Sep 17 00:00:00 2001 From: Ruzan Pithawala <36655798+RuzanPithawala@users.noreply.github.com> Date: Thu, 19 Sep 2024 23:18:09 -0400 Subject: [PATCH 1/3] Create SearchRotatedSortedArray.java --- SearchRotatedSortedArray.java | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 SearchRotatedSortedArray.java diff --git a/SearchRotatedSortedArray.java b/SearchRotatedSortedArray.java new file mode 100644 index 00000000..9bbd5373 --- /dev/null +++ b/SearchRotatedSortedArray.java @@ -0,0 +1,37 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) // since we are not using additional we created apart from the original nums array given to us +// 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 +class Solution { + public int search(int[] nums, int target) { + int low=0,high=nums.length-1; + + while(low<=high){ + int mid = low + (high - low)/2; + if(nums[mid]==target) return mid; + else{ + if(nums[mid]<=nums[high]){ //if right part is sorted or not + if(target>nums[mid] && target<=nums[high]){ // checking if the target is there in the sorted portion of the array or not + low=mid+1; + } + else{ + high=mid-1; + } + } + else{ + if(target>=nums[low] && target Date: Fri, 20 Sep 2024 00:33:29 -0400 Subject: [PATCH 2/3] Create Search2DMatrix.java --- Search2DMatrix.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Search2DMatrix.java diff --git a/Search2DMatrix.java b/Search2DMatrix.java new file mode 100644 index 00000000..a9917fc1 --- /dev/null +++ b/Search2DMatrix.java @@ -0,0 +1,26 @@ +// Time Complexity : O(log(m+n)) +// Space Complexity : O(1) no extra space used +// 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 +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int m=matrix.length; + int n= matrix[0].length; + int i=0, j=m*n-1; + while(i<=j){ + int mid = i+(j-i)/2; + if(matrix[mid/n][mid%n]==target){ + return true; + } + else if(matrix[mid/n][mid%n]>target){ + j=mid-1; + } + else i=mid+1; + } + return false; + + } +} From c5eaa9432ff18c280b230220f77db4559a963aa5 Mon Sep 17 00:00:00 2001 From: Ruzan Pithawala <36655798+RuzanPithawala@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:46:33 -0400 Subject: [PATCH 3/3] Create InfiniteSortedArray.java --- InfiniteSortedArray.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 InfiniteSortedArray.java diff --git a/InfiniteSortedArray.java b/InfiniteSortedArray.java new file mode 100644 index 00000000..59d257b0 --- /dev/null +++ b/InfiniteSortedArray.java @@ -0,0 +1,29 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +class solution{ + public int search(ArrayReader reader, int target){ + int low =0,high=0; + if(reader.get(0)==Integer.MAX_VALUE) return -1; + while(reader.get(high)target){ + high = mid-1; + } + else{ + low = mid+1; + } + } + return -1; + } +}