Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
k elements that are just before the index i are in non-increasing order.k elements that are just after the index i are in non-decreasing order.Return an array of all good indices sorted in increasing order.
Example 1:
Input: nums = [2,1,1,1,3,4,1], k = 2 Output: [2,3] Explanation: There are two good indices in the array: - Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order. - Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order. Note that the index 4 is not good because [4,1] is not non-decreasing.
Example 2:
Input: nums = [2,1,1,2], k = 2 Output: [] Explanation: There are no good indices in this array.
Constraints:
n == nums.length3 <= n <= 1051 <= nums[i] <= 1061 <= k <= n / 2Problem summary: 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 k elements that are just before the index i are in non-increasing order. The k elements that are just after the index i are in non-decreasing order. Return an array of all good indices sorted in increasing order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,1,1,1,3,4,1] 2
[2,1,1,2] 2
find-good-days-to-rob-the-bank)abbreviating-the-product-of-a-range)count-the-number-of-k-big-indices)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2420: Find All Good Indices
class Solution {
public List<Integer> goodIndices(int[] nums, int k) {
int n = nums.length;
int[] decr = new int[n];
int[] incr = new int[n];
Arrays.fill(decr, 1);
Arrays.fill(incr, 1);
for (int i = 2; i < n - 1; ++i) {
if (nums[i - 1] <= nums[i - 2]) {
decr[i] = decr[i - 1] + 1;
}
}
for (int i = n - 3; i >= 0; --i) {
if (nums[i + 1] <= nums[i + 2]) {
incr[i] = incr[i + 1] + 1;
}
}
List<Integer> ans = new ArrayList<>();
for (int i = k; i < n - k; ++i) {
if (decr[i] >= k && incr[i] >= k) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2420: Find All Good Indices
func goodIndices(nums []int, k int) []int {
n := len(nums)
decr := make([]int, n)
incr := make([]int, n)
for i := range decr {
decr[i] = 1
incr[i] = 1
}
for i := 2; i < n; i++ {
if nums[i-1] <= nums[i-2] {
decr[i] = decr[i-1] + 1
}
}
for i := n - 3; i >= 0; i-- {
if nums[i+1] <= nums[i+2] {
incr[i] = incr[i+1] + 1
}
}
ans := []int{}
for i := k; i < n-k; i++ {
if decr[i] >= k && incr[i] >= k {
ans = append(ans, i)
}
}
return ans
}
# Accepted solution for LeetCode #2420: Find All Good Indices
class Solution:
def goodIndices(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
decr = [1] * (n + 1)
incr = [1] * (n + 1)
for i in range(2, n - 1):
if nums[i - 1] <= nums[i - 2]:
decr[i] = decr[i - 1] + 1
for i in range(n - 3, -1, -1):
if nums[i + 1] <= nums[i + 2]:
incr[i] = incr[i + 1] + 1
return [i for i in range(k, n - k) if decr[i] >= k and incr[i] >= k]
// Accepted solution for LeetCode #2420: Find All Good Indices
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2420: Find All Good Indices
// class Solution {
// public List<Integer> goodIndices(int[] nums, int k) {
// int n = nums.length;
// int[] decr = new int[n];
// int[] incr = new int[n];
// Arrays.fill(decr, 1);
// Arrays.fill(incr, 1);
// for (int i = 2; i < n - 1; ++i) {
// if (nums[i - 1] <= nums[i - 2]) {
// decr[i] = decr[i - 1] + 1;
// }
// }
// for (int i = n - 3; i >= 0; --i) {
// if (nums[i + 1] <= nums[i + 2]) {
// incr[i] = incr[i + 1] + 1;
// }
// }
// List<Integer> ans = new ArrayList<>();
// for (int i = k; i < n - k; ++i) {
// if (decr[i] >= k && incr[i] >= k) {
// ans.add(i);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2420: Find All Good Indices
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2420: Find All Good Indices
// class Solution {
// public List<Integer> goodIndices(int[] nums, int k) {
// int n = nums.length;
// int[] decr = new int[n];
// int[] incr = new int[n];
// Arrays.fill(decr, 1);
// Arrays.fill(incr, 1);
// for (int i = 2; i < n - 1; ++i) {
// if (nums[i - 1] <= nums[i - 2]) {
// decr[i] = decr[i - 1] + 1;
// }
// }
// for (int i = n - 3; i >= 0; --i) {
// if (nums[i + 1] <= nums[i + 2]) {
// incr[i] = incr[i + 1] + 1;
// }
// }
// List<Integer> ans = new ArrayList<>();
// for (int i = k; i < n - k; ++i) {
// if (decr[i] >= k && incr[i] >= k) {
// ans.add(i);
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.