N digit numbers divisible by 5 formed from the M digits
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 : 20Input : 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++
// CPP program to find the count of all // possible N digit numbers which are // divisible by 5 formed from M digits #include <bits/stdc++.h> 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
<?php // PHP 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 function 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 = 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 ($i = 0; $i < $n - 1; $i++) { $result = $result * (--$m); } } else if ($isZero || $isFive) { $result = 1; // Remaining N-1 iterations for ($i = 0; $i < $n - 1; $i++) { $result = $result * (--$m); } } else $result = -1; return $result; } // Driver code $n = 3; $m = 6; $arr = array( 2, 3, 5, 6, 7, 9 ); echo numbers($n, $arr, $m); // This code is contributed by jit_t ?> |
20
Recommended Posts:
- Count numbers formed by given two digit with sum having given digits
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of all numbers that can be formed with permutations of n digits
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Sum of n digit numbers divisible by a given number
- Largest N digit number divisible by given three numbers
- Smallest n digit number divisible by given three numbers
- Count n digit numbers divisible by given number
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Print all n-digit numbers whose sum of digits equals to given sum
- Count of n digit numbers whose sum of digits equals to given sum
- Count of N-digit numbers with all distinct digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



