Given an array A, I have to multiply all the elements of array. Since the numbers can go upto 10^9 , I have to find the trailing zeroes of the product. I did the following :
int ans= 0; // to count no of trailing zeroes
for(long int i =0; i<n ; i++) // n can be very large(upto 10^5)
{
long long int p=1;
p=p*a[i];
while (p2℅10 ==0)
{ ans++;
p=p/10;
}
}
Function to calculate number of 2's is as follows. I replaced 2 with 5 to calculate nunber of 5's.
Int nm2(long long a)
{
Int b=0;
while(a℅2==0){
a=a/2;
b++;
}
return b;
}
Int p2=0,p5=0;
For(long long i=L;i<R;i++)
{
p2 += nm2(a[i]);
p5 += nm5 (a[i]);
}
Int ans += min(p2,p5); // storing no of zeroes every time I multiply elements of array from Index L to Index R of array a[].
How can I improve it ? Or is there any other different way to calculate it with faster efficiency. Please help me out.