Find the Power of K-Size Subarrays I

You are given an array of integers nums of length n and a positive integer k.

The power of an array is defined as:

  • Its maximum element if all of its elements are consecutive and sorted in ascending order.
  • -1 otherwise.

You need to find the power of all subarrays of nums of size k.

Return an integer array results of size n - k + 1, where results[i] is the power of nums[i..(i + k - 1)].

Example 1
Inputnums = [1,2,3,4,3,2,5], k = 3
Output[3,4,-1,-1,-1]
The valid consecutive ascending subarrays of size 3 are [1, 2, 3] and [2, 3, 4], so their powers are 3 and 4, while the remaining subarrays have power -1.
Example 2
Inputnums = [2,2,2,2,2], k = 4
Output[-1,-1]
Each subarray of size 4 contains equal elements, so none are consecutive ascending subarrays.

Constraints

  • 1 <= n == nums.length <= 500
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= n

Asked at 3 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