3254. Find the Power of K-Size Subarrays I LeetCode Solution
In this guide, you will get 3254. Find the Power of K-Size Subarrays I LeetCode Solution with the best time and space complexity. The solution to Find the Power of K-Size Subarrays I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Power of K-Size Subarrays I solution in C++
- Find the Power of K-Size Subarrays I solution in Java
- Find the Power of K-Size Subarrays I solution in Python
- Additional Resources

Problem Statement of 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:
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3254. Find the Power of K-Size Subarrays I LeetCode Solution in C++
class Solution {
public:
vector<int> resultsArray(vector<int>& nums, int k) {
vector<int> ans;
int start = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i > 0 && nums[i] != nums[i - 1] + 1)
start = i;
if (i >= k - 1)
ans.push_back(i - start + 1 >= k ? nums[i] : -1);
}
return ans;
}
};
/* code provided by PROGIEZ */
3254. Find the Power of K-Size Subarrays I LeetCode Solution in Java
class Solution {
public int[] resultsArray(int[] nums, int k) {
final int n = nums.length;
int[] ans = new int[n - k + 1];
int start = 0;
for (int i = 0; i < n; ++i) {
if (i > 0 && nums[i] != nums[i - 1] + 1)
start = i;
if (i >= k - 1)
ans[i - k + 1] = i - start + 1 >= k ? nums[i] : -1;
}
return ans;
}
}
// code provided by PROGIEZ
3254. Find the Power of K-Size Subarrays I LeetCode Solution in Python
class Solution:
def resultsArray(self, nums: list[int], k: int) -> list[int]:
ans = []
start = 0
for i, num in enumerate(nums):
if i > 0 and num != nums[i - 1] + 1:
start = i
if i >= k - 1:
ans.append(num if i - start + 1 >= k else -1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.