Dada una array arr[] de tamaño N , la tarea es imprimir el recuento de números no palindrómicos presentes en la array dada cuyo primer y último dígito es el mismo.
Ejemplos:
Entrada: arr[]={121, 134, 2342, 4514}
Salida: 2
Explicación: 2342 y 4514 son los números no palindrómicos que tienen el mismo primer y último dígito. Por lo tanto, la salida requerida es 2.Entrada: arr[]={1, 22, 4545}
Salida: 0
Enfoque: el problema se puede resolver comprobando cada elemento de la array, ya sea palíndromo o no. Siga los pasos para resolver el problema.
- Recorre la array arr[] .
- Para cada elemento de la array arr[i] , compruebe si es palíndromo o no.
- Para cada elemento de la array que no sea palindrómico, extraiga los últimos dígitos antes y después de invertir el número. Una vez extraído, comprueba si los dígitos son iguales o no.
- Si se determina que es cierto, aumente el conteo.
- Finalmente, imprima el conteo de dichos números.
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 reverse a number int revNum(int N) { // Store the reverse of N int x = 0; while (N) { x = x * 10 + N % 10; N = N / 10; } // Return reverse of N return x; } // Function to get the count of non-palindromic // numbers having same first and last digit int ctNonPalin(int arr[], int N) { // Store the required count int Res = 0; // Traverse the array for (int i = 0; i < N; i++) { // Store reverse of arr[i] int x = revNum(arr[i]); // Check for palindrome if (x == arr[i]) { continue; } // IF non-palindromic else { // Check if first and last // digits are equal Res += (arr[i] % 10 == N % 10); } } return Res; } // Driver Code int main() { int arr[] = { 121, 134, 2342, 4514 }; int N = sizeof(arr) / sizeof(arr[0]); cout << ctNonPalin(arr, N); }
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to reverse a number static int revNum(int N) { // Store the reverse of N int x = 0; while (N != 0) { x = x * 10 + N % 10; N = N / 10; } // Return reverse of N return x; } // Function to get the count of non-palindromic // numbers having same first and last digit static int ctNonPalin(int arr[], int N) { // Store the required count int Res = 0; // Traverse the array for(int i = 0; i < N; i++) { // Store reverse of arr[i] int x = revNum(arr[i]); // Check for palindrome if (x == arr[i]) { continue; } // IF non-palindromic else { // Check if first and last // digits are equal if(arr[i] % 10 == x % 10) Res += 1; } } return Res; } // Driver code public static void main (String[] args) { int arr[] = { 121, 134, 2342, 4514 }; int N = arr.length; System.out.println(ctNonPalin(arr, N)); } } // This code is contributed by jana_sayantan
Python3
# Python3 program to implement # the above approach # Function to reverse a number def revNum(N): # Store the reverse of N x = 0 while (N): x = x * 10 + N % 10 N = N // 10 # Return reverse of N return x # Function to get the count of non-palindromic # numbers having same first and last digit def ctNonPalin(arr, N): # Store the required count Res = 0 # Traverse the array for i in range(N): # Store reverse of arr[i] x = revNum(arr[i]) # Check for palindrome if (x == arr[i]): continue # IF non-palindromic else: # Check if first and last # digits are equal Res += (arr[i] % 10 == N % 10) return Res # Driver Code if __name__ == '__main__': arr = [ 121, 134, 2342, 4514 ] N = len(arr) print(ctNonPalin(arr, N)) # This code is contributed by mohit kumar 29
C#
// C# program to implement // the above approach using System; class GFG{ // Function to reverse a number static int revNum(int N) { // Store the reverse of N int x = 0; while (N != 0) { x = x * 10 + N % 10; N = N / 10; } // Return reverse of N return x; } // Function to get the count of non-palindromic // numbers having same first and last digit static int ctNonPalin(int[] arr, int N) { // Store the required count int Res = 0; // Traverse the array for(int i = 0; i < N; i++) { // Store reverse of arr[i] int x = revNum(arr[i]); // Check for palindrome if (x == arr[i]) { continue; } // IF non-palindromic else { // Check if first and last // digits are equal if(arr[i] % 10 == x % 10) Res += 1; } } return Res; } // Driver code public static void Main () { int[] arr = new int[]{ 121, 134, 2342, 4514 }; int N = arr.Length; Console.WriteLine(ctNonPalin(arr, N)); } } // This code is contributed by sanjoy_62
Javascript
<script> // JavaScript Program to implement // the above approach // Function to reverse a number function revNum(N) { // Store the reverse of N var x = 0; while (N) { x = x * 10 + (N % 10); N = N / 10; } // Return reverse of N return x; } // Function to get the count of non-palindromic // numbers having same first and last digit function ctNonPalin(arr, N) { // Store the required count var Res = 0; // Traverse the array for (var i = 0; i < N; i++) { // Store reverse of arr[i] var x = revNum(arr[i]); // Check for palindrome if (x == arr[i]) { continue; } // IF non-palindromic else { // Check if first and last // digits are equal Res += arr[i] % 10 == N % 10; } } return Res; } // Driver Code var arr = [121, 134, 2342, 4514]; var N = arr.length; document.write(ctNonPalin(arr, N)); </script>
2
Complejidad de tiempo: O(N * D) donde D es la longitud del número más grande de la array.
Espacio Auxiliar: O(D)
Publicación traducida automáticamente
Artículo escrito por kartiksrivastava6 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA