diff options
author | Jack Sangdahl <[email protected]> | 2025-09-29 20:20:34 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-29 20:20:34 -0600 |
commit | 3c005c0254993983579e453c39eee51504ab7fa8 (patch) | |
tree | 6cea3d5e7650d7370983c6c14cff1423220e4da7 | |
parent | c940fddbdbf26edb345954e2b155f87ad3aae7ae (diff) | |
download | leetcode-3c005c0254993983579e453c39eee51504ab7fa8.tar.gz leetcode-3c005c0254993983579e453c39eee51504ab7fa8.zip |
2221. find triangular sum of an array
-rw-r--r-- | find_triangular_sum_arr.c | 26 | ||||
-rw-r--r-- | find_triangular_sum_arr.py | 28 |
2 files changed, 54 insertions, 0 deletions
diff --git a/find_triangular_sum_arr.c b/find_triangular_sum_arr.c new file mode 100644 index 0000000..0033f7c --- /dev/null +++ b/find_triangular_sum_arr.c @@ -0,0 +1,26 @@ +// 2221. Find Triangular Sum of an Array +#include "leetcode.h" + +/* + * 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 concludes: let + * 'nums' comprise of 'n' elements. if 'n == 1', end the process. otherwise, + * create a new 0-indexed integer array 'new_nums' of length 'n - 1'. for each + * index 'i' where '0 <= i < n - 1', assign the value of 'new_nums[i]' as + * '(nums[i] + nums[i + 1]) % 10'. replace the array 'nums' with 'new_nums', and + * repeat the entire process starting again. + */ + +int triangularSum(int *nums, int numsSize) { + for (int i = numsSize - 1; i >= 1; i--) + for (int j = 0; j < i; j++) + nums[j] = (nums[j] + nums[j + 1]) % 10; + return nums[0]; +} + +int main() { + int n1[] = {1, 2, 3, 4, 5}, n2[] = {5}; + printf("%d\n", triangularSum(n1, ARRAY_SIZE(n1))); // expect: 8 + printf("%d\n", triangularSum(n2, ARRAY_SIZE(n2))); // expect: 5 +} diff --git a/find_triangular_sum_arr.py b/find_triangular_sum_arr.py new file mode 100644 index 0000000..55496c7 --- /dev/null +++ b/find_triangular_sum_arr.py @@ -0,0 +1,28 @@ +# 2221. Find Triangular Sum of an Array +from math import comb + +""" +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 concludes: let +'nums' comprise of 'n' elements. if 'n == 1', end the process. otherwise, +create a new 0-indexed integer array 'new_nums' of length 'n - 1'. for each +index 'i' where '0 <= i < n - 1', assign the value of 'new_nums[i]' as +'(nums[i] + nums[i + 1]) % 10'. replace the array 'nums' with 'new_nums', and +repeat the entire process starting again. +""" + + +class Solution(object): + def triangularSum(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return sum(n * comb(len(nums) - 1, i) for i, n in enumerate(nums)) % 10 + + +if __name__ == "__main__": + obj = Solution() + print(obj.triangularSum(nums=[1, 2, 3, 4, 5])) + print(obj.triangularSum(nums=[5])) |