Digitally balanced numbers
Last Updated :
10 Sep, 2021
Given two integers N and B, the task is to check N is a Digitally balanced number in base B.
Digitally balanced numbers in the base B are those numbers that have the same number of (0, 1, 2 ….B-1) digits in base B.
Examples:
Input: N = 9, B = 2
Output: Yes
Equivalent binary number of 9 is 1001,
which has equal number of 0’s as 1’s = 2
Input: N = 5, N = 2
Output: No
Equivalent binary number of 5 is 101.
Approach: The idea is to traverse the digits of the number in base B and store the frequency of digits in a hash-map. Finally, Iterate over the hash-map and check if the frequency of every digit in the hash-map is the same then the number is said to be the Digitally balanced number in base B.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkSame( int n, int b)
{
map< int , int > m;
while (n != 0) {
int r = n % b;
n = n / b;
m[r]++;
}
int last = -1;
for ( auto i = m.begin(); i != m.end(); i++) {
if (last != -1 && i->second != last) {
return false ;
}
else {
last = i->second;
}
}
}
int main()
{
int n = 9;
int base = 2;
if (checkSame(n, base))
cout << "Yes" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkSame( int n, int b)
{
HashMap<Integer,
Integer> m = new HashMap<Integer,
Integer>();
while (n != 0 )
{
int r = n % b;
n = n / b;
if (m.containsKey(r))
{
m.put(r, m.get(r) + 1 );
}
else
{
m.put(r, 1 );
}
}
int last = - 1 ;
for (Map.Entry<Integer,
Integer> i : m.entrySet())
{
if (last != - 1 && i.getValue() != last)
{
return false ;
}
else
{
last = i.getValue();
}
}
return true ;
}
public static void main(String[] args)
{
int n = 9 ;
int base = 2 ;
if (checkSame(n, base))
System.out.print( "Yes" );
else
System.out.print( "NO" );
}
}
|
Python3
def checkSame(n, b):
m = {}
while (n ! = 0 ):
r = n % b
n = n / / b
if r in m:
m[r] + = 1
else :
m[r] = 1
last = - 1
for i in m:
if last ! = - 1 and m[i] ! = last:
return False
else :
last = m[i]
return True
n = 9
base = 2
if (checkSame(n, base)):
print ( "Yes" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool checkSame( int n, int b)
{
Dictionary< int ,
int > m = new Dictionary< int ,
int >();
while (n != 0)
{
int r = n % b;
n = n / b;
if (m.ContainsKey(r))
{
m[r] = m[r] + 1;
}
else
{
m.Add(r, 1);
}
}
int last = -1;
foreach (KeyValuePair< int , int > i in m)
{
if (last != -1 && i.Value != last)
{
return false ;
}
else
{
last = i.Value;
}
}
return true ;
}
public static void Main(String[] args)
{
int n = 9;
int Base = 2;
if (checkSame(n, Base))
Console.Write( "Yes" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
function checkSame( n, b)
{
var m = {};
while (n != 0) {
var r = n % b;
n = n / b;
if (r in m)
m[r] += 1
else
m[r] = 1
}
var last = -1;
for ( var i in m) {
if (last != -1 && m[i] != last) {
return false ;
}
else {
last = m[i];
}
}
return true ;
}
var n = 9;
var base = 2;
if (checkSame(n, base))
document.write( "Yes" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Reference: https://oeis.org/A031443
Share your thoughts in the comments
Please Login to comment...