


A number of N digits composed of M numbers that is divisible by 5 written in C++
We are given a number N and an array of M digits. Our job is to find n numbers A number divisible by 5 consisting of the given M digits.
Let's look at some examples to understand the input and output of the problem.
In -
N = 2 M = 3 arr = {5, 6, 3}
Out -
2
There are 2 N numbers 35 and 65 that may be evenly divisible by 5. Let's look at another example.
Input-
N = 1 M = 7 arr = {2, 3, 4, 5, 6, 7, 8}
Output-
1
Only one 1-digit number in the given array is divisible by 5 . Therefore, our task is to find the number of numbers that are divisible by 5 given N numbers. < /p>
The number must end with the digit 0 or 5 to be divisible by 5. Let’s see the algorithm
Algorithm
- Checks for 0 and 5 in the given array. 2. If there are both 0 and 5, there are two ways to put the number into the ones place. Otherwise, there would be a way to place the numbers.
- Initialize the count to 2.
- Now, the remaining positions can have m - 1, m - 2, m - 3, ... n ways to fill them respectively.
- Write a loop that iterates from 0 to n - 1.
- Reduce the array.
- Multiply it with the count.
- If you have the single digit 0 or 5, there is only one way to put the number into the ones place.
- Initialize the count to 2.
- Now, the remaining positions can have m - 1, m - 2, m - 3, ... n ways to fill them respectively.
- Write a loop that iterates from 0 to n - 1.
- Reduce the array.
- Multiply it with the count.
- If there is no number 0 or 5, then we can form a number that is divisible by 5. Returns -1 at this time.
Implementation
The following is the C implementation of the above algorithm
#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; }
Output
If you run the above code, you will get the following results.
2
The above is the detailed content of A number of N digits composed of M numbers that is divisible by 5 written in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

The manipulation of date and time values is an important aspect of programming, and the Python language provides a useful built-in module for this called datetime. However, in some cases, you may need to convert a DateTime object to an integer value in order to perform specific operations or calculations. There are multiple ways to convert a DateTime to an integer in Python, each with its own advantages and disadvantages. In this article, we'll take a closer look at these methods and examine when each method is appropriate to use. After reading this article, you will have a complete understanding of how to efficiently convert DateTime objects to integers in Python and be able to choose the most appropriate method for your specific programming task. Method 1: Use timestamp

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

The regular expressions for integers are: 1. Match positive integers: ^[1-9]\d*$; 2. Match negative integers: ^-[1-9]\d*$; 3. Match positive integers and negative integers :^-?\d+$; 4. Match non-zero integers: ^(0|[1-9]\d*)$; 5. Match integers (including zero): ^-?\d+$.

Installing an SSL certificate on SharePoint is a critical step in securing your website and providing an encrypted connection. By following the correct installation steps, you can ensure the security of your website data, improve your ranking in search engines, and provide a better user experience for your visitors. Get an SSL Certificate Contact a trusted Certificate Authority (CA) to purchase an SSL certificate. Provide the required authentication and domain ownership verification information. After completing the verification process, you will receive the SSL certificate file. Prepare the Certificate File Open your SSL certificate file using a text editor. Copy the certificate contents to a new text file. Save the file as yourdomain.cer, making sure to change "yourdomain”

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

In this article, we will learn about the reversal algorithm to rotate a given array to the right by k elements, for example −Input:arr[]={4,6,2,6,43,7,3,7},k= 4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8 ,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1} Find the solution

A circle is a closed figure. All points on a circle are equidistant from a point inside the circle. The center point is called the center of the circle. The distance from a point to the center of a circle is called the radius. Area is a quantitative representation of the span of dimensions of a closed figure. The area of a circle is the area enclosed within the dimensions of the circle. The formula to calculate the area of a circle, Area=π*r*r To calculate the area, we give the radius of the circle as input, we will use the formula to calculate the area, algorithm STEP1: Takeradiusasinputfromtheuserusingstdinput.STEP2: Calculatetheareaofcircleusing, area=(
