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
Inputn = 521
Output4
(+5) + (-2) + (+1) = 4.
Example 2
Inputn = 111
Output1
(+1) + (-1) + (+1) = 1.

Constraints

  • 1 <= n <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate