Open In App

Count of quadruplets with given Sum | Set 2

Improve
Improve
Like Article
Like
Save
Share
Report

Given four arrays containing integer elements and an integer sum, the task is to count the quadruplets such that each element is chosen from a different array and the sum of all the four elements is equal to the given sum.
Examples: 
 

Input: P[] = {0, 2}, Q[] = {-1, -2}, R[] = {2, 1}, S[] = {2, -1}, sum = 0 
Output:
(0, -1, 2, -1) and (2, -2, 1, -1) are the required quadruplets.
Input: P[] = {1, -1, 2, 3, 4}, Q[] = {3, 2, 4}, R[] = {-2, -1, 2, 1}, S[] = {4, -1}, sum = 3 
Output: 10 
 

 

Approach: We pick any two arrays and calculate all possible sums and and keep their counts in a map. Using the remaining two arrays, we calculate all possible sums and check how many times their additive inverse exists in the map which will be the count of required quadruplets.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of the required quadruplets
int countQuadruplets(int arr1[], int n1, int arr2[], int n2,
                     int arr3[], int n3, int arr4[], int n4, int value)
{
    int cnt = 0;
    unordered_map<int, int> sum;
 
    // All possible sums from arr1[] and arr2[]
    for (int i = 0; i < n1; i++)
        for (int j = 0; j < n2; j++)
            sum[arr1[i] + arr2[j]]++;
 
    // Find the count of quadruplets
    for (int i = 0; i < n3; i++)
        for (int j = 0; j < n4; j++)
            cnt += sum[value - (arr3[i] + arr4[j])];
 
    return cnt;
}
 
// Driver code
int main()
{
 
    int arr1[] = { 0, 2 };
    int arr2[] = { -1, -2 };
    int arr3[] = { 2, 1 };
    int arr4[] = { 2, -1 };
    int sum = 0;
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    int n3 = sizeof(arr3) / sizeof(arr3[0]);
    int n4 = sizeof(arr4) / sizeof(arr4[0]);
 
    cout << countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return the count of the required quadruplets
    static int countQuadruplets(int arr1[], int n1, int arr2[], int n2,
                                int arr3[], int n3, int arr4[], int n4, int value)
    {
        int cnt = 0;
        Map<Integer, Integer> sum = new HashMap<>();
 
        // All possible sums from arr1[] and arr2[]
        for (int i = 0; i < n1; i++)
            for (int j = 0; j < n2; j++) {
                if (sum.containsKey(arr1[i] + arr2[j])) {
                    sum.put(arr1[i] + arr2[j], sum.get(arr1[i] + arr2[j]) + 1);
                }
                else {
                    sum.put(arr1[i] + arr2[j], 1);
                }
            }
 
        // Find the count of quadruplets
        for (int i = 0; i < n3; i++)
            for (int j = 0; j < n4; j++)
                if (sum.containsKey(value - (arr3[i] + arr4[j])))
                    cnt += sum.get(value - (arr3[i] + arr4[j]));
 
        return cnt;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr1[] = { 0, 2 };
        int arr2[] = { -1, -2 };
        int arr3[] = { 2, 1 };
        int arr4[] = { 2, -1 };
        int sum = 0;
        int n1 = arr1.length;
        int n2 = arr2.length;
        int n3 = arr3.length;
        int n4 = arr4.length;
 
        System.out.println(countQuadruplets(arr1, n1, arr2, n2,
                                            arr3, n3, arr4, n4, sum));
    }
}
 
// This code contributed by Rajput-Ji


Python3




# Python 3 implementation of the approach
 
# Function to return the count
# of the required quadruplets
def countQuadruplets(arr1, n1, arr2, n2,
                     arr3, n3, arr4, n4, value):
    cnt = 0
    sum = {i:0 for i in range(-4, 10, 1)}
 
    # All possible sums from arr1[] and arr2[]
    for i in range(n1):
        for j in range(n2):
            sum[arr1[i] + arr2[j]] += 1
 
    # Find the count of quadruplets
    for i in range(n3):
        for j in range(n4):
            cnt += sum[value - (arr3[i] + arr4[j])]
 
    return cnt
 
# Driver code
if __name__ == '__main__':
    arr1 = [0, 2]
    arr2 = [-1, -2]
    arr3 = [2, 1]
    arr4 = [2, -1]
    sum = 0
    n1 = len(arr1)
    n2 = len(arr2)
    n3 = len(arr3)
    n4 = len(arr4)
 
    print(countQuadruplets(arr1, n1, arr2, n2,
                           arr3, n3, arr4, n4, sum))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to return the count of the required quadruplets
    static int countQuadruplets(int[] arr1, int n1,
                                int[] arr2, int n2,
                                int[] arr3, int n3,
                                int[] arr4, int n4, int value)
    {
        int cnt = 0;
        Dictionary<int, int> sum = new Dictionary<int, int>();
 
        // All possible sums from arr1[] and arr2[]
        for (int i = 0; i < n1; i++)
            for (int j = 0; j < n2; j++) {
                if (sum.ContainsKey(arr1[i] + arr2[j])) {
                    var obj = sum[arr1[i] + arr2[j]] + 1;
                    sum.Remove(arr1[i] + arr2[j]);
                    sum.Add(arr1[i] + arr2[j], obj);
                }
                else {
                    sum.Add(arr1[i] + arr2[j], 1);
                }
            }
 
        // Find the count of quadruplets
        for (int i = 0; i < n3; i++)
            for (int j = 0; j < n4; j++)
                if (sum.ContainsKey(value - (arr3[i] + arr4[j])))
                    cnt += sum[value - (arr3[i] + arr4[j])];
 
        return cnt;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr1 = { 0, 2 };
        int[] arr2 = { -1, -2 };
        int[] arr3 = { 2, 1 };
        int[] arr4 = { 2, -1 };
        int sum = 0;
        int n1 = arr1.Length;
        int n2 = arr2.Length;
        int n3 = arr3.Length;
        int n4 = arr4.Length;
 
        Console.WriteLine(countQuadruplets(arr1, n1, arr2, n2,
                                           arr3, n3, arr4, n4, sum));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP




<?php
// PHP implementation of the approach
 
// Function to return the count of the required quadruplets
function countQuadruplets($arr1, $n1, $arr2, $n2,
                    $arr3, $n3, $arr4, $n4, $value)
{
    $cnt = 0;
    $sum = array();
 
    for ($i = 0; $i < $n1; $i++)
        for ($j = 0; $j < $n2; $j++)
            $sum[$arr1[$i] + $arr2[$j]] = 0 ;
             
    // All possible sums from arr1[] and arr2[]
    for ($i = 0; $i < $n1; $i++)
        for ($j = 0; $j < $n2; $j++)
            $sum[$arr1[$i] + $arr2[$j]]++;
 
    // Find the count of quadruplets
    for ($i = 0; $i < $n3; $i++)
        for ($j = 0; $j < $n4; $j++)
            $cnt += $sum[$value - ($arr3[$i] + $arr4[$j])];
 
    return $cnt;
}
 
    // Driver code
    $arr1 = array(0, 2 );
    $arr2 = array( -1, -2 );
    $arr3 = array( 2, 1 );
    $arr4 = array( 2, -1 );
    $sum = 0;
    $n1 = count($arr1) ;
    $n2 = count($arr2) ;
    $n3 = count($arr3) ;
    $n4 = count($arr4) ;
 
    echo countQuadruplets($arr1, $n1, $arr2, $n2,
                        $arr3, $n3, $arr4, $n4, $sum);
     
    // This code is contributed by Ryuga
 
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the count of the required quadruplets
function countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, value)
{
    var cnt = 0;
    var sum = new Map();
 
    // All possible sums from arr1[] and arr2[]
    for (var i = 0; i < n1; i++)
    {
        for (var j = 0; j < n2; j++)
        {
            if(sum.has(arr1[i] + arr2[j]))
            {
                sum.set(arr1[i] + arr2[j], sum.get(arr1[i] + arr2[j])+1);
            }
            else
            {
                sum.set(arr1[i] + arr2[j], 1);
            }
        }
    }
 
    // Find the count of quadruplets
    for (var i = 0; i < n3; i++)
        for (var j = 0; j < n4; j++)
            if(sum.has((value - (arr3[i] + arr4[j]))))
            {
                cnt += sum.get((value - (arr3[i] + arr4[j])));
            }
             
    return cnt;
}
 
// Driver code
var arr1 = [0, 2];
var arr2 = [-1, -2];
var arr3 = [2, 1];
var arr4 = [2, -1];
var sum = 0;
var n1 = arr1.length;
var n2 = arr2.length;
var n3 = arr3.length;
var n4 = arr4.length;
document.write( countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum));
 
// This code is contributed by noob2000.
</script>


Output: 

2

 

Time Complexity : O(n1*n2+n3*n4) where, n1, n2, n3, n4 are size of arrays arr1, arr2, arr3 and arr4 respectively.

Auxiliary Space : O(n) , to store the elements in map. 



Last Updated : 16 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads