Problem · Array

Find All Local Peaks

Learn this problem
EasyTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

Given an integer array nums, find every local peak.

An element nums[i] is a local peak when it has both an immediate left neighbor and an immediate right neighbor, and nums[i] > nums[i - 1] and nums[i] > nums[i + 1].

Return the values of all local peaks in their original left-to-right order. The first and last elements are never local peaks because they do not have two neighbors. If no local peak exists, return an empty array.

Function

findAllLocalPeaks(nums: int[]) → int[]

Examples

Example 1

nums = [1, 3, 2, 4, 1]return = [3, 4]

The value 3 is greater than 1 and 2, and the value 4 is greater than 2 and 1. They appear in that order in nums.

Example 2

nums = [5, 4, 3, 2, 1]return = []

No element with two neighbors is greater than both of them. The boundary values 5 and 1 are not eligible.

More Tiktok problems

drafts saved locally
public int[] findAllLocalPeaks(int[] nums) {
    // write your code here
}
nums[1, 3, 2, 4, 1]
expected[3, 4]
checking account