Skip to main content

Find Triangular Sum of an Array - Solution & Explanation

MediumArrayMathSimulationCombinatorics16 min readAsked at: Amazon, Microsoft, Google +1
Practice this problem

Problem Statement

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:

  1. Let nums comprise of n elements. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n - 1.
  2. For each index i, where 0 <= i < n - 1, assign the value of newNums[i] as (nums[i] + nums[i+1]) % 10, where % denotes modulo operator.
  3. Replace the array nums with newNums.
  4. Repeat the entire process starting from step 1.

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.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 9

Approach Overview

Problem Overview: You start with an integer array nums. Repeatedly create a new array where each element is (nums[i] + nums[i+1]) % 10. Continue until only one element remains. That final value is the triangular sum.

Approach 1: Iterative Simulation (O(n2) time, O(1) space)

This method directly simulates the process described in the problem. Iterate over the array multiple times, and during each pass update nums[i] with (nums[i] + nums[i+1]) % 10. After the first pass the effective array length becomes n-1, then n-2, and so on until one element remains. Because each level processes almost the entire array, the total work forms a triangular pattern: (n-1) + (n-2) + ... + 1, which results in O(n^2) time. Updating values in-place keeps the extra memory at O(1). This approach is straightforward and mirrors the problem statement, making it useful for quick implementation during interviews.

The logic relies purely on sequential iteration and modular arithmetic, which makes it a classic simulation problem combined with simple array manipulation.

Approach 2: Combinatorial Formula (O(n) time, O(1) space)

The triangular construction is equivalent to building rows of Pascal's triangle. Each element in the final result can be expressed as the original element multiplied by a binomial coefficient. Specifically, the final value equals sum(nums[i] * C(n-1, i)) % 10. Instead of performing every simulation step, compute the contribution of each element directly using binomial coefficients from the (n-1)th row of Pascal's triangle.

The challenge is that the result is taken modulo 10, which is not a prime number. Efficient implementations track factors of 2 and 5 while building the binomial coefficient iteratively to maintain correct modulo arithmetic. Each coefficient can be derived from the previous one using multiplicative updates, allowing the entire row to be processed in linear time. This reduces the complexity to O(n) time and constant extra space.

This technique transforms the problem into a mathematical observation about Pascal's triangle, making it a good example of applying math and combinatorics to eliminate repeated simulation.

Recommended for interviews: Start with the iterative simulation since it follows the problem definition and demonstrates clear reasoning. Strong candidates recognize the Pascal's triangle relationship and derive the combinatorial formula, which reduces the complexity to linear time and shows deeper algorithmic insight.

Approach 1: Iterative Approach

This approach involves simulating the process step-by-step by iteratively reducing the array size as described in the problem. We repeatedly create a new array where each element is the sum of consecutive elements modulo 10, until we are left with a single element.

The C implementation declares a function to perform the iterative triangular sum operation. It uses a loop to repeatedly calculate degenerated arrays until only one element is left, which is returned as the answer.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Combinatorial Formula

A more advanced approach is to express the result of transforming the array of length `n` into a single number using combinatorial mathematics. By recognizing pattern relationships that dictate how often each number influences the final result, you can optimize the process.

This C solution uses a combinatorial function to calculate the binomial coefficients for each element of `nums` and sums them up to find the result, thereby optimizing the previous iterative approach.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) for computing all combinations,
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Simulation

We can directly simulate the operations described in the problem. Perform n - 1 rounds of operations on the array nums, updating the array nums according to the rules described in the problem for each round. Finally, return the only remaining element in the array nums.

The time complexity is O(n^2), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n^2)
Space Complexity: O(1)

Combinatorial Formula

Time Complexity: O(n^2) for computing all combinations,
Space Complexity: O(1)

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative SimulationO(n^2)O(1)Best for quick implementation or when constraints are small
Combinatorial Formula (Pascal's Triangle Insight)O(n)O(1)Preferred for large inputs and when recognizing binomial coefficient patterns

Video Solution

Find Triangular Sum of an Array | 2 Approaches | Constant Space | Leetcode 2221 | codestorywithMIK • codestorywithMIK • 6,272 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Triangular Sum of an Array easy or hard?
The problem is generally rated medium difficulty. The simulation solution is straightforward, but recognizing the Pascal's triangle relationship and deriving the combinatorial formula requires stronger mathematical and algorithmic insight.
Find Triangular Sum of an Array Python/Java solution
Python, Java, C++, C#, and JavaScript implementations typically start with the iterative simulation because it directly follows the problem statement. Optimized versions compute binomial coefficients and accumulate the weighted sum to achieve O(n) performance.
How to solve Find Triangular Sum of an Array in O(n)?
Use the identity that the final element equals the weighted sum of the original array with binomial coefficients from row n-1 of Pascal's triangle. Compute each coefficient iteratively and accumulate nums[i] * C(n-1, i) mod 10. This processes the array once, giving O(n) time.
What is the best approach for Find Triangular Sum of an Array?
The most efficient approach uses a combinatorial formula based on Pascal's triangle. The final triangular sum equals sum(nums[i] * C(n-1, i)) mod 10. This avoids repeated simulation and computes the answer in O(n) time and O(1) space.
Is Find Triangular Sum of an Array asked at Google/Amazon/Meta?
This problem reflects common interview themes such as array simulation and recognizing mathematical patterns like Pascal's triangle. Variations involving triangular reductions or combinatorial transformations have appeared in interviews at large tech companies including Google and Amazon.
What data structure is used in Find Triangular Sum of an Array?
The core data structure is a simple array. The simulation approach repeatedly updates adjacent elements, while the optimized solution treats the array as coefficients in a combinatorial expansion using binomial coefficients.
What is the time complexity of Find Triangular Sum of an Array?
The straightforward simulation takes O(n^2) time because each iteration processes nearly the entire array and the length shrinks by one each round. Using the binomial coefficient observation reduces the complexity to O(n) time with constant extra space.

Ready to solve this problem?

Practice Find Triangular Sum of an Array with our built-in code editor and test cases.

Practice on FleetCode