Suppose we have a number num we have to check whether is balanced or not. A number is balanced when frequencies of all digits are same or not.
So, if the input is like num = 562256, then the output will be True as frequency of each digit is 2.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import defaultdict def solve(num): number = str(num) freq = defaultdict(int) n = len(number) for i in range(n): freq[int(number[i])] += 1 freq_values = set(freq.values()) if len(freq_values) == 1: return True return False num = 562256 print(solve(num))