Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Peak Element In Rotated Array

Easy Acceptance 88.00% Points 20.00

Given a rotated sorted array of distinct integers, return the peak element in the array. The peak element is the maximum element in the rotated sorted array. The array was originally sorted in ascending order and then rotated at some unknown pivot. The peak element is the point where the rotation occurs and is greater than its neighboring elements.

Examples
Example 1

Example 1

Input: nums = [4,5,6,7,0,1,2]

Output: 7

Explanation: The maximum element is 7, which is greater than its neighbors and represents the peak of the rotated array.

Example 2

Example 2

Input: nums = [3,4,5,1,2]

Output: 5

Explanation: The element 5 is the largest element and is the peak where the rotation happens.

Example 3

Example 3

Input: nums = [1,2,3,4,5]

Output: 5

Explanation: The array is not rotated. The last element 5 is the maximum and therefore the peak element.

Hints
Hint 1
N/A
Constraints
  • 1 = nums.length = 105
  • -104 = nums[i] = 104
  • All values in nums are distinct
Companies
Samsung
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions