Algorithm to check if given array is palindrome.

Array which is a palindrome read from first to last is the same as read from last to first letter.

Example of a palindrome (after making all letters small, removing spaces and question mark): Was it a car or a cat I saw?

Algorithm:

IN: Array of chars
OUT: True - word is a palindrome or False - word is not a palindrome
1. Begin with i = 0, n = length of array.
2. Check if array[i] is not equal to array[n - 1 - i], if this is true end algorithm and return false.
3. Increase i by 1.
4. Check if i is less than n/2, if true go to step 2, if not end algoritm and return true.

Pseudo Code:

function isPalindrome(array):
  for(i = 0; i < n/2; i++):
    if(array[i] != array[n - 1 - i]):
      return false;

  return true;

Sample input:

IN:
wasitacaroracatisaw
hello
OUT:
true
false
Python Implementation C# Implementation C++ Implementation Java Implementation



Check If Array Is Palindrome – Algorithm
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close