Skip to content
Related Articles

Related Articles

Improve Article

Generate an N-length array having GCD of all its pairs present in a given 2D array

  • Last Updated : 30 Jul, 2021

Given a 2D array arr[][] consisting of N*N positive integers, the task is to generate an N-length array such that Greatest Common Divisors(GCD) of all possible pairs of that array is present in the array arr[][].

Examples:

Input: N = 4, arr[] = {2, 1, 2, 3, 4, 3, 2, 6, 1, 1, 2, 2, 1, 2, 3, 2}
Output: 4, 3, 6, 2
Explanation:
Considering the array A[] as {4, 3, 6, 2}, then the GCD of all possible pairs of this array is given below which is the given array arr[].
{{4, 1, 2, 2},
{1, 3, 3, 1},
{2, 3, 6, 2},
{2, 1, 2, 2}}

Input: N = 1, mat = {100}
Output: 100

Approach: The above problem can be solved by using the fact that, GCD of the largest element in the original array with itself is the largest in the arr[] and after removing the gcd pairs with that element, the next element can be found. Follow the steps below to solve the given problem:



  • Initialize a map say, M store the frequency of negation of array element in the map M.
  • Initialize a variable, say pos as (N – 1).
  • Now, for all array elements arr[] find the maximum element.
  • Traverse the map M.
  • For each element of the original array, find the element with the maximum frequency and store it in the ans.
  • Find ans[pos] and remove all GCD from pos+1 to N-1 of the ans.
  • Update pos as pos-1.
  • Repeat above steps to find all elements of the original array.
  • Finally, print the ans.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int n;
  
// Function to calculate GCD of two numbers
int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
  
// Function to generate an N-length
// array having GCD of all pairs
// present in the array mat[][]
void restoreArray(vector<int> mat)
{
    map<int, int> cnt;
  
    // Stores the required array
    vector<int> ans(n);
  
    for (int i = 0; i < (n * n); ++i) {
  
        // Store frequencies in map
        // in decreasing order
        cnt[-mat[i]]++;
    }
  
    int pos = n - 1;
  
    for (auto it = cnt.begin();
         it != cnt.end(); ++it) {
  
        int x = -it->first;
        while (it->second) {
  
            // gcd(x, x)
            ans[pos] = x;
            --it->second;
  
            // Remove all GCDs for
            // indices pos + 1 -> n - 1
            for (int i = pos + 1; i < n; ++i)
  
                cnt[-gcd(ans[pos], ans[i])] -= 2;
  
            // Decreasing pos
            pos--;
        }
    }
  
    // Print restored array
    for (int i = 0; i < n; ++i)
        printf("%d ", ans[i]);
}
  
// Driver Code
int main()
{
  
    // Given Input
    n = 4;
    vector<int> mat{ 2, 1, 2, 3, 4, 3,
                     2, 6, 1, 1, 2,
                     2, 1, 2, 3, 2 };
  
    // Function Call
    restoreArray(mat);
    return 0;
}
Output:
2 3 4 6

Time Complexity: O(N2LogN)
Auxiliary Space: O(N2)

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :