Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Find the Longest Palindrome in an Array

Last Updated on May 9, 2023 by Prepbytes

In programming, a palindrome is a sequence of characters or numbers that reads the same backward as forward. For example, "racecar" and "121" are palindromes, while "hello" and "123" are not.

What is Palindrome in an Array?

When it comes to arrays, a palindrome array is an array where the elements read the same from both ends. In other words, the first element is the same as the last element, the second element is the same as the second-to-last element, and so on.

For example, [1, 2, 3, 2, 1] is a palindrome array because the first element (1) is the same as the last element (1), the second element (2) is the same as the second-to-last element (2), and the third element (3) is the same as itself.

Palindrome arrays can be useful in programming for tasks such as checking if a string or number is a palindrome or identifying patterns in data.

There are different types of methods to find palindrome in an array.

Method 1 ( Using Naive Approach)

The steps to find the longest palindrome in an array are as follows:

  • Step 1: Make a function called ispalindrome(int n) that returns 1 if the integer it receives is a palindrome and 0 otherwise.
  • Step 2: Make a variable called res = INT_MIN that will retain the largest palindrome integer inside the main function.
  • Step 3: Starting with [0, n], create a loop, check if (ispalindrome(arr[i]) && res < arr[i]), and then change res to arr[i].
  • Step 4:Check if (res==INT_MIN) once the loop is finished, then change the res to -1.
  • Step 5: Give the res value in print.

Code Implementation

#include <stdio.h>
#include <limits.h>
int ispalindrome(int n){
     int rev=0, temp = n;

     while(temp>0){
          int rem = temp%10;
          rev = rev*10 + rem;
          temp /= 10;
     }

     if(n==rev)
        return 1;

     return 0;
}

int main(){
    int arr[] = {1, 121, 155551, 10111};
    int n = sizeof(arr)/sizeof(arr[0]);
    int res = INT_MIN;

    for(int i=0; i<n; i++){

          if(ispalindrome(arr[i]) && res<arr[i])
             res = arr[i];

    }

    if(res==INT_MIN)
       res = -1;

    printf("%d ",res);
}

Output

155551

Method 2 ( Find the Longest palindrome in an array using Sorting)

This method sorts the array first, then begins traversing it from the end and returns the element that meets the requirement with the same algorithm as discussed above and checks whether it is a palindrome.

Code Implementation

#include <stdio.h>
#include <limits.h>
int ispalindrome(int n){
    int rev=0, temp = n;

    while(temp>0){
      int rem = temp%10;
      rev = rev*10 + rem;
      temp /= 10;
    }

    if(n==rev)
       return 1;

    return 0;

}

int main(){
    int arr[] = {1, 121, 155551, 57390};
    int n = sizeof(arr)/sizeof(arr[0]);
    int j=n;

    for(int i=0; i<arr[j];i++){ 
int temp = arr[i]; 
arr[i] = arr[j]; 
arr[j] = temp; 
} 
int res = INT_MIN; 
for(int i=n-1; i>=0; i--){

     if(ispalindrome(arr[i])){
           res = arr[i];
           break;
       }

    }

    if(res==INT_MIN)
      res = -1;

    printf("%d ",res);
}

Output

155551

Conclusion
So finding a palindrome in an array is a common programming problem. We have discussed the naive approach to find the longest palindrome in an array followed by an optimized approach with code implementation.
Regardless of the approach used, checking if an array is a palindrome is an essential problem to understand for a programmer, as it can be applied to various real-world scenarios, such as checking if a string is a palindrome or identifying patterns in data.

Frequently Asked Questions

Q1. What algorithm is employed to determine the lengthiest palindrome?
Ans. To locate the longest palindrome substring in a given text, use Manacher’s Algorithm.

Q2. What are C’s two recursion types?
Ans. Here are two examples of C-language recursion. Direct and indirect recursion is the first. When a function calls itself directly from within, this is known as direct recursion in C. When a function calls another function, and the second function then calls the original function once again, this is known as indirect recursion.

Q3. What is C loop versus recursion?
Ans. While looping is used at the instruction level, recursion operates at the method or function level. Iteration is the process of continually carrying out a series of commands up until the termination condition is satisfied. An infinite loop will result if the termination condition is not satisfied.

Leave a Reply

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