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; }

## 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.

**Output :**

3

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.

**Practice Tags :**