Dados dos enteros N y B , la tarea es comprobar que N es un número balanceado digitalmente en base B.
Los números balanceados digitalmente en base B son aquellos números que tienen el mismo número de (0, 1, 2….B-1) dígitos en base B.
Ejemplos:
Entrada: N = 9, B = 2
Salida: Sí El
número binario equivalente de 9 es 1001,
que tiene el mismo número de 0 que 1 = 2Entrada: N = 5, N = 2
Salida: No
Número binario equivalente de 5 es 101.
Enfoque: La idea es recorrer los dígitos del número en base B y almacenar la frecuencia de los dígitos en un mapa hash . Finalmente, itere sobre el mapa hash y verifique si la frecuencia de cada dígito en el mapa hash es la misma, entonces se dice que el número es el número balanceado digitalmente en la base B.
A continuación se muestra la implementación del enfoque anterior:
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>
Yes
Complejidad de tiempo: O(N * log N)
Espacio Auxiliar: O(N)
Referencia: https://oeis.org/A031443