Skip to content

Practice Alternating Digit Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2544
LeetCode Problem

Alternating Digit Sum

Problem Statement

Read the decimal digits of positive n from left to right. Add the first digit, subtract the second, and continue alternating signs. Return the signed sum.

Source: Alternating Digit Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 521
Output
4

# Example 2

Input
n = 111
Output
1

# Example 3

Input
n = 886996
Output
0

Constraints

1 <= n <= 10^9

Related LeetCode Problems

Practice more problems similar to Alternating Digit Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.