Program to check if an Array is Palindrome or not using STL in C++
Given an array, the task is to determine whether an array is a palindrome or not, using STL in C++.
Examples:
Input: arr[] = {3, 6, 0, 6, 3}
Output: Palindrome
Input: arr[] = {1, 2, 3, 4, 5}
Output: Not Palindrome
Approach:
- Get the reverse of the Array using reverse() method, provided in STL.
- Initialise flag to unset int flag = 0.
- Loop the array till size n and check if the original array and the reversed array are same. If not set flag = 1
- After the loop has ended, If flag is set the print “Not Palindrome” else print “Palindrome”
Below is the implementation of above Approach:
// C++ program to check if an Array // is Palindrome or not using STL #include <bits/stdc++.h> using namespace std; void palindrome(int arr[], int n) { // Initialise flag to zero. int flag = 0; // Create another array // to store the original array int arr2[n]; memcpy(arr2, arr, n * sizeof(int)); // Reverse the array reverse(arr, arr + n); // Check if the array is Palindrome for (int i = 0; i < n; i++) if (arr[i] != arr2[i]) { flag = 1; break; } // Print the result if (flag == 0) cout << "Palindrome\n"; else cout << "Not Palindrome\n"; } int main() { // Get the array int arr[] = { 1, 2, 3, 2, 1 }; // Compute the size int n = sizeof(arr) / sizeof(arr[0]); palindrome(arr, n); return 0; } |
chevron_right
filter_none
Output:
Palindrome
Related Articles:
- https://www.geeksforgeeks.org/program-to-check-if-an-array-is-palindrome-or-not/
- https://www.geeksforgeeks.org/program-to-check-if-an-array-is-palindrome-or-not-using-recursion/
Recommended Posts:
- Program to check if an Array is Palindrome or not
- Program to check if an array is palindrome or not using Recursion
- C Program to Check if a Given String is Palindrome
- Program to check the number is Palindrome or not
- Recursive program to check if number is palindrome or not
- Java program to check whether a string is a Palindrome
- Bash program to check if the Number is a Palindrome
- Java program to check palindrome (using library methods)
- TCP Client-Server Program to Check if a Given String is Palindrome
- Python program to check if given string is vowel Palindrome
- Check if all elements of the array are palindrome or not
- Check if a number is Palindrome
- Check if a number is Palindrome in PL/SQL
- Check if a string is palindrome in C using pointers
- Check if a given string is a rotation of a palindrome
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.



