Easy
You are given a positive integer array nums.
nums.nums.Return the absolute difference between the element sum and digit sum of nums.
Note that the absolute difference between two integers x and y is defined as |x - y|.
Example 1:
Input: nums = [1,15,6,3]
Output: 9
Explanation:
The element sum of nums is 1 + 15 + 6 + 3 = 25.
The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16.
The absolute difference between the element sum and digit sum is |25 - 16| = 9.
Example 2:
Input: nums = [1,2,3,4]
Output: 0
Explanation:
The element sum of nums is 1 + 2 + 3 + 4 = 10.
The digit sum of nums is 1 + 2 + 3 + 4 = 10.
The absolute difference between the element sum and digit sum is |10 - 10| = 0.
Constraints:
1 <= nums.length <= 20001 <= nums[i] <= 2000@Suppress("NAME_SHADOWING")
class Solution {
private fun getsum(n: Int): Int {
var n = n
var sum = 0
while (n > 0) {
val r = n % 10
sum += r
n = n / 10
}
return sum
}
fun differenceOfSum(nums: IntArray): Int {
var eleSum = 0
var digitSum = 0
for (j in nums) {
digitSum += if (j >= 10) {
val sumC = getsum(j)
sumC
} else {
j
}
}
for (num in nums) {
eleSum += num
}
val max = Math.max(eleSum, digitSum)
val min = Math.min(eleSum, digitSum)
return max - min
}
}