Find All Good Indices - Problem
You are given a 0-indexed integer array nums of size n and a positive integer k.
We call an index i in the range k ≤ i < n - k good if the following conditions are satisfied:
- The
kelements that are just before the indexiare in non-increasing order. - The
kelements that are just after the indexiare in non-decreasing order.
Return an array of all good indices sorted in increasing order.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,1,1,1,3,4,1], k = 2
›
Output:
[2,3]
💡 Note:
For index 2: elements [2,1] before are non-increasing, elements [1,3] after are non-decreasing. For index 3: elements [1,1] before are non-increasing, elements [3,4] after are non-decreasing.
Example 2 — No Good Indices
$
Input:
nums = [1,1,1,1], k = 2
›
Output:
[]
💡 Note:
No valid indices exist since we need at least k elements before and after any potential good index, but array length is only 4.
Example 3 — Minimum Valid Case
$
Input:
nums = [3,2,1,2,3], k = 1
›
Output:
[1,2,3]
💡 Note:
Index 1: [3] before non-increasing, [1] after non-decreasing. Index 2: [2] before non-increasing, [2] after non-decreasing. Index 3: [1] before non-increasing, [3] after non-decreasing.
Constraints
- n == nums.length
- 3 ≤ n ≤ 105
- 1 ≤ nums[i] ≤ 106
- 1 ≤ k ≤ n / 2
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array with elements and target k value
2
Check Conditions
Verify k elements before non-increasing, k after non-decreasing
3
Good Indices
Return all indices satisfying both conditions
Key Takeaway
🎯 Key Insight: Pre-compute sequence properties to avoid redundant validation checks
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code