Dada una array. La tarea es determinar si una array es un palíndromo o no usa recursividad.
Ejemplos:
Input: arr[] = {3, 6, 0, 6, 3} Output: Palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Not Palindrome
Acercarse:
- Caso base: si la array tiene solo un elemento, es decir, comenzar == finalizar, devuelve 1, también si comienza> final, lo que significa que la array es palíndromo, también devuelve 1.
- Si el primero y el último elemento son iguales, vuelva a llamar recursivamente a la función, pero el incremento comienza en 1 y el decremento final en 1.
- Si el primer y el último elemento no son iguales, devuelve 0.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <iostream> using namespace std; // Recursive function that returns 1 if // palindrome, 0 if not palindrome int palindrome(int arr[], int begin, int end) { // base case if (begin >= end) { return 1; } if (arr[begin] == arr[end]) { return palindrome(arr, begin + 1, end - 1); } else { return 0; } } // Driver code int main() { int a[] = { 3, 6, 0, 6, 3 }; int n = sizeof(a) / sizeof(a[0]); if (palindrome(a, 0, n - 1) == 1) cout << "Palindrome"; else cout << "Not Palindrome"; return 0; }
Java
// Java implementation of above approach import java.io.*; class GFG { // Recursive function that returns 1 if // palindrome, 0 if not palindrome static int palindrome(int arr[], int begin, int end) { // base case if (begin >= end) { return 1; } if (arr[begin] == arr[end]) { return palindrome(arr, begin + 1, end - 1); } else { return 0; } } // Driver code public static void main (String[] args) { int a[] = { 3, 6, 0, 6, 3 }; int n = a.length; if (palindrome(a, 0, n - 1) == 1) System.out.print( "Palindrome"); else System.out.println( "Not Palindrome"); } }
Python 3
# Python 3 implementation of above approach # Recursive function that returns 1 if # palindrome, 0 if not palindrome def palindrome(arr, begin, end): # base case if (begin >= end) : return 1 if (arr[begin] == arr[end]) : return palindrome(arr, begin + 1, end - 1) else : return 0 # Driver code if __name__ == "__main__": a = [ 3, 6, 0, 6, 3 ] n = len(a) if (palindrome(a, 0, n - 1) == 1): print("Palindrome") else: print("Not Palindrome") # This code is contributed # by ChitraNayal
C#
// C# implementation of above approach using System; class GFG { // Recursive function that returns 1 // if palindrome, 0 if not palindrome static int palindrome(int []arr, int begin, int end) { // base case if (begin >= end) { return 1; } if (arr[begin] == arr[end]) { return palindrome(arr, begin + 1, end - 1); } else { return 0; } } // Driver code public static void Main () { int []a = { 3, 6, 0, 6, 3 }; int n = a.Length; if (palindrome(a, 0, n - 1) == 1) Console.WriteLine("Palindrome"); else Console.WriteLine("Not Palindrome"); } } // This code is contributed by inder_verma
PHP
<?php // PHP implementation of above approach // Recursive function that returns 1 // if palindrome, 0 if not palindrome function palindrome($arr, $begin, $end) { // base case if ($begin >= $end) { return 1; } if ($arr[$begin] == $arr[$end]) { return palindrome($arr, $begin + 1, $end - 1); } else { return 0; } } // Driver code $a = array( 3, 6, 0, 6, 3 ); $n = count($a); if (palindrome($a, 0, $n - 1) == 1) echo "Palindrome"; else echo "Not Palindrome"; // This code is contributed // by inder_verma ?>
Javascript
<script> // Javascript implementation of above approach // Recursive function that returns 1 if // palindrome, 0 if not palindrome function palindrome(arr, begin, end) { // base case if (begin >= end) { return 1; } if (arr[begin] == arr[end]) { return palindrome(arr, begin + 1, end - 1); } else { return 0; } } // Driver code let a = [ 3, 6, 0, 6, 3 ]; let n = a.length; if (palindrome(a, 0, n - 1) == 1) document.write("Palindrome"); else document.write("Not Palindrome"); // This code is contributed by divyeshrabadiya07. </script>
Producción:
Palindrome