Dados los números enteros N y K , la tarea es comprobar si un número se puede representar como la suma de números que tienen al menos un dígito igual a K.
Ejemplo:
Entrada: N = 68, K = 7
Salida: SI
Explicación: 68 = (27 + 17 + 17 + 7). Cada número tiene al menos un dígito igual a 7.Entrada: N = 23, K = 3
Salida: SÍ
Explicación: 23 en sí mismo contiene un dígito igual a 3.
Enfoque: El problema dado se puede resolver usando conceptos matemáticos simples . Siga los pasos a continuación para resolver el problema:
- Inicialice una variable temp a k , y también tome un contador, digamos contar , asígnelo con 0
- Iterar hasta que los últimos dígitos de temp y N no sean iguales y en cada iteración
- Incrementa el valor de temp por k
- Mantenga un conteo de iteraciones y rompa el ciclo si el conteo es mayor a 10
- Compruebe si los últimos dígitos de temp y N son iguales, y si el valor de temp <= N :
- Si se cumple la condición anterior, devuelve verdadero
- De lo contrario, devuelve falso
- También si k * 10 <= N , devuelve verdadero
- De lo contrario, devuelve falso
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to Check if a number can // be equal to sum of numbers having // at least one digit equal to k bool checkEqualtoSum(int N, int k) { // Temporary variable to // store k int temp = k; // Variable for count int count = 0; // Iterating till count is less or // equal to 10 and N % 10 is not // equal to temp % 10 while(count <= 10 && N % 10 != temp % 10) { temp += k; count++; } // If N % 10 is equal to temp % 10 // and temp is less or equal to N, // return true if(N % 10 == temp % 10 && temp <= N) return true; // If k * 10 <= N, return true if(k * 10 <= N) return true; // Else return false return false; } // Driver Code int main() { int N = 68; int K = 7; // Call the function if(checkEqualtoSum(N, K)) cout << "YES"; else cout << "NO"; return 0; }
Java
// Java program for the above approach import java.io.*; class GFG { // Function to Check if a number can // be equal to sum of numbers having // at least one digit equal to k static boolean checkEqualtoSum(int N, int k) { // Temporary variable to // store k int temp = k; // Variable for count int count = 0; // Iterating till count is less or // equal to 10 and N % 10 is not // equal to temp % 10 while (count <= 10 && N % 10 != temp % 10) { temp += k; count++; } // If N % 10 is equal to temp % 10 // and temp is less or equal to N, // return true if (N % 10 == temp % 10 && temp <= N) return true; // If k * 10 <= N, return true if (k * 10 <= N) return true; // Else return false return false; } // Driver Code public static void main(String[] args) { // Given Input int N = 68; int K = 7; // Call the function if (checkEqualtoSum(N, K)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by dwivediyash
Python3
# python implementation for the above approach # Function to Check if a number can # be equal to sum of numbers having # at least one digit equal to k def checkEqualtoSum(N, k): # Temporary variable to # store k temp = k # Variable for count count = 0 # Iterating till count is less or # equal to 10 and N % 10 is not # equal to temp % 10 while(count <= 10 and N % 10 != temp % 10): temp += k count += 1 # If N % 10 is equal to temp % 10 # and temp is less or equal to N, # return true if(N % 10 == temp % 10 and temp <= N): return True # If k * 10 <= N, return true if(k * 10 <= N): return True # Else return false return False # Driver Code if __name__ == "__main__": N = 68 K = 7 # Call the function if(checkEqualtoSum(N, K)): print("YES") else: print("NO") # This code is contributed by rakeshsahni
Javascript
<script> // JavaScript implementation for the above approach // Function to Check if a number can // be equal to sum of numbers having // at least one digit equal to k const checkEqualtoSum = (N, k) => { // Temporary variable to // store k let temp = k; // Variable for count let count = 0; // Iterating till count is less or // equal to 10 and N % 10 is not // equal to temp % 10 while (count <= 10 && N % 10 != temp % 10) { temp += k; count++; } // If N % 10 is equal to temp % 10 // and temp is less or equal to N, // return true if (N % 10 == temp % 10 && temp <= N) return true; // If k * 10 <= N, return true if (k * 10 <= N) return true; // Else return false return false; } // Driver Code let N = 68; let K = 7; // Call the function if (checkEqualtoSum(N, K)) document.write("YES"); else document.write("NO"); // This code is contributed by rakeshsahni </script>
C#
// C# implementation for the above approach using System; class gFG { // Function to Check if a number can // be equal to sum of numbers having // at least one digit equal to k static bool checkEqualtoSum(int N, int k) { // Temporary variable to // store k int temp = k; // Variable for count int count = 0; // Iterating till count is less or // equal to 10 and N % 10 is not // equal to temp % 10 while (count <= 10 && N % 10 != temp % 10) { temp += k; count++; } // If N % 10 is equal to temp % 10 // and temp is less or equal to N, // return true if (N % 10 == temp % 10 && temp <= N) return true; // If k * 10 <= N, return true if (k * 10 <= N) return true; // Else return false return false; } // Driver Code public static void Main() { int N = 68; int K = 7; // Call the function if (checkEqualtoSum(N, K)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed by ukasp.
Producción
YES
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)