Program to check if an array is palindrome or not using Recursion - GeeksforGeeks

01 Apr, 2021
Given an array. The task is to determine whether an array is a palindrome or not using recursion.Examples:  
Input: arr[] = {3, 6, 0, 6, 3}
Output: Palindrome

Input: arr[] = {1, 2, 3, 4, 5}
Output: Not Palindrome
 
Approach:  
Base case: If array has only one element i.e. begin == end then return 1, also if begin>end which means the array is palindrome then also return 1.If the first and the last elements are equal then recursively call the function again but increment begin by 1 and decrement end by 1.If the first and last element is not equal then return 0.
Base case: If array has only one element i.e. begin == end then return 1, also if begin>end which means the array is palindrome then also return 1.
If the first and the last elements are equal then recursively call the function again but increment begin by 1 and decrement end by 1.
If the first and last element is not equal then return 0.
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of above approach#include <iostream>using namespace std; // Recursive function that returns 1 if// palindrome, 0 if not palindromeint 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 codeint 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 implementation of above approach import java.io.*; class GFG { // Recursive function that returns 1 if// palindrome, 0 if not palindromestatic 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 implementation of above approach # Recursive function that returns 1 if# palindrome, 0 if not palindromedef 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 codeif __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# implementation of above approachusing System; class GFG{ // Recursive function that returns 1// if palindrome, 0 if not palindromestatic 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 codepublic 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 implementation of above approach // Recursive function that returns 1// if palindrome, 0 if not palindromefunction 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?>
<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>
Palindrome
 
Shashank12
inderDuMCA
ukasp
divyeshrabadiya07
palindrome
Arrays
Recursion
Arrays
Recursion
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction