#2772

Apply Operations to Make All Array Elements Equal to Zero

expert · 1090 · lc medium +32 · verified · 33.3% accepted · 447 likes · top 11%

Description

A 0-indexed integer array nums and a positive integer k are given. You may apply the following operation any number of times:

- Choose any contiguous subarray of size k and decrement every element by 1.

Return true if all elements can be reduced 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.

Code

1
2
3