Largest palindromic prime in an array
Given an array arr[] of integers, the task is to print the largest palindromic prime from the array. If no element from the array is a palindromic prime then print -1.
Examples:
Input: arr[] = {11, 5, 121, 7, 89}
Output: 11
11, 5 and 7 are the only primes from the array which are palindromes.
11 is the maximum among them.Input: arr[] = {2, 4, 6, 8, 10}
Output: 2
A simple approach is to go through every array element, check if it is prime and check if it is palindrome. If yes, the update the result if it is greater than current result also.
Efficient approach for large number of elements:
- Use Sieve of Eratosthenes to calculate whether a number is prime or not upto the maximum element from the array.
- Now, initialize a variable currentMax = -1 and start traversing the array arr[].
- For every i, if arr[i] is prime as well as palindrome and arr[i] > currentMax then update currentMax = arr[i].
- Print currentMax in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if n is a palindrome bool isPal(int n) { // Find the appropriate divisor // to extract the leading digit int divisor = 1; while (n / divisor >= 10) divisor *= 10; while (n != 0) { int leading = n / divisor; int trailing = n % 10; // If first and last digit // not same return false if (leading != trailing) return false; // Removing the leading and trailing // digit from number n = (n % divisor) / 10; // Reducing divisor by a factor // of 2 as 2 digits are dropped divisor = divisor / 100; } return true; } // Function to return the largest // palindromic prime present in the array int maxPalindromicPrime(int arr[], int n) { int maxElement = *max_element(arr, arr + n); // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // Not a prime, else true. bool prime[maxElement + 1]; memset(prime, true, sizeof(prime)); // 0 and 1 are not primes prime[0] = prime[1] = false; for (int p = 2; p * p <= maxElement; p++) { // If prime[p] is not changed, then it is // a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= maxElement; i += p) prime[i] = false; } } int currentMax = -1; for (int i = 0; i < n; i++) // If arr[i] is prime as well as palindrome if (prime[arr[i]] && isPal(arr[i])) currentMax = max(currentMax, arr[i]); return currentMax; } // Driver Program int main() { int arr[] = { 11, 5, 121, 7, 89 }; int n = sizeof(arr) / sizeof(arr[0]); cout << maxPalindromicPrime(arr, n); return 0; } |
Java
// Java implementation of the above approach import java.util.Arrays; public class GFG{ // Function that returns true if n is a palindrome static boolean isPal(int n) { // Find the appropriate divisor // to extract the leading digit int divisor = 1; while (n / divisor >= 10) divisor *= 10; while (n != 0) { int leading = n / divisor; int trailing = n % 10; // If first and last digit // not same return false if (leading != trailing) return false; // Removing the leading and trailing // digit from number n = (n % divisor) / 10; // Reducing divisor by a factor // of 2 as 2 digits are dropped divisor = divisor / 100; } return true; } // Function to return the largest // palindromic prime present in the array static int maxPalindromicPrime(int []arr, int n) { int maxElement = Arrays.stream(arr).max().getAsInt(); // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // Not a prime, else true. boolean []prime = new boolean[maxElement + 1]; for (int i = 0; i < maxElement + 1 ; i++) prime[i] = true ; // 0 and 1 are not primes prime[0] = prime[1] = false; for (int p = 2; p * p <= maxElement; p++) { // If prime[p] is not changed, then it is // a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= maxElement; i += p) prime[i] = false; } } int currentMax = -1; for (int i = 0; i < n; i++) // If arr[i] is prime as well as palindrome if (prime[arr[i]] == true && isPal(arr[i]) == true) currentMax = Math.max(currentMax, arr[i]); return currentMax; } // Driver Program public static void main(String []args) { int []arr = { 11, 5, 121, 7, 89 }; int n = arr.length ; System.out.println(maxPalindromicPrime(arr, n)) ; } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 implementation of the approach from math import sqrt # Function that returns true # if n is a palindrome def isPal(n): # Find the appropriate divisor # to extract the leading digit divisor = 1 while (n / divisor >= 10): divisor *= 10 while (n != 0): leading = int(n / divisor) trailing = n % 10 # If first and last digit # not same return false if (leading != trailing): return False # Removing the leading and trailing # digit from number n = int((n % divisor) / 10) # Reducing divisor by a factor # of 2 as 2 digits are dropped divisor = int(divisor / 100) return True # Function to return the largest # palindromic prime present in the array def maxPalindromicPrime(arr, n): maxElement = arr[0] for i in range(len(arr)): if (arr[i]>maxElement): maxElement = arr[i] # Create a boolean array "prime[0..n]" and # initialize all entries it as true. A value # in prime[i] will finally be false if i is # Not a prime, else true. prime = [True for i in range(maxElement + 1)] # 0 and 1 are not primes prime[0] = False prime[1] = False for p in range(2, int(sqrt(maxElement)) + 1, 1): # If prime[p] is not changed, # then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2,maxElement + 1, p): prime[i] = False currentMax = -1 for i in range(n): # If arr[i] is prime as well as palindrome if (prime[arr[i]] and isPal(arr[i])): currentMax = max(currentMax, arr[i]) return currentMax # Driver Code if __name__ == '__main__': arr = [11, 5, 121, 7, 89] n = len(arr) print(maxPalindromicPrime(arr, n)) # This code is contributed by # Shashank_Shamra |
C#
// C# implementation of the above approach using System ; using System.Linq ; public class GFG{ // Function that returns true if n is a palindrome static bool isPal(int n) { // Find the appropriate divisor // to extract the leading digit int divisor = 1; while (n / divisor >= 10) divisor *= 10; while (n != 0) { int leading = n / divisor; int trailing = n % 10; // If first and last digit // not same return false if (leading != trailing) return false; // Removing the leading and trailing // digit from number n = (n % divisor) / 10; // Reducing divisor by a factor // of 2 as 2 digits are dropped divisor = divisor / 100; } return true; } // Function to return the largest // palindromic prime present in the array static int maxPalindromicPrime(int []arr, int n) { int maxElement = arr.Max() ; // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // Not a prime, else true. bool []prime = new bool [maxElement + 1]; for (int i = 0; i < maxElement + 1 ; i++) prime[i] = true ; // 0 and 1 are not primes prime[0] = prime[1] = false; for (int p = 2; p * p <= maxElement; p++) { // If prime[p] is not changed, then it is // a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= maxElement; i += p) prime[i] = false; } } int currentMax = -1; for (int i = 0; i < n; i++) // If arr[i] is prime as well as palindrome if (prime[arr[i]] == true && isPal(arr[i]) == true) currentMax = Math.Max(currentMax, arr[i]); return currentMax; } // Driver Program public static void Main() { int []arr = { 11, 5, 121, 7, 89 }; int n = arr.Length ; Console.WriteLine(maxPalindromicPrime(arr, n)) ; } // This code is contributed by Ryuga } |
PHP
= 10)
$divisor *= 10;
while ($n != 0)
{
$leading = (int)($n / $divisor);
$trailing = $n % 10;
// If first and last digit
// not same return false
if ($leading != $trailing)
return false;
// Removing the leading and trailing
// digit from number
$n = (int)(($n % $divisor) / 10);
// Reducing divisor by a factor
// of 2 as 2 digits are dropped
$divisor = (int)($divisor / 100);
}
return true;
}
// Function to return the largest
// palindromic prime present in the array
function maxPalindromicPrime($arr, $n)
{
$maxElement = max($arr);
// Create a boolean array “prime[0..n]” and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// Not a prime, else true.
$prime = array_fill(0, ($maxElement + 1), true);
// 0 and 1 are not primes
$prime[0] = $prime[1] = false;
for ($p = 2; $p * $p <= $maxElement; $p++)
{
// If prime[p] is not changed,
// then it is a prime
if ($prime[$p] == true)
{
// Update all multiples of p
for ($i = $p * 2;
$i <= $maxElement; $i += $p)
$prime[$i] = false;
}
}
$currentMax = -1;
for ($i = 0; $i < $n; $i++)
// If arr[i] is prime as well as palindrome
if ($prime[$arr[$i]] && isPal($arr[$i]))
$currentMax = max($currentMax, $arr[$i]);
return $currentMax;
}
// Driver Code
$arr = array( 11, 5, 121, 7, 89 );
$n = count($arr);
echo maxPalindromicPrime($arr, $n);
// This code is contributed by mits
?>
11
Recommended Posts:
- Largest palindromic number in an array
- Sum and product of k smallest and k largest prime numbers in the array
- Count all Prime Length Palindromic Substrings
- Largest palindromic number by permuting digits
- Form the largest palindromic number using atmost two swaps
- Largest subset with sum of every pair as prime
- Largest number with prime digits
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Print prime numbers with prime sum of digits in an array
- Sum of largest prime factor of each number less than equal to n
- Check whether the sum of prime elements of the array is prime or not
- Find Largest Special Prime which is less than or equal to a given number
- Sum of largest divisible powers of p (a prime number) in a range
- Largest number not greater than N which can become prime after rearranging its digits
- Make palindromic string non-palindromic by rearranging its letters
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.



