- Related Questions & Answers
- C Program for Difference between sums of odd and even digits?
- Difference between sums of odd and even digits.
- C Program for the Difference between sums of odd and even digits?
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Difference between sums of odd position and even position nodes of a Binary Tree in Java
- Python Program for Odd-Even Sort / Brick Sort
- Count number of ordered pairs with Even and Odd Sums in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- Odd even index difference - JavaScript
- Count even and odd digits in an Integer in C++
- Python Program for Check if the count of divisors is even or odd
- Check if product of digits of a number at even and odd places is equal in Python
- Count odd and even digits in a number in PL/SQL
- C/C++ Program for Odd-Even Sort (Brick Sort)?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution and approach to solve the given problem statement.

**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.

The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.

To reduce the computational time we use the concept of mental mathematics .

The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the divisibility of the number by 11.

Here the complexity reduces from O(n) to some contant amount of time involved in divibility and comparision..

Now let’s see the implementation −

def isDiff(n): return (n % 11 == 0) # main n = 785643 if (isDiff(n)): print("Yes") else: print("No")

No

All variables and functions are declared in global scope as shown in the figure below.

In this article, we learned about the approach to compute the difference between sums of odd and even digits

Advertisements