Leetcode : (Find Minimum in Rotated Sorted Array)

  package com.demo.leetcode;


public class FindMinInRotatedArray {
public static void main(String[] args) {
int[] numArr = new int[]{3,4,5,1,2};
System.out.println("Min is = "+findMin(numArr));
}

private static int findMin(int[] nums) {
int low = 0;
int high = nums.length -1;
while (low < high){
int mid = low + (high - low) / 2; // just to escape integer overflow
if(nums[mid] > nums[high]){
low = mid + 1;
}
else {
high = mid;
}
}
return nums[low];
}

} 



Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer