# Find GCD of factorial of elements of given array

Given an array with N positive integers. Find the GCD of factorials of all elements of array.

Examples:

```Input : arr[] = {3, 4, 8, 6}
Output : 6

Input : arr[] = {13, 24, 8, 5}
Output : 120
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : To find the GCD of factorial of all elements, first of all calculate the factorial of all elements and then find out their GCD. But this seems to be very lengthy process. GCD of two numbers is the greatest number which divides both of the numbers. Hence, GCD of factorial of two number is the value of factorial of smallest number itself.

For example: GCD of 3! (6) and 5! (120) is 3! (i.e. 6) itself.

Hence to find the GCD of factorial of all elements of given array, find the smallest element and then print its factorial that will be our required answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Implementation of factorial function ` `int` `factorial(``int` `n) ` `{ ` `    ``return` `(n == 1 || n == 0) ? 1 : factorial(n - 1) * n; ` `} ` ` `  `// Function to find GCD of factorial of  ` `// elements from array ` `int` `gcdOfFactorial(``int` `arr[], ``int` `n) ` `{ ` `    ``// find the minimum element of array ` `    ``int` `minm = arr[0]; ` `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``minm = minm > arr[i] ? arr[i] : minm; ` ` `  `    ``// return the factorial of minimum element ` `    ``return` `factorial(minm); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 9, 12, 122, 34, 15 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``cout << gcdOfFactorial(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `class` `GFG ` `{ ` `     `  `// Implementation of factorial function ` `static` `int` `factorial(``int` `n) ` `{ ` `    ``return` `(n == ``1` `|| n == ``0``) ? ``1` `: factorial(n - ``1``) * n; ` `} ` ` `  `// Function to find GCD of factorial of  ` `// elements from array ` `static` `int` `gcdOfFactorial(``int` `[]arr, ``int` `n) ` `{ ` `    ``// find the minimum element of array ` `    ``int` `minm = arr[``0``]; ` `    ``for` `(``int` `i = ``1``; i < n; i++) ` `        ``minm = minm > arr[i] ? arr[i] : minm; ` ` `  `    ``// return the factorial of minimum element ` `    ``return` `factorial(minm); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `[]arr = { ``9``, ``12``, ``122``, ``34``, ``15` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println(gcdOfFactorial(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## Python3

 `# Implementation of factorial function ` `def` `factorial(n): ` `    ``if` `n ``=``=` `1` `or` `n ``=``=` `0``: ` `        ``return` `1` `    ``else``: ` `        ``return` `factorial(n ``-` `1``) ``*` `n ` ` `  `# Function to find GCD of factorial  ` `# of elements from array ` `def` `gcdOfFactorial(arr, n): ` ` `  `    ``# find the minimum element  ` `    ``# of array ` `    ``minm ``=` `arr[``0``] ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``if` `minm > arr[i]: ` `            ``minm ``=` `arr[i] ` `        ``else``: ` `            ``arr[i] ``=` `minm ` ` `  `    ``# return the factorial of  ` `    ``# minimum element ` `    ``return` `factorial(minm) ` ` `  `# Driver Code ` `arr ``=` `[``9``, ``12``, ``122``, ``34``, ``15` `] ` `n ``=` `len``(arr) ` `print``(gcdOfFactorial(arr, n)) ` ` `  `# This code is contributed  ` `# by mohit kumar `

[/sourcecode]

## C#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Implementation of factorial function ` `static` `int` `factorial(``int` `n) ` `{ ` `    ``return` `(n == 1 || n == 0) ? 1 : factorial(n - 1) * n; ` `} ` ` `  `// Function to find GCD of factorial of  ` `// elements from array ` `static` `int` `gcdOfFactorial(``int` `[]arr, ``int` `n) ` `{ ` `    ``// find the minimum element of array ` `    ``int` `minm = arr[0]; ` `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``minm = minm > arr[i] ? arr[i] : minm; ` ` `  `    ``// return the factorial of minimum element ` `    ``return` `factorial(minm); ` `} ` ` `  `// Driver Code ` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = { 9, 12, 122, 34, 15 }; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(gcdOfFactorial(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` ``\$arr``[``\$i``] ?  ` `                        ``\$arr``[``\$i``] : ``\$minm``;  ` ` `  `    ``// return the factorial of minimum element  ` `    ``return` `factorial(``\$minm``);  ` `}  ` ` `  `// Driver Code  ` `\$arr` `= ``array``( 9, 12, 122, 34, 15 );  ` `\$n` `= ``count``(``\$arr``);  ` `echo` `gcdOfFactorial(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by Srathore ` `?> `

Output:

```362880
```

My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.