BackmediumArrays Oracle

Optimal Harvest Index Solution

Problem Statement

In a farm with varying crop yields, a position is considered optimal for harvesting if its yield is strictly higher than its neighboring positions. However, if a position's yield is equal to its neighbors, it is still considered optimal if its yield is the highest among all positions with the same yield. Find the index of the first optimal position for harvesting in the given array of crop yields.

Example 1
Input
[1, 2, 3, 2, 1]
Output
0

Explanation: Step-by-step: with input [1, 2, 3, 2, 1], we first check if the first element is strictly higher than its neighbors. Since 1 is not strictly higher than 2, we then check if the first element is the highest among all positions with the same yield. Since 1 is the highest among all positions with the same yield, we return 0.

Example 2
Input
[1, 1, 1, 1, 1]
Output
0

Explanation: Step-by-step: with input [1, 1, 1, 1, 1], we first check if the first element is strictly higher than its neighbors. Since 1 is not strictly higher than 1, we then check if the first element is the highest among all positions with the same yield. Since 1 is the highest among all positions with the same yield, we return 0.

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 Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free