Open In App

Digitally balanced numbers

Last Updated : 10 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ implementation to check if a
// number is a digitally balanced number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the digits in the
// number is the same number of digits
int checkSame(int n, int b)
{
    map<int, int> m;
 
    // Loop to iterate over the
    // digits of the number N
    while (n != 0) {
        int r = n % b;
        n = n / b;
        m[r]++;
    }
    int last = -1;
 
    // Loop to iterate over the map
    for (auto i = m.begin(); i != m.end(); i++) {
        if (last != -1 && i->second != last) {
            return false;
        }
        else {
            last = i->second;
        }
    }
}
 
// Driver Code
int main()
{
    int n = 9;
    int base = 2;
 
    // function to check
    if (checkSame(n, base))
        cout << "Yes";
    else
        cout << "NO";
    return 0;
}


Java




// Java implementation to check if a
// number is a digitally balanced number
import java.util.*;
class GFG{
 
// Function to check if the digits in the
// number is the same number of digits
static boolean checkSame(int n, int b)
{
    HashMap<Integer,
            Integer> m = new HashMap<Integer,
                                     Integer>();
 
    // Loop to iterate over the
    // digits of the number N
    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;
 
    // Loop to iterate over the map
    for (Map.Entry<Integer,
                   Integer> i : m.entrySet())
    {
        if (last != -1 && i.getValue() != last)
        {
            return false;
        }
        else
        {
            last = i.getValue();
        }
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 9;
    int base = 2;
 
    // function to check
    if (checkSame(n, base))
        System.out.print("Yes");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation to check if a
# number is a digitally balanced number
 
# Function to check if the digits in the
# number is the same number of digits
def checkSame(n, b):
     
    m = {}
 
    # Loop to iterate over the
    # digits of the number N
    while (n != 0):
        r = n % b
        n = n // b
         
        if r in m:
            m[r] += 1
        else:
            m[r] = 1
     
    last = -1
 
    # Loop to iterate over the map
    for i in m:
        if last != -1 and m[i] != last:
            return False
        else:
            last = m[i]
             
    return True
 
# Driver code
n = 9
base = 2
 
# Function to check
if (checkSame(n, base)):
    print("Yes")
else:
    print("NO")
 
# This code is contributed by divyeshrabadiya07


C#




// C# implementation to check if a
// number is a digitally balanced number
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if the digits in the
// number is the same number of digits
static bool checkSame(int n, int b)
{
    Dictionary<int,
               int> m = new Dictionary<int,
                                       int>();
 
    // Loop to iterate over the
    // digits of the number N
    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;
 
    // Loop to iterate over the map
    foreach(KeyValuePair<int, int> i in m)
    {
        if (last != -1 && i.Value != last)
        {
            return false;
        }
        else
        {
            last = i.Value;
        }
    }
    return true;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 9;
    int Base = 2;
 
    // Function to check
    if (checkSame(n, Base))
        Console.Write("Yes");
    else
        Console.Write("NO");
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// Javascript implementation
// Function to check if the digits in the
// number is the same number of digits
function checkSame( n,  b)
{
    var m = {};
 
    // Loop to iterate over the
    // digits of the number N
    while (n != 0) {
        var r = n % b;
        n = n / b;
        if (r in m)
            m[r] += 1
        else
            m[r] = 1
    }
    var last = -1;
 
    // Loop to iterate over the map
    for (var i in m) {
        if (last != -1 && m[i] != last) {
            return false;
        }
        else {
            last = m[i];
        }
    }
    return true;
}
 
// Driver Code
var n = 9;
var base = 2;
 
// function to check
if (checkSame(n, base))
document.write("Yes");
else
document.write("NO");
 
</script>


Output: 

Yes

 

Time Complexity: O(N * log N)

Auxiliary Space: O(N)

Reference: https://oeis.org/A031443
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads