Back to questions

Triangular Sum TikTok Python Interview Question

Triangular Sum

TikTok Python Interview Question

You are given an integer array , where each element in it is a single digit (0-9).

The triangular sum of is the value of the only element present in nums after the following process terminates:

  1. Let comprise of elements. If , end the process. Otherwise, create a new integer array of length .
  2. For each index , assign the value of as , where denotes the modulo operator.
  3. Replace the array with .
  4. Repeat the entire process starting from step 1.

Return the triangular sum of nums.

Test Case #1:

Input: nums =

Triangular Sum DataLemur Example

  1. Iteration #1: Form = [(1 + 3) % 10, (3 + 5) % 10, (5 + 7) % 10] = [4, 8, 2].

  2. Iteration #2: Form = [(4 + 8) % 10, (8 + 2) % 10] = [2, 0].

  3. Iteration #3: Form = [(2 + 0) % 10] = [2].

The triangular sum of is 2.


Test Case #2:

Input: nums =

  1. Iteration #1: Form = [(9 + 7) % 10, (7 + 5) % 10, (5 + 3) % 10] = [6, 2, 8].

  2. Iteration #2: Form = [(6 + 2) % 10, (2 + 8) % 10] = [8, 0].

  3. Iteration #3: Form = [(8 + 0) % 10] = [8].

The triangular sum of is 8.

Input

Python

Output