Check if the first and last digit of the smallest number forms a prime
Given an array arr[] containing numbers from 0 to 9 only, the task is to form the minimum possible number from the given digits and then check if the first and last digit of the number thus created can be rearranged to form a prime number or not.
Examples:
Input: arr[]={2, 6, 4, 9}
Output: Minimum number: 2469
Prime number combinations: 29
The first and last digits are 2 and 9 respectively. The combinations are 29 and 92. Only 29 is prime.
Input: arr[]={2, 6, 4, 3, 1, 7}
Output: Minimum number: 123467
Prime number combinations: 17 71
The first and last digits are 1 and 7 respectively. The combinations are 17 and 71, and both are primes
Approach:
- Create a hash of size 10 to store the number of occurrences of the digits in the given array into the hash table.
- Print the digits the number of times they occur in descending order starting from the digit 0. It is similar to Smallest number by rearranging digits of a given number.
- For the prime checking, check if the number formed using the first and last digits is prime or not. Do the same for its reverse.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPrime( int n)
{
int i, c = 0;
for (i = 1; i < n / 2; i++) {
if (n % i == 0)
c++;
}
if (c == 1)
return 1;
else
return 0;
}
void findMinNum( int arr[], int n)
{
int first = 0, last = 0, num, rev, i;
int hash[10] = { 0 };
for ( int i = 0; i < n; i++) {
hash[arr[i]]++;
}
cout << "Minimum number: " ;
for ( int i = 0; i <= 9; i++) {
for ( int j = 0; j < hash[i]; j++)
cout << i;
}
cout << endl;
for (i = 0; i <= 9; i++) {
if (hash[i] != 0) {
first = i;
break ;
}
}
for (i = 9; i >= 0; i--) {
if (hash[i] != 0) {
last = i;
break ;
}
}
num = first * 10 + last;
rev = last * 10 + first;
cout << "Prime combinations: " ;
if (isPrime(num) && isPrime(rev))
cout << num << " " << rev;
else if (isPrime(num))
cout << num;
else if (isPrime(rev))
cout << rev;
else
cout << "No combinations exist" ;
}
int main()
{
int arr[] = { 1, 2, 4, 7, 8};
findMinNum(arr, 5);
return 0;
}
|
Java
import java.io.*;
class SmallPrime
{
static boolean isPrime( int n)
{
int i, c = 0 ;
for (i = 1 ; i < n / 2 ; i++)
{
if (n % i == 0 )
c++;
}
if (c == 1 )
{
return true ;
}
else
{
return false ;
}
}
static void findMinNum( int arr[], int n)
{
int first = 0 , last = 0 , num, rev, i;
int hash[] = new int [ 10 ];
for ( i = 0 ; i < n; i++)
{
hash[arr[i]]++;
}
System.out.print( "Minimum number: " );
for ( i = 0 ; i <= 9 ; i++)
{
for ( int j = 0 ; j < hash[i]; j++)
System.out.print(i);
}
System.out.println();
System.out.println();
for (i = 0 ; i <= 9 ; i++)
{
if (hash[i] != 0 )
{
first = i;
break ;
}
}
for (i = 9 ; i >= 0 ; i--)
{
if (hash[i] != 0 )
{
last = i;
break ;
}
}
num = first * 10 + last;
rev = last * 10 + first;
System.out.print( "Prime combinations: " );
if (isPrime(num) && isPrime(rev))
{
System.out.println(num + " " + rev);
}
else if (isPrime(num))
{
System.out.println(num);
}
else if (isPrime(rev))
{
System.out.println(rev);
}
else
{
System.out.println( "No combinations exist" );
}
}
public static void main (String[] args)
{
SmallPrime smallprime = new SmallPrime();
int arr[] = { 1 , 2 , 4 , 7 , 8 };
smallprime.findMinNum(arr, 5 );
}
}
|
Python3
import math as mt
def isPrime(n):
i, c = 0 , 0
for i in range ( 1 , n / / 2 ):
if (n % i = = 0 ):
c + = 1
if (c = = 1 ):
return 1
else :
return 0
def findMinNum(arr, n):
first, last = 0 , 0
Hash = [ 0 for i in range ( 10 )]
for i in range (n):
Hash [arr[i]] + = 1
print ( "Minimum number: " , end = "")
for i in range ( 0 , 10 ):
for j in range ( Hash [i]):
print (i, end = "")
print ()
for i in range ( 10 ):
if ( Hash [i] ! = 0 ):
first = i
break
for i in range ( 9 , - 1 , - 1 ):
if ( Hash [i] ! = 0 ):
last = i
break
num = first * 10 + last
rev = last * 10 + first
print ( "Prime combinations: " , end = "")
if (isPrime(num) and isPrime(rev)):
print (num, " " , rev)
elif (isPrime(num)):
print (num)
elif (isPrime(rev)):
print (rev)
else :
print ( "No combinations exist" )
arr = [ 1 , 2 , 4 , 7 , 8 ]
findMinNum(arr, 5 )
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
int i, c = 0;
for (i = 1; i < n / 2; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 1)
{
return true ;
}
else
{
return false ;
}
}
static void findMinNum( int [] arr, int n)
{
int first = 0, last = 0, num, rev, i;
int [] hash = new int [10];
for (i = 0; i < n; i++)
{
hash[arr[i]]++;
}
Console.Write( "Minimum number: " );
for (i = 0; i <= 9; i++)
{
for ( int j = 0; j < hash[i]; j++)
{
Console.Write(i);
}
}
Console.WriteLine();
Console.WriteLine();
for (i = 0; i <= 9; i++)
{
if (hash[i] != 0)
{
first = i;
break ;
}
}
for (i = 9; i >= 0; i--)
{
if (hash[i] != 0)
{
last = i;
break ;
}
}
num = first * 10 + last;
rev = last * 10 + first;
Console.Write( "Prime combinations: " );
if (isPrime(num) && isPrime(rev))
{
Console.WriteLine(num + " " + rev);
}
else if (isPrime(num))
{
Console.WriteLine(num);
}
else if (isPrime(rev))
{
Console.WriteLine(rev);
}
else
{
Console.WriteLine( "No combinations exist" );
}
}
public static void Main()
{
int [] arr = {1, 2, 4, 7, 8};
findMinNum(arr, 5);
}
}
|
PHP
<?php
function isPrime( $n )
{
$c = 0;
for ( $i = 1; $i < $n / 2; $i ++)
{
if ( $n % $i == 0)
$c ++;
}
if ( $c == 1)
return 1;
else
return 0;
}
function findMinNum( $arr , $n )
{
$first = 0; $last = 0;
$num ; $rev ; $i ;
$hash = array_fill (0, 20, 0);
for ( $i = 0; $i < $n ; $i ++)
{
$hash [ $arr [ $i ]]++;
}
echo "Minimum number: " ;
for ( $i = 0; $i <= 9; $i ++)
{
for ( $j = 0; $j < $hash [ $i ]; $j ++)
echo $i ;
}
for ( $i = 0; $i <= 9; $i ++)
{
if ( $hash [ $i ] != 0)
{
$first = $i ;
break ;
}
}
for ( $i = 9; $i >= 0; $i --)
{
if ( $hash [ $i ] != 0)
{
$last = $i ;
break ;
}
}
$num = $first * 10 + $last ;
$rev = $last * 10 + $first ;
echo "\nPrime combinations: " ;
if (isPrime( $num ) && isPrime( $rev ))
echo $num . " " . $rev ;
else if (isPrime( $num ))
echo $num ;
else if (isPrime( $rev ))
echo $rev ;
else
echo "No combinations exist" ;
}
$arr = array (1, 2, 4, 7, 8);
findMinNum( $arr , 5);
?>
|
Javascript
<script>
function isPrime(n) {
var i,
c = 0;
for (i = 1; i < n / 2; i++) {
if (n % i == 0) c++;
}
if (c == 1) return 1;
else return 0;
}
function findMinNum(arr, n) {
var first = 0,
last = 0,
num,
rev,
i;
var hash = new Array(10).fill(0);
for ( var i = 0; i < n; i++) {
hash[arr[i]]++;
}
document.write( "Minimum number: " );
for ( var i = 0; i <= 9; i++) {
for ( var j = 0; j < hash[i]; j++) document.write(i);
}
document.write( "<br>" );
for (i = 0; i <= 9; i++) {
if (hash[i] != 0) {
first = i;
break ;
}
}
for (i = 9; i >= 0; i--) {
if (hash[i] != 0) {
last = i;
break ;
}
}
num = first * 10 + last;
rev = last * 10 + first;
document.write( "Prime combinations: " );
if (isPrime(num) && isPrime(rev)) document.write(num + " " + rev);
else if (isPrime(num)) document.write(num);
else if (isPrime(rev)) document.write(rev);
else document.write( "No combinations exist" );
}
var arr = [1, 2, 4, 7, 8];
findMinNum(arr, 5);
</script>
|
Output
Minimum number: 12478
Prime combinations: No combinations exist
Time Complexity: O(n),The time complexity of this algorithm is O(n) where n is the size of the array.
Space Complexity: O(1),The space complexity is O(1) since no extra space is used.
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...