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
Use two pointers; One from the beginning and the other from the end until they cross.
See if these values match
if not return false
Increment the first pointer and decrement the second pointer
return true
bool isPalindrome(const vector<int> &arr) { int p1 = 0, p2 = arr.size() - 1; while(p1 < p2) if(arr[p1++] != arr[p2--]) return false; return true; }
Time Complexity: O(n)O(n)O(n)
Space Complexity: O(1)O(1)O(1)
Last updated 3 years ago