3354. Make Array Elements Equal to Zero LeetCode Solution
In this guide, you will get 3354. Make Array Elements Equal to Zero LeetCode Solution with the best time and space complexity. The solution to Make Array Elements Equal to Zero problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Make Array Elements Equal to Zero solution in C++
- Make Array Elements Equal to Zero solution in Java
- Make Array Elements Equal to Zero solution in Python
- Additional Resources
Problem Statement of Make Array Elements Equal to Zero
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 incrementing curr if you are moving right, or decrementing curr 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.
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 where nums[i] == 0.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3354. Make Array Elements Equal to Zero LeetCode Solution in C++
class Solution {
public:
int countValidSelections(vector<int>& nums) {
const int n = nums.size();
int ans = 0;
vector<int> prefix(n); // sum(nums[0..i - 1])
vector<int> suffix(n); // sum(nums[i + 1..n - 1])
for (int i = 1; i < n; ++i)
prefix[i] = prefix[i - 1] + nums[i - 1];
for (int i = n - 2; i >= 0; --i)
suffix[i] = suffix[i + 1] + nums[i + 1];
for (int i = 0; i < n; ++i) {
if (nums[i] > 0)
continue;
if (prefix[i] == suffix[i])
ans += 2; // Go to either direction.
if (abs(prefix[i] - suffix[i]) == 1)
ans += 1; // Go to the direction with the larger sum.
}
return ans;
}
};
/* code provided by PROGIEZ */
3354. Make Array Elements Equal to Zero LeetCode Solution in Java
class Solution {
public int countValidSelections(int[] nums) {
final int n = nums.length;
int ans = 0;
int[] prefix = new int[n]; // sum(nums[0..i - 1])
int[] suffix = new int[n]; // sum(nums[i + 1..n - 1])
for (int i = 1; i < n; ++i)
prefix[i] = prefix[i - 1] + nums[i - 1];
for (int i = n - 2; i >= 0; --i)
suffix[i] = suffix[i + 1] + nums[i + 1];
for (int i = 0; i < n; ++i) {
if (nums[i] > 0)
continue;
if (prefix[i] == suffix[i])
ans += 2; // Go to either direction.
if (Math.abs(prefix[i] - suffix[i]) == 1)
ans += 1; // Go to the direction with the larger sum.
}
return ans;
}
}
// code provided by PROGIEZ
3354. Make Array Elements Equal to Zero LeetCode Solution in Python
class Solution:
def countValidSelections(self, nums: list[int]) -> int:
ans = 0
prefix = list(itertools.accumulate(nums))
suffix = list(itertools.accumulate(nums[::-1]))[::-1]
for i, num in enumerate(nums):
if num > 0:
continue
if prefix[i] == suffix[i]:
ans += 2 # Go to either direction.
if abs(prefix[i] - suffix[i]) == 1:
ans += 1 # Go to the direction with the larger sum.
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.