FastPrepSum Leftmost Subtraction Levels

Sum Leftmost Subtraction Levels

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

Repeat until every nonnegative value is zero:

  1. Choose the leftmost nonzero value x and add x to the result.
  2. Starting there, subtract x from consecutive values while each current value is at least x; stop before the first smaller value.

Return the accumulated result.

Function

sumSubtractionLevels(values: int[]) → long

Examples

Example 1

values = [3,3,5,2,3]return = 6

The reported process accumulates six.

Example 2

values = [3,3,5,2,4]return = 7

The successive added levels are 3, 2, and 2.

Constraints

  • 1 <= values.length <= 1000
  • 0 <= values[i] <= 1000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long sumSubtractionLevels(int[] values) {
    // Write your code here.
}
values[3,3,5,2,3]
expected6
Checking account…