Difficulty: Easy | Acceptance: 69.10% | Paid: No Topics: Math
You are given a positive integer n. Each digit of n is alternatingly summed as follows:
- The leftmost digit is added.
- The next digit is subtracted.
- The next digit is added.
- And so on.
Return the alternating digit sum of n.
Examples
Input: n = 521
Output: 4
Explanation: +5 - 2 + 1 = 4
Input: n = 111
Output: 1
Explanation: +1 - 1 + 1 = 1
Input: n = 886996
Output: 0
Explanation: +8 - 8 + 6 - 9 + 9 - 6 = 0
Constraints
1 <= n <= 10⁹
- String Conversion
- Mathematical - Reverse and Count
- Mathematical - Count Digits First
- Array/Stack Approach
String Conversion
Intuition Convert the number to a string and iterate through each character, adding or subtracting based on the position index.
Steps
- Convert the integer to a string
- Iterate through each character with its index
- Add the digit value if index is even, subtract if index is odd
- Return the final result
class Solution:
def alternateDigitSum(self, n: int) -> int:
s = str(n)
result = 0
for i, ch in enumerate(s):
if i % 2 == 0:
result += int(ch)
else:
result -= int(ch)
return resultComplexity
- Time: O(log n) - number of digits in n
- Space: O(log n) - for storing the string representation
- Notes: Simple and readable, but uses extra space for string conversion
Mathematical - Reverse and Count
Intuition Reverse the number first, then process digits from right to left (which becomes left to right after reversal) with alternating signs.
Steps
- Reverse the number by extracting digits and building a new number
- Process the reversed number digit by digit
- Alternate between adding and subtracting each digit
- Return the final result
class Solution:
def alternateDigitSum(self, n: int) -> int:
# Reverse the number
rev = 0
temp = n
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
# Calculate alternating sum
result = 0
sign = 1
while rev > 0:
result += sign * (rev % 10)
rev //= 10
sign = -sign
return resultComplexity
- Time: O(log n) - two passes through the digits
- Space: O(1) - only uses a few integer variables
- Notes: Pure mathematical approach with constant space, but requires two passes
Mathematical - Count Digits First
Intuition Count the total number of digits first to determine the starting sign, then process from right to left with alternating signs.
Steps
- Count the number of digits in the number
- Determine starting sign: positive if odd number of digits, negative if even
- Process digits from right to left, alternating signs
- Return the final result
class Solution:
def alternateDigitSum(self, n: int) -> int:
# Count digits
temp = n
num_digits = 0
while temp > 0:
num_digits += 1
temp //= 10
# Calculate alternating sum from right to left
result = 0
sign = 1 if num_digits % 2 == 1 else -1
while n > 0:
result += sign * (n % 10)
n //= 10
sign = -sign
return resultComplexity
- Time: O(log n) - two passes through the digits
- Space: O(1) - only uses a few integer variables
- Notes: Similar to reverse approach but avoids number reversal, still requires two passes
Array/Stack Approach
Intuition Extract all digits into an array, then process them in reverse order with alternating signs starting from positive.
Steps
- Extract all digits from right to left into an array
- Iterate through the array in reverse order (left to right)
- Alternate between adding and subtracting each digit
- Return the final result
class Solution:
def alternateDigitSum(self, n: int) -> int:
digits = []
while n > 0:
digits.append(n % 10)
n //= 10
result = 0
sign = 1
for i in range(len(digits) - 1, -1, -1):
result += sign * digits[i]
sign = -sign
return resultComplexity
- Time: O(log n) - extracting and processing digits
- Space: O(log n) - storing all digits in an array
- Notes: More intuitive but uses extra space for the array