AfterAcademy Tech
•
14 Sep 2020

Difficulty: Easy
Problem Description
Write a program to find the factorial of a given number n.
Problem Note
n is a non-negative integer.n is the multiplication of all integers smaller than or equal to nExample
Input: n = 6
Output: 720
Explanation: Factorial of 6 is 6*5*4*3*2*1 which is 720.
return 1 when x is 0, otherwise return x*f(x-1)factorial variable.You can try the problem here.
We have to find the factorial of a number. Mathematically, the factorial of a number is the product from 1 to that number.
i.e. factorial(Z) = 1 x 2 x 3 x 4 . . . x (Z-2) x (Z-1) x Z
Looking over the above equation, we can conclude that the factorial(Z) = factorial(Z-1) x Z
Now, the equation seems like a recursive structure, where factorial is a function and we have to find the factorial of Z. Now, We know that the factorial of 0 is 1 and factorial doesn’t exist for -ve numbers. We can use this as a base case of the recursive function.
Base Case
Now think about the base case, where the function will directly give you results without breaking it again into sub-problem.
fact(5)
= 5 * fact(4)
= 5 * 4 * fact(3)
= 5 * 4 * 3 * fact(2)
= 5 * 4 * 3 * 2 * fact(1)
= 5 * 4 * 3 * 2 * 1 * fact(0)
The factorial of a negative number is not defined, so fact(0) is the smallest version of the factorial problem where our solution will terminate. Here n = 0 is the base case which will return 1. Since 0! = 1.
To understand more about recursion, refer here.
Solution Steps
factorial(Z)Z is 0.Z * factorial(Z-1)Pseudo Code
int findFactorial(int n) {
if(n is 0)
return 1
return n * (findFactorial(n-1))
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Critical Ideas To Think
Instead of going recursive, we can use the logic of factorial in the way we do it in our notebooks and that is multiplying numbers from 1 to n.
So, the straight forward way is to use a for loop and iterate till n while maintaining a variable that will store the factorial.
Solution Steps
factorial variable and initialize it with 1nfactorial with ifactorialPseudo Code
int findFactorial(int n) {
int factorial = 1
for(int i = 1 to i <= n){
factorial = factorial * i
}
return factorial
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas To Think
n is 0 ?
n to 1 using a recursive functionIf you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
Given an integer k, write a program to find the closest integer (not including itself), which is a palindrome.

AfterAcademy Tech
Given a binary tree, write a program to find the maximum depth of the binary tree. The maximum depth is the number of nodes along the longest path from the root node to the leaf node. A leaf is a node with no child nodes.

AfterAcademy Tech
Given an array, find the smallest missing positive integer.

AfterAcademy Tech
Given an array A[] of n elements and a positive integer K, find the Kth smallest element in the array. It is given that all array elements are distinct.
