Overview
You are given a 0-indexed integer arraynumswhere each element is a single digit between0and9(inclusive). The goal is to compute the triangular sum โ the single digit that remains after repeatedly transforming the array until only one element remains.
i with 0 <= i < n-1, use the expression newNums[i] = (nums[i] + nums[i+1]) % 10.% 10 at every step?% 10 keeps only the units (last) digit of each sum.(7 + 8) % 10 = 15 % 10 = 5.0โ9), matching the input domain.Input: nums = [1, 2, 3, 4, 5]
Step-by-step:
[1, 2, 3, 4, 5](1 + 2) % 10 = 3 โ first element(2 + 3) % 10 = 5 โ second element(3 + 4) % 10 = 7 โ third element(4 + 5) % 10 = 9 โ fourth element[3, 5, 7, 9](3 + 5) % 10 = 8(5 + 7) % 10 = 2 (12 % 10 = 2)(7 + 9) % 10 = 6 (16 % 10 = 6)[8, 2, 6](8 + 2) % 10 = 0 (10 % 10 = 0)(2 + 6) % 10 = 8 (8 % 10 = 8)[0, 8](0 + 8) % 10 = 8[8]โ
Result: The triangular sum for [1, 2, 3, 4, 5] is 8.
nums is an integer digit from 0 through 9. The modulo step preserves that range for all intermediate values.nums already has one element (e.g., nums = [5]), that element is the triangular sum.7 + 8 = 15), the tens place is dropped โ only 5 remains because 15 % 10 = 5.Return the single remaining digit (an integer 0โ9) after performing the repeated reductions. That value is the triangular sum of the input array.
https://leetcode.com/problems/find-triangular-sum-of-an-array/description/
Loading component...
Loading component...