Ctrl K

Plus-Minus Digits

Easy
You are given a positive integer number. Put signs in front of its digits starting from the leftmost one: the first digit gets a plus, the second a minus, the third a plus again, and so on, alternating. Return the sum of the digits with those signs.
Example 1
number=7302 → 2

(+7) + (−3) + (+0) + (−2) = 2.

Example 2
number=4 → 4

A single digit, taken with a plus sign.

Example 3
number=1919 → -16

1 − 9 + 1 − 9 = −16: the answer can be negative.

Constraints
  • 1 ≤ number ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Number That Reads Both Ways EasyValue of a Roman Numeral EasyAdd One to a Digit Array EasySum of Binary Strings EasyWhole Square Root Easy
loading Python…
Input
number=7302
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 39 tests on the server, hidden ones included.