Skip to content
Related Articles

Related Articles

Improve Article

Find unique lexicographically increasing quadruplets with sum as B and GCD of absolute values of all elements is 1

  • Last Updated : 01 Jul, 2021

Given an array A of size N and an integer B, the task is to find all unique quadruplets arranged in the lexicographically increasing order such that the sum of the elements of each quadrupled is B and the gcd of the absolute values of all elements of a quadrupled is 1.

Example:

Input: A = {1, 0, -1, 0, -2, 2 }, B = 0
Output:
-2 -1 1 2
-1  0 0 1
Explanation: There are only three unique quadruplets which have sum = 0 which are {{-2, 0, 0, 2}, {-2, -1, 1, 2}, {-1, 0, 0, 1}} and out of these quadruplets only the second and the third quadrupled have gcd equal to 1.

Input: A = { 1, 5, 1, 0, 6, 0 }, B = 7
Output:
0 0 1 6
0 1 1 5

Approach: The idea is to store the sum of each pair of elements in a hashmap, then iterate through all pairs of elements and then lookup the hashmap to find pairs such that the sum of the quadrupled becomes equal to B and the gcd of absolute values of all elements of the quadrupled is equal to 1. The detailed approach using hashmap has been discussed in this article.  Follow the steps to solve the problem.



  • Insert the sum of each pair of elements of the array into a hashmap mp.
  • Initialize a set st, to store all the quadruplets.
  • Traverse the array from i = 0 to N-1
    • Traverse the array from j = i+1 to N-1
      • Find all pairs from the hashmap whose sum is equal to B- A[i]-A[j]. Initialize a vector of pairs v to mp[B-A[i]-A[j]]. 
      • Traverse through the vector v using a variable k
        • If v[k].first or v[k].second is equal to either i or j then continue to the next iteration.
        • Store the elements of the quadrupled in a temp array. Sort the temp array. If the gcd of all elements of temp array is 1 then insert the temp array into st.
  • Traverse through the set st and print all the quadruplets.

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find all
// quadruplets with sum B.
void find4Sum(int A[], int N, int B)
{
    // Hashmap to store sum
    // of all pairs
    unordered_map<int,
                  vector<pair<int, int> > >
        mp;
  
    // Set to store all quadruplets
    set<vector<int> > st;
  
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
        // Traverse the array
        for (int j = i + 1; j < N; j++) {
            int sum = A[i] + A[j];
            // Insert sum of
            // current pair into
            // the hashmap
            mp[sum].push_back({ i, j });
        }
    }
  
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
        // Traverse the array
        for (int j = i + 1; j < N; j++) {
            int sum = A[i] + A[j];
            // Lookup the hashmap
            if (mp.find(B - sum) != mp.end()) {
                vector<pair<int, int> > v
                    = mp[B - sum];
  
                for (int k = 0; k < v.size(); k++) {
  
                    pair<int, int> it = v[k];
                    if (it.first != i && it.second != i
                        && it.first != j
                        && it.second != j) {
                        vector<int> temp;
                        temp.push_back(A[i]);
                        temp.push_back(A[j]);
                        temp.push_back(A[it.first]);
                        temp.push_back(A[it.second]);
  
                        // Stores the gcd of the
                        // quadrupled
                        int gc = abs(temp[0]);
                        gc = __gcd(abs(temp[1]), gc);
                        gc = __gcd(abs(temp[2]), gc);
                        gc = __gcd(abs(temp[3]), gc);
                        // Arrange in
                        // ascending order
                        sort(temp.begin(), temp.end());
                        // Insert into set if gcd is 1
                        if (gc == 1)
                            st.insert(temp);
                    }
                }
            }
        }
    }
    // Iterate through set
    for (auto it = st.begin(); it != st.end(); it++) {
        vector<int> temp = *it;
        // Print the elements
        for (int i = 0; i < 4; i++) {
            cout << temp[i] << " ";
        }
        cout << endl;
    }
}
  
// Driver Code
int main()
{
    // Input
    int N = 6;
    int A[6]
        = { 1, 0, -1, 0, -2, 2 };
    int B = 0;
  
    // Function Call
    find4Sum(A, N, B);
    return 0;
}
Output
-2 -1 1 2 
-1 0 0 1 

Time Complexity: O(N^3)
Auxiliary Space: O(N^2)

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 :