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.
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.
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.
Master coding challenges related to Arrays and solve the [Backup] Optimal Harvest Index problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak