Program to check if an Array is Palindrome or not
Last Updated :
20 Jan, 2023
Given an array, the task is to determine whether an array is a palindrome or not.
Examples:
Input: arr[] = {3, 6, 0, 6, 3}
Output: Palindrome
Input: arr[] = {1, 2, 3, 4, 5}
Output: Not Palindrome
Approach:
- Initialise flag to unset int flag = 0.
- Loop the array till size n/2
- In a loop check if arr[i]! = arr[n-i-1] then set the flag = 1 and break
- After the loop has ended, If the flag is set to 1 then print "Not Palindrome" else print "Palindrome"
Below is the implementation of above Approach:
C++
// C++ Program to check whether the
// Array is palindrome or not
#include <iostream>
using namespace std;
void palindrome(int arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
cout << "Not Palindrome";
else
cout << "Palindrome";
}
// Driver code.
int main()
{
int arr[] = { 1, 2, 3, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
palindrome(arr, n);
return 0;
}
Java
// Java Program to check whether the
// Array is palindrome or not
class GfG {
static void palindrome(int arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
}
// Driver code.
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 2, 1 };
int n = arr.length;
palindrome(arr, n);
}
}
Python3
# Python3 Program to check whether the
# Array is palindromic or not
def palindrome(arr, n):
# Initialise flag to zero.
flag = 0;
# Loop till array size n/2.
i = 0;
while (i <= n // 2 and n != 0):
# Check if first and last element
# are different. Then set flag to 1.
if (arr[i] != arr[n - i - 1]):
flag = 1;
break;
i += 1;
# If flag is set then print Not Palindrome
# else print Palindrome.
if (flag == 1):
print("Not Palindrome");
else:
print("Palindrome");
# Driver code.
arr = [ 1, 2, 3, 2, 1 ];
n = len(arr);
palindrome(arr, n);
# This code is contributed
# by chandan_jnu
C#
// C# Program to check whether the
// Array is palindromic or not
class GfG
{
static void palindrome(int []arr, int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++)
{
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1])
{
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
System.Console.WriteLine("Not Palindrome");
else
System.Console.WriteLine("Palindrome");
}
// Driver code.
static void Main()
{
int []arr = { 1, 2, 3, 2, 1 };
int n = arr.Length;
palindrome(arr, n);
}
}
// This code is contributed by chadan_jnu
PHP
<?php
// PHP Program to check whether the
// Array is palindrome or not
function palindrome($arr, $n)
{
// Initialise flag to zero.
$flag = 0;
// Loop till array size n/2.
for ($i = 0; $i <= $n / 2 &&
$n != 0; $i++)
{
// Check if first and last element are
// different then set flag to 1.
if ($arr[$i] != $arr[$n - $i - 1])
{
$flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if ($flag == 1)
echo "Not Palindrome";
else
echo "Palindrome";
}
// Driver code.
$arr = array( 1, 2, 3, 2, 1 );
$n = count($arr);
palindrome($arr, $n);
// This code is contributed by chandan_jnu
?>
JavaScript
<script>
// Javascript Program to check whether the
// Array is palindrome or not
function palindrome(arr, n)
{
// Initialise flag to zero.
let flag = 0;
// Loop till array size n/2.
for (let i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
document.write("Not Palindrome");
else
document.write("Palindrome");
}
// Driver code.
let arr = [ 1, 2, 3, 2, 1 ];
let n = arr.length;
palindrome(arr, n);
// This code is contributed by Mayank Tyagi
</script>
Time complexity: O(n) where n is size of given array
Auxiliary space: O(1)
Related Article: Program to check if an array is palindrome or not using Recursion
Similar Reads
Program to check if an array is palindrome or not using Recursion 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 i
4 min read
Bash program to check if the Number is a Palindrome Given a number num, find whether the given number is palindrome or not using Bash Scripting. Examples: Input : 666 Output : Number is palindrome Input : 45667 Output : Number is NOT palindrome Approach To find the given number is palindrome just check if the number is same from beginning and the end
1 min read
Check if sum of any subarray is Palindrome or not Given an array arr[] of size N. the task is to check whether there exists any subarray of size atleast 2 such that its sum is palindrome. If such a subarray exists, then print YES. Otherwise, print NO.Examples: Input: arr[] = {10, 6, 7, 9, 12} Output: Yes Explanation: The subarray [6, 7, 9] with sum
8 min read
Program to check the number is Palindrome or not Given an integer N, write a program that returns true if the given number is a palindrome, else return false. Examples: Input: N = 2002 Output: trueInput: N = 1234Output: false Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. Approach: A simple method for this pro
10 min read
Check if any anagram of a string is palindrome or not Given an anagram string S of length N, the task is to check whether it can be made palindrome or not. Examples: Input : S = geeksforgeeks Output: NoExplanation: There is no palindrome anagram of given string Input: S = geeksgeeksOutput: YesExplanation: There are palindrome anagrams of given string.
8 min read