Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Trailing Zeroes Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Trailing Zeroes | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a number ‘n’, find the no of trailing zeroes in the factorial of ‘n’.

Naive Approach

In the naive approach, we can calculate the factorial of n, and then naively calculate the number of trailing zeroes in it using a while loop.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
int trailingZeroesInFactorial(int n) {
   int count = 0;
   int factorial = 1;
   for (int i = 1; i <= n; i++) {
       factorial *= i;
   }
   while (factorial % 10 == 0) {
       count++;
       factorial /= 10;
   }
   return count;
}
Java
class Solution {
   int trailingZeroesInFactorial(int n) {
       int count = 0;
       int factorial = 1;
       for (int i = 1; i <= n; i++) {
           factorial *= i;
       }
       while (factorial % 10 == 0) {
           count++;
           factorial /= 10;
       }
       return count;
   }
}

Optimal Approach

We will try to solve this problem in logarithmic time complexity.

Observe that we basically want to find the number of 10s in the factors of n!. Since 10 = 2 * 5, we basically need the minimum of the number of 2s and the number of 5s in the factorial of the number, so that we can pair up all the 2s and 5s to form 10s.

It can be proved that the number of 2s in n! is always greater than or equal to the number of 5s in n!. So, our problem boils down to find the number of 5s in the prime factors of n!.

This can be solved by using the following formula: Answer = floor(n / 5) + floor(n / 25) + …. till the sequence converges to zero.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
int trailingZeroesInFactorial(int n) {
   int count = 0;
   int powerOf5 = 5;
   while(n >= powerOf5) {
       count += (n / powerOf5);
       powerOf5 *= 5;
   }
   return count;
}
Java
class Solution {
   int trailingZeroesInFactorial(int n) {
       int count = 0;
       int powerOf5 = 5;
       while(n >= powerOf5) {
           count += (n / powerOf5);
           powerOf5 *= 5;
       }
       return count;
   }
}
Related Content
Excel Column Number
Exponentiation With Modulus
Greatest Common Divisor
Matrix Paths
Primes upto N
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet