All problems

Problem 94

Minimum in Rotated Sorted Array

Medium · 20 pts · Array, Binary Search

Problem

An array of distinct integers was sorted in increasing order and then rotated at an unknown position, so it now looks like two increasing runs, for example 3 4 5 1 2. The array may also not be rotated at all. Find its minimum element in O(log N). Compare the middle element with the last element. If it is larger, the minimum lies to the right of the middle; otherwise the minimum is the middle element or somewhere to its left. Keep narrowing until one element remains.

Input

First line: integer N. Second line: the N rotated array values.

Output

Print the minimum element.

Constraints

1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000; values are distinct

Example input

5
3 4 5 1 2

Example output

1

Explanation

3 4 5 1 2 is the sorted array 1 2 3 4 5 rotated so that 3 comes first. The minimum is the first element of the second run, which is 1.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Search in Rotated Sorted Array Next: Peak of a Mountain Array