#3255
Medium Algorithms Find the power of k size subarrays ii
Array Sliding Window
31.4% acceptance
Feb 25, 2026
160
12
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 elements are consecutive and sorted ascending, -1 otherwise.
Return an integer array results of size n - k + 1.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k as usize;
let mut streak = vec![0usize; n];
for i in 1..n {
if nums[i] == nums[i - 1] + 1 {
streak[i] = streak[i - 1] + 1;
}
}
(0..=(n - k))
.map(|l| if streak[l + k - 1] >= k - 1 { nums[l + k - 1] } else { -1 })
.collect()
}
}