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:
- Initialize a variable gcd as 1.
- Traverse the given array and while traversing update gcd as:
 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 <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 |
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
<script> Â
// JavaScript program for // the above approach Â
    // Function to return gcd of a and b     function gcd(a, b)     {         // Base Case         if (a == 0)             return b;           // Recursive Call         return gcd(b % a, a);     }       // Function to find gcd of array     function findGCD(arr, N)     {         // Initialise the result         let result = 0;           // Traverse the array arr[]         for (let 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[]         let arr = [ 2, 3, 4 ];           let N = arr.length;           // Function Call         document.write(findGCD(arr, N));                              </script> |
1
Time Complexity: O(N*logN), where N is the size of the given array.
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!