Find the Power of K-Size Subarrays I

Medium Topics Company Tags

For every contiguous subarray of length k, check whether each element after the first is exactly one larger than its predecessor. If so, its power is its last (and largest) element. Otherwise, its power is -1.

Return the powers in order of increasing starting index. The result contains nums.length - k + 1 elements.

Example 1:

Input: nums = [4,5,6,2,3]
k = 2

Output: [5,6,-1,3]

Explanation: The windows [4,5], [5,6], and [2,3] increase by one at each step, giving powers 5, 6, and 3. The window [6,2] fails that condition, so its power is -1.

Example 2:

Input: nums = [8,8,9]
k = 3

Output: [-1]

Explanation: The only window is [8,8,9]. Its repeated 8 means it is not a run of consecutive increasing integers, so its power is -1.

Example 3:

Input: nums = [5,2,7]
k = 1

Output: [5,2,7]

Explanation: Each window contains one element and satisfies the condition automatically. Its power equals that element, yielding [5,2,7].

Constraints:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 100000
  • 1 <= k <= nums.length


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.