
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Sums of Odd and Even Digits in Python
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 −
Example
def isDiff(n): return (n % 11 == 0) # main n = 785643 if (isDiff(n)): print("Yes") else: print("No")
Output
No
All variables and functions are declared in global scope as shown in the figure below.
Conclusion
In this article, we learned about the approach to compute the difference between sums of odd and even digits