Given an array Q[] consisting of N integers, the task for each element of the array Q[] is to check whether any of the numbers, formed by concatenating the first and the last digits of Q[i] is a prime number or not.
Examples:
Input: Q[] = {30, 66}
Output:
True
False
Explanation:
Q[0]: Possible combinations are 3 and 30. Since 3 is a prime number, the output is True.
Q[1]: Only possible combination is 66, which is not a prime number. Hence, the output is False.
Input: Q[] = {2127, 13}
Output:
False
True
Explanation:
Q[0]: Possible combinations are 27 and 72. Since none of them is a prime number, the output is False.
Q[1]: Possible combinations are 13 and 31. Since both of them are prime numbers, the output is True.
Approach: The problem can be solved efficiently using the Sieve of Eratosthenes. Follow the steps below to solve the given problem:
- Since the highest number possible by combining a pair of digits is 99, pre-compute and store all prime numbers up to 99 using Sieve of Eratosthenes and store it in a boolean array, say prime[], where prime[i] = 0 (non-prime) and 1 (prime).
- Traverse the array Q[], and perform the following steps:
- Extract the last digit of Q[i] by performing Q[i] % 10 and store it in a variable, say last.
- Extract the first digit of Q[i] by continuously dividing Q[i] by 10 until Q[i] reduces to less than 10 and store it in a variable, say first.
- Now, generate the two possible combinations:
- first * 10 + last.
- last * 10 + first.
- For each of the above two combinations, check if any of them is a prime number or not.
- If any of the numbers formed are found to be prime then print True, otherwise False.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int sieve[105];
void buildSieve()
{
for ( int i = 2; i < 100; i++)
sieve[i] = 1;
for ( int i = 2; i < 100; i++) {
if (sieve[i] == 1) {
for ( int j = i * i; j < 100; j += i)
sieve[j] = 0;
}
}
}
bool isAnyPrime( int first, int last)
{
int num1 = first * 10 + last;
int num2 = last * 10 + first;
if (sieve[num1] == 1 || sieve[num2] == 1)
return true ;
else
return false ;
}
void performQueries(vector< int > q)
{
for ( int i = 0; i < q.size(); i++) {
int A = q[i];
int last = A % 10;
int first;
while (A >= 10)
A = A / 10;
first = A;
if (isAnyPrime(first, last))
cout << "True\n" ;
else
cout << "False\n" ;
}
}
int main()
{
vector< int > q = { 30, 66 };
buildSieve();
performQueries(q);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int [] sieve = new int [ 105 ];
static void buildSieve()
{
for ( int i = 2 ; i < 100 ; i++)
sieve[i] = 1 ;
for ( int i = 2 ; i < 100 ; i++) {
if (sieve[i] == 1 ) {
for ( int j = i * i; j < 100 ; j += i)
sieve[j] = 0 ;
}
}
}
static boolean isAnyPrime( int first, int last)
{
int num1 = first * 10 + last;
int num2 = last * 10 + first;
if (sieve[num1] == 1 || sieve[num2] == 1 )
return true ;
else
return false ;
}
static void performQueries( int [] q)
{
for ( int i = 0 ; i < q.length; i++) {
int A = q[i];
int last = A % 10 ;
int first;
while (A >= 10 )
A = A / 10 ;
first = A;
if (isAnyPrime(first, last))
System.out.println( "True\n" );
else
System.out.print( "False\n" );
}
}
public static void main(String[] args)
{
int [] q = { 30 , 66 };
buildSieve();
performQueries(q);
}
}
|
Python3
sieve = [ 0 for i in range ( 105 )]
def buildSieve():
global sieve
for i in range ( 2 , 100 ):
sieve[i] = 1
for i in range ( 2 , 100 ):
if (sieve[i] = = 1 ):
for j in range ( i * i, 100 , i):
sieve[j] = 0
def isAnyPrime(first, last):
global sieve
num1 = first * 10 + last
num2 = last * 10 + first
if (sieve[num1] = = 1 or sieve[num2] = = 1 ):
return True
else :
return False
def performQueries(q):
for i in range ( len (q)):
A = q[i]
last = A % 10
first = 0
while (A > = 10 ):
A = A / / 10
first = A
if (isAnyPrime(first, last)):
print ( "True" )
else :
print ( "False" )
if __name__ = = '__main__' :
q = [ 30 , 66 ]
buildSieve()
performQueries(q)
|
C#
using System;
public class GFG
{
static int [] sieve = new int [105];
static void buildSieve()
{
for ( int i = 2; i < 100; i++)
sieve[i] = 1;
for ( int i = 2; i < 100; i++)
{
if (sieve[i] == 1)
{
for ( int j = i * i; j < 100; j += i)
sieve[j] = 0;
}
}
}
static bool isAnyPrime( int first, int last)
{
int num1 = first * 10 + last;
int num2 = last * 10 + first;
if (sieve[num1] == 1 || sieve[num2] == 1)
return true ;
else
return false ;
}
static void performQueries( int [] q)
{
for ( int i = 0; i < q.Length; i++) {
int A = q[i];
int last = A % 10;
int first;
while (A >= 10)
A = A / 10;
first = A;
if (isAnyPrime(first, last))
Console.Write( "True\n" );
else
Console.Write( "False\n" );
}
}
public static void Main(String[] args)
{
int [] q = { 30, 66 };
buildSieve();
performQueries(q);
}
}
|
Javascript
<script>
var sieve = Array.from({length: 105}, (_, i) => 0);
function buildSieve()
{
for (i = 2; i < 100; i++)
sieve[i] = 1;
for (i = 2; i < 100; i++) {
if (sieve[i] == 1) {
for (j = i * i; j < 100; j += i)
sieve[j] = 0;
}
}
}
function isAnyPrime(first , last)
{
var num1 = first * 10 + last;
var num2 = last * 10 + first;
if (sieve[num1] == 1 || sieve[num2] == 1)
return true ;
else
return false ;
}
function performQueries(q)
{
for (i = 0; i < q.length; i++) {
var A = q[i];
var last = A % 10;
var first;
while (A >= 10)
A = A / 10;
first = A;
if (isAnyPrime(first, last))
document.write( "True<br>" );
else
document.write( "False" );
}
}
var q = [ 30, 66 ];
buildSieve();
performQueries(q);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)