Dado un número entero N , la tarea es verificar si N contiene un dígito D tal que sea el promedio de todos los demás dígitos presentes en N.
Ejemplos:
Entrada: N = 132
Salida: Sí
Explicación:
Ya que, (1 + 3)/2 = 2.
Entrada: N = 436
Salida: No
Explicación:
No existe tal dígito que sea el promedio de todos los demás dígitos.
Enfoque:
Para resolver el problema, siga los pasos a continuación:
- Almacene la suma y el conteo de dígitos de N .
- Almacene los dígitos en un Set .
- Si sum%count es 0 y el entero sum/count está presente en el Set , es decir, sum/count es un dígito de N , luego imprima Sí . De lo contrario , imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to check whether a // given number contains a digit // which is the average of all // other digits #include <bits/stdc++.h> using namespace std; // Function which checks if a // digits exists in n which // is the average of all other digits void check(int n) { set<int> digits; int temp = n; int sum = 0; int count = 0; while (temp > 0) { // Calculate sum of // digits in n sum += temp % 10; // Store the digits digits.insert(temp % 10); // Increase the count // of digits in n count++; temp = temp / 10; } // If average of all digits // is an integer if (sum % count == 0 // If the average is a digit // in n && digits.find(sum / count) != digits.end()) cout << "Yes" << endl; // Otherwise else cout << "No" << endl; } // Driver Code int main() { int n = 42644; check(n); }
Java
// Java program to check whether a // given number contains a digit // which is the average of all // other digits import java.util.*; class GFG{ // Function which checks if a // digits exists in n which // is the average of all other digits static void check(int n) { HashSet<Integer> digits = new HashSet<Integer>(); int temp = n; int sum = 0; int count = 0; while (temp > 0) { // Calculate sum of // digits in n sum += temp % 10; // Store the digits digits.add(temp % 10); // Increase the count // of digits in n count++; temp = temp / 10; } // If average of all digits // is an integer if (sum % count == 0 && digits.contains(sum / count)) System.out.print("Yes" + "\n"); // Otherwise else System.out.print("No" + "\n"); } // Driver Code public static void main(String[] args) { int n = 42644; check(n); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to check whether a given # number contains a digit which is # the average of all other digits # Function which checks if a # digits exists in n which is # the average of all other digits def check (n): digits = set() temp = n Sum = 0 count = 0 while(temp > 0): # Calculate sum of # digits in n Sum += temp % 10 # Store digits digits.add(temp % 10) # Increase the count of # digits in n count += 1 temp = temp // 10 # If average of all digits is integer if ((Sum % count == 0) and ((int)(Sum / count) in digits)): print("Yes") else: print("No") # Driver code n = 42644 # Function calling check(n) # This code is contributed by himanshu77
C#
// C# program to check whether a given // number contains a digit which is the // average of all other digits using System; using System.Collections.Generic; class GFG{ // Function which checks if a // digits exists in n which // is the average of all other digits static void check(int n) { HashSet<int> digits = new HashSet<int>(); int temp = n; int sum = 0; int count = 0; while (temp > 0) { // Calculate sum of // digits in n sum += temp % 10; // Store the digits digits.Add(temp % 10); // Increase the count // of digits in n count++; temp = temp / 10; } // If average of all digits // is an integer if (sum % count == 0 && digits.Contains(sum / count)) Console.Write("Yes" + "\n"); // Otherwise else Console.Write("No" + "\n"); } // Driver Code public static void Main(String[] args) { int n = 42644; check(n); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript Program to check whether a // given number contains a digit // which is the average of all // other digits // Function which checks if a // digits exists in n which // is the average of all other digits function check(n) { var digits = new Set(); var temp = n; var sum = 0; var count = 0; while (temp > 0) { // Calculate sum of // digits in n sum += temp % 10; // Store the digits digits.add(temp % 10); // Increase the count // of digits in n count++; temp = parseInt(temp / 10); } // If average of all digits // is an integer if (sum % count == 0 // If the average is a digit // in n && digits.has(sum / count)) document.write( "Yes" ); // Otherwise else document.write( "No" ); } // Driver Code var n = 42644; check(n); </script>
Producción:
Yes
Complejidad de tiempo: O (log 10 N)
Publicación traducida automáticamente
Artículo escrito por shobhitgupta907 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA