N digit numbers divisible by 5 formed from the M digits
Last Updated :
25 Jul, 2022
Given M unique digits and a number N. The task is to find the number of N-digit numbers which can be formed from the given M digits, which are divisible by 5 and none of the digits is repeated.
Note: If it is not possible to form a N digit number from the given digits, print -1.
Examples:
Input : N = 3, M = 6, digits[] = {2, 3, 5, 6, 7, 9}
Output : 20
Input : N = 5, M = 6, digits[] = {0, 3, 5, 6, 7, 9}
Output : 240
For a number to be divisible by 5, the only condition is that the digit at the unit place in the number must be either 0 or 5.
So, to find the count of numbers that are divisible by 5 and can be formed from the given digits, do the following:
- Check if the given digits contain both 0 and 5.
- If the given digits contain both 0 and 5, then the unit place can be filled in 2 ways otherwise the unit place can be filled in 1 way.
- Now, the tens place can now be filled by any of the remaining M-1 digits. So, there are (M-1) ways of filling the tens place.
- Similarly, the hundred’s place can now be filled by any of the remaining (M-2) digits and so on.
Therefore, if the given digits have both 0 and 5:
Required number of numbers = 2 * (M-1)* (M-2)...N-times.
Otherwise, if the given digits have either one of 0 and 5 and not both:
Required number of numbers = 1 * (M-1)* (M-2)...N-times.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG {
static 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 == 1 && isFive == 1 ) {
result = 2 ;
for ( int i = 0 ; i < n - 1 ; i++) {
result = result * (--m);
}
} else if (isZero == 1 || isFive == 1 ) {
result = 1 ;
for ( int i = 0 ; i < n - 1 ; i++) {
result = result * (--m);
}
} else {
result = - 1 ;
}
return result;
}
public static void main(String[] args) {
int n = 3 , m = 6 ;
int arr[] = { 2 , 3 , 5 , 6 , 7 , 9 };
System.out.println(numbers(n, arr, m));
}
}
|
Python 3
def numbers(n, arr, m):
isZero = 0
isFive = 0
result = 0
if (m < n) :
return - 1
for i in range (m) :
if (arr[i] = = 0 ):
isZero = 1
if (arr[i] = = 5 ):
isFive = 1
if (isZero and isFive) :
result = 2
for i in range ( n - 1 ):
m - = 1
result = result * (m)
elif (isZero or isFive) :
result = 1
for i in range (n - 1 ) :
m - = 1
result = result * (m)
else :
result = - 1
return result
if __name__ = = "__main__" :
n = 3
m = 6
arr = [ 2 , 3 , 5 , 6 , 7 , 9 ]
print (numbers(n, arr, m))
|
C#
using System;
public class GFG {
static 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 == 1 && isFive == 1) {
result = 2;
for ( int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else if (isZero == 1 || isFive == 1) {
result = 1;
for ( int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else {
result = -1;
}
return result;
}
public static void Main() {
int n = 3, m = 6;
int []arr = {2, 3, 5, 6, 7, 9};
Console.WriteLine(numbers(n, arr, m));
}
}
|
PHP
<?php
function numbers( $n , $arr , $m )
{
$isZero = 0;
$isFive = 0;
$result = 0;
if ( $m < $n )
{
return -1;
}
for ( $i = 0; $i < $m ; $i ++)
{
if ( $arr [ $i ] == 0)
$isZero = 1;
if ( $arr [ $i ] == 5)
$isFive = 1;
}
if ( $isZero && $isFive )
{
$result = 2;
for ( $i = 0; $i < $n - 1; $i ++)
{
$result = $result * (-- $m );
}
}
else if ( $isZero || $isFive )
{
$result = 1;
for ( $i = 0; $i < $n - 1; $i ++)
{
$result = $result * (-- $m );
}
}
else
$result = -1;
return $result ;
}
$n = 3;
$m = 6;
$arr = array ( 2, 3, 5, 6, 7, 9 );
echo numbers( $n , $arr , $m );
?>
|
Javascript
<script>
function numbers(n, arr, m) {
let isZero = 0, isFive = 0;
let result = 0;
if (m < n) {
return -1;
}
for (let i = 0; i < m; i++) {
if (arr[i] == 0) {
isZero = 1;
}
if (arr[i] == 5) {
isFive = 1;
}
}
if (isZero == 1 && isFive == 1) {
result = 2;
for (let i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else if (isZero == 1 || isFive == 1) {
result = 1;
for (let i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else {
result = -1;
}
return result;
}
let n = 3, m = 6;
let arr = [2, 3, 5, 6, 7, 9];
document.write(numbers(n, arr, m));
</script>
|
Time Complexity: O(m + n), Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...