- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We have given a number N along with an array of M digits. Our job is to find the number of n digit numbers formed from the given M digits that are divisible by 5.

Let's see some examples to understand the problem inputs and outputs.

**In −**

N = 2 M = 3 arr = {5, 6, 3}

**Out −**

2

There are 2 N digit numbers 35 and 65 possible that are divisible by 5. Let's see another example.

**Input −**

N = 1 M = 7 arr = {2, 3, 4, 5, 6, 7, 8}

**Output −**

1

There is only 1 number with 1 digit in the given array that is divisible by 5. So, our task is to find the number of numbers that can be formed from the given number with N digits that are divisible by 5.

The number must end with the digits 0 or 5 to be divisible by 5. Let's see the algorithm

- Check for the 0 and 5 in the given array. 2. If there are both 0 and 5 then there are two ways to place a digit in the units place. Otherwise, there will be a single way to place a digit.
- Initialise the count to 2.
- Now, the remaining place can have m - 1, m - 2, m - 3, ... n ways to fill them respectively.
- Write a loop that iterates from 0 to n - 1.
- Decrement that size of the array.
- Multiply it with count.

- If there is a single digit 0 or 5 then there is only one way to place a digit in the units place.
- Initialise the count to 2.
- Now, the remaining place can have m - 1, m - 2, m - 3, ... n ways to fill them respectively.
- Write a loop that iterates from 0 to n - 1.
- Decrement that size of the array.
- Multiply it with count.

- If there are no digits 0 or 5 than we can form a number that can be divisible by 5. Return -1 in that case.

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int numbers(int n, int m, int arr[]) { bool isZeroPresent = false, isFivePresent = false; int numbersCount = 0; if (m < n) { return -1; } for (int i = 0; i < m; i++) { if (arr[i] == 0) { isZeroPresent = true; } if (arr[i] == 5) { isFivePresent = true; } } if (isZeroPresent && isFivePresent) { numbersCount = 2; for (int i = 0; i < n - 1; i++) { m--; numbersCount = numbersCount * m; } } else if (isZeroPresent || isFivePresent) { numbersCount = 1; for (int i = 0; i < n - 1; i++) { m--; numbersCount = numbersCount * m; } } else { return -1; } return numbersCount; } int main() { int arr[] = {5, 6, 3}; cout << numbers(2, 3, arr) << endl; return 0; }

If you run the above code, then you will get the following result.

2

- Related Questions & Answers
- Count n digit numbers divisible by given number in C++
- Largest N digit number divisible by given three numbers in C++
- Count of m digit integers that are divisible by an integer n in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Find the number closest to n and divisible by m in C++
- Print all n digit patterns formed by mobile Keypad in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Program to print all the numbers divisible by 3 and 5 in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Find N digits number which is divisible by D in C++
- Counting n digit Numbers with all unique digits in JavaScript

Advertisements