Array ( [0] => [1] => questions [2] => Strings [3] => Trailing-Zeros )
Given a Number N. You have to find the number of trailing zeros of a factorial of given number N.
First line contains a number N.
Print the tailing zeros.
5
1
8
1
10
2
Login to see Discussion
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.
Login to see Solution