We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3726906 commit b96a3cfCopy full SHA for b96a3cf
my-folder/problems/difference_between_element_sum_and_digit_sum_of_an_array/solution.rs
@@ -0,0 +1,23 @@
1
+impl Solution {
2
+ pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
3
+ let element_sum: i32 = nums.iter().sum();
4
+
5
+ let digit_sum: i32 = nums.into_iter().map(Solution::number_digit_sum).sum();
6
7
+ return (element_sum - digit_sum).abs();
8
+ }
9
10
+ #[inline(always)]
11
+ fn number_digit_sum(mut num: i32) -> i32 {
12
+ let mut total = 0;
13
+ loop {
14
+ total += num % 10;
15
+ num /= 10;
16
17
+ if num == 0 {
18
+ break;
19
20
21
+ total
22
23
+}
0 commit comments