给定一个由N 个正整数组成的数组arr[] ,任务是找到给定数组的前缀和数组的前缀阶乘,即, .
例子:
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
朴素方法:解决给定问题的最简单方法是找到给定数组的前缀和,然后在前缀和数组中找到每个数组元素的阶乘。计算前缀和后打印阶乘数组。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the factorial of
// a number N
int fact(int N)
{
// Base Case
if (N == 1 || N == 0)
return 1;
// Find the factorial recursively
return N * fact(N - 1);
}
// Function to find the prefix
// factorial array
void prefixFactorialArray(int arr,
int N)
{
// Find the prefix sum array
for (int i = 1; i < N; i++) {
arr[i] += arr[i - 1];
}
// Find the factorials of each
// array element
for (int i = 0; i < N; i++) {
arr[i] = fact(arr[i]);
}
// Print the resultant array
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
prefixFactorialArray(arr, N);
return 0;
}
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the factorial of
// a number N
static int fact(int N)
{
// Base Case
if (N == 1 || N == 0)
return 1;
// Find the factorial recursively
return N * fact(N - 1);
}
// Function to find the prefix
// factorial array
static void prefixFactorialArray(int[] arr,
int N)
{
// Find the prefix sum array
for(int i = 1; i < N; i++)
{
arr[i] += arr[i - 1];
}
// Find the factorials of each
// array element
for(int i = 0; i < N; i++)
{
arr[i] = fact(arr[i]);
}
// Print the resultant array
for(int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 4 };
int N = arr.Length;
prefixFactorialArray(arr, N);
}
}
// This code is contributed by code_hunt
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the factorial of
// prefix sum at every possible index
void prefixFactorialArray(int A[], int N)
{
// Find the prefix sum array
for (int i = 1; i < N; i++) {
A[i] += A[i - 1];
}
// Stores the factorial of all the
// element till the sum of array
int fact[A[N - 1] + 1];
fact[0] = 1;
// Find the factorial array
for (int i = 1; i <= A[N - 1]; i++) {
fact[i] = i * fact[i - 1];
}
// Find the factorials of
// each array element
for (int i = 0; i < N; i++) {
A[i] = fact[A[i]];
}
// Print the resultant array
for (int i = 0; i < N; i++) {
cout << A[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
prefixFactorialArray(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the factorial of
// prefix sum at every possible index
static void prefixFactorialArray(int A[], int N)
{
// Find the prefix sum array
for(int i = 1; i < N; i++)
{
A[i] += A[i - 1];
}
// Stores the factorial of all the
// element till the sum of array
int fact[] = new int[A[N - 1] + 1];
fact[0] = 1;
// Find the factorial array
for(int i = 1; i <= A[N - 1]; i++)
{
fact[i] = i * fact[i - 1];
}
// Find the factorials of
// each array element
for(int i = 0; i < N; i++)
{
A[i] = fact[A[i]];
}
// Print the resultant array
for(int i = 0; i < N; i++)
{
System.out.print(A[i] + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
prefixFactorialArray(arr, N);
}
}
// This code is contributed by abhinavjain194
输出:
1 6 720 3628800
时间复杂度: O(N*M),其中 M 是数组元素的总和。
辅助空间: O(1)
高效方法:上述方法也可以通过预先计算数组元素之和的阶乘来优化,使得每个索引处的阶乘计算可以在O(1) 时间内完成。
下面是上述方法的一个实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the factorial of
// prefix sum at every possible index
void prefixFactorialArray(int A[], int N)
{
// Find the prefix sum array
for (int i = 1; i < N; i++) {
A[i] += A[i - 1];
}
// Stores the factorial of all the
// element till the sum of array
int fact[A[N - 1] + 1];
fact[0] = 1;
// Find the factorial array
for (int i = 1; i <= A[N - 1]; i++) {
fact[i] = i * fact[i - 1];
}
// Find the factorials of
// each array element
for (int i = 0; i < N; i++) {
A[i] = fact[A[i]];
}
// Print the resultant array
for (int i = 0; i < N; i++) {
cout << A[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
prefixFactorialArray(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the factorial of
// prefix sum at every possible index
static void prefixFactorialArray(int A[], int N)
{
// Find the prefix sum array
for(int i = 1; i < N; i++)
{
A[i] += A[i - 1];
}
// Stores the factorial of all the
// element till the sum of array
int fact[] = new int[A[N - 1] + 1];
fact[0] = 1;
// Find the factorial array
for(int i = 1; i <= A[N - 1]; i++)
{
fact[i] = i * fact[i - 1];
}
// Find the factorials of
// each array element
for(int i = 0; i < N; i++)
{
A[i] = fact[A[i]];
}
// Print the resultant array
for(int i = 0; i < N; i++)
{
System.out.print(A[i] + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
prefixFactorialArray(arr, N);
}
}
// This code is contributed by abhinavjain194
输出:
1 6 720 3628800
时间复杂度: O(N + M),其中 M 是数组元素的总和。
辅助空间: O(M),其中 M 是数组元素的总和。