Prefix Factorials of a Prefix Sum Array | Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
def fact ( N ) : NEW_LINE INDENT if ( N == 1 or N == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return N * fact ( N - 1 ) NEW_LINE DEDENT def prefixFactorialArray ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = fact ( arr [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE prefixFactorialArray ( arr , N ) NEW_LINE DEDENT