📜  查找给定数组的后缀和数组的后缀阶乘

📅  最后修改于: 2022-05-13 01:56:05.169000             🧑  作者: Mango

查找给定数组的后缀和数组的后缀阶乘

给定一个数组 arr[]N正整数组成,任务是找到给定数组后缀和数组的后缀阶乘。

例子:

方法:可以通过预先计算所有数字的阶乘直到数组的整个总和来解决该任务。这样后缀和数组的每个索引处的阶乘计算就可以在单位时间内计算出来

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the factorial of
// suffix sum at every possible index
void suffixFactorialArray(int A[], int N)
{
    // Find the suffix sum array
    for (int i = N - 2; i >= 0; i--) {
        A[i] += A[i + 1];
    }
 
    // Stores the factorial of all the
    // element till the sum of array
    int fact[A[0] + 1];
    fact[0] = 1;
 
    // Find the factorial array
    for (int i = 1; i <= A[0]; 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]);
 
    suffixFactorialArray(arr, N);
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
  // Function to find the factorial of
  // suffix sum at every possible index
  static void suffixFactorialArray(int[] A, int N) {
 
    // Find the suffix sum array
    for (int i = N - 2; i >= 0; i--) {
      A[i] += A[i + 1];
    }
 
    // Stores the factorial of all the
    // element till the sum of array
    int[] fact = new int[A[0] + 1];
    fact[0] = 1;
 
    // Find the factorial array
    for (int i = 1; i <= A[0]; 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;
 
    suffixFactorialArray(arr, N);
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# python3 program for the above approach
 
# Function to find the factorial of
# suffix sum at every possible index
def suffixFactorialArray(A, N):
 
    # Find the suffix sum array
    for i in range(N-2, -1, -1):
        A[i] += A[i + 1]
 
    # Stores the factorial of all the
    # element till the sum of array
    fact = [0 for _ in range(A[0] + 1)]
    fact[0] = 1
 
    # Find the factorial array
    for i in range(1, A[0] + 1):
        fact[i] = i * fact[i - 1]
 
    # Find the factorials of
    # each array element
    for i in range(0, N):
        A[i] = fact[A[i]]
 
    # Print the resultant array
    for i in range(0, N):
        print(A[i], end=" ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4]
    N = len(arr)
 
    suffixFactorialArray(arr, N)
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the factorial of
  // suffix sum at every possible index
  static void suffixFactorialArray(int []A, int N)
  {
 
    // Find the suffix sum array
    for (int i = N - 2; i >= 0; i--) {
      A[i] += A[i + 1];
    }
 
    // Stores the factorial of all the
    // element till the sum of array
    int []fact = new int[A[0] + 1];
    fact[0] = 1;
 
    // Find the factorial array
    for (int i = 1; i <= A[0]; 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++) {
      Console.Write(A[i] + " ");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int []arr = { 1, 2, 3, 4 };
    int N = arr.Length;
 
    suffixFactorialArray(arr, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3628800 362880 5040 24 

时间复杂度: O(N + M),其中 M 是数组元素的总和。
辅助空间: O(M)