forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMininRotatedArray.java
More file actions
35 lines (29 loc) · 1.06 KB
/
Copy pathFindMininRotatedArray.java
File metadata and controls
35 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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
class Solution {
public int findMin(int[] nums) {
if(nums==null|| nums.length==0) return -1;
int low=0,high = nums.length-1;
while(low<=high)
{
if(nums[low]<=nums[high])
{
return nums[low];
}
int mid= low+(high-low)/2;
if((mid==0||nums[mid]<nums[mid-1]) && (nums[mid]< nums[mid+1]) ) // if first element or it is less than its both neighbours return it;
{
return nums[mid];
}
else if(nums[low]<=nums[mid]){ //left sorted array, so check right unsorted array
low=mid+1;
}
else
high= mid-1; // right sorted array , so check left unsorted array
}
return -1;
}
}