  Given an array of n and a positive integer k, find the of trailing in the of the of size k.

Examples:

```Input : arr = {50, 4, 20}
k = 2
Output : 3
Here, we have 3 subsets of size 2. [50, 4]
has product 200, having 2 zeros at the end,
[4, 20] — product 80, having 1 zero at the
end, [50, 20] — product 1000, having 3 zeros
at the end. Therefore, the maximum zeros at
the end of the product is 3.

Input : arr = {15, 16, 3, 25, 9}
k = 3
Output : 3
Here, the subset [15, 16, 25] has product 6000.

Input : arr = {9, 77, 13}
k = 3
Output : 0
Here, the subset [9, 77, 13] has product 9009
having no zeros at the end.
```

Dynamic Programming approach :
Obviously, the number of zeros at the end of the number is determined by minimum of powers of 2 and 5 in the number. Let pw5 be the maximal power of 5 in the number and pw2 be the maximal power of 2.

Let, subset[i][j] be the maximum amount of 2s we can collect considering i numbers having j number of 5s in each of them.

We traverse through all numbers of given, for every array element, we count number of 2s and 5s in it. Let pw2 be the number of 2s in current number and pw5 be the number of 5s.

Now, there is one transition for subset[i][j]:

// For current number (pw2 two’s and pw5 five’s) we check
// if we can increase value of subset[i][j].
subset[i][j] = max(subset[i][j], subset[i-1][j-pw5] + pw2)

The above expression can also be written as below.
subset[i + 1][j + pw5] = max(subset[i + 1][j + pw5], subset[i][j] + pw2);

The answer will be max(ans, min(i, subset[k][i])

## C++

```// CPP program for finding the maximum number
// of trailing zeros in the product of the
// selected subset of size k.
#include <bits/stdc++.h>
using namespace std;
#define MAX5 100

// Function to calculate maximum zeros.
int maximumZeros(int* arr, int n, int k)
{
// Initializing each value with -1;
int subset[k+1][MAX5+5];
memset(subset, -1, sizeof(subset));

subset = 0;

for (int p = 0; p < n; p++) {
int pw2 = 0, pw5 = 0;

// Calculating maximal power of 2 for
// arr[p].
while (arr[p] % 2 == 0) {
pw2++;
arr[p] /= 2;
}

// Calculating maximal power of 5 for
// arr[p].
while (arr[p] % 5 == 0) {
pw5++;
arr[p] /= 5;
}

// Calculating subset[i][j] for maximum
// amount of twos we can collect by
// checking first i numbers and taking
// j of them with total power of five.
for (int i = k - 1; i >= 0; i--)
for (int j = 0; j < MAX5; j++)

// If subset[i][j] is not calculated.
if (subset[i][j] != -1)
subset[i + 1][j + pw5] =
max(subset[i + 1][j + pw5],
subset[i][j] + pw2);
}

// Calculating maximal number of zeros.
// by taking minimum of 5 or 2 and then
// taking maximum.
int ans = 0;
for (int i = 0; i < MAX5; i++)
ans = max(ans, min(i, subset[k][i]));

return ans;
}

// Driver function
int main()
{
int arr[] = { 50, 4, 20 };
int k = 2;
int n = sizeof(arr) / sizeof(arr);
cout << maximumZeros(arr, n, k) << endl;
return 0;
}
``` 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

SHARE