Skip to main content

Find the Power of K-Size Subarrays I - Solution & Explanation

MediumArraySliding Window17 min readAsked at: Meta, Google, Bloomberg
Practice this problem

Problem Statement

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:

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

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.

Example 2:

Input: nums = [2,2,2,2,2], k = 4

Output: [-1,-1]

Example 3:

Input: nums = [3,2,3,2,3,2], k = 2

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

 

Constraints:

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

Approach Overview

Problem Overview: You are given an integer array and a window size k. For every contiguous subarray of length k, determine whether the elements form a strictly increasing consecutive sequence where nums[i] + 1 = nums[i+1]. If the condition holds, the subarray's power is the maximum element (the last element of that window). Otherwise, the power is -1. The result is an array containing the power of each k-length window.

Approach 1: Brute Force Checking of Subarrays (Time: O(n*k), Space: O(1))

The straightforward solution checks every subarray of size k. For each starting index, iterate through the next k-1 elements and verify the consecutive condition nums[j] + 1 == nums[j+1]. If any pair breaks the rule, the current window is invalid and its power becomes -1. If all comparisons succeed, return the last element of that window as the power. This method directly follows the problem definition and is useful for validating logic during implementation. However, because each window requires up to k comparisons, the total runtime grows to O(n*k) for an array of size n. The approach uses constant extra space since it only tracks indices and comparisons.

Approach 2: Optimized Sliding Window Technique (Time: O(n), Space: O(1))

The optimized solution observes that consecutive windows overlap heavily. Instead of rechecking all k elements each time, track the length of the current streak where adjacent numbers increase by exactly one. Iterate through the array once and update a counter whenever nums[i] == nums[i-1] + 1. If the condition breaks, reset the streak length. For a window ending at index i, the subarray of size k is valid if the streak length is at least k. When valid, record nums[i] as the power; otherwise record -1. This effectively converts repeated window validation into a single pass over the array. The algorithm runs in linear time O(n) with constant space.

This pattern is common in problems involving contiguous segments and overlapping windows. Understanding how to reuse information from the previous window is the key optimization in sliding window techniques. Since the data is processed sequentially, the solution relies only on simple comparisons and counters over the array.

Recommended for interviews: Start by describing the brute force approach to show you understand the requirement for validating each window. Then transition to the sliding window optimization that tracks consecutive increments in a single pass. Interviewers typically expect the O(n) sliding window solution because it demonstrates pattern recognition and efficient handling of overlapping subarrays.

Approach 1: Brute Force Checking of Subarrays

We can solve this problem by checking all possible subarrays of size k explicitly. For each subarray, we check if it is sorted and if the elements are consecutive. If both conditions are met, we calculate the power as the maximum element of the subarray. Otherwise, the power is -1.

This C implementation uses a function to check whether each subarray of size k is sorted and the elements are consecutive. If both conditions are satisfied, the function calculates the power as the maximum element of the subarray.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * k) because we process each element of each subarray of size k.
Space Complexity: O(n-k+1) for storing the results array.

Try this approach in the editor →

Approach 2: Optimized Sliding Window Technique

This approach employs a sliding window technique to process each subarray of size k efficiently. We slide over the array and check whether each segment meets the criteria of being both sorted and consecutive. This reduces unnecessary re-checks by leveraging overlapping subarray properties.

This C implementation uses a function to verify both order and consecutiveness of elements in a k-length sliding window. The maximum element is calculated if the conditions are met.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n * k), reduced by potentially not rechecking unchanged segments.
Space Complexity: O(n-k+1) for the results array.

Try this approach in the editor →

Approach 3: Recursion

We define an array f, where f[i] represents the length of the continuous increasing subsequence ending at the i-th element. Initially, f[i] = 1.

Next, we traverse the array nums to calculate the values of the array f. If nums[i] = nums[i - 1] + 1, then f[i] = f[i - 1] + 1; otherwise, f[i] = 1.

Then, we traverse the array f in the range [k - 1, n). If f[i] \ge k, we add nums[i] to the answer array; otherwise, we add -1.

After the traversal, we return the answer array.

The time complexity is O(n), and the space complexity is O(n). Here, n represents the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 4: Default Approach

Code

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Checking of Subarrays

Time Complexity: O(n * k) because we process each element of each subarray of size k.
Space Complexity: O(n-k+1) for storing the results array.

Optimized Sliding Window Technique

Time Complexity: O(n * k), reduced by potentially not rechecking unchanged segments.
Space Complexity: O(n-k+1) for the results array.

Recursion—
Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Checking of SubarraysO(n*k)O(1)Good for understanding the problem or when k is very small
Sliding Window with Consecutive Streak TrackingO(n)O(1)Preferred for large arrays and interview settings where optimal performance is required

Video Solution

Find the Power of K-Size Subarrays I - Leetcode 3254 - Python • NeetCodeIO • 9,363 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find the Power of K-Size Subarrays I easy or hard?
Find the Power of K-Size Subarrays I is typically rated Medium difficulty. The brute force logic is straightforward, but recognizing that overlapping windows allow a linear-time sliding window optimization is the key step that raises the difficulty.
Find the Power of K-Size Subarrays I Python/Java solution
Python, Java, C++, and similar languages implement the same idea: iterate once through the array and maintain a counter for consecutive increments. When the streak length reaches k, record the last element as the power; otherwise store -1. The logic remains identical across languages with O(n) time complexity.
How to solve Find the Power of K-Size Subarrays I in O(n)?
Traverse the array while maintaining a counter for consecutive elements where nums[i] == nums[i-1] + 1. If the condition holds, increment the streak; otherwise reset it. Whenever the streak length is at least k, the subarray ending at index i forms a valid window and its power is nums[i]; otherwise record -1.
What is the best approach for Find the Power of K-Size Subarrays I?
The optimal approach uses a sliding window with a counter that tracks how long the current consecutive increasing streak lasts. When the streak length reaches at least k, the current window is valid and its power equals the last element. This reduces the runtime to O(n) while using O(1) extra space.
Is Find the Power of K-Size Subarrays I asked at Google/Amazon/Meta?
Problems involving sliding windows and consecutive sequence validation appear frequently in interviews at companies like Amazon, Google, and Meta. Variations often test your ability to reuse computations across overlapping subarrays to achieve O(n) performance.
What data structure is used in Find the Power of K-Size Subarrays I?
The solution primarily uses arrays and a sliding window traversal pattern. No additional complex data structures are required; a simple counter or index tracking consecutive increments is enough to compute valid windows efficiently.
What is the time complexity of Find the Power of K-Size Subarrays I?
The brute force method runs in O(n*k) time because each k-length window is checked independently. The optimized sliding window approach processes the array once and tracks consecutive increments, reducing the time complexity to O(n) with constant space.

Ready to solve this problem?

Practice Find the Power of K-Size Subarrays I with our built-in code editor and test cases.

Practice on FleetCode