text: Check if the first and last digit of the smallest number forms a prime | C # implementation of above approach ; function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver code
code: 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 ) ; } }