Dados dos números positivos N y M , la tarea es contar el número de dígitos que están presentes tanto en N como en M .
Ejemplos:
Entrada: N = 748294, M = 34298156
Salida: 4
Explicación: Los dígitos que están presentes en ambos números son {4, 8, 2, 9}. Por lo tanto, el conteo requerido es 4.Entrada: N = 111222, M = 333444
Salida: 0
Explicación: No hay dígitos comunes presentes en los dos números dados.
Enfoque: El problema dado se puede resolver usando Hashing . Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos contar como 0 , para almacenar la cantidad de dígitos que son comunes en ambos números.
- Inicialice dos arrays , digamos freq1[10] y freq2[10] como {0} , para almacenar el recuento de dígitos presentes en los números enteros N y M respectivamente.
- Itere sobre los dígitos del entero N e incremente el recuento de cada dígito en freq1[] en 1 .
- Itere sobre los dígitos del entero M e incremente el recuento de cada dígito en freq2[] en 1 .
- Itere sobre el rango [0, 9] e incremente el conteo en 1 si tanto freq1[i] como freq2[i] exceden 0 .
- Finalmente, luego de completar los pasos anteriores, imprima el conteo obtenido como la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count number of digits // that are common in both N and M int CommonDigits(int N, int M) { // Stores the count of common digits int count = 0; // Stores the count of digits of N int freq1[10] = { 0 }; // Stores the count of digits of M int freq2[10] = { 0 }; // Iterate over the digits of N while (N > 0) { // Increment the count of // last digit of N freq1[N % 10]++; // Update N N = N / 10; } // Iterate over the digits of M while (M > 0) { // Increment the count of // last digit of M freq2[M % 10]++; // Update M M = M / 10; } // Iterate over the range [0, 9] for (int i = 0; i < 10; i++) { // If freq1[i] and freq2[i] both exceeds 0 if (freq1[i] > 0 & freq2[i] > 0) { // Increment count by 1 count++; } } // Return the count return count; } // Driver Code int main() { // Input int N = 748294; int M = 34298156; cout << CommonDigits(N, M); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to count number of digits // that are common in both N and M static int CommonDigits(int N, int M) { // Stores the count of common digits int count = 0; // Stores the count of digits of N int freq1[] = new int[10]; // Stores the count of digits of M int freq2[] = new int[10]; // Iterate over the digits of N while (N > 0) { // Increment the count of // last digit of N freq1[N % 10]++; // Update N N = N / 10; } // Iterate over the digits of M while (M > 0) { // Increment the count of // last digit of M freq2[M % 10]++; // Update M M = M / 10; } // Iterate over the range [0, 9] for(int i = 0; i < 10; i++) { // If freq1[i] and freq2[i] both exceeds 0 if (freq1[i] > 0 & freq2[i] > 0) { // Increment count by 1 count++; } } // Return the count return count; } // Driver Code public static void main(String[] args) { // Input int N = 748294; int M = 34298156; System.out.print(CommonDigits(N, M)); } } // This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach # Function to count number of digits # that are common in both N and M def CommonDigits(N, M): # Stores the count of common digits count = 0 # Stores the count of digits of N freq1 = [0] * 10 # Stores the count of digits of M freq2 = [0] * 10 # Iterate over the digits of N while (N > 0): # Increment the count of # last digit of N freq1[N % 10] += 1 # Update N N = N // 10 # Iterate over the digits of M while (M > 0): # Increment the count of # last digit of M freq2[M % 10] += 1 # Update M M = M // 10 # Iterate over the range [0, 9] for i in range(10): # If freq1[i] and freq2[i] both exceeds 0 if (freq1[i] > 0 and freq2[i] > 0): # Increment count by 1 count += 1 # Return the count return count # Driver Code if __name__ == '__main__': # Input N = 748294 M = 34298156 print (CommonDigits(N, M)) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG{ // Function to count number of digits // that are common in both N and M static int CommonDigits(int N, int M) { // Stores the count of common digits int count = 0; // Stores the count of digits of N int[] freq1 = new int[10]; // Stores the count of digits of M int[] freq2 = new int[10]; // Iterate over the digits of N while (N > 0) { // Increment the count of // last digit of N freq1[N % 10]++; // Update N N = N / 10; } // Iterate over the digits of M while (M > 0) { // Increment the count of // last digit of M freq2[M % 10]++; // Update M M = M / 10; } // Iterate over the range [0, 9] for(int i = 0; i < 10; i++) { // If freq1[i] and freq2[i] // both exceeds 0 if (freq1[i] > 0 & freq2[i] > 0) { // Increment count by 1 count++; } } // Return the count return count; } // Driver code static void Main() { // Input int N = 748294; int M = 34298156; Console.WriteLine(CommonDigits(N, M)); } } // This code is contributed by sanjoy_62
Javascript
<script> // javascript program for the above approach // Function to count number of digits // that are common in both N and M function CommonDigits(N,M) { // Stores the count of common digits var count = 0; // Stores the count of digits of N var freq1 = Array(10).fill(0); // Stores the count of digits of M var freq2 = Array(10).fill(0); // Iterate over the digits of N while (N > 0) { // Increment the count of // last digit of N freq1[N % 10]++; // Update N N = Math.floor(N / 10); } // Iterate over the digits of M while (M > 0) { // Increment the count of // last digit of M freq2[M % 10]++; // Update M M = Math.floor(M / 10); } var i; // Iterate over the range [0, 9] for (i = 0; i < 10; i++) { // If freq1[i] and freq2[i] both exceeds 0 if (freq1[i] > 0 & freq2[i] > 0) { // Increment count by 1 count++; } } // Return the count return count; } // Driver Code // Input var N = 748294; var M = 34298156; document.write(CommonDigits(N, M)); </script>
4
Complejidad de tiempo: O(dígitos(N)+dígitos(M))
Espacio auxiliar: O(10)
Publicación traducida automáticamente
Artículo escrito por vikkycirus y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA