JuniorMath
Alternating Digit Sum
You are given a positive integer n. Each digit of n has a sign according to the following rules:
- The most significant digit is assigned a positive sign.
- Each other digit has an opposite sign to its adjacent digits.
Return the sum of all digits with their corresponding sign.
Example 1
Input
n = 521Output
4(+5) + (-2) + (+1) = 4.
Example 2
Input
n = 111Output
1(+1) + (-1) + (+1) = 1.
Constraints
- 1 <= n <= 10^9