- 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++
- 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
- Count odd and even digits in a number in PL/SQL
- Python program to Count Even and Odd numbers in a List
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- Find the sum of digits of a number at even and odd places in C++

- 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