2221. Find Triangular Sum of an Array Medium
1/**
2 * [2221] Find Triangular Sum of an Array
3 *
4 * You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive).
5 * The triangular sum of nums is the value of the only element present in nums after the following process terminates:
6 * <ol>
7 * 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.
8 * 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.
9 * Replace the array nums with newNums.
10 * Repeat the entire process starting from step 1.
11 * </ol>
12 * Return the triangular sum of nums.
13 *
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" />
16 * Input: nums = [1,2,3,4,5]
17 * Output: 8
18 * Explanation:
19 * The above diagram depicts the process from which we obtain the triangular sum of the array.
20 * Example 2:
21 *
22 * Input: nums = [5]
23 * Output: 5
24 * Explanation:
25 * Since there is only one element in nums, the triangular sum is the value of that element itself.
26 *
27 * Constraints:
28 *
29 * 1 <= nums.length <= 1000
30 * 0 <= nums[i] <= 9
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/find-triangular-sum-of-an-array/
36// discuss: https://leetcode.com/problems/find-triangular-sum-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn triangular_sum(nums: Vec<i32>) -> i32 {
42 0
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_2221() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.