Knowee
Questions
Features
Study Tools

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:[4,5,6,7,0,1,2] if it was rotated 4 times.[0,1,2,4,5,6,7] if it was rotated 7 times.Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].Given the sorted rotated array nums of unique elements, return the minimum element of this array.You must write an algorithm that runs in O(log n) time.

Question

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:[4,5,6,7,0,1,2] if it was rotated 4 times.[0,1,2,4,5,6,7] if it was rotated 7 times.Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].Given the sorted rotated array nums of unique elements, return the minimum element of this array.You must write an algorithm that runs in O(log n) time.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to find the minimum element in a rotated sorted array. The array was initially sorted in ascending order, and then rotated. The rotation is to the right, and it can be done between 1 and n times.

Here is a Python solution using Binary Search, which ensures the time complexity is O(log n):

def findMin(nums):
    if len(nums) == 1:
        return nums[0]
    left = 0
    right = len(nums) - 1
    if nums[right] > nums[0]:
        return nums[0]
    while right >= left:
        mid = left + (right - left) // 2
        if nums[mid] > nums[mid + 1]:
            return nums[mid + 1]
        if nums[mid - 1] > nums[mid]:
            return nums[mid]
        if nums[mid] > nums[0]:
            left = mid + 1
        else:
            right = mid - 1

This function works by first checking if the last element of the array is greater than the first element. If it is, that means the array is not rotated, so we return the first element.

Then, we enter a while loop where we calculate the middle index. If the element at the middle index is greater than the element next to it, that means we found the point of rotation, so we return the next element.

If the element before the middle element is greater than the middle element, we found the point of rotation, so we return the middle element.

If the middle element is greater than the first element, that means the smallest element is on the right side of the middle element, so we move the left pointer to mid + 1.

Otherwise, the smallest element is on the left side, so we move the right pointer to mid - 1.

We keep doing this until we find the smallest element.

This problem has been solved

Similar Questions

A left rotation operation on an array of size shifts each of the array's elements unit to the left. Given an integer, , rotate the array that many steps left and return the result.ExampleAfter rotations, .Function DescriptionComplete the rotateLeft function in the editor below.rotateLeft has the following parameters:int d: the amount to rotate byint arr[n]: the array to rotateReturnsint[n]: the rotated arrayInput FormatThe first line contains two space-separated integers that denote , the number of integers, and , the number of left rotations to perform.The second line contains space-separated integers that describe .ConstraintsSample Input5 41 2 3 4 5Sample Output5 1 2 3 4ExplanationTo perform left rotations, the array undergoes the following sequence of changes:

Implement a function that returns an updated array with 1 left rotation on an array of integersrotateLeft([1,2,3,4]) // returns [2,3,4,1]

There is an integer array nums sorted in ascending order (with distinct values).Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.You must write an algorithm with O(log n) runtime complexity. Example 1:Input: nums = [4,5,6,7,0,1,2], target = 0Output: 4Example 2:Input: nums = [4,5,6,7,0,1,2], target = 3Output: -1Example 3:Input: nums = [1], target = 0Output: -1 Constraints:1 <= nums.length <= 5000-104 <= nums[i] <= 104All values of nums are unique.nums is an ascending array that is possibly rotated.-104 <= target <= 104

Given an array Arr[] of N integers and a positive integer K. The task is to cyclically rotate the arrayclockwise by K.Note: Keep the first position of the array unaltered.

public static List<Integer> rotateLeft(int d, List<Integer> arr) { int n = arr.size(); List<Integer> result = new ArrayList<>(n); for (int i = 0; i < n; i++) { int newIndex = (i + n - d) % n; result.add(arr.get(newIndex)); } return result; }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.