Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for Difference between sums of odd and even digits
In this article, we will learn how to find the difference between the sum of odd digits and sum of even digits in a number, and check if this difference equals zero.
Problem Statement ? Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.
Method 1: Brute Force Approach
The straightforward approach calculates the sum of all even and odd digits separately, then finds their difference ?
def difference_odd_even_digits(n):
n = abs(n) # Handle negative numbers
odd_sum = 0
even_sum = 0
while n > 0:
digit = n % 10
if digit % 2 == 0:
even_sum += digit
else:
odd_sum += digit
n //= 10
return odd_sum - even_sum
# Test with examples
numbers = [1234, 785643, 123321, 4628]
for num in numbers:
diff = difference_odd_even_digits(num)
print(f"Number: {num}, Difference: {diff}, Equal sums: {'Yes' if diff == 0 else 'No'}")
Number: 1234, Difference: 0, Equal sums: Yes Number: 785643, Difference: 1, Equal sums: No Number: 123321, Difference: 0, Equal sums: Yes Number: 4628, Difference: -10, Equal sums: No
Method 2: Using Divisibility by 11
An optimized approach uses the mathematical property that a number is divisible by 11 if and only if the alternating sum of its digits is divisible by 11. This relates to our problem since the alternating sum equals the difference between odd-positioned and even-positioned digits ?
def is_difference_zero(n):
return (n % 11 == 0)
# Test with examples
numbers = [1234, 785643, 123321, 4628]
for num in numbers:
result = is_difference_zero(num)
print(f"Number: {num}, Difference is zero: {'Yes' if result else 'No'}")
Number: 1234, Difference is zero: No Number: 785643, Difference is zero: No Number: 123321, Difference is zero: No Number: 4628, Difference is zero: No
Understanding the Mathematical Concept
Let's verify why divisibility by 11 works with a detailed example ?
def analyze_number(n):
original_n = n
n = abs(n)
digits = []
# Extract digits
while n > 0:
digits.append(n % 10)
n //= 10
digits.reverse() # Get digits in original order
odd_sum = sum(digit for digit in digits if digit % 2 == 1)
even_sum = sum(digit for digit in digits if digit % 2 == 0)
difference = odd_sum - even_sum
# Alternating sum (divisibility by 11 rule)
alternating_sum = sum(digit * ((-1) ** i) for i, digit in enumerate(digits))
print(f"Number: {original_n}")
print(f"Digits: {digits}")
print(f"Odd digits sum: {odd_sum}")
print(f"Even digits sum: {even_sum}")
print(f"Difference (odd - even): {difference}")
print(f"Alternating sum: {alternating_sum}")
print(f"Divisible by 11: {original_n % 11 == 0}")
print(f"Difference is zero: {'Yes' if difference == 0 else 'No'}")
print("-" * 40)
# Test with examples
analyze_number(1234)
analyze_number(121)
Number: 1234 Digits: [1, 2, 3, 4] Odd digits sum: 4 Even digits sum: 6 Difference (odd - even): -2 Alternating sum: -2 Divisible by 11: False Difference is zero: No ---------------------------------------- Number: 121 Digits: [1, 2, 1] Odd digits sum: 2 Even digits sum: 2 Difference (odd - even): 0 Alternating sum: 0 Divisible by 11: True Difference is zero: Yes ----------------------------------------
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(log n) | O(1) | General case, getting actual difference |
| Divisibility by 11 | O(1) | O(1) | Only checking if difference is zero |
Conclusion
The brute force approach gives the actual difference between odd and even digit sums, while the divisibility by 11 method only checks if the difference is zero. Choose the method based on whether you need the exact difference value or just a zero-check.
---