Count number of trailing zeros in product of array
Given a array size of n, we need to find the total number of zeros in the product of array.
Examples:
Input : a[] = {100, 20, 40, 25, 4}
Output : 6
Product is 100 * 20 * 40 * 25 * 4
which is 8000000 and has 6 trailing 0s.
Input : a[] = {10, 100, 20, 30, 25, 4,
43, 25, 50, 90, 12, 80}
Output : 13
A simple solution is simply multiply and count trailing 0s in product. This solution may cause integer overflow. A better solution is based on the fact that zeros are formed by a combination of 2 and 5. Hence the number of zeros will depend on the number of pairs of 2’s and 5’s that can be formed.
Ex.: 8 * 3 * 5 * 23 * 17 * 25 * 4 * 11
23 * 31 * 51 * 231 * 171 * 52 * 22 * 111
In this example there are 5 twos and 3 fives. Hence, we shall be able to form only 3 pairs of (2*5). Hence will be 3 Zeros in the product.
C++
// CPP program for count total zero in product of array #include <iostream> using namespace std; // Returns count of zeros in product of array int countZeros(int a[], int n) { int count2 = 0, count5 = 0; for (int i = 0; i < n; i++) { // count number of 2s in each element while (a[i] % 2 == 0) { a[i] = a[i] / 2; count2++; } // count number of 5s in each element while (a[i] % 5 == 0) { a[i] = a[i] / 5; count5++; } } // return the minimum return (count2 < count5) ? count2 : count5; } // Driven Program int main() { int a[] = { 10, 100, 20, 30, 50, 90, 12, 80 }; int n = sizeof(a) / sizeof(a[0]); cout << countZeroso(a, n); return 0; } |
Java
// Java program for count total // zero in product of array import java.util.*; import java.lang.*; public class GfG { // Returns count of zeros in product of array public static int countZeroso(int[] a, int n) { int count2 = 0, count5 = 0; for (int i = 0; i < n; i++) { // count number of 2s // in each element while (a[i] % 2 == 0) { a[i] = a[i] / 2; count2++; } // count number of 5s // in each element while (a[i] % 5 == 0) { a[i] = a[i] / 5; count5++; } } // return the minimum return (count2 < count5) ? count2 : count5; } // Driver function public static void main(String argc[]) { int[] a = new int[]{ 10, 100, 20, 30, 50, 91, 12, 80 }; int n = 8; System.out.println(countZeroso(a, n)); } } // This code is contributed // by Sagar Shukla |
Python 3
# Python 3 program for count # total zero in product of array # Returns count of zeros # in product of array def countZeros(a, n) : count2 = 0 count5 = 0 for i in range(0, n) : # count number of 2s # in each element while (a[i] % 2 == 0) : a[i] = a[i] // 2 count2 = count2 + 1 # count number of 5s # in each element while (a[i] % 5 == 0) : a[i] = a[i] // 5 count5 = count5 + 1 # return the minimum if(count2 < count5) : return count2 else : return count5 # Driven Program a = [ 10, 100, 20, 30, 50, 90, 12, 80 ] n = len(a) print(countZeros(a, n)) # This code is contributed # by Nikita Tiwari. |
C#
// C# program for count total // zero in product of array using System; public class GfG { // Returns count of zeros in product of array public static int countZeroso(int[] a, int n) { int count2 = 0, count5 = 0; for (int i = 0; i < n; i++) { // count number of 2s // in each element while (a[i] % 2 == 0) { a[i] = a[i] / 2; count2++; } // count number of 5s // in each element while (a[i] % 5 == 0) { a[i] = a[i] / 5; count5++; } } // return the minimum return (count2 < count5) ? count2 : count5; } // Driver function public static void Main() { int[] a = new int[]{ 10, 100, 20, 30, 50, 91, 12, 80 }; int n = 8; Console.WriteLine(countZeroso(a, n)); } } // This code is contributed // by vt_m |
PHP
<?php // PHP program for count total // zero in product of array function countZeros($a, $n) { $count2 = 0; $count5 = 0; for ($i = 0; $i < $n; $i++) { // count number of 2s // in each element while ($a[$i] % 2 == 0) { $a[$i] = $a[$i] / 2; $count2++; } // count number of 5s // in each element while ($a[$i] % 5 == 0) { $a[$i] = $a[$i] / 5; $count5++; } } // return the minimum return ($count2 < $count5) ? $count2 : $count5; } // Driver Code $a = array(10, 100, 20, 30, 50, 90, 12, 80); $n = sizeof($a); echo(countZeros($a, $n)); // This code is contributed by Ajit. ?> |
Output:
9
Recommended Posts:
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Smallest number divisible by n and has at-least k trailing zeros
- Trailing number of 0s in product of two factorials
- Count trailing zeroes in factorial of a number
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Count number of triplets with product equal to given number with duplicates allowed
- Count pairs in a sorted array whose product is less than k
- Count number of triplets with product equal to given number | Set 2
- Count number of triplets with product equal to given number
- Count arrays of length K whose product of elements is same as that of given array
- Count number of ordered pairs with Even and Odd Product
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.
Improved By : jit_t



