BackmediumArrays Accenture

Galaxy Navigator Solution

Problem Statement

Given the sizes of the asteroids in the belt, find the index of the largest asteroid if the pattern can be restored by removing at most one asteroid.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we calculate the maximum increasing subsequence ending at each index. The maximum increasing subsequence ending at index 4 is 1, 2, 3, 4, 5. We then remove index 4 and calculate the maximum increasing subsequence of the subsequence without index 4, which is 1, 2, 3, 4, 1, 2, 3. Since the maximum increasing subsequence ending at index 4 is not equal to the maximum increasing subsequence of the subsequence without index 4, we return -1.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 1, 2, 3], we calculate the maximum increasing subsequence ending at each index. The maximum increasing subsequence ending at each index is the same. We then remove each index and calculate the maximum increasing subsequence of the subsequence without that index. Since the maximum increasing subsequence ending at each index is the same, we return -1.

Constraints

  • 1 <= array length <= 10^5
  • Each element in the array is a positive integer.
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