Advertisement
amitsen01ei

MinElementInRotatedArray.java

Sep 18th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | Source Code | 0 0
  1. public class MinimumInRotatedArray {
  2.  
  3.     /**
  4.      * The function returns the minimum number of an
  5.      * arbitrary number of rotated sorted array using
  6.      * binary search. Hence, the time complexity is O(LogN)
  7.      * and space complexity is O(1)
  8.      * @param nums the rotated sorted array
  9.      * @return minimum number in the array
  10.      */
  11.     public int findMin(int[] nums) {
  12.         var start = 0;
  13.         var end = nums.length - 1;
  14.  
  15.         while (start < end) {
  16.             var mid = start + ((end - start) >> 1);
  17.  
  18.             if (nums[mid] > nums[end]) {
  19.                 start = mid + 1;
  20.             } else {
  21.                 end = mid;
  22.             }
  23.         }
  24.  
  25.         return nums[start];
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement