Mid/SeniorArrayPrefix Sum

Apply Operations to Make All Array Elements Equal to Zero

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.

Example 1
Inputnums = [2,2,3,1,1,0], k = 3
Outputtrue
By repeatedly decreasing valid subarrays of size 3, the array can be transformed into all zeros.
Example 2
Inputnums = [1,3,1,1], k = 2
Outputfalse
It is not possible to make all the array elements equal to 0.

Constraints

  • 1 <= k <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^6

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate