Dado un número entero N , la tarea es comprobar si
( N en base B ) es palíndromo o no.
Ejemplos:
Entrada: N = 5, B = 2
Salida: Sí
Explicación:
(5) 10 = (101) 2 que es palíndromo. Por lo tanto, la salida requerida es Sí.
Entrada: N = 4, B = 2
Salida: No
Enfoque: El problema se puede resolver comprobando si el valor decimal del reverso de
es igual a N o no. Siga los pasos a continuación para resolver el problema.
- Inicialice la variable, rev = 0 para almacenar el reverso de N .
- Extraiga los dígitos de
- por N % B .
- Por cada dígito de
- Actualizar rev= rev * B + N % B
- Finalmente, verifique si N es igual a rev o no
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 check if N in // base B is palindrome or not int checkPalindromeB(int N, int B) { // Stores the reverse of N int rev = 0; // Stores the value of N int N1 = N; // Extract all the digits of N while (N1) { // Generate its reverse rev = rev * B + N1 % B; N1 = N1 / B; } return N == rev; } // Driver Code int main() { int N = 5, B = 2; if (checkPalindromeB(N, B)) { cout << "Yes"; } else { cout << "No"; } }
Java
// Java program to implement // the above approach class GFG{ // Function to check if N in // base B is palindrome or not static boolean checkPalindromeB(int N, int B) { // Stores the reverse of N int rev = 0; // Stores the value of N int N1 = N; // Extract all the digits of N while (N1 > 0) { // Generate its reverse rev = rev * B + N1 % B; N1 = N1 / B; } return N == rev; } // Driver code public static void main(String[] args) { int N = 5, B = 2; if (checkPalindromeB(N, B)) { System.out.print("Yes"); } else { System.out.print("No"); } } } // This code is contributed by Dewanti
Python3
# Python3 program to implement # the above approach # Function to check if N in # base B is palindrome or not def checkPalindromeB(N, B): # Stores the reverse of N rev = 0; # Stores the value of N N1 = N; # Extract all the digits of N while (N1 > 0): # Generate its reverse rev = rev * B + N1 % B; N1 = N1 // B; return N == rev; # Driver code if __name__ == '__main__': N = 5; B = 2; if (checkPalindromeB(N, B)): print("Yes"); else: print("No"); # This code is contributed by Princi Singh
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check if N in // base B is palindrome or not static bool checkPalindromeB(int N, int B) { // Stores the reverse of N int rev = 0; // Stores the value of N int N1 = N; // Extract all the digits of N while (N1 > 0) { // Generate its reverse rev = rev * B + N1 % B; N1 = N1 / B; } return N == rev; } // Driver code public static void Main(String[] args) { int N = 5, B = 2; if (checkPalindromeB(N, B)) { Console.Write("Yes"); } else { Console.Write("No"); } } } // This code is contributed by Amit Katiyar
Javascript
<script> // Javascript program to implement // the above approach // Function to check if N in // base B is palindrome or not function checkPalindromeB(N, B) { // Stores the reverse of N var rev = 0; // Stores the value of N var N1 = N; // Extract all the digits of N while (N1) { // Generate its reverse rev = rev * B + N1 % B; N1 = parseInt(N1 / B); } return N == rev; } // Driver Code var N = 5, B = 2; if (checkPalindromeB(N, B)) { document.write("Yes"); } else { document.write("No"); } </script>
Producción:
Yes
Complejidad temporal: O(log B N)
Espacio auxiliar: O(1)