Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created September 30, 2025 17:23
Show Gist options
  • Select an option

  • Save tatsuyax25/1360341ba28040319c41d931501981b9 to your computer and use it in GitHub Desktop.

Select an option

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.
/**
* @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