LeetCode solutions

3616. Make Array Elements Equal to Zero

My accepted Python solution to LeetCode problem 3616, Make Array Elements Equal to Zero, running in 3658ms.

  • Difficulty: Easy
  • Python
  • Runtime 3658ms
  • Memory 17.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3658ms, memory 17.2MB, accepted 2025-12-31.

python
class Solution:
    def countValidSelections(self, nums: List[int]) -> int:
        n = len(nums)
        count = 0
        
        for start in range(n):
            if nums[start] != 0:
                continue
            
            # Try both directions
            for direction in [-1, 1]:  # -1 = left, 1 = right
                arr = nums[:]  # Copy
                curr = start
                d = direction
                
                while 0 <= curr < n:
                    if arr[curr] == 0:
                        curr += d  # Move in current direction
                    else:
                        arr[curr] -= 1
                        d = -d  # Reverse direction
                        curr += d  # Step in new direction
                
                # Check if all zeros
                if all(x == 0 for x in arr):
                    count += 1
        
        return count

Source