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: 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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findGCD( int arr[], int N)
{
int result = 0;
for ( int i = 0; i < N; i++)
{
result = gcd(result, arr[i]);
if (result == 1)
{
return 1;
}
}
return result;
}
int main()
{
int arr[] = {2, 3, 4};
int N = sizeof (arr) /
sizeof (arr[0]);
cout << findGCD(arr, N);
return 0;
}
|
Java
public class GCD {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findGCD( int arr[], int N)
{
int result = 0 ;
for ( int element : arr) {
result = gcd(result, element);
if (result == 1 ) {
return 1 ;
}
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 };
int N = arr.length;
System.out.println(findGCD(arr, N));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def findGCD(arr, N):
result = 0
for element in arr:
result = gcd(result, element)
if (result = = 1 ):
return 1
return result
arr = [ 2 , 3 , 4 ]
N = len (arr)
print (findGCD(arr, N))
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findGCD( int [] arr, int N)
{
int result = 0;
foreach ( int element in arr)
{
result = gcd(result, element);
if (result == 1)
{
return 1;
}
}
return result;
}
public static void Main()
{
int [] arr = { 2, 3, 4 };
int N = arr.Length;
Console.WriteLine(findGCD(arr, N));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function findGCD(arr, N)
{
let result = 0;
for (let element in arr) {
result = gcd(result, element);
if (result == 1) {
return 1;
}
}
return result;
}
let arr = [ 2, 3, 4 ];
let N = arr.length;
document.write(findGCD(arr, N));
</script>
|
Time Complexity: O(N*logN), where N is the size of the given array.
Auxiliary Space: O(N)