12 Check If an Array is a Palindrome

chevron-rightQuestionhashtag

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

Two Pointer

chevron-rightLogichashtag
  1. Use two pointers; One from the beginning and the other from the end until they cross.

    1. See if these values match

      1. if not return false

    2. Increment the first pointer and decrement the second pointer

  2. return true

chevron-rightCodehashtag
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;
}
chevron-rightComplexityhashtag

Time Complexity: O(n)O(n)

Space Complexity: O(1)O(1)

Last updated