Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array of integers nums of length n and a positive integer k.
The power of an array is defined as:
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 <= 5001 <= nums[i] <= 1051 <= k <= nProblem summary: 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)].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Sliding Window
[1,2,3,4,3,2,5] 3
[2,2,2,2,2] 4
[3,2,3,2,3,2] 2
maximum-sum-of-distinct-subarrays-with-length-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3254: Find the Power of K-Size Subarrays I
class Solution {
public int[] resultsArray(int[] nums, int k) {
int n = nums.length;
int[] f = new int[n];
Arrays.fill(f, 1);
for (int i = 1; i < n; ++i) {
if (nums[i] == nums[i - 1] + 1) {
f[i] = f[i - 1] + 1;
}
}
int[] ans = new int[n - k + 1];
for (int i = k - 1; i < n; ++i) {
ans[i - k + 1] = f[i] >= k ? nums[i] : -1;
}
return ans;
}
}
// Accepted solution for LeetCode #3254: Find the Power of K-Size Subarrays I
func resultsArray(nums []int, k int) (ans []int) {
n := len(nums)
f := make([]int, n)
f[0] = 1
for i := 1; i < n; i++ {
if nums[i] == nums[i-1]+1 {
f[i] = f[i-1] + 1
} else {
f[i] = 1
}
}
for i := k - 1; i < n; i++ {
if f[i] >= k {
ans = append(ans, nums[i])
} else {
ans = append(ans, -1)
}
}
return
}
# Accepted solution for LeetCode #3254: Find the Power of K-Size Subarrays I
class Solution:
def resultsArray(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
f = [1] * n
for i in range(1, n):
if nums[i] == nums[i - 1] + 1:
f[i] = f[i - 1] + 1
return [nums[i] if f[i] >= k else -1 for i in range(k - 1, n)]
// Accepted solution for LeetCode #3254: Find the Power of K-Size Subarrays I
/**
* [3254] Find the Power of K-Size Subarrays I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
let k = k as usize;
let n = nums.len();
let mut count = 0;
for i in 0..(k - 1) {
if nums[i] + 1 == nums[i + 1] {
count += 1;
}
}
let mut result = Vec::with_capacity(n - k + 1);
result.push(if count == k - 1 { nums[k - 1] } else { -1 });
for i in k..n {
if nums[i - 1] + 1 == nums[i] {
count += 1;
}
if nums[i - k] + 1 == nums[i - k + 1] {
count -= 1;
}
result.push(if count == k - 1 { nums[i] } else { -1 });
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3254() {
assert_eq!(vec![1, 2, 3], Solution::results_array(vec![1, 2, 3], 1));
assert_eq!(
vec![3, 4, -1, -1, -1],
Solution::results_array(vec![1, 2, 3, 4, 3, 2, 5], 3)
);
assert_eq!(
vec![-1, -1],
Solution::results_array(vec![2, 2, 2, 2, 2], 4)
);
assert_eq!(
vec![-1, 3, -1, 3, -1],
Solution::results_array(vec![3, 2, 3, 2, 3, 2], 2)
);
}
}
// Accepted solution for LeetCode #3254: Find the Power of K-Size Subarrays I
function resultsArray(nums: number[], k: number): number[] {
const n = nums.length;
const f: number[] = Array(n).fill(1);
for (let i = 1; i < n; ++i) {
if (nums[i] === nums[i - 1] + 1) {
f[i] = f[i - 1] + 1;
}
}
const ans: number[] = [];
for (let i = k - 1; i < n; ++i) {
ans.push(f[i] >= k ? nums[i] : -1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.