Count Decimal Zeros at the End of a Factorial

Quick Overview

Count factorial trailing zeros through repeated factor-of-five quotients without constructing the factorial.

Count Decimal Zeros at the End of a Factorial

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the number of trailing decimal zeros in `n!`, the factorial of `n`. Use the convention `0! = 1`. ```cpp int trailingZeroes(int n); ``` ### Examples ```text Input: n = 25 Output: 6 ``` ```text Input: n = 100 Output: 24 ``` ### Constraints - `0 <= n <= 10000`. ### Optional follow-up Can you solve it in logarithmic time?

Overview: Count factorial trailing zeros through repeated factor-of-five quotients without constructing the factorial.

Community answers

Answer by Coderka14

#include using namespace std; class Solution { public: int trailingZeroes(int n) { int count = 0; while (n > 0) { n /= 5; count += n; } return count; } };
Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the number of trailing decimal zeros in n!, the factorial of n. Use the convention 0! = 1.

int trailingZeroes(int n);

Examples

Input: n = 25
Output: 6
Input: n = 100
Output: 24

Constraints

  • 0 <= n <= 10000 .

Optional follow-up

Can you solve it in logarithmic time?