Dado un entero X , la tarea es contar el número total de números de tres dígitos que tienen diferencia X con su reverso . Si no existe tal número, imprima -1.
Ejemplos:
Entrada: X = 792
Salida: 10
Explicación:
901 – 109 = 792
911 – 119 = 792
921 – 129 = 792
931 – 139 = 792
941 – 149 = 792
951 – 159 = 792
961 – 169 = 792
971 – 7 9279
981 – 189 = 792
991 – 199 = 792Entrada: X = 0
Salida: 90
Enfoque: El problema dado se puede resolver con base en las siguientes observaciones:
Sea N = rpq
Por lo tanto, N = 100r + 10q + p
Por lo tanto, al revés de N = 100p + 10q + r
Por lo tanto, el problema se reduce a resolver (100r + 10q + p) – (r + 10q + 100p) = X
-> 99(r – p) = X
-> r – p = X / 99
Por lo tanto, si X dado es un múltiplo de 99, entonces la solución existe.
Siga los pasos a continuación para resolver el problema basado en las observaciones anteriores:
- Comprueba si X es múltiplo de 99 o no. Si no es cierto, imprima -1 ya que no existe solución.
- De lo contrario, calcule X / 99 . Genere todos los pares usando los dígitos [1, 9] y para cada par, verifique si su diferencia es igual a X / 99 o no.
- Si se determina que es cierto para cualquier par, aumente la cuenta en 10, ya que el dígito del medio se puede permutar para colocar cualquier valor del rango [0, 9] para el par obtenido.
- Finalmente, imprime el valor del conteo obtenido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to count three-digit // numbers having difference x // with its reverse int Count_Number(int x) { int ans = 0; // if x is not multiple of 99 if (x % 99 != 0) { // No solution exists ans = -1; } else { int diff = x / 99; // Generate all possible pairs // of digits [1, 9] for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { // If any pair is obtained // with difference x / 99 if ((i - j) == diff) { // Increase count ans += 10; } } } } // Return the count return ans; } // Driver Code int main() { int x = 792; cout << Count_Number(x) << endl; return 0; }
Java
// Java program to implement // the above approach import java.io.*; import java.util.Arrays; class GFG{ // Function to count three-digit // numbers having difference x // with its reverse static int Count_Number(int x) { int ans = 0; // If x is not multiple of 99 if (x % 99 != 0) { // No solution exists ans = -1; } else { int diff = x / 99; // Generate all possible pairs // of digits [1, 9] for(int i = 1; i < 10; i++) { for(int j = 1; j < 10; j++) { // If any pair is obtained // with difference x / 99 if ((i - j) == diff) { // Increase count ans += 10; } } } } // Return the count return ans; } // Driver Code public static void main (String[] args) { int x = 792; System.out.println(Count_Number(x)); } } // This code is contributed by sanjoy_62
Python3
# Python3 program to implement # the above approach # Function to count three-digit # numbers having difference x # with its reverse def Count_Number(x): ans = 0; # If x is not multiple # of 99 if (x % 99 != 0): # No solution exists ans = -1; else: diff = x / 99; # Generate all possible pairs # of digits [1, 9] for i in range(1, 10): for j in range(1, 10): # If any pair is obtained # with difference x / 99 if ((i - j) == diff): # Increase count ans += 10; # Return the count return ans; # Driver Code if __name__ == '__main__': x = 792; print(Count_Number(x)); # This code is contributed by shikhasingrajput
C#
// C# program to implement // the above approach using System; class GFG{ // Function to count three-digit // numbers having difference x // with its reverse static int Count_Number(int x) { int ans = 0; // If x is not multiple of 99 if (x % 99 != 0) { // No solution exists ans = -1; } else { int diff = x / 99; // Generate all possible pairs // of digits [1, 9] for(int i = 1; i < 10; i++) { for(int j = 1; j < 10; j++) { // If any pair is obtained // with difference x / 99 if ((i - j) == diff) { // Increase count ans += 10; } } } } // Return the count return ans; } // Driver Code public static void Main() { int x = 792; Console.WriteLine(Count_Number(x)); } } // This code is contributed by code_hunt
Javascript
<script> // Javascript program to implement // the above approach // Function to count three-digit // numbers having difference x // with its reverse function Count_Number(x) { let ans = 0; // If x is not multiple of 99 if (x % 99 != 0) { // No solution exists ans = -1; } else { let diff = x / 99; // Generate all possible pairs // of digits [1, 9] for(let i = 1; i < 10; i++) { for(let j = 1; j < 10; j++) { // If any pair is obtained // with difference x / 99 if ((i - j) == diff) { // Increase count ans += 10; } } } } // Return the count return ans; } // Driver code let x = 792; document.write(Count_Number(x)); // This code is contributed by splevel62 </script>
10
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)