2025-10-28 Daily Challenge
Today I have done leetcode's October LeetCoding Challenge with cpp.
October LeetCoding Challenge 28
Description
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
curris out of the range[0, n - 1], this process ends. - If
nums[curr] == 0, move in the current direction by incrementingcurrif you are moving right, or decrementingcurrif 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.<ul> <li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,<strong><u>2</u></strong>,0,3] -> [1,0,1,<strong><u>0</u></strong>,3] -> [1,0,1,0,<strong><u>3</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>1</u></strong>,0,2] -> [1,0,0,<strong><u>0</u></strong>,2] -> [1,0,0,0,<strong><u>2</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>0</u></strong>,0,1] -> [1,<strong><u>0</u></strong>,0,0,1] -> [<strong><u>1</u></strong>,0,0,0,1] -> [0,<strong><u>0</u></strong>,0,0,1] -> [0,0,<strong><u>0</u></strong>,0,1] -> [0,0,0,<strong><u>0</u></strong>,1] -> [0,0,0,0,<strong><u>1</u></strong>] -> [0,0,0,0,0]</code>.</li> </ul> </li> <li>Choose <code>curr = 3</code>, and a movement direction to the right. <ul> <li><code>[1,0,2,<strong><u>0</u></strong>,3] -> [1,0,2,0,<strong><u>3</u></strong>] -> [1,0,2,<strong><u>0</u></strong>,2] -> [1,0,<strong><u>2</u></strong>,0,2] -> [1,0,1,<strong><u>0</u></strong>,2] -> [1,0,1,0,<strong><u>2</u></strong>] -> [1,0,1,<strong><u>0</u></strong>,1] -> [1,0,<strong><u>1</u></strong>,0,1] -> [1,0,0,<strong><u>0</u></strong>,1] -> [1,0,0,0,<strong><u>1</u></strong>] -> [1,0,0,<strong><u>0</u></strong>,0] -> [1,0,<strong><u>0</u></strong>,0,0] -> [1,<strong><u>0</u></strong>,0,0,0] -> [<strong><u>1</u></strong>,0,0,0,0] -> [0,0,0,0,0].</code></li> </ul> </li>
Example 2:
Input: nums = [2,3,4,0,4,1,0]
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100- There is at least one element
iwherenums[i] == 0.
Solution
class Solution {
public:
int countValidSelections(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
int left = 0;
int answer = 0;
for(auto n : nums) {
if(n) {
left += n;
continue;
}
int right = (sum - left);
if(left == right) answer += 2;
else if(abs(left - right) == 1) answer += 1;
}
return answer;
}
};
// Accepted
// 584/584 cases passed (6 ms)
// Your runtime beats 49.08 % of cpp submissions
// Your memory usage beats 67.89 % of cpp submissions (22.1 MB)