#2772
Medium Algorithms Apply operations to make all array elements equal to zero
Array Prefix Sum
33.3% acceptance
Feb 25, 2026
447
32
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn check_array(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let k = k as usize;
// ops[i] is used as difference array for operations starting at each position
let mut ops = vec![0i64; n + 1];
let mut cur_ops = 0i64;
for i in 0..n {
cur_ops += ops[i];
let need = nums[i] as i64 - cur_ops;
if need < 0 { return false; }
if need > 0 {
if i + k > n { return false; }
cur_ops += need;
ops[i + k] -= need;
}
}
true
}
}