Given an array of size n and a positive integer k, find the maximum number of trailing zeros in the product of the subsets 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][0] = 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[0]); ` ` ` `cout << maximumZeros(arr, n, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for finding the maximum number ` `# of trailing zeros in the product of the ` `# selected subset of size k. ` `MAX5 ` `=` `100` ` ` `# Function to calculate maximum zeros. ` `def` `maximumZeros(arr, n, k): ` ` ` `global` `MAX5 ` ` ` ` ` `# Initializing each value with -1 ` ` ` `subset ` `=` `[[` `-` `1` `] ` `*` `(MAX5 ` `+` `5` `) ` `for` `_ ` `in` `range` `(k ` `+` `1` `)] ` ` ` ` ` `subset[` `0` `][` `0` `] ` `=` `0` ` ` ` ` `for` `p ` `in` `arr: ` ` ` ` ` `pw2, pw5 ` `=` `0` `, ` `0` ` ` ` ` `# Calculating maximal power ` ` ` `# of 2 for arr[p]. ` ` ` `while` `not` `p ` `%` `2` `: ` ` ` `pw2 ` `+` `=` `1` ` ` `p ` `/` `/` `=` `2` ` ` ` ` `# Calculating maximal power ` ` ` `# of 5 for arr[p]. ` ` ` `while` `not` `p ` `%` `5` `: ` ` ` `pw5 ` `+` `=` `1` ` ` `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` `i ` `in` `range` `(k` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` ` ` `for` `j ` `in` `range` `(MAX5): ` ` ` ` ` `# 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. ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(MAX5): ` ` ` `ans ` `=` `max` `(ans, ` `min` `(i, subset[k][i])) ` ` ` ` ` `return` `ans ` ` ` ` ` `# Driver function ` `arr ` `=` `[ ` `50` `, ` `4` `, ` `20` `] ` `k ` `=` `2` `n ` `=` `len` `(arr) ` ` ` `print` `(maximumZeros(arr, n, k)) ` ` ` `# This code is contributed by Ansu Kumari. ` |

*chevron_right*

*filter_none*

**Output :**

3

## Recommended Posts:

- Maximum difference of zeros and ones in binary string
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Maximum Product Subarray | Added negative product case
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Maximum size subset with given sum
- Size of The Subarray With Maximum Sum
- Number of subsets with sum divisible by m
- Count number of subsets having a particular XOR value
- Maximum size square sub-matrix with all 1s
- Maximum Product Cutting | DP-36
- Count number of ways to partition a set into k subsets
- Maximum product of an increasing subsequence
- Find Maximum dot product of two arrays with insertion of 0's
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Count number of increasing subsequences of size k

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.