We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Apply Operations to Make All Array Elements Equal to Zero

Number: 2878

Difficulty: Medium

Paid? No

Companies: Amazon


Problem Description

Given a 0-indexed integer array nums and a positive integer k, you are allowed to choose any subarray of size k and decrease all its elements by 1. The task is to determine whether it is possible to make all array elements equal to 0 by applying this operation any number of times.


Key Insights

  • The main idea is to simulate the decrement operations in a greedy way from left to right.
  • Instead of decrementing k elements multiple times and incurring repetitive work, use a difference array (or lazy propagation) technique to track the cumulative operations.
  • By maintaining a running sum of applied operations, the effective value at any index can be computed quickly.
  • If at any index the effective value is greater than 0 and there is insufficient room to perform a complete operation (i.e. i+k exceeds array length), then it’s impossible to reduce all elements to zero.

Space and Time Complexity

Time Complexity: O(n) - The algorithm processes each element of the array once. Space Complexity: O(n) - An extra array (or list) is used to hold the difference data for lazy propagation.


Solution

We use a sliding window combined with a difference array to simulate the operations efficiently. As we iterate through the nums array:

  1. Maintain a running sum that represents the cumulative decrease applied to the current element.
  2. Compute the adjusted value at the current index by subtracting the running sum from the original value.
  3. If the adjusted value is positive, it represents the number of additional full operations needed starting at that index.
  4. If there is not enough room to perform an operation (i.e. i+k > n), then return false.
  5. Otherwise, update the running sum and mark the end of the influence of the operation in the difference array.
  6. If the entire array is processed without issues, return true.

Code Solutions

# Python solution using a difference array approach

def canMakeAllZeros(nums, k):
    n = len(nums)
    # Create an array to track the effect of operations; size n+1 to avoid extra bounds checking
    ops = [0] * (n + 1)
    current_ops = 0  # running cumulative operations effect

    # Iterate over each index in the array
    for i in range(n):
        current_ops += ops[i]  # add any pending operation effect
        # Determine the remaining amount needed at index i after previous operations
        remaining = nums[i] - current_ops
        if remaining < 0:
            # More operations have been applied than needed; this should not happen.
            return False
        # If remaining is positive, we need to apply additional operations
        if remaining > 0:
            if i + k > n:
                # If not enough elements remain to form a subarray of size k, it's impossible
                return False
            current_ops += remaining  # update running sum with new operations
            ops[i + k] -= remaining   # mark the end of influence of the new operations
    return True

# Example usage:
print(canMakeAllZeros([2, 2, 3, 1, 1, 0], 3))  # Expected output: True
print(canMakeAllZeros([1, 3, 1, 1], 2))         # Expected output: False
← Back to All Questions