BackmediumArrays Atlassian

Galaxy Navigation Peak Solution

Problem Statement

Given a distorted galaxy map represented by an array of celestial body masses, find the index of the first occurrence of the peak element, which is the maximum element in the array.

Example 1
Input
[23, 12, 45, 89, 23, 45, 12, 23, 45]
Output
0

Explanation: Step-by-step: with input [23, 12, 45, 89, 23, 45, 12, 23, 45], we find the maximum element 89 at index 3, but since it appears multiple times, we need to find the first occurrence of the maximum element, which is 23 at index 0.

Example 2
Input
[42, 12, 45, 89, 42, 45, 12, 42, 45]
Output
2

Explanation: Step-by-step: with input [42, 12, 45, 89, 42, 45, 12, 42, 45], we find the maximum element 89 at index 3, but since it appears multiple times, we need to find the first occurrence of the maximum element, which is 42 at index 0, but since 42 appears multiple times, we need to find the first occurrence of the maximum element, which is 42 at index 2.

Constraints

  • 1 <= array length <= 10^5
  • All elements in the array are unique and within the range of 1 to 10^9
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