Saturday, October 11, 2025
HomeData Modelling & AINumber of trailing zeros in N * (N – 2) * (N...

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:
f(12) = 12 * 10 * 8 * 6 * 4 * 2 = 46080

Input: N = 7 
Output:
 

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:  

  1. 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.
  2. 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)

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!

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6719 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS