Created
September 30, 2025 17:23
-
-
Save tatsuyax25/1360341ba28040319c41d931501981b9 to your computer and use it in GitHub Desktop.
You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive). The triangular sum of nums is the value of the only element present in nums after the following process terminates: Let nums comprise of n elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var triangularSum = function(nums) { | |
| // Continue the process until only one element remains | |
| while (nums.length > 1) { | |
| let newNums = []; | |
| // Generate the next row by summing adjacent elements modulo 10 | |
| for (let i = 0; i < nums.length - 1; i++) { | |
| newNums.push((nums[i] + nums[i + 1]) % 10); | |
| } | |
| // Replace nums with the new row | |
| nums = newNums; | |
| } | |
| // Return the final remaining element | |
| return nums[0]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment