Skip to content

Commit ad6e539

Browse files
Accepted
1 parent 18246ca commit ad6e539

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 10/04/2017.
3+
* Accepted
4+
*/
5+
public class MinSortedRotatedArray
6+
{
7+
/**
8+
* Main method
9+
* @param args
10+
* @throws Exception
11+
*/
12+
public static void main(String[] args) throws Exception
13+
{
14+
int[] A = {5, 1, 2, 3, 4};
15+
System.out.println(new MinSortedRotatedArray().findMin(A));
16+
}
17+
18+
public int findMin(int[] nums)
19+
{
20+
if(nums.length == 0) return 0;
21+
else if(nums.length == 1) return nums[0];
22+
int low = 0, high = nums.length - 1;
23+
while(low < high)
24+
{
25+
int mid = (low + high) / 2;
26+
if(mid > 0 && nums[mid] < nums[mid - 1])
27+
return nums[mid];
28+
if(nums[low] > nums[mid])
29+
high = mid - 1;
30+
else if(nums[high] < nums[mid])
31+
low = mid + 1;
32+
else high = mid - 1;
33+
}
34+
return nums[low];
35+
}
36+
}

0 commit comments

Comments
 (0)