> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Power of K-Size Subarrays I

> Tested Python solution for LeetCode 3254 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 3254, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3254   # by problem number
lcpy gen -s find_the_power_of_k_size_subarrays_i   # by problem name
```

## Problem

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)]`.

### Examples

```
Input: nums = [1,2,3,4,3,2,5]
Output: [3,4,-1,-1,-1]
Explanation:
There are 5 subarrays of nums of size 3:
- [1, 2, 3] with the maximum element 3.
- [2, 3, 4] with the maximum element 4.
- [3, 4, 3] whose elements are not consecutive.
- [4, 3, 2] whose elements are not sorted.
- [3, 2, 5] whose elements are not consecutive.
```

```
Input: nums = [2,2,2,2,2]
Output: [-1,-1]
```

```
Input: nums = [3,2,3,2,3,2]
Output: [-1,3,-1,3,-1]
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_power_of_k_size_subarrays_i/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_power_of_k_size_subarrays_i/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) extra (output excluded)
    def results_array(self, nums: list[int], k: int) -> list[int]:
        # run[i] = length of the consecutive ascending run ending at index i
        results: list[int] = []
        run = 1
        for i, num in enumerate(nums):
            if i > 0 and num == nums[i - 1] + 1:
                run += 1
            else:
                run = 1
            if i >= k - 1:
                results.append(num if run >= k else -1)
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) extra (output excluded) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.