Given an array, the task is to determine whether an array is a palindrome or not.
Examples:Â
Input: arr[] = [1, 2, 3, 2, 1]
Output: true
Explanation: Elements match from both ends,3==3,6==6, middle0,all match.
Input: arr[] = [1, 2, 3, 4, 5]
Output: false
Explanation: First and last elements differ,1 != 5, so it is not a palindrome.
Try It Yourself
Table of Content
Two Pointer Technique - O(n) Time and O(1) Space
Traverse the array from index
0ton/2, comparing each elementarr[i]with its corresponding element from the endarr[n - i - 1].If any pair does not match, the array is not a palindrome; otherwise, it is a palindrome.
#include <iostream>
#include <vector>
using namespace std;
bool isPalindrome(vector<int> &arr) {
int n = arr.size();
// Traverse till middle
for (int i = 0; i <= n / 2; i++) {
// Check for mismatch
if (arr[i]!= arr[n - i - 1]) {
return false;
}
}
// If all elements match
return true;
}
// Driver code
int main() {
vector<int> arr = {1, 2, 3, 2, 1};
cout << boolalpha << isPalindrome(arr);
return 0;
}
public class GFG {
public static boolean isPalindrome(int[] arr) {
int n = arr.length;
// Traverse till middle
for (int i = 0; i <= n / 2; i++) {
// Check for mismatch
if (arr[i] != arr[n - i - 1]) {
return false;
}
}
// If all elements match
return true;
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 1};
System.out.println(isPalindrome(arr)); // Output: true
}
}
def isPalindrome(arr):
n = len(arr)
# Traverse till middle
for i in range(n // 2):
# Check for mismatch
if arr[i] != arr[n - i - 1]:
return False
# If all elements match
return True
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 2, 1]
print(isPalindrome(arr))
using System;
class GFG {
static bool isPalindrome(int[] arr) {
int n = arr.Length;
// Traverse till middle
for (int i = 0; i <= n / 2; i++) {
// Check for mismatch
if (arr[i] != arr[n - i - 1]) {
return false;
}
}
// If all elements match
return true;
}
static void Main() {
int[] arr = { 1, 2, 3, 2, 1 };
Console.WriteLine(isPalindrome(arr).ToString().ToLower()); // Output: true
}
}
function isPalindrome(arr)
{
let n = arr.length;
// Traverse till middle
for (let i = 0; i <= Math.floor(n / 2); i++) {
// Check for mismatch
if (arr[i] !== arr[n - i - 1]) {
return false;
}
}
// If all elements match
return true;
}
// Driver code
let arr = [ 1, 2, 3, 2, 1 ];
console.log(isPalindrome(arr));
<?php
function isPalindrome($arr) {
$n = count($arr);
// Traverse till middle
for ($i = 0; $i <= intdiv($n, 2); $i++) {
// Check for mismatch
if ($arr[$i]!= $arr[$n - $i - 1]) {
return false;
}
}
// If all elements match
return true;
}
$arr = [1, 2, 3, 2, 1];
if (isPalindrome($arr)) {
echo "true";
} else {
echo "false";
}
?>
Output
true
Using Recursion - O(n) Time and O(n) SpaceÂ
First compares the first and last elements,
- If do not match, return false.
- Else recursively check the remaining subarray by moving inward by
begin + 1,end - 1.If all pairs match, the array is a palindrome.
#include <iostream>
#include <vector>
using namespace std;
bool checkPalindrome(vector<int> &arr, int begin, int end)
{
// Base case
if (begin >= end)
return true;
// If mismatch found
if (arr[begin] != arr[end])
return false;
// Recursive call
return checkPalindrome(arr, begin + 1, end - 1);
}
bool isPalindrome(vector<int> &arr)
{
return checkPalindrome(arr, 0, arr.size() - 1);
}
// Driver code
int main()
{
vector<int> arr = {1, 2, 3, 2, 1};
cout << boolalpha << isPalindrome(arr);
return 0;
}
public class Main {
static boolean checkPalindrome(int[] arr, int begin, int end) {
// Base case
if (begin >= end)
return true;
// If mismatch found
if (arr[begin] != arr[end])
return false;
// Recursive call
return checkPalindrome(arr, begin + 1, end - 1);
}
static boolean isPalindrome(int[] arr) {
return checkPalindrome(arr, 0, arr.length - 1);
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 1};
System.out.println(isPalindrome(arr)); // Output: true
}
}
def checkPalindrome(arr, begin, end):
# Base case
if begin >= end:
return True
# If mismatch found
if arr[begin] != arr[end]:
return False
# Recursive call
return checkPalindrome(arr, begin + 1, end - 1)
def isPalindrome(arr):
return checkPalindrome(arr, 0, len(arr) - 1)
if __name__ == '__main__':
arr = [1, 2, 3, 2, 1]
print(isPalindrome(arr))
using System;
class GFG {
static bool checkPalindrome(int[] arr, int begin, int end) {
// Base case
if (begin >= end)
return true;
// If mismatch found
if (arr[begin] != arr[end])
return false;
// Recursive call
return checkPalindrome(arr, begin + 1, end - 1);
}
static bool isPalindrome(int[] arr) {
return checkPalindrome(arr, 0, arr.Length - 1);
}
// Driver code
static void Main() {
int[] arr = { 1, 2, 3, 2, 1 };
Console.WriteLine(isPalindrome(arr).ToString().ToLower()); // Output: true
}
}
function checkPalindrome(arr, begin, end)
{
// Base case
if (begin >= end)
return true;
// If mismatch found
if (arr[begin] !== arr[end])
return false;
// Recursive call
return checkPalindrome(arr, begin + 1, end - 1);
}
function isPalindrome(arr)
{
return checkPalindrome(arr, 0, arr.length - 1);
}
// Driver code
const arr = [ 1, 2, 3, 2, 1 ];
console.log(isPalindrome(arr));
Output
true