Factorial of an Array of integers
Last Updated :
15 Dec, 2021
Given an array with positive integers. The task is to find the factorial of all the array elements.
Note: As the numbers would be very large, print them by taking modulus with 109+7.
Examples:
Input: arr[] = {3, 10, 200, 20, 12}
Output: 6 3628800 722479105 146326063 479001600
Input: arr[] = {5, 7, 10}
Output: 120 5040 3628800
Naive approach: We know that there is a simple approach to calculate the factorial of a number. We can run a loop for all array values and can find the factorial of every number using the above approach.
Time Complexity would be O(N2)
Space Complexity would be O(1)
Efficient Approach: We know that the factorial of a number:
N! = N*(N-1)*(N-2)*(N-3)*****3*2*1
The recursive formulae to calculate the factorial of a number is:
fact(N) = N*fact(N-1).
Hence, we will build an array in a bottom-up manner using the above recursion. Once we have stored the values in the array then we can answer the queries in O(1) time. Hence, the overall time complexity would be O(N). We can use this method only if the array values are less than 10^6. Otherwise, we would not be able to store them in an array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007 ;
int SIZE = 10000;
vector< long > fact;
void factorial()
{
int i;
fact.push_back(( long )1);
for (i = 1; i <= SIZE; i++)
{
fact.push_back((fact[i - 1] * i) % MOD);
}
}
void PrintFactorial( int arr[], int n)
{
for ( int i = 0; i < n; i += 1)
{
cout << fact[arr[i]] << " " ;
}
}
int main()
{
int arr[] = {3, 10, 200, 20, 12};
int n = sizeof (arr) / sizeof (arr[0]);
factorial();
PrintFactorial(arr,n);
return 0;
}
|
Java
<script>
import java.util.*;
class Sol
{
static int MOD = 1000000007 ;
static int SIZE = 10000 ;
static Vector<Long> fact = new Vector<Long>();
static void factorial()
{
int i;
fact.add(( long ) 1 );
for (i = 1 ; i <= SIZE; i++)
{
fact.add((fact.get(i - 1 ) * i) % MOD);
}
}
static void PrintFactorial( int arr[], int n)
{
for ( int i = 0 ; i < n; i += 1 )
{
System.out.print(fact.get(arr[i])+ " " );
}
}
public static void main(String args[])
{
int arr[] = { 3 , 10 , 200 , 20 , 12 };
int n = arr.length;
factorial();
PrintFactorial(arr,n);
}
}
</script>
|
Python3
mod = 1000000007
SIZE = 10000
fact = [ 1 ] * SIZE
def factorial():
for i in range ( 1 , SIZE):
fact[i] = (fact[i - 1 ] * i) % mod
factorial()
arr = [ 3 , 10 , 200 , 20 , 12 ]
for i in arr:
print (fact[i])
|
PHP
<?php
$MOD = 1000000007 ;
$SIZE = 10000 ;
function factorial( $fact )
{
$fact [0] = 1;
for ( $i = 1; $i <= $GLOBALS [ 'SIZE' ]; $i ++)
{
$fact [ $i ] = ( $fact [ $i - 1] * $i ) % $GLOBALS [ 'MOD' ];
}
return $fact ;
}
function PrintFactorial( $fact , $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
echo $fact [ $arr [ $i ]], " " ;
}
}
$fact = array_fill (0, $SIZE + 1, 0);
$arr = array (3, 10, 200, 20, 12);
$n = count ( $arr );
$fact = factorial( $fact );
PrintFactorial( $fact , $arr , $n );
?>
|
C#
using System.Collections.Generic;
using System;
class Sol
{
static int MOD = 1000000007 ;
static int SIZE = 10000;
static List< long > fact = new List< long >();
static void factorial()
{
int i;
fact.Add(( long )1);
for (i = 1; i <= SIZE; i++)
{
fact.Add((fact[i - 1] * i) % MOD);
}
}
static void PrintFactorial( int []arr, int n)
{
for ( int i = 0; i < n; i += 1)
{
Console.Write(fact[arr[i]]+ " " );
}
}
public static void Main(String []args)
{
int []arr = {3, 10, 200, 20, 12};
int n = arr.Length;
factorial();
PrintFactorial(arr,n);
}
}
|
Javascript
let MOD = 1000000007;
let SIZE = 10000;
function factorial(fact)
{
fact[0] = 1;
for (let i = 1; i <= SIZE; i++)
{
fact[i] = (fact[i - 1] * i) % MOD;
}
return fact;
}
function PrintFactorial(fact, arr, n)
{
for (let i = 0; i < n; i++)
{
document.write(fact[arr[i]] + " " );
}
}
let fact = new Array(SIZE + 1).fill(0);
let arr = new Array(3, 10, 200, 20, 12);
let n = arr.length;
fact = factorial(fact);
PrintFactorial(fact,arr,n);
|
Output:
6 3628800 722479105 146326063 479001600
Time Complexity : O(N)
Space Complexity : O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...