Given an array of positive distinct integers **arr[]**, the task is to find the final number obtained by performing the following operation on the elements of the array:

**Operation:** Take two unequal numbers and replace the larger number with their difference until all numbers become equal.

**Examples:**

Input:arr[] = {5, 2, 3}

Output:1

5 – 3 = 2, arr[] = {2, 2, 3}

3 – 2 = 1, arr[] = {2, 2, 1}

2 – 1 = 1, arr[] = {2, 1, 1}

2 – 1 = 1, arr[] = {1, 1, 1}

Input:arr[] = {3, 9, 6, 36}

Output:3

**Naive approach:** Since final answer will always be distinct, one can just sort the array and replace the largest term with the difference of the two largest elements and repeat the process until all the numbers become equal.

**Efficient approach:** From Euclidean’s algorithm, it is known that **gcd(a, b) = gcd(a – b, b)**. This can be extended to **gcd(A _{1}, A_{2}, A_{3}, …, A_{n}) = gcd(A_{1} – A_{2}, A_{2}, A_{3}, …, A_{n})**.

Also, let’s say that after applying the given operation, the final number obtained be K. Hence, from the extended algorithm, it can be said that

**gcd(A**. Since

_{1}, A_{2}, A_{3}, …, A_{n}) = gcd(K, K, …, n times)**gcd(K, K, …, n times) = K**, the solution of the given problem can be found

by finding the gcd of all the elements of the array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the final number ` `// obtained after performing the ` `// given operation ` `int` `finalNum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the gcd of the array elements ` ` ` `int` `result = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `result = __gcd(result, arr[i]); ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 9, 6, 36 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << finalNum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the final number ` `// obtained after performing the ` `// given operation ` `static` `int` `finalNum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Find the gcd of the array elements ` ` ` `int` `result = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `result = __gcd(result, arr[i]); ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == ` `0` `? a:__gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `3` `, ` `9` `, ` `6` `, ` `36` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(finalNum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd as __gcd ` ` ` `# Function to return the final number ` `# obtained after performing the ` `# given operation ` `def` `finalNum(arr, n): ` ` ` ` ` `# Find the gcd of the array elements ` ` ` `result ` `=` `arr[` `0` `] ` ` ` `for` `i ` `in` `arr: ` ` ` `result ` `=` `__gcd(result, i) ` ` ` `return` `result ` ` ` `# Driver code ` `arr ` `=` `[` `3` `, ` `9` `, ` `6` `, ` `36` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(finalNum(arr, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the readonly number ` `// obtained after performing the ` `// given operation ` `static` `int` `finalNum(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Find the gcd of the array elements ` ` ` `int` `result = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `result = __gcd(result, arr[i]); ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == 0 ? a : __gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 3, 9, 6, 36 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(finalNum(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Find the final sequence of the array after performing given operations
- Find the index which is the last to be reduced to zero after performing a given operation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Minimum possible sum of array elements after performing the given operation
- Sum of the updated array after performing the given operation
- Maximum possible array sum after performing the given operation
- Minimum possible sum of array elements after performing the given operation
- Check if the last element of array is even or odd after performing a operation p times
- Maximum value obtained by performing given operations in an Array
- Arrange the array such that upon performing given operations an increasing order is obtained
- Print the final string when minimum value strings get concatenated in every operation
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Minimum N-Digit number required to obtain largest N-digit number after performing given operations
- Find final value if we double after every successful search in array
- Find the number of different numbers in the array after applying the given operation q times
- Find the modified array after performing k operations of given type
- Number of elements from the array which are reachable after performing given operations on D
- Maximum number of unique values in the array after performing given operations
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Array obtained by repeatedly reversing array after every insertion from given array

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.