Count number of trailing zeros in product of array
Last Updated :
01 Aug, 2022
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.
Implementation:
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 << countZeros(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 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;
}
// 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
Python3
# 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 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;
}
// 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.
?>
JavaScript
<script>
// Javascript program for count total
// zero in product of array
// Returns count of zeros in product of array
function countZeros(a, n)
{
let count2 = 0, count5 = 0;
for(let i = 0; i < n; i++)
{
// Count number of 2s in each element
while (a[i] % 2 == 0)
{
a[i] = parseInt(a[i] / 2);
count2++;
}
// Count number of 5s in each element
while (a[i] % 5 == 0)
{
a[i] = parseInt(a[i] / 5);
count5++;
}
}
// Return the minimum
return (count2 < count5) ? count2 : count5;
}
// Driver code
let a = [ 10, 100, 20, 30, 50, 90, 12, 80 ];
let n = a.length;
document.write(countZeros(a, n));
// This code is contributed by souravmahato348
</script>
Time Complexity: O(n * (log2m + log5m)), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Trailing number of 0s in product of two factorials Given two integer N or M find the number of zero's trailing in product of factorials (N!*M!)? Examples: Input : N = 4, M = 5 Output : 1 Explanation : 4! = 24, 5! = 120 Product has only 1 trailing 0. Input : N = 127!, M = 57! Output : 44 As discussed in number of zeros in N! can be calculated by recu
5 min read
Count trailing zeroes in factorial of a number Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5Output: 1 Explanation: Factorial of 5 is 120 which has one trailing 0.Input: n = 20Output: 4Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.Input: n = 100Output: 2
8 min read
Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. Given an integer n, the task is to find the number of trailing zeros in the function f(n)=\prod\limits_{i = 1}^{n} i^{i} i.e. f(n) = 11 * 22 * 33 * ... * nn. Examples: Input: n = 5 Output: 5 f(5) = 11 * 22 * 33 * 44 * 55 = 1 * 4 * 27 * 256 * 3125 = 86400000 Input: n = 12 Output: 15 Approach: We know
7 min read
Count number of trailing zeros in Binary representation of a number using Bitset Given a number. The task is to count the number of Trailing Zero in Binary representation of a number using bitset.Examples: Input : N = 16Output : 4Binary representation of N is 10000. Therefore,number of zeroes at the end is 4.Input : N = 8Output : 3Approach: We simply set the number in the bitset
5 min read
Number of trailing zeros in N * (N - 2) * (N - 4)*.... Given an integer N, the task is to find the number of trailing zeros in the decimal notation of f(N) where f(N) = 1 if N < 2 and f(N) = N * f(N - 2) if N ? 2 Examples: Input: N = 12 Output: 1 f(12) = 12 * 10 * 8 * 6 * 4 * 2 = 46080 Input: N = 7 Output: 0 Approach: The number of trailing zeros whe
4 min read
Maximum number of trailing zeros in the product of the subsets of size k 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, havin
9 min read
Count trailing zeroes present in binary representation of a given number using XOR Given an integer N, the task is to find the number of trailing zeroes in the binary representation of the given number. Examples: Input: N = 12Output: 2Explanation:The binary representation of the number 13 is "1100".Therefore, there are two trailing zeros in the 12. Input: N = -56Output: 3Explanati
4 min read
Count of index pairs in array whose range product is a positive integer Given an array A of non-zero integers, the task is to find the number of pairs (l, r) where (l <= r) such that A[l]*A[l+1]*A[l+2]....A[r] is positive. Examples: Input: A = {5, -3, 3, -1, 1} Output: 7 Explanation: First pair, (1, 1) = 5 is positive Second pair, (3, 3) = 3 is positive Third pair, (
6 min read
Number of pairs in an array having sum equal to product Given an array arr[], the task is to find the number of pairs (arr[i], arr[j]) in the array such that arr[i] + arr[j] = arr[i] * arr[j]Examples: Input: arr[] = {2, 2, 3, 4, 6} Output: 1 (2, 2) is the only possible pair as (2 + 2) = (2 * 2) = 4.Input: arr[] = {1, 2, 3, 4, 5} Output: 0 Approach: The o
5 min read
Count triplets having product 0 from a given array Given an array arr[] of size N, the task is to count the number of triplets (arr[i], arr[j], arr[k]) such that arr[i] * arr[j] = arr[j] * arr[k] = 0 (i < j < k). Examples: Input: arr[] = {0, 8, 12, 0} Output: 2 Explanation: Triplets satisfying the given conditions are (0, 8, 0) and (0, 12, 0).
8 min read