# Find number from its divisors

Given an array arr[] of N integers. The integers represent all the divisors of a number X except 1 and X itself. The task is to find the number X. If no such element is possible then print -1.

Examples:

Input: arr[] = {2, 10, 5, 4}
Output: 20

Input: arr[] = {2, 10, 5}
Output: 20

Input: arr[] = {2, 15}
Output: -1

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

Approach: Sort the given N divisors and the number X will be the first number * last number in the sorted array. Cross check if the X contradicts the given statement or not by storing all the divisors of X except 1 and X in another array and if the formed array and given array are not same then print -1, else print X.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach  ` `#include   ` `using` `namespace` `std;  ` `   `  `// Function that returns X  ` `int` `findX(``int` `a[], ``int` `n)  ` `{  ` `    ``// Sort the given array  ` `    ``sort(a, a + n);  ` `   `  `    ``// Get the possible X  ` `    ``int` `x = a[0] * a[n - 1];  ` `   `  `    ``// Container to store divisors  ` `    ``vector<``int``> vec;  ` `   `  `    ``// Find the divisors of a number  ` `    ``for` `(``int` `i = 2; i * i <= x; i++) {  ` `   `  `        ``// Check if divisor  ` `        ``if` `(n % i == 0) {  ` `            ``vec.push_back(i);  ` `            ``if` `((n / i) != i)  ` `                ``vec.push_back(n / i);  ` `        ``}  ` `    ``}  ` `   `  `    ``// Check if a and vec have  ` `    ``// same elements in them  ` `    ``if` `(vec.size() == n) {  ` `        ``int` `i = 0;  ` `        ``for` `(``auto` `it : vec) {  ` `            ``if` `(a[i++] != it)  ` `                ``return` `-1;  ` `        ``}  ` `    ``}  ` `   `  `    ``return` `x;  ` `}  ` `   `  `// Drivers code  ` `int` `main()  ` `{  ` `    ``int` `a[] = { 2, 5, 4, 10 };  ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]);  ` `    ``cout << findX(a, n);  ` `   `  `    ``return` `0;  ` `}  `

## Java

 `// Java implementation of the approach  ` `import` `java.util.*; ` ` `  `class` `GFG ` `{  ` `     `  `// Function that returns X  ` `static` `int` `findX(``int` `a[], ``int` `n)  ` `{  ` `    ``// Sort the given array  ` `    ``Arrays.sort(a);  ` `     `  `    ``// Get the possible X  ` `    ``int` `x = a[``0``] * a[n - ``1``];  ` `     `  `    ``// Container to store divisors  ` `    ``Vector vec = ``new` `Vector();  ` `     `  `    ``// Find the divisors of a number  ` `    ``for` `(``int` `i = ``2``; i * i <= x; i++) ` `    ``{  ` `     `  `        ``// Check if divisor  ` `        ``if` `(n % i == ``0``)  ` `        ``{  ` `            ``vec.add(i);  ` `            ``if` `((n / i) != i)  ` `                ``vec.add(n / i);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Check if a and vec have  ` `    ``// same elements in them  ` `    ``if` `(vec.size() == n)  ` `    ``{  ` `        ``int` `i = ``0``;  ` `        ``for` `(``int` `it : vec) ` `        ``{  ` `            ``if` `(a[i++] != it)  ` `                ``return` `-``1``;  ` `        ``}  ` `    ``}  ` `     `  `    ``return` `x;  ` `}  ` `     `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `a[] = { ``2``, ``5``, ``4``, ``10` `};  ` `    ``int` `n = a.length;  ` `    ``System.out.print(findX(a, n));  ` `}  ` `}  ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 implementation of the approach ` `# Function that returns X ` `import` `math ` `def` `findX(``list``,``int``): ` `    ``#Sort the given array ` `    ``list``.sort() ` `     `  `    ``#Get the possible X ` `    ``x ``=` `list``[``0``]``*``list``[``int``-``1``]; ` ` `  `    ``# Container to store divisors ` `    ``vec``=``[] ` ` `  `    ``# Find the divisors of a number ` `    ``i ``=` `2` `    ``while``(i ``*` `i <``=` `x): ` `        ``#Check if divisor ` `        ``if``(n ``%` `i ``=``=` `0``): ` `            ``vec.append(i) ` `            ``if` `((``int``/``/``i) !``=` `i): ` `                ``vec.append(``int``/``/``i) ` `        ``i ``+``=``1` ` `  `    ``# Check if a and vec have ` `    ``# same elements in them ` `    ``if``(``len``(vec) ``=``=` `int``): ` `        ``j ``=` `0` `        ``for` `it ``in` `range``(``int``): ` `            ``if``(a[j] !``=` `it): ` `                ``return` `-``1` `            ``else``: ` `                ``j ``+``=``1` `    ``return` `x ` ` `  `# Drivers code ` `a ``=` `[``2``, ``5``, ``4``, ``10``] ` `n ``=` `len``(a) ` `print``(findX(a,n)) ` ` `  `# This code is contributed by souviksarkhelkgp `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{  ` `      `  `// Function that returns X  ` `static` `int` `findX(``int` `[]a, ``int` `n)  ` `{  ` `    ``// Sort the given array  ` `    ``Array.Sort(a);  ` `      `  `    ``// Get the possible X  ` `    ``int` `x = a[0] * a[n - 1];  ` `      `  `    ``// Container to store divisors  ` `    ``List<``int``> vec = ``new` `List<``int``>();  ` `      `  `    ``// Find the divisors of a number  ` `    ``for` `(``int` `i = 2; i * i <= x; i++) ` `    ``{  ` `      `  `        ``// Check if divisor  ` `        ``if` `(n % i == 0)  ` `        ``{  ` `            ``vec.Add(i);  ` `            ``if` `((n / i) != i)  ` `                ``vec.Add(n / i);  ` `        ``}  ` `    ``}  ` `      `  `    ``// Check if a and vec have  ` `    ``// same elements in them  ` `    ``if` `(vec.Count == n)  ` `    ``{  ` `        ``int` `i = 0;  ` `        ``foreach` `(``int` `it ``in` `vec) ` `        ``{  ` `            ``if` `(a[i++] != it)  ` `                ``return` `-1;  ` `        ``}  ` `    ``}  ` `      `  `    ``return` `x;  ` `}  ` `      `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `[]a = { 2, 5, 4, 10 };  ` `    ``int` `n = a.Length;  ` `    ``Console.Write(findX(a, n));  ` `}  ` `}  ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```20
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : souviksarkhel, 29AjayKumar