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 = 46080Input: N = 7
Output: 0
Approach: The number of trailing zeros when f(N) is expressed in decimal notation is the number of times f(N) is divisible by 2 and the number of times f(N) is divisible by 5. There are two cases:
- When N is odd then f(N) is the product of some odd numbers, so it does not break at 2. So the answer is always 0.
- When N is even then f(N) can be represented as 2 (1 * 2 * 3 * …. * N/2). The number of times f(N) is divisible by 2 is greater than the number of times divisible by 5, so only consider the number of times divisible by 5. Now, this problem is similar to count trailing zeroes in factorial of a number.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to return the count of // trailing 0s in the given function int findTrailingZeros( int n)
{ // If n is odd
if (n & 1)
return 0;
// If n is even
else {
int ans = 0;
// Find the trailing zeros
// in n/2 factorial
n /= 2;
while (n) {
ans += n / 5;
n /= 5;
}
// Return the required answer
return ans;
}
} // Driver code int main()
{ int n = 12;
cout << findTrailingZeros(n);
return 0;
} |
Java
// Java implementation of the approach import java.util.*;
class GFG
{ // Function to return the count of
// trailing 0s in the given function
static int findTrailingZeros( int n)
{
// If n is odd
if ((n & 1 ) == 1 )
return 0 ;
// If n is even
else
{
int ans = 0 ;
// Find the trailing zeros
// in n/2 factorial
n /= 2 ;
while (n != 0 )
{
ans += n / 5 ;
n /= 5 ;
}
// Return the required answer
return ans;
}
}
// Driver code
public static void main (String[] args)
{
int n = 12 ;
System.out.println(findTrailingZeros(n));
}
} // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the count of # trailing 0s in the given function def findTrailingZeros(n):
# If n is odd
if (n & 1 ):
return 0
# If n is even
else :
ans = 0
# Find the trailing zeros
# in n/2 factorial
n / / = 2
while (n):
ans + = n / / 5
n / / = 5
# Return the required answer
return ans
# Driver code n = 12
print (findTrailingZeros(n))
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System;
class GFG
{ // Function to return the count of
// trailing 0s in the given function
static int findTrailingZeros( int n)
{
// If n is odd
if ((n & 1) == 1)
return 0;
// If n is even
else
{
int ans = 0;
// Find the trailing zeros
// in n/2 factorial
n /= 2;
while (n != 0)
{
ans += n / 5;
n /= 5;
}
// Return the required answer
return ans;
}
}
// Driver code
public static void Main(String[] args)
{
int n = 12;
Console.WriteLine(findTrailingZeros(n));
}
} // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript implementation of the approach // Function to return the count of // trailing 0s in the given function function findTrailingZeros(n)
{ // If n is odd
if (n & 1)
return 0;
// If n is even
else
{
let ans = 0;
// Find the trailing zeros
// in n/2 factorial
n = parseInt(n / 2);
while (n)
{
ans += parseInt(n / 5);
n = parseInt(n / 5);
}
// Return the required answer
return ans;
}
} // Driver code let n = 12; document.write(findTrailingZeros(n)); // This code is contributed by subhammahato348 </script> |
Output:
1
Time Complexity: O(log5n)
Auxiliary Space: O(1)