- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Difference between sums of odd and even digits
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
- Related Articles
- C Program for 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 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 to find the sum of all even and odd digits of an integer list
- 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++
- Python Program for Check if the count of divisors is even or odd
- Count even and odd digits in an Integer in C++
- Check if product of digits of a number at even and odd places is equal in Python
- Odd even index difference - JavaScript
- Count odd and even digits in a number in PL/SQL
