#2221

Find Triangular Sum of an Array

pupil · 420 · lc medium +25 · verified · 82% accepted · 1,542 likes · top 93%

Description

You are given a 0-indexed integer array nums where each element is a digit between 0 and 9 inclusive.

The triangular sum is computed by repeatedly collapsing the array until one element remains:

- If nums has n elements and n == 1, stop. Otherwise create a new array newNums of length n - 1.

- Set newNums[i] = (nums[i] + nums[i+1]) % 10 for every index i where 0 <= i < n - 1.

- Replace nums with newNums and repeat.

Return the triangular sum of nums.

Example 1:

Input: nums = [1,2,3,4,5]
Output: 8
Explanation:
The above diagram depicts the process from which we obtain the triangular sum of the array.

Example 2:

Input: nums = [5]
Output: 5
Explanation:
Since there is only one element in nums, the triangular sum is the value of that element itself.

Code

1
2
3