BackmediumArraysAmazonAtlassian

Peak Index in Array Solution

Problem Statement

You are given an array of integers yields where yields[i] represents the yield at position i. Find the index of the first position that is considered optimal for harvesting. A position is considered optimal if its yield is strictly higher than its neighboring positions. If no such position exists, return -1.

Example 1
Input
[1, 2, 3, 4, 5, 6]
Output
-1

Explanation: Step-by-step: We iterate through the array from left to right. At position 0, the yield is 1, which is not strictly higher than its neighboring positions. At position 1, the yield is 2, which is not strictly higher than its neighboring positions. At position 2, the yield is 3, which is not strictly higher than its neighboring positions. At position 3, the yield is 4, which is not strictly higher than its neighboring positions. At position 4, the yield is 5, which is not strictly higher than its neighboring positions. At position 5, the yield is 6, but it is not strictly higher than its left neighboring position (5). Therefore, we return -1.

Example 2
Input
[1, 2, 3, 4, 5, 3]
Output
-1

Explanation: Step-by-step: We iterate through the array from left to right. At position 0, the yield is 1, which is not strictly higher than its neighboring positions. At position 1, the yield is 2, which is not strictly higher than its neighboring positions. At position 2, the yield is 3, which is not strictly higher than its neighboring positions. At position 3, the yield is 4, which is not strictly higher than its neighboring positions. At position 4, the yield is 5, which is strictly higher than its neighboring positions. However, we need to check if it is strictly higher than its left neighboring position (4). Since it is not, we continue to the next position. At position 5, the yield is 3, which is not strictly higher than its neighboring positions. Therefore, we return -1.

Constraints

  • The length of the input array will be in the range [3, 1000].
  • Each element in the array will be an integer in the range [1, 1000].
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Peak Index in Array — Problem Statement & Solution Guide

ArraysMediumFinding the maximum/minimum index with specific conditions
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers yields where yields[i] represents the yield at position i. Find the index of the first position that is considered optimal for harvesting. A position is considered optimal if its yield is strictly higher than its neighboring positions. If no such position exists, return -1.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6]

Output

-1

Explanation: Step-by-step: We iterate through the array from left to right. At position 0, the yield is 1, which is not strictly higher than its neighboring positions. At position 1, the yield is 2, which is not strictly higher than its neighboring positions. At position 2, the yield is 3, which is not strictly higher than its neighboring positions. At position 3, the yield is 4, which is not strictly higher than its neighboring positions. At position 4, the yield is 5, which is not strictly higher than its neighboring positions. At position 5, the yield is 6, but it is not strictly higher than its left neighboring position (5). Therefore, we return -1.

Example 2

Input

[1, 2, 3, 4, 5, 3]

Output

-1

Explanation: Step-by-step: We iterate through the array from left to right. At position 0, the yield is 1, which is not strictly higher than its neighboring positions. At position 1, the yield is 2, which is not strictly higher than its neighboring positions. At position 2, the yield is 3, which is not strictly higher than its neighboring positions. At position 3, the yield is 4, which is not strictly higher than its neighboring positions. At position 4, the yield is 5, which is strictly higher than its neighboring positions. However, we need to check if it is strictly higher than its left neighboring position (4). Since it is not, we continue to the next position. At position 5, the yield is 3, which is not strictly higher than its neighboring positions. Therefore, we return -1.

Constraints

  • The length of the input array will be in the range [3, 1000].
  • Each element in the array will be an integer in the range [1, 1000].

Optimal Approach & Strategy

An optimized approach would involve iterating through the array and keeping track of the maximum yield and its index, while also considering the conditions for an optimal position, resulting in a time complexity of O(n). This approach is more efficient and scalable for large inputs.

Brute Force Approach

A brute-force approach would involve iterating through the array and for each element, comparing it with its neighbors and checking if it's the highest yield among all positions with the same yield, resulting in a time complexity of O(n²). This approach is inefficient and not scalable for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function findPeakIndex(yields) { if (yields.length === 0) return -1; if (yields.length === 1) return 0; for (let i = 1; i < yields.length - 1; i++) { if (yields[i] > yields[i - 1] && yields[i] > yields[i + 1]) return i; } if (yields[0] > yields[1] && yields[0] > yields[yields.length - 1]) return 0; if (yields[yields.length - 1] > yields[yields.length - 2] && yields[yields.length - 1] > yields[0]) return yields.length - 1; return -1; }

Asked in Top Tech Interviews

AmazonAtlassian

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.