X DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Large Factorial
Medium Accuracy: 62.7% Submissions: 19198 Points: 4

You are given an array A of integers of length N. You need to calculate factorial of each number. The answer can be very large, so print it modulo 10+ 7.

Example 1:

Input:
N = 5
A[] = {0, 1, 2, 3, 4}
Output:
1 1 2 6 24
Explanation:
Factorial of 0 is 1,
factorial of 1 is 1, factorial of 2 is 2,
factorial of 3 is 6 and so on.

Example 2:

Input:
N = 3
A[] = {5, 6, 3}
Output:
120 720 6
Explanation:
Factorial of 5 is 120,
factorial of 6 is 720,
factorial of 3 is 6.

Complete the function factorial() which takes listand single integer n, as input parameters and returns a list of factorials of the numbers in the list a.

Expected Time Complexity: O(max(A) + N)
Expected Auxiliary Space: O(max(A))

Constraints:
1 <= N <= 105
0 <= A[i] <= 105

We are replacing the old Disqus forum with the new Discussions section given below.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

My Submissions:  