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 a 0-indexed integer array nums and a positive integer k.
You can apply the following operation on the array any number of times:
k from the array and decrease all its elements by 1.Return true if you can make all the array elements equal to 0, or false otherwise.
A subarray is a contiguous non-empty part of an array.
Example 1:
Input: nums = [2,2,3,1,1,0], k = 3 Output: true Explanation: We can do the following operations: - Choose the subarray [2,2,3]. The resulting array will be nums = [1,1,2,1,1,0]. - Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,1,0,0,0]. - Choose the subarray [1,1,1]. The resulting array will be nums = [0,0,0,0,0,0].
Example 2:
Input: nums = [1,3,1,1], k = 2 Output: false Explanation: It is not possible to make all the array elements equal to 0.
Constraints:
1 <= k <= nums.length <= 1050 <= nums[i] <= 106Problem summary: You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: Choose any subarray of size k from the array and decrease all its elements by 1. Return true if you can make all the array elements equal to 0, or false otherwise. A subarray is a contiguous non-empty part of an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,2,3,1,1,0] 3
[1,3,1,1] 2
continuous-subarray-sum)number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
class Solution {
public boolean checkArray(int[] nums, int k) {
int n = nums.length;
int[] d = new int[n + 1];
int s = 0;
for (int i = 0; i < n; ++i) {
s += d[i];
nums[i] += s;
if (nums[i] == 0) {
continue;
}
if (nums[i] < 0 || i + k > n) {
return false;
}
s -= nums[i];
d[i + k] += nums[i];
}
return true;
}
}
// Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
func checkArray(nums []int, k int) bool {
n := len(nums)
d := make([]int, n+1)
s := 0
for i, x := range nums {
s += d[i]
x += s
if x == 0 {
continue
}
if x < 0 || i+k > n {
return false
}
s -= x
d[i+k] += x
}
return true
}
# Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
class Solution:
def checkArray(self, nums: List[int], k: int) -> bool:
n = len(nums)
d = [0] * (n + 1)
s = 0
for i, x in enumerate(nums):
s += d[i]
x += s
if x == 0:
continue
if x < 0 or i + k > n:
return False
s -= x
d[i + k] += x
return True
// Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
// class Solution {
// public boolean checkArray(int[] nums, int k) {
// int n = nums.length;
// int[] d = new int[n + 1];
// int s = 0;
// for (int i = 0; i < n; ++i) {
// s += d[i];
// nums[i] += s;
// if (nums[i] == 0) {
// continue;
// }
// if (nums[i] < 0 || i + k > n) {
// return false;
// }
// s -= nums[i];
// d[i + k] += nums[i];
// }
// return true;
// }
// }
// Accepted solution for LeetCode #2772: Apply Operations to Make All Array Elements Equal to Zero
function checkArray(nums: number[], k: number): boolean {
const n = nums.length;
const d: number[] = Array(n + 1).fill(0);
let s = 0;
for (let i = 0; i < n; ++i) {
s += d[i];
nums[i] += s;
if (nums[i] === 0) {
continue;
}
if (nums[i] < 0 || i + k > n) {
return false;
}
s -= nums[i];
d[i + k] += nums[i];
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.