FastPrepSpecial Sum of an Array

Special Sum of an Array

Mastercard logoMastercard● EasyNEW GRADOA
Learn

Problem statement

You are given an integer array values whose elements are decimal digits.

First, add all elements of the array. While the sum contains more than one digit, replace it with the sum of its decimal digits.

Return the final single-digit value.

Function

specialSum(values: int[]) → int

Examples

Example 1

values = [5,8,4,9]return = 8

The array sum is 26. Its digit sum is 2 + 6 = 8, which is a single digit.

Example 2

values = [8,7,0,1,2]return = 9

The array sum is 18, and 1 + 8 = 9.

Example 3

values = [0,0,0]return = 0

The sum is already the single digit 0.

Constraints

  • 1 <= values.length <= 10^3.
  • 0 <= values[i] <= 9.

More Mastercard problems

See Mastercard hiring insights
public int specialSum(int[] values) {
  // Write your code here.
}
values[5,8,4,9]
expected8
Checking account…