Find All Good Indices

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.

Example 1
Inputnums = [2,1,1,1,3,4,1], k = 2
Output[2,3]
There are two good indices: index 2 because [2,1] is non-increasing and [1,3] is non-decreasing, and index 3 because [1,1] is non-increasing and [3,4] is non-decreasing.
Example 2
Inputnums = [2,1,1,2], k = 2
Output[]
There are no good indices in this array.

Constraints

  • n == nums.length
  • 3 <= n <= 10^5
  • 1 <= nums[i] <= 10^6
  • 1 <= k <= n / 2

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate