LeetCode solutions

1776. Minimum Operations to Reduce X to Zero

My accepted Python solution to LeetCode problem 1776, Minimum Operations to Reduce X to Zero, running in 57ms.

  • Difficulty: Medium
  • Python
  • Runtime 57ms
  • Memory 28.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 57ms, memory 28.4MB, accepted 2025-12-31.

python
class Solution:
    def minOperations(self, nums: List[int], x: int) -> int:
        # Equivalent to finding longest subarray with sum = total - x
        target = sum(nums) - x
        
        if target < 0:
            return -1
        if target == 0:
            return len(nums)
        
        # Sliding window to find longest subarray with sum = target
        n = len(nums)
        max_len = -1
        current_sum = 0
        left = 0
        
        for right in range(n):
            current_sum += nums[right]
            
            while current_sum > target and left <= right:
                current_sum -= nums[left]
                left += 1
            
            if current_sum == target:
                max_len = max(max_len, right - left + 1)
        
        return n - max_len if max_len != -1 else -1

Source