Array ( [0] => [1] => questions [2] => Arrays [3] => Trailing-Zeros ) Arrays | Trailing Zeros | THE INQUISITIVE





Trailing Zeros

LEVEL:Beginner

Description

Given a Number N. You have to find the number of trailing zeros of a factorial of given number N.

Input Format

First line contains a number N.

Output Format

Print the tailing zeros.


Example 1:

Input
5
Output
1
Example 2:

Input
8
Output
1
Example 3:

Input
10
Output
2

oops

Login to see Discussion




Approach

Approach 1: using mod operator

Step-i) calculate the factorial of the given number
Step-ii) using mod 10 get the remainder of that number
Step-iii) if the remainder is 0 increment the count
Step-iv) Repeat step ii and iii till the remainder not equal to 0
Step-v) return the count

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

Approach 2: coverting to string

Step-i) calculate the factorial of the given number
Step-ii) covert it to string
Step-iii) traverse from back of string till a non zero number arrives and keep updating count if zero arrives
Step-iv) return the count

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

Approach 3: Diving the number by power of 5

Step-i) initialize i = 5
Step-ii) divide given number by 5 and add it to count
Step-iii)update i value to i = i* 5
Step-iv) repeat step ii and iii till num/i >=i
Step-v) return the count

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


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution