#2420

Find All Good Indices

expert · 1000 · lc medium +32 · verified · 40.7% accepted · 673 likes · top 20%

Description

You are given a 0-indexed integer array nums of size n and a positive integer k.

An index i in range k <= i < n - k is good when:

- The k elements directly before index i are in non-increasing order.

- The k elements directly after index i are in non-decreasing order.

Return an array of all good indices in ascending 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.

Code

1
2
3