Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIMake all array elements equal by repeated subtraction of absolute difference of...

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

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++




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


Output

1

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments