You are given an array arr of integers of length n. You need to calculate factorial of each number. The answer can be very large, so print it modulo 109 + 7.
Examples:
Input: arr[] = [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.
Input: arr[] = [5, 6, 3]
Output: 120 720 6
Explanation: factorial of 5 is 120, factorial of 6 is 720, factorial of 3 is 6.
Try It Yourself
Table of Content
[Naive Approach] Compute Factorial Separately - O(n × max(arr)) Time and O(n) Space
For every element, compute its factorial separately under modulo. This repeats work for larger values, so it is slower.
#include <iostream>
#include <vector>
using namespace std;
vector<int> factorial(vector<int> &arr) {
const int mod = 1000000007;
vector<int> res;
for (int x : arr) {
int fact = 1;
for (int i = 1; i <= x; i++) {
fact = (1LL * fact * i) % mod;
}
res.push_back(fact);
}
return res;
}
int main() {
vector<int> arr = {0, 1, 2, 3, 4};
vector<int> res = factorial(arr);
for (int x : res) cout << x << " ";
cout << endl;
arr = {5, 6, 3};
res = factorial(arr);
for (int x : res) cout << x << " ";
return 0;
}
import java.util.ArrayList;
class GFG {
static ArrayList<Integer> factorial(int arr[]) {
final int MOD = 1000000007;
ArrayList<Integer> res = new ArrayList<>();
for (int x : arr) {
int fact = 1;
for (int i = 1; i <= x; i++) {
fact = (int)((1L * fact * i) % MOD);
}
res.add(fact);
}
return res;
}
public static void main(String[] args) {
int arr[] = {0, 1, 2, 3, 4};
System.out.println(factorial(arr));
arr = new int[]{5, 6, 3};
System.out.println(factorial(arr));
}
}
def factorial(arr):
MOD = 1000000007
res = []
for x in arr:
fact = 1
for i in range(1, x + 1):
fact = (fact * i) % MOD
res.append(fact)
return res
if __name__ == "__main__":
arr = [0, 1, 2, 3, 4]
print(factorial(arr))
arr = [5, 6, 3]
print(factorial(arr))
using System;
using System.Collections.Generic;
class GFG {
static List<int> factorial(int[] arr) {
const int MOD = 1000000007;
List<int> res = new List<int>();
foreach (int x in arr) {
int fact = 1;
for (int i = 1; i <= x; i++) {
fact = (int)((1L * fact * i) % MOD);
}
res.Add(fact);
}
return res;
}
static void Main() {
int[] arr = {0, 1, 2, 3, 4};
Console.WriteLine(string.Join(" ", factorial(arr)));
arr = new int[] {5, 6, 3};
Console.WriteLine(string.Join(" ", factorial(arr)));
}
}
function factorial(arr) {
const MOD = 1000000007;
let res = [];
for (let x of arr) {
let fact = 1;
for (let i = 1; i <= x; i++) {
fact = (fact * i) % MOD;
}
res.push(fact);
}
return res;
}
// Driver Code
let arr = [0, 1, 2, 3, 4];
console.log(factorial(arr).join(" "));
arr = [5, 6, 3];
console.log(factorial(arr).join(" "));
Output
1 1 2 6 24 120 720 6
[Expected Approach] Precompute Factorials - O(max(arr) + n) Time and O(max(arr)) Space
Precompute factorials from 0 to the maximum element of the array once. Then directly use the precomputed value for each element.
#include <iostream>
#include <vector>
using namespace std;
vector<int> factorial(vector<int> &arr) {
const int mod = 1000000007;
int mx = 0;
for (int x : arr) {
mx = max(mx, x);
}
vector<int> fact(mx + 1);
fact[0] = 1;
for (int i = 1; i <= mx; i++) {
fact[i] = (1LL * fact[i - 1] * i) % mod;
}
vector<int> res;
for (int x : arr) {
res.push_back(fact[x]);
}
return res;
}
int main() {
vector<int> arr = {0, 1, 2, 3, 4};
vector<int> res = factorial(arr);
for (int x : res) cout << x << " ";
cout << endl;
arr = {5, 6, 3};
res = factorial(arr);
for (int x : res) cout << x << " ";
return 0;
}
import java.util.ArrayList;
class GFG {
static ArrayList<Integer> factorial(int arr[]) {
final int MOD = 1000000007;
int mx = 0;
for (int x : arr) {
mx = Math.max(mx, x);
}
int[] fact = new int[mx + 1];
fact[0] = 1;
for (int i = 1; i <= mx; i++) {
fact[i] = (int)((1L * fact[i - 1] * i) % MOD);
}
ArrayList<Integer> res = new ArrayList<>();
for (int x : arr) {
res.add(fact[x]);
}
return res;
}
public static void main(String[] args) {
int arr[] = {0, 1, 2, 3, 4};
System.out.println(factorial(arr));
arr = new int[]{5, 6, 3};
System.out.println(factorial(arr));
}
}
def factorial(arr):
MOD = 1000000007
mx = max(arr)
fact = [1] * (mx + 1)
for i in range(1, mx + 1):
fact[i] = (fact[i - 1] * i) % MOD
res = []
for x in arr:
res.append(fact[x])
return res
if __name__ == "__main__":
arr = [0, 1, 2, 3, 4]
print(factorial(arr))
arr = [5, 6, 3]
print(factorial(arr))
using System;
using System.Collections.Generic;
class GFG {
static List<int> factorial(int[] arr) {
const int MOD = 1000000007;
int mx = 0;
foreach (int x in arr) {
mx = Math.Max(mx, x);
}
int[] fact = new int[mx + 1];
fact[0] = 1;
for (int i = 1; i <= mx; i++) {
fact[i] = (int)((1L * fact[i - 1] * i) % MOD);
}
List<int> res = new List<int>();
foreach (int x in arr) {
res.Add(fact[x]);
}
return res;
}
static void Main() {
int[] arr = {0, 1, 2, 3, 4};
Console.WriteLine(string.Join(" ", factorial(arr)));
arr = new int[] {5, 6, 3};
Console.WriteLine(string.Join(" ", factorial(arr)));
}
}
function factorial(arr) {
const MOD = 1000000007;
let mx = Math.max(...arr);
let fact = new Array(mx + 1);
fact[0] = 1;
for (let i = 1; i <= mx; i++) {
fact[i] = (fact[i - 1] * i) % MOD;
}
let res = [];
for (let x of arr) {
res.push(fact[x]);
}
return res;
}
// Driver Code
let arr = [0, 1, 2, 3, 4];
console.log(factorial(arr).join(" "));
arr = [5, 6, 3];
console.log(factorial(arr).join(" "));
Output
1 1 2 6 24 120 720 6