Given an array **arr[] consisting** of **N **integers, the task is to make all array elements equal by selecting any pair of integers from the array and replacing the larger integer from the pair with their absolute difference any number of times. Print the final value of all array elements.

**Examples:**

Input:arr[] ={2, 3, 4}Output:1Explanation:

Step 1: Performing on the pair (2, 3) modifies arr[] = {2, 1, 4}

Step 2: Performing on the pair (2, 4) modifies arr[] = {2, 1, 2}

Step 3: Performing on the pair (2, 1) modifies {1, 1, 2}

Step 4: Performing on the pair (1, 2) modifies arr[] = {1, 1, 1}

Input:arr[] = {24, 60}Output:12

**Approach:** From the above problem statement, it can be observed that for any pair **(a, b)**, the absolute difference is subtracted from the maximum element. Then this operation is similar to finding GCD of the pair. Therefore, from this observation, it is clear that all array elements need to be reduced to the GCD of the array. Follow the steps below to solve the problem:

- Initialize a variable
**gcd**as**1**. -
Traverse the given array and while traversing update
**gcd**as:

gcd = gcd(arr[i], gcd), where0 ≤ i < N

- After the above step, the value of
**gcd**is the required array element after the given operation is applied to every distinct pair of elements.

Below is the implementation of the above approach:

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// Function to return ` `// gcd of a and b` `int` `gcd(` `int` `a, ` `int` `b)`
`{` ` ` `// Base Case`
` ` `if` `(a == 0)`
` ` `return` `b;`
` ` `// Recursive Call`
` ` `return` `gcd(b % a, a);`
`}` `// Function to find gcd of array` `int` `findGCD(` `int` `arr[], ` `int` `N)`
`{` ` ` `// Initialise the result`
` ` `int` `result = 0;`
` ` `// Traverse the array arr[]`
` ` `for` `(` `int` `i = 0; i < N; i++) `
` ` `{`
` ` `// Update result as gcd of`
` ` `// the result and arr[i]`
` ` `result = gcd(result, arr[i]);`
` ` `if` `(result == 1) `
` ` `{`
` ` `return` `1;`
` ` `}`
` ` `}`
` ` `// Return the resultant GCD`
` ` `return` `result;`
`}` `// Driver Code` `int` `main()`
`{` ` ` `// Given array arr[]`
` ` `int` `arr[] = {2, 3, 4};`
` ` `int` `N = ` `sizeof` `(arr) / `
` ` `sizeof` `(arr[0]);`
` ` `// Function Call`
` ` `cout << findGCD(arr, N);`
` ` `return` `0;`
`}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

`// Java program for the above approach` `public` `class` `GCD {`
` ` `// Function to return gcd of a and b`
` ` `static` `int` `gcd(` `int` `a, ` `int` `b)`
` ` `{`
` ` `// Base Case`
` ` `if` `(a == ` `0` `)`
` ` `return` `b;`
` ` `// Recursive Call`
` ` `return` `gcd(b % a, a);`
` ` `}`
` ` `// Function to find gcd of array`
` ` `static` `int` `findGCD(` `int` `arr[], ` `int` `N)`
` ` `{`
` ` `// Initialise the result`
` ` `int` `result = ` `0` `;`
` ` `// Traverse the array arr[]`
` ` `for` `(` `int` `element : arr) {`
` ` `// Update result as gcd of`
` ` `// the result and arr[i]`
` ` `result = gcd(result, element);`
` ` `if` `(result == ` `1` `) {`
` ` `return` `1` `;`
` ` `}`
` ` `}`
` ` `// Return the resultant GCD`
` ` `return` `result;`
` ` `}`
` ` `// Driver Code`
` ` `public` `static` `void` `main(String[] args)`
` ` `{`
` ` `// Given array arr[]`
` ` `int` `arr[] = { ` `2` `, ` `3` `, ` `4` `};`
` ` `int` `N = arr.length;`
` ` `// Function Call`
` ` `System.out.println(findGCD(arr, N));`
` ` `}`
`}` |

*chevron_right*

*filter_none*

`# Python3 program for the above approach ` `# Function to return gcd of a and b` `def` `gcd(a, b):`
` ` ` ` `# Base Case`
` ` `if` `(a ` `=` `=` `0` `):`
` ` `return` `b`
` ` `# Recursive call`
` ` `return` `gcd(b ` `%` `a, a)`
`# Function to find gcd of array` `def` `findGCD(arr, N):`
` ` ` ` `# Initialise the result`
` ` `result ` `=` `0`
` ` `# Traverse the array arr[]`
` ` `for` `element ` `in` `arr:`
` ` `# Update result as gcd of`
` ` `# the result and arr[i]`
` ` `result ` `=` `gcd(result, element)`
` ` `if` `(result ` `=` `=` `1` `):`
` ` `return` `1`
` ` `# Return the resultant GCD`
` ` `return` `result`
`# Driver Code` `# Given array arr[]` `arr ` `=` `[ ` `2` `, ` `3` `, ` `4` `]`
`N ` `=` `len` `(arr)`
`# Function call` `print` `(findGCD(arr, N))`
`# This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

`// C# program for the above approach ` `using` `System; `
`class` `GFG{`
`// Function to return gcd of a and b` `static` `int` `gcd(` `int` `a, ` `int` `b)`
`{` ` ` ` ` `// Base Case`
` ` `if` `(a == 0)`
` ` `return` `b;`
` ` `// Recursive call`
` ` `return` `gcd(b % a, a);`
`}` `// Function to find gcd of array` `static` `int` `findGCD(` `int` `[] arr, ` `int` `N)`
`{` ` ` ` ` `// Initialise the result`
` ` `int` `result = 0;`
` ` `// Traverse the array arr[]`
` ` `foreach` `(` `int` `element ` `in` `arr)`
` ` `{`
` ` `// Update result as gcd of`
` ` `// the result and arr[i]`
` ` `result = gcd(result, element);`
` ` `if` `(result == 1)`
` ` `{`
` ` `return` `1;`
` ` `}`
` ` `}`
` ` `// Return the resultant GCD`
` ` `return` `result;`
`}` `// Driver Code` `public` `static` `void` `Main()`
`{` ` ` ` ` `// Given array arr[]`
` ` `int` `[] arr = { 2, 3, 4 };`
` ` `int` `N = arr.Length;`
` ` `// Function call`
` ` `Console.WriteLine(findGCD(arr, N));`
`}` `}` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

**Output**

1

**Time Complexity:** O(N*logN), where N is the size of the given array.**Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find maximum array sum after making all elements same with repeated subtraction
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Repeated subtraction among two numbers
- Square root of a number by Repeated Subtraction method
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Minimum peak elements from an array by their repeated removal at every iteration of the array
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Count the pairs in an array such that the difference between them and their indices is equal
- Count pairs from an array having product of their sum and difference equal to 0
- Count pairs from an array having product of their sum and difference equal to 1
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Maximize removal of adjacent array elements based on their absolute value
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Partition the array into two odd length groups with minimized absolute difference between their median
- Count of all pairs in an Array with minimum absolute difference
- Count all disjoint pairs having absolute difference at least K from a given array
- Possible values of Q such that, for any value of R, their product is equal to X times their sum

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.