Program to check if an array is palindrome or not using Recursion
Last Updated :
05 Sep, 2022
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.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int palindrome( int arr[], int begin, int end)
{
if (begin >= end) {
return 1;
}
if (arr[begin] == arr[end]) {
return palindrome(arr, begin + 1, end - 1);
}
else {
return 0;
}
}
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
import java.io.*;
class GFG {
static int palindrome( int arr[], int begin, int end)
{
if (begin >= end) {
return 1 ;
}
if (arr[begin] == arr[end]) {
return palindrome(arr, begin + 1 , end - 1 );
}
else {
return 0 ;
}
}
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
def palindrome(arr, begin, end):
if (begin > = end) :
return 1
if (arr[begin] = = arr[end]) :
return palindrome(arr, begin + 1 ,
end - 1 )
else :
return 0
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" )
|
C#
using System;
class GFG
{
static int palindrome( int []arr,
int begin, int end)
{
if (begin >= end)
{
return 1;
}
if (arr[begin] == arr[end])
{
return palindrome(arr, begin + 1,
end - 1);
}
else
{
return 0;
}
}
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" );
}
}
|
PHP
<?php
function palindrome( $arr , $begin , $end )
{
if ( $begin >= $end )
{
return 1;
}
if ( $arr [ $begin ] == $arr [ $end ])
{
return palindrome( $arr , $begin + 1,
$end - 1);
}
else
{
return 0;
}
}
$a = array ( 3, 6, 0, 6, 3 );
$n = count ( $a );
if (palindrome( $a , 0, $n - 1) == 1)
echo "Palindrome" ;
else
echo "Not Palindrome" ;
?>
|
Javascript
<script>
function palindrome(arr, begin, end)
{
if (begin >= end) {
return 1;
}
if (arr[begin] == arr[end]) {
return palindrome(arr, begin + 1, end - 1);
}
else {
return 0;
}
}
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" );
</script>
|
Time complexity: O(N), where N is the size of the given array.
Auxiliary space: O(N), recursive stack space of size N will be required.
Share your thoughts in the comments
Please Login to comment...