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, repeat the following process:

  • If curr is out of the range [0, n - 1], the 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 right becomes left.
  • 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
Inputnums = [1,0,2,0,3]
Output2
The only possible valid selections are choosing curr = 3 and moving either left or right.
Example 2
Inputnums = [2,3,4,0,4,1,0]
Output0
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.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate