Welcome to Subscribe On Youtube
3354. Make Array Elements Equal to Zero
Description
You are given an integer array nums
.
Start by selecting a starting position curr
such that nums[curr] == 0
, and choose a movement direction of either left or right.
After that, you repeat the following process:
- If
curr
is out of the range[0, n - 1]
, this process ends. - If
nums[curr] == 0
, move in the current direction by incrementingcurr
if you are moving right, or decrementingcurr
if you are moving left. - Else if
nums[curr] > 0
:- Decrement
nums[curr]
by 1. - Reverse your movement direction (left becomes right and vice versa).
- Take a step in your new direction.
- Decrement
A selection of the initial position curr
and movement direction is considered valid if every element in nums
becomes 0 by the end of the process.
Return the number of possible valid selections.
Example 1:
Input: nums = [1,0,2,0,3]
Output: 2
Explanation:
The only possible valid selections are the following:
- Choose
curr = 3
, and a movement direction to the left.[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0]
.
- Choose
curr = 3
, and a movement direction to the right.[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
Example 2:
Input: nums = [2,3,4,0,4,1,0]
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 100
- There is at least one element
i
wherenums[i] == 0
.
Solutions
Solution 1: Enumeration + Prefix Sum
Suppose we initially move to the left and encounter a non-zero element. In that case, we need to decrement this element by one, then change the direction of movement and continue moving.
Therefore, we can maintain the sum of elements to the left of each zero-value element as $l$, and the sum of elements to the right as $s - l$. If $l = s - l$, meaning the sum of elements on the left equals the sum of elements on the right, we can choose the current zero-value element and move either left or right, adding $2$ to the answer. If $|l - (s - l)| = 1$, and the sum of elements on the left is greater, we can choose the current zero-value element and move left, adding $1$ to the answer. If the sum of elements on the right is greater, we can choose the current zero-value element and move right, adding $1$ to the answer.
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
-
class Solution { public int countValidSelections(int[] nums) { int s = Arrays.stream(nums).sum(); int ans = 0, l = 0; for (int x : nums) { if (x != 0) { l += x; } else if (l * 2 == s) { ans += 2; } else if (Math.abs(l * 2 - s) <= 1) { ++ans; } } return ans; } }
-
class Solution { public: int countValidSelections(vector<int>& nums) { int s = accumulate(nums.begin(), nums.end(), 0); int ans = 0, l = 0; for (int x : nums) { if (x) { l += x; } else if (l * 2 == s) { ans += 2; } else if (abs(l * 2 - s) <= 1) { ++ans; } } return ans; } };
-
class Solution: def countValidSelections(self, nums: List[int]) -> int: s = sum(nums) ans = l = 0 for x in nums: if x: l += x elif l * 2 == s: ans += 2 elif abs(l * 2 - s) == 1: ans += 1 return ans
-
func countValidSelections(nums []int) (ans int) { l, s := 0, 0 for _, x := range nums { s += x } for _, x := range nums { if x != 0 { l += x } else if l*2 == s { ans += 2 } else if abs(l*2-s) <= 1 { ans++ } } return } func abs(x int) int { if x < 0 { return -x } return x }
-
function countValidSelections(nums: number[]): number { const s = nums.reduce((acc, x) => acc + x, 0); let [ans, l] = [0, 0]; for (const x of nums) { if (x) { l += x; } else if (l * 2 === s) { ans += 2; } else if (Math.abs(l * 2 - s) <= 1) { ++ans; } } return ans; }