Find All Good Indices - Problem

Imagine you're analyzing a financial time series where you need to find stable pivot points - moments where the trend was declining before and rising after.

Given a 0-indexed integer array nums of size n and a positive integer k, your task is to find all "good indices" that represent these stable points.

An index i is considered good if k โ‰ค i < n - k and both conditions are met:

  • The k elements immediately before index i are in non-increasing order (declining or stable)
  • The k elements immediately after index i are in non-decreasing order (rising or stable)

Goal: Return an array of all good indices sorted in increasing order.

Example: For nums = [2,1,1,1,3,4,1], k = 2, index 3 is good because the 2 elements before it [1,1] are non-increasing and the 2 elements after it [3,4] are non-decreasing.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,1,1,1,3,4,1], k = 2
โ€บ Output: [3]
๐Ÿ’ก Note: Index 3 is the only good index. The 2 elements before it [1,1] are non-increasing, and the 2 elements after it [3,4] are non-decreasing.
example_2.py โ€” Multiple Good Indices
$ Input: nums = [2,1,1,2], k = 2
โ€บ Output: []
๐Ÿ’ก Note: No valid indices exist. The only potential candidate would be at index 2, but we need k=2 elements before and after, which requires array length โ‰ฅ 2*2+1 = 5.
example_3.py โ€” Edge Case Small k
$ Input: nums = [1,1,1,1,1], k = 1
โ€บ Output: [1,2,3]
๐Ÿ’ก Note: Indices 1, 2, and 3 are all good. For each: 1 element before is non-increasing (โ‰ค), and 1 element after is non-decreasing (โ‰ฅ).

Visualization

Tap to expand
Finding Good Indices: Valley DetectionGood Index!k=2 descentk=2 ascentAlgorithm Steps1. Track Descents: Count consecutive non-increasing elements ending at each position2. Track Ascents: Count consecutive non-decreasing elements starting at each position3. Find Valleys: Identify positions where both counts โ‰ฅ k
Understanding the Visualization
1
Identify Pattern
Look for valley-like patterns where descent transitions to ascent
2
Precompute Descents
For each position, track how long the descent has been continuing
3
Precompute Ascents
For each position, track how long the ascent will continue
4
Find Intersections
Good indices are where both descent and ascent lengths meet the k requirement
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking k elements repeatedly, precompute sequence lengths to find valley patterns efficiently in linear time

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Three linear passes through the array

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Two additional arrays to store precomputed lengths

n
2n
โšก Linearithmic Space

Constraints

  • n == nums.length
  • 3 โ‰ค n โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค k โ‰ค n / 2
  • Valid range: k โ‰ค i < n - k must be non-empty
Asked in
Google 45 Amazon 38 Meta 22 Microsoft 31
89.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen