Program to check if an Array is Palindrome or not
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 flag is set the print “Not Palindrome” else print “Palindrome”
Below is the implementation of above Approach:
C++
// C++ Program to check whether the // Array is palindromic 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; } |
chevron_right
filter_none
Java
// Java 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.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); } } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
Output:
Palindrome
Related Article: Program to check if an array is palindrome or not using Recursion
Recommended Posts:
- Program to check if an Array is Palindrome or not using STL in C++
- Program to check if an array is palindrome or not using Recursion
- Program to check the number is Palindrome or not
- C Program to Check if a Given String is Palindrome
- Bash program to check if the Number is a Palindrome
- Recursive program to check if number is palindrome or not
- Python program to check if a string is palindrome or not
- Java program to check whether a string is a Palindrome
- Check if all elements of the array are palindrome or not
- Java program to check palindrome (using library methods)
- Python program to check if given string is vowel Palindrome
- TCP Client-Server Program to Check if a Given String is Palindrome
- Program to check if an array is bitonic or not
- Python Program to check if given array is Monotonic
- Program to check if an array is sorted or not (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



