Check if the first and last digit of the smallest number forms a prime
Last Updated :
11 Jul, 2025
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++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// function to check prime
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;
}
// Function to generate smallest possible
// number with given digits
void findMinNum(int arr[], int n)
{
// Declare a hash array of size 10
// and initialize all the elements to zero
int first = 0, last = 0, num, rev, i;
int hash[10] = { 0 };
// store the number of occurrences of the digits
// in the given array into the hash table
for (int i = 0; i < n; i++) {
hash[arr[i]]++;
}
// Traverse the hash in ascending order
// to print the required number
cout << "Minimum number: ";
for (int i = 0; i <= 9; i++) {
// Print the number of times a digits occurs
for (int j = 0; j < hash[i]; j++)
cout << i;
}
cout << endl;
// extracting the first digit
for (i = 0; i <= 9; i++) {
if (hash[i] != 0) {
first = i;
break;
}
}
// extracting the last digit
for (i = 9; i >= 0; i--) {
if (hash[i] != 0) {
last = i;
break;
}
}
num = first * 10 + last;
rev = last * 10 + first;
// printing the prime combinations
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";
}
// Driver code
int main()
{
int arr[] = { 1, 2, 4, 7, 8};
findMinNum(arr, 5);
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class SmallPrime
{
// function to check prime
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;
}
}
// Function to generate smallest possible
// number with given digits
static void findMinNum(int arr[], int n)
{
// Declare a hash array of size 10
// and initialize all the elements to zero
int first = 0, last = 0, num, rev, i;
int hash[] = new int[10];
// store the number of occurrences of the digits
// in the given array into the hash table
for ( i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Traverse the hash in ascending order
// to print the required number
System.out.print("Minimum number: ");
for ( i = 0; i <= 9; i++)
{
// Print the number of times a digits occurs
for (int j = 0; j < hash[i]; j++)
System.out.print(i);
}
System.out.println();
System.out.println();
// extracting the first digit
for (i = 0; i <= 9; i++)
{
if (hash[i] != 0)
{
first = i;
break;
}
}
// extracting the last digit
for (i = 9; i >= 0; i--)
{
if (hash[i] != 0)
{
last = i;
break;
}
}
num = first * 10 + last;
rev = last * 10 + first;
// printing the prime combinations
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");
}
}
// Driver code
public static void main (String[] args)
{
SmallPrime smallprime = new SmallPrime();
int arr[] = {1, 2, 4, 7, 8};
smallprime.findMinNum(arr, 5);
}
}
// This code has been contributed by inder_verma.
Python3
# Python3 implementation of above
# approach
import math as mt
# function to check prime
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
# Function to generate smallest possible
# number with given digits
def findMinNum(arr, n):
# Declare a Hash array of size 10
# and initialize all the elements to zero
first, last = 0, 0
Hash = [0 for i in range(10)]
# store the number of occurrences of
# the digits in the given array into
# the Hash table
for i in range(n):
Hash[arr[i]] += 1
# Traverse the Hash in ascending order
# to print the required number
print("Minimum number: ", end = "")
for i in range(0, 10):
# Print the number of times
# a digits occurs
for j in range(Hash[i]):
print(i, end = "")
print()
# extracting the first digit
for i in range(10):
if (Hash[i] != 0):
first = i
break
# extracting the last digit
for i in range(9, -1, -1):
if (Hash[i] != 0):
last = i
break
num = first * 10 + last
rev = last * 10 + first
# printing the prime combinations
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")
# Driver code
arr = [ 1, 2, 4, 7, 8]
findMinNum(arr, 5)
# This code is contributed by
# Mohit kumar 29
C#
// C# implementation of above approach
using System;
class GFG
{
// function to check prime
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;
}
}
// Function to generate smallest
// possible number with given digits
static void findMinNum(int[] arr, int n)
{
// Declare a hash array of
// size 10 and initialize
// all the elements to zero
int first = 0, last = 0, num, rev, i;
int[] hash = new int[10];
// store the number of occurrences
// of the digits in the given array
// into the hash table
for (i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Traverse the hash in ascending order
// to print the required number
Console.Write("Minimum number: ");
for (i = 0; i <= 9; i++)
{
// Print the number of times
// a digits occurs
for (int j = 0; j < hash[i]; j++)
{
Console.Write(i);
}
}
Console.WriteLine();
Console.WriteLine();
// extracting the first digit
for (i = 0; i <= 9; i++)
{
if (hash[i] != 0)
{
first = i;
break;
}
}
// extracting the last digit
for (i = 9; i >= 0; i--)
{
if (hash[i] != 0)
{
last = i;
break;
}
}
num = first * 10 + last;
rev = last * 10 + first;
// printing the prime combinations
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");
}
}
// Driver code
public static void Main()
{
int[] arr = {1, 2, 4, 7, 8};
findMinNum(arr, 5);
}
}
// This code is contributed
// by PrinciRaj1992
PHP
<?php
// PHP implementation of above approach
// function to check prime
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 to generate smallest possible
// number with given digits
function findMinNum($arr, $n)
{
// Declare a hash array of size 10
// and initialize all the elements to zero
$first = 0; $last = 0;
$num; $rev ; $i;
$hash = array_fill(0, 20, 0);
// store the number of occurrences of
// the digits in the given array into
// the hash table
for ($i = 0; $i < $n; $i++)
{
$hash[$arr[$i]]++;
}
// Traverse the hash in ascending order
// to print the required number
echo "Minimum number: ";
for ( $i = 0; $i <= 9; $i++)
{
// Print the number of times a
// digits occurs
for ($j = 0; $j < $hash[$i]; $j++)
echo $i;
}
// extracting the first digit
for ($i = 0; $i <= 9; $i++)
{
if ($hash[$i] != 0)
{
$first = $i;
break;
}
}
// extracting the last digit
for ($i = 9; $i >= 0; $i--)
{
if ($hash[$i] != 0)
{
$last = $i;
break;
}
}
$num = $first * 10 + $last;
$rev = $last * 10 + $first;
// printing the prime combinations
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";
}
// Driver Code
$arr = array(1, 2, 4, 7, 8);
findMinNum($arr, 5);
// This code is contributed
// by Rajput-Ji
?>
JavaScript
<script>
// JavaScript implementation of above approach
// function to check prime
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 to generate smallest possible
// number with given digits
function findMinNum(arr, n) {
// Declare a hash array of size 10
// and initialize all the elements to zero
var first = 0,
last = 0,
num,
rev,
i;
var hash = new Array(10).fill(0);
// store the number of occurrences of the digits
// in the given array into the hash table
for (var i = 0; i < n; i++) {
hash[arr[i]]++;
}
// Traverse the hash in ascending order
// to print the required number
document.write("Minimum number: ");
for (var i = 0; i <= 9; i++) {
// Print the number of times a digits occurs
for (var j = 0; j < hash[i]; j++) document.write(i);
}
document.write("<br>");
// extracting the first digit
for (i = 0; i <= 9; i++) {
if (hash[i] != 0) {
first = i;
break;
}
}
// extracting the last digit
for (i = 9; i >= 0; i--) {
if (hash[i] != 0) {
last = i;
break;
}
}
num = first * 10 + last;
rev = last * 10 + first;
// printing the prime combinations
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");
}
// Driver code
var arr = [1, 2, 4, 7, 8];
findMinNum(arr, 5);
</script>
OutputMinimum 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.
Similar Reads
Queries for the smallest and the largest prime number of given digit Given Q queries where every query consists of an integer D, the task is to find the smallest and the largest prime number with D digits. If no such prime number exists then print -1.Examples: Input: Q[] = {2, 5} Output: 11 97 10007 99991Input: Q[] = {4, 3, 1} Output: 1009 9973 101 997 1 7 Approach:
9 min read
Check whether a number is Good prime or not Given a positive integer N, the task is to check whether the given number is good prime or not. If the given number is good prime print âYESâ Otherwise Print âNOâ. Good Prime: In Mathematics, a good prime is a prime number whose square is greater than the product of any two primes at the same number
8 min read
Check if concatenation of first and last digits forms a prime number or not for each array element 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: TrueFalseExplanation:Q[0]: Possible combinati
9 min read
Check whether a number is circular prime or not We are given a number n. Our task is to check whether the number is circular prime or not.Circular Prime : A prime number is said to be a circular prime if after any cyclic permutations of the digits, it remains a prime.Examples: Input : n = 113 Output : Yes All cyclic permutations of 113 (311 and 1
9 min read
Check if a number is a Pythagorean Prime or not Given a positive integer N, check if it is Pythagorean prime or not. If it is a Pythagorean prime, print 'Yes' otherwise print 'No'.Pythagorean primes : A prime number of the form 4*n + 1 is a Pythagorean prime. It can also be expressed as sum of two squares. Pythagorean primes in the range 1 - 100
6 min read
Smallest Special Prime which is greater than or equal to a given number Given a number N. The task is to find the smallest special prime which is greater than or equal to N.A special prime is a number which can be created by placing digits one after another such the all the resulting numbers are prime. Examples: Input: N = 379 Output: 379 379 can be created as => 3 =
9 min read