# Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum

• Difficulty Level : Easy
• Last Updated : 13 Apr, 2021

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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] ={2, 3, 4}
Output: 1
Explanation:
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:

gcd = gcd(arr[i], gcd), where 0 ≤ 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++

 `// C++ Program to implement``// the above approach``#include ``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);` `  ``// Function Call``  ``cout << findGCD(arr, N);``  ``return` `0;``}` `// This code is contributed by 29AjayKumar`

## Java

 `// 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));``    ``}``}`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``
Output
`1`

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

My Personal Notes arrow_drop_up