Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Make Array Elements Equal to Zero

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums.
You must begin at some index curr where the value is already 0, and choose an initial motion direction: either left or right.

From there, follow this repeating procedure:

  1. If curr moves outside the array, the process stops.

  2. If nums[curr] == 0, continue moving in the same direction by stepping to the next index.

  3. If nums[curr] > 0, decrease that value by 1, reverse your movement direction, and then take one step in the opposite direction.

A starting index (with an associated direction) is valid if this process eventually turns every element in the array into zero.

Return the total number of valid (index, direction) pairs.

Example 1:

Input: nums = [1, 0, 1, 0]

Output: 0

Explanation:

Even though there are two zeros to start from, no direction choice allows all numbers to reach zero simultaneously.

Example 2:

Input: nums = [0, 2, 0]

Output: 2

Explanation:

Starting at index 0 going right, or starting at index 2 going left, both fully reduce the array to all zeros. Other choices fail.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result