N digit numbers divisible by 5 formed from the M digits | CPP program to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numbers ( int n , int arr [ ] , int m ) { int isZero = 0 , isFive = 0 ; int result = 0 ; if ( m < n ) { return -1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( arr [ i ] == 0 ) isZero = 1 ; if ( arr [ i ] == 5 ) isFive = 1 ; } if ( isZero && isFive ) { result = 2 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else if ( isZero isFive ) { result = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else result = -1 ; return result ; } int main ( ) { int n = 3 , m = 6 ; int arr [ ] = { 2 , 3 , 5 , 6 , 7 , 9 } ; cout << numbers ( n , arr , m ) ; return 0 ; }