Evaluate the expression ( N1 * (N – 1)2 * … * 1N) % (109 + 7)
Last Updated :
23 Apr, 2021
Given an integer N, the task is to find the value of the expression ( N1 * (N – 1)2 * … * 1N) % (109 + 7).
Input: N = 1
Output: 1
Explanation:
11 = 1
Input: N = 4
Output: 288
Explanation:
41 * (4 – 1)2 * (4 – 2)3 * (4-3)4
= 4 * 9 * 8 * 1
= 288
Naive Approach: The simplest approach to solve this problem is to iterate over the range [1, N]. For every ith iteration, calculate the value of (N – i + 1)i. Finally, print the product of all the calculated values from each iteration.
Time Complexity: O(N2 * log2(N))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
F(N) = N1 * (N – 1)2 * … * 1N
= N * (N – 1) * (N – 1) * (N – 2) * (N – 2) * (N – 2)* … 1 * 1 * 1
= N * (N – 1) * (N – 2)*… * 1 * (N -1) * (N – 2) * …* 1 * …
= N! * (N – 1)! * (N – 2)! * … * 1!
Follow the steps below to solve the problem:
- Precompute the value of the factorial from 1 to N using factorial(N) = N * factorial(N – 1).
- Iterate over the range [1, N] and find the product of all the factorials over the range [1, N] using the above observations
- Finally, print the value of the expression.
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int ValOfTheExpression( int n)
{
int factorial[n] = { 0 };
factorial[0] = factorial[1] = 1;
for ( int i = 2; i <= n; i++) {
factorial[i] = ((factorial[i - 1] % mod)
* (i % mod))
% mod;
}
int dp[n] = { 0 };
dp[1] = 1;
for ( int i = 2; i <= n; i++) {
dp[i] = ((dp[i - 1] % mod)
* (factorial[i] % mod))
% mod;
}
return dp[n];
}
int main()
{
int n = 4;
cout << ValOfTheExpression(n) << "\n" ;
}
|
Java
class GFG
{
static int mod = 1000000007 ;
static int ValOfTheExpression( int n)
{
int [] factorial = new int [n + 1 ];
factorial[ 0 ] = factorial[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++)
{
factorial[i] = ((factorial[i - 1 ] % mod)
* (i % mod)) % mod;
}
int [] dp = new int [n + 1 ];
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++)
{
dp[i] = ((dp[i - 1 ] % mod)
* (factorial[i] % mod)) % mod;
}
return dp[n];
}
public static void main(String[] args) {
int n = 4 ;
System.out.println(ValOfTheExpression(n));
}
}
|
Python3
mod = 1000000007
def ValOfTheExpression(n):
global mod
factorial = [ 0 for i in range (n + 1 )]
factorial[ 0 ] = 1
factorial[ 1 ] = 1
for i in range ( 2 , n + 1 , 1 ):
factorial[i] = ((factorial[i - 1 ] % mod) * (i % mod)) % mod
dp = [ 0 for i in range (n + 1 )]
dp[ 1 ] = 1
for i in range ( 2 , n + 1 , 1 ):
dp[i] = ((dp[i - 1 ] % mod) * (factorial[i] % mod)) % mod
return dp[n]
if __name__ = = '__main__' :
n = 4
print (ValOfTheExpression(n))
|
C#
using System;
class GFG
{
static int mod = 1000000007;
static int ValOfTheExpression( int n)
{
int [] factorial = new int [n + 1];
factorial[0] = factorial[1] = 1;
for ( int i = 2; i <= n; i++)
{
factorial[i] = ((factorial[i - 1] % mod)
* (i % mod))
% mod;
}
int [] dp = new int [n + 1];
dp[1] = 1;
for ( int i = 2; i <= n; i++)
{
dp[i] = ((dp[i - 1] % mod)
* (factorial[i] % mod))
% mod;
}
return dp[n];
}
static void Main()
{
int n = 4;
Console.WriteLine(ValOfTheExpression(n));
}
}
|
Javascript
<script>
let mod = 1000000007;
function ValOfTheExpression(n)
{
let factorial = new Array(n + 1);
factorial[0] = factorial[1] = 1;
for (let i = 2; i <= n; i++)
{
factorial[i] = ((factorial[i - 1] % mod) *
(i % mod)) % mod;
}
let dp = new Array(n + 1);
dp[1] = 1;
for (let i = 2; i <= n; i++)
{
dp[i] = ((dp[i - 1] % mod) *
(factorial[i] % mod)) % mod;
}
return dp[n];
}
let n = 4;
document.write(ValOfTheExpression(n));
</script>
|
Time Complexity: O(N )
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...