Related Articles
N digit numbers divisible by 5 formed from the M digits
• Last Updated : 29 Nov, 2018

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

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

## Java

 `// Java program to find the count of all ``// possible N digit numbers which are ``// divisible by 5 formed from M digits `` ` `class` `GFG {`` ` `// Function to find the count of all ``// possible N digit numbers which are ``// divisible by 5 formed from M digits ``    ``static` `int` `numbers(``int` `n, ``int` `arr[], ``int` `m) {``        ``int` `isZero = ``0``, isFive = ``0``;``        ``int` `result = ``0``;`` ` `        ``// If it is not possible to form ``        ``// n digit number from the given ``        ``// m digits without repetition ``        ``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 both zero and five exists ``        ``if` `(isZero == ``1` `&& isFive == ``1``) {``            ``result = ``2``;`` ` `            ``// Remaining N-1 iterations ``            ``for` `(``int` `i = ``0``; i < n - ``1``; i++) {``                ``result = result * (--m);``            ``}``        ``} ``else` `if` `(isZero == ``1` `|| isFive == ``1``) {``            ``result = ``1``;`` ` `            ``// Remaining N-1 iterations ``            ``for` `(``int` `i = ``0``; i < n - ``1``; i++) {``                ``result = result * (--m);``            ``}``        ``} ``else` `{``            ``result = -``1``;``        ``}`` ` `        ``return` `result;``    ``}`` ` `// Driver code ``    ``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));`` ` `    ``}``}``// This code is contributed by RAJPUT-JI`

## Python 3

 `# Python 3 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``def` `numbers(n, arr, m):`` ` `    ``isZero ``=` `0``    ``isFive ``=` `0``    ``result ``=` `0`` ` `    ``# If it is not possible to form``    ``# n digit number from the given``    ``# m digits without repetition``    ``if` `(m < n) :``        ``return` `-``1`` ` `    ``for` `i ``in` `range``(m) :``        ``if` `(arr[i] ``=``=` `0``):``            ``isZero ``=` `1`` ` `        ``if` `(arr[i] ``=``=` `5``):``            ``isFive ``=` `1`` ` `    ``# If both zero and five exists``    ``if` `(isZero ``and` `isFive) :``        ``result ``=` `2`` ` `        ``# Remaining N-1 iterations``        ``for` `i ``in` `range``( n ``-` `1``):``            ``m ``-``=` `1``            ``result ``=` `result ``*` `(m)`` ` `    ``elif` `(isZero ``or` `isFive) :``        ``result ``=` `1`` ` `        ``# Remaining N-1 iterations``        ``for` `i ``in` `range``(n ``-` `1``) :``            ``m ``-``=` `1``            ``result ``=` `result ``*` `(m)``    ``else``:``        ``result ``=` `-``1`` ` `    ``return` `result`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``n ``=` `3``    ``m ``=` `6`` ` `    ``arr ``=` `[ ``2``, ``3``, ``5``, ``6``, ``7``, ``9``]`` ` `    ``print``(numbers(n, arr, m))`` ` `# This code is contributed by ChitraNayal`

## C#

 `// C# program to find the count of all ``// possible N digit numbers which are ``// divisible by 5 formed from M digits ``using` `System;``public` `class` `GFG {`` ` `// Function to find the count of all ``// possible N digit numbers which are ``// divisible by 5 formed from M digits ``    ``static` `int` `numbers(``int` `n, ``int` `[]arr, ``int` `m) {``        ``int` `isZero = 0, isFive = 0;``        ``int` `result = 0;`` ` `        ``// If it is not possible to form ``        ``// n digit number from the given ``        ``// m digits without repetition ``        ``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 both zero and five exists ``        ``if` `(isZero == 1 && isFive == 1) {``            ``result = 2;`` ` `            ``// Remaining N-1 iterations ``            ``for` `(``int` `i = 0; i < n - 1; i++) {``                ``result = result * (--m);``            ``}``        ``} ``else` `if` `(isZero == 1 || isFive == 1) {``            ``result = 1;`` ` `            ``// Remaining N-1 iterations ``            ``for` `(``int` `i = 0; i < n - 1; i++) {``                ``result = result * (--m);``            ``}``        ``} ``else` `{``            ``result = -1;``        ``}`` ` `        ``return` `result;``    ``}`` ` `// Driver code ``    ``public` `static` `void` `Main() {``        ``int` `n = 3, m = 6;`` ` `        ``int` `[]arr = {2, 3, 5, 6, 7, 9};``        ``Console.WriteLine(numbers(n, arr, m));`` ` `    ``}``}``// This code is contributed by RAJPUT-JI`

## PHP

 ``
Output:
```20
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up