Given an array with n numbers. The task is to print number of consecutive zero’s at the end after multiplying all the n number.
Examples :
Input : arr[] = {100, 10}
Output : 3
Explanation : 100 x 10 = 1000,
3 zero's at the end.
Input : arr[] = {100, 10, 5, 25, 35, 14}
Output : 4
Explanation :
100 x 10 x 5 x 25 x 35 x 14 = 61250000,
4 zero's at the end
Naive Approach :
First multiple all the number and save into the string(because if the multiplicative number is big as 2^64 then it give wrong, so store every multiple in string) and then count the number of zero’s.
Efficient approach :
First count the 2’s factor of n numbers and then count the 5’s factor of all n number then print the smallest one.
For Example –
n_number's | 2's factor | 5's factor
100 | 2 | 2
10 | 1 | 1
5 | 0 | 1
25 | 0 | 2
35 | 0 | 1
14 | 1 | 0
Total | 4 | 7
we can take a minimum so there number of
zero's is 4
Below is the implementation of above approach :
C++
#include<iostream>
using namespace std;
int two_factor( int n)
{
int twocount = 0;
while (n % 2 == 0)
{
twocount++;
n = n / 2;
}
return twocount;
}
int five_factor( int n)
{
int fivecount = 0;
while (n % 5 == 0)
{
fivecount++;
n = n / 5;
}
return fivecount;
}
int find_con_zero( int arr[], int n)
{
int twocount = 0;
int fivecount = 0;
for ( int i = 0; i < n; i++) {
twocount += two_factor(arr[i]);
fivecount += five_factor(arr[i]);
}
if (twocount < fivecount)
return twocount;
else
return fivecount;
}
int main()
{
int arr[] = { 100, 10, 5, 25, 35, 14 };
int n = 6;
cout << find_con_zero(arr, n);
}
|
Java
public class GfG{
static int two_factor( int n)
{
int twocount = 0 ;
while (n % 2 == 0 )
{
twocount++;
n = n / 2 ;
}
return twocount;
}
static int five_factor( int n)
{
int fivecount = 0 ;
while (n % 5 == 0 )
{
fivecount++;
n = n / 5 ;
}
return fivecount;
}
static int find_con_zero( int arr[], int n)
{
int twocount = 0 ;
int fivecount = 0 ;
for ( int i = 0 ; i < n; i++) {
twocount += two_factor(arr[i]);
fivecount += five_factor(arr[i]);
}
if (twocount < fivecount)
return twocount;
else
return fivecount;
}
public static void main(String s[])
{
int arr[] = { 100 , 10 , 5 , 25 , 35 , 14 };
int n = 6 ;
System.out.println(find_con_zero(arr, n));
}
}
|
Python3
def two_factor( n ):
twocount = 0
while n % 2 = = 0 :
twocount + = 1
n = int ( n / 2 )
return twocount
def five_factor( n ):
fivecount = 0
while n % 5 = = 0 :
fivecount + = 1
n = int (n / 5 )
return fivecount
def find_con_zero( arr, n ):
twocount = 0
fivecount = 0
for i in range (n):
twocount + = two_factor(arr[i])
fivecount + = five_factor(arr[i])
if twocount < fivecount:
return twocount
else :
return fivecount
arr = [ 100 , 10 , 5 , 25 , 35 , 14 ]
n = 6
print (find_con_zero(arr, n))
|
C#
using System;
public class GfG {
static int two_factor( int n)
{
int twocount = 0;
while (n % 2 == 0)
{
twocount++;
n = n / 2;
}
return twocount;
}
static int five_factor( int n)
{
int fivecount = 0;
while (n % 5 == 0)
{
fivecount++;
n = n / 5;
}
return fivecount;
}
static int find_con_zero( int []arr, int n)
{
int twocount = 0;
int fivecount = 0;
for ( int i = 0; i < n; i++) {
twocount += two_factor(arr[i]);
fivecount += five_factor(arr[i]);
}
if (twocount < fivecount)
return twocount;
else
return fivecount;
}
public static void Main()
{
int []arr = { 100, 10, 5, 25, 35, 14 };
int n = 6;
Console.WriteLine(find_con_zero(arr, n));
}
}
|
Javascript
<script>
function two_factor(n)
{
let twocount = 0;
while (n % 2 == 0)
{
twocount++;
n = n / 2;
}
return twocount;
}
function five_factor(n)
{
let fivecount = 0;
while (n % 5 == 0)
{
fivecount++;
n = n / 5;
}
return fivecount;
}
function find_con_zero(arr, n)
{
let twocount = 0;
let fivecount = 0;
for (let i = 0; i < n; i++)
{
twocount += two_factor(arr[i]);
fivecount += five_factor(arr[i]);
}
if (twocount < fivecount)
return twocount;
else
return fivecount;
}
let arr = [ 100, 10, 5, 25, 35, 14 ];
let n = 6;
document.write(find_con_zero(arr, n));
</script>
|
PHP
<?php
function two_factor( $n )
{
$twocount = 0;
while ( $n % 2 == 0)
{
$twocount ++;
$n = (int)( $n / 2);
}
return $twocount ;
}
function five_factor( $n )
{
$fivecount = 0;
while ( $n % 5 == 0)
{
$fivecount ++;
$n = (int)( $n / 5);
}
return $fivecount ;
}
function find_con_zero( $arr , $n )
{
$twocount = 0;
$fivecount = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$twocount += two_factor( $arr [ $i ]);
$fivecount += five_factor( $arr [ $i ]);
}
if ( $twocount < $fivecount )
return $twocount ;
else
return $fivecount ;
}
$arr = array (100, 10, 5,
25, 35, 14);
$n = 6;
echo find_con_zero( $arr , $n );
?>
|
Output :
4
Approach#2: Using for and while
Multiply all the numbers and count the number of trailing zeros.
Algorithm
1. Initialize a variable “prod” to 1.
2. Traverse the given array and for each number:
a. Multiply it with “prod”.
3. Count the number of trailing zeros in “prod”.
4. Return the count.
C++
#include <iostream>
#include <vector>
using namespace std;
int count_zeros(vector< int > arr) {
int prod = 1;
for ( int num : arr) {
prod *= num;
}
int count = 0;
while (prod % 10 == 0) {
count++;
prod /= 10;
}
return count;
}
int main() {
vector< int > arr = { 100, 10, 5, 25, 35, 14 };
cout << count_zeros(arr) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int count_zeros( int [] arr)
{
int prod = 1 ;
for ( int num : arr) {
prod *= num;
}
int count = 0 ;
while (prod % 10 == 0 ) {
count++;
prod /= 10 ;
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 100 , 10 , 5 , 25 , 35 , 14 };
System.out.println(count_zeros(arr));
}
}
|
Python3
def count_zeros(arr):
prod = 1
for num in arr:
prod * = num
count = 0
while prod % 10 = = 0 :
count + = 1
prod / / = 10
return count
arr = [ 100 , 10 , 5 , 25 , 35 , 14 ]
print (count_zeros(arr))
|
C#
using System;
class GFG
{
static int CountZeros( int [] arr)
{
int prod = 1;
foreach ( int num in arr)
{
prod *= num;
}
int count = 0;
while (prod % 10 == 0)
{
count++;
prod /= 10;
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 100, 10, 5, 25, 35, 14 };
Console.WriteLine(CountZeros(arr));
}
}
|
Javascript
function count_zeros(arr) {
let prod = 1;
for (let num of arr) {
prod *= num;
}
let count = 0;
while (prod % 10 == 0) {
count++;
prod = Math.floor(prod/10);
}
return count;
}
let arr = [ 100, 10, 5, 25, 35, 14 ];
console.log(count_zeros(arr));
|
Time Complexity: O(N*log_10(max_num)) where N is the number of elements in the array and max_num is the maximum element in the array.
Auxiliary Space: O(1).
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!