Given an array arr[] consisting of N positive integers, the task is to find the prefix factorials of a prefix sum array of the given array i.e.,
.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 1 6 720 3628800
Explanation:
The prefix sum of the given array is {1, 3, 6, 10}. Therefore, prefix factorials of the obtained prefix sum array is {1!, (1+2)!, (1+2+3)!, (1+2+3+4)!} = {1!, 3!, 6!, 10!} = {1 6 720 3628800}.
Input: arr[] = {2, 4, 3, 1}
Output: 2 720 362880 3628800
Naive Approach: The simplest approach to solve the given problem is to find the prefix sum of the given array and then find the factorial of each array element in the prefix sum array. After calculating the prefix sum print the factorial array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int N)
{
if (N == 1 || N == 0)
return 1;
return N * fact(N - 1);
}
void prefixFactorialArray( int arr,
int N)
{
for ( int i = 1; i < N; i++) {
arr[i] += arr[i - 1];
}
for ( int i = 0; i < N; i++) {
arr[i] = fact(arr[i]);
}
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
prefixFactorialArray(arr, N);
return 0;
}
|
Java
class GFG{
static int fact( int N)
{
if (N == 1 || N == 0 )
return 1 ;
return N * fact(N - 1 );
}
static void prefixFactorialArray( int [] arr, int N)
{
for ( int i = 1 ; i < N; i++)
{
arr[i] += arr[i - 1 ];
}
for ( int i = 0 ; i < N; i++)
{
arr[i] = fact(arr[i]);
}
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 };
int N = arr.length;
prefixFactorialArray(arr, N);
}
}
|
Python3
def fact(N):
if (N = = 1 or N = = 0 ):
return 1
return N * fact(N - 1 )
def prefixFactorialArray(arr, N):
for i in range ( 1 , N):
arr[i] + = arr[i - 1 ]
for i in range (N):
arr[i] = fact(arr[i])
for i in range (N):
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
prefixFactorialArray(arr, N)
|
C#
using System;
class GFG{
static int fact( int N)
{
if (N == 1 || N == 0)
return 1;
return N * fact(N - 1);
}
static void prefixFactorialArray( int [] arr,
int N)
{
for ( int i = 1; i < N; i++)
{
arr[i] += arr[i - 1];
}
for ( int i = 0; i < N; i++)
{
arr[i] = fact(arr[i]);
}
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
prefixFactorialArray(arr, N);
}
}
|
Javascript
<script>
function fact(N) {
if (N == 1 || N == 0)
return 1;
return N * fact(N - 1);
}
function prefixFactorialArray(arr, N) {
for (let i = 1; i < N; i++) {
arr[i] += arr[i - 1];
}
for (let i = 0; i < N; i++) {
arr[i] = fact(arr[i]);
}
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
let arr = [1, 2, 3, 4];
let N = arr.length;
prefixFactorialArray(arr, N);
</script>
|
Output:
1 6 720 3628800
Time Complexity: O(N*M), where M is the sum of the array elements.
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by precalculating the factorial of sum of the array elements so that the factorial calculation at each index is can be calculated in O(1) time.
Below is an implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void prefixFactorialArray( int A[], int N)
{
for ( int i = 1; i < N; i++) {
A[i] += A[i - 1];
}
int fact[A[N - 1] + 1];
fact[0] = 1;
for ( int i = 1; i <= A[N - 1]; i++) {
fact[i] = i * fact[i - 1];
}
for ( int i = 0; i < N; i++) {
A[i] = fact[A[i]];
}
for ( int i = 0; i < N; i++) {
cout << A[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
prefixFactorialArray(arr, N);
return 0;
}
|
Java
class GFG{
static void prefixFactorialArray( int A[], int N)
{
for ( int i = 1 ; i < N; i++)
{
A[i] += A[i - 1 ];
}
int fact[] = new int [A[N - 1 ] + 1 ];
fact[ 0 ] = 1 ;
for ( int i = 1 ; i <= A[N - 1 ]; i++)
{
fact[i] = i * fact[i - 1 ];
}
for ( int i = 0 ; i < N; i++)
{
A[i] = fact[A[i]];
}
for ( int i = 0 ; i < N; i++)
{
System.out.print(A[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
prefixFactorialArray(arr, N);
}
}
|
Python3
def prefixFactorialArray(A, N):
for i in range ( 1 , N):
A[i] + = A[i - 1 ]
fact = [ 0 for x in range (A[N - 1 ] + 1 )]
fact[ 0 ] = 1
for i in range ( 1 , A[N - 1 ] + 1 ):
fact[i] = i * fact[i - 1 ]
for i in range ( 0 , N):
A[i] = fact[A[i]]
for i in range ( 0 , N):
print (A[i], end = " " )
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
prefixFactorialArray(arr, N)
|
C#
using System;
class GFG {
static void prefixFactorialArray( int [] A, int N)
{
for ( int i = 1; i < N; i++)
{
A[i] += A[i - 1];
}
int [] fact = new int [A[N - 1] + 1];
fact[0] = 1;
for ( int i = 1; i <= A[N - 1]; i++)
{
fact[i] = i * fact[i - 1];
}
for ( int i = 0; i < N; i++)
{
A[i] = fact[A[i]];
}
for ( int i = 0; i < N; i++)
{
Console.Write(A[i] + " " );
}
}
static void Main() {
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
prefixFactorialArray(arr, N);
}
}
|
Javascript
<script>
function prefixFactorialArray(A, N) {
for (let i = 1; i < N; i++) {
A[i] += A[i - 1];
}
let fact = new Array(A[N - 1] + 1);
fact[0] = 1;
for (let i = 1; i <= A[N - 1]; i++) {
fact[i] = i * fact[i - 1];
}
for (let i = 0; i < N; i++) {
A[i] = fact[A[i]];
}
for (let i = 0; i < N; i++) {
document.write(A[i] + " " );
}
}
let arr = [1, 2, 3, 4];
let N = arr.length
prefixFactorialArray(arr, N);
</script>
|
Time Complexity: O(N + M), where M is the sum of the array elements.
Auxiliary Space: O(M), where M is the sum of the array elements.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!