Related Articles

Related Articles

Count of quadruplets with given Sum
  • Last Updated : 07 May, 2019

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: 2
(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: Generate all possible quadruplets and calculate the sum of every quadruplets. Count all such quadruplets whose sum is equal to the given sum.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 sum)
{
  
    // To store the count of required quadruplets
    int cnt = 0;
  
    // For arr1[]
    for (int i = 0; i < n1; i++) {
  
        // For arr2[]
        for (int j = 0; j < n2; j++) {
  
            // For arr3[]
            for (int k = 0; k < n3; k++) {
  
                // For arr4[]
                for (int l = 0; l < n4; l++) {
  
                    // If current quadruplet has the required sum
                    if (arr1[i] + arr2[j] + arr3[k] + arr4[l] == sum) {
                        cnt++;
                    }
                }
            }
        }
    }
  
    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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
  
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 sum)
{
  
    // To store the count of required quadruplets
    int cnt = 0;
  
    // For arr1[]
    for (int i = 0; i < n1; i++)
    {
  
        // For arr2[]
        for (int j = 0; j < n2; j++)
        {
  
            // For arr3[]
            for (int k = 0; k < n3; k++) 
            {
  
                // For arr4[]
                for (int l = 0; l < n4; l++) 
                {
  
                    // If current quadruplet has the required sum
                    if (arr1[i] + arr2[j] + arr3[k] + arr4[l] == sum)
                    {
                        cnt++;
                    }
                }
            }
        }
    }
  
    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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach
  
# Function to return the count of the required quadruplets
def countQuadruplets(P, Q, R, S, sum):
      
    # To store the count of required quadruplets
    cnt = 0
      
    # Using four loops generate all possible quadruplets
    for elem1 in P:
        for elem2 in Q:
            for elem3 in R:
                for elem4 in S:
                    if elem1 + elem2 + elem3 + elem4 == sum:
                        cnt = cnt + 1
    return cnt
  
# Driver code
P = [ 0, 2]
Q = [-1, -2]
R = [2, 1]
S = [ 2, -1]
sum = 0
  
print(countQuadruplets(P, Q, R, S, sum))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
  
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 sum)
{
  
    // To store the count of required quadruplets
    int cnt = 0;
  
    // For arr1[]
    for (int i = 0; i < n1; i++)
    {
  
        // For arr2[]
        for (int j = 0; j < n2; j++)
        {
  
            // For arr3[]
            for (int k = 0; k < n3; k++) 
            {
  
                // For arr4[]
                for (int l = 0; l < n4; l++) 
                {
  
                    // If current quadruplet has the required sum
                    if (arr1[i] + arr2[j] + arr3[k] + arr4[l] == sum)
                    {
                        cnt++;
                    }
                }
            }
        }
    }
  
    return cnt;
}
  
// Driver code
static public void Main ()
{
      
    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 akt_mit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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, $sum)
{
  
    // To store the count of required quadruplets
    $cnt = 0;
  
    // For arr1[]
    for ($i = 0; $i < $n1; $i++) 
    {
  
        // For arr2[]
        for ($j = 0; $j < $n2; $j++)
        {
  
            // For arr3[]
            for ($k = 0; $k < $n3; $k++)
            {
  
                // For arr4[]
                for ( $l = 0; $l < $n4; $l++) 
                {
  
                    // If current quadruplet has the required sum
                    if ($arr1[$i] + $arr2[$j] + $arr3[$k] + 
                                       $arr4[$l] == $sum
                    {
                        $cnt++;
                    }
                }
            }
        }
    }
  
    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 ajit
?>

chevron_right


Output:

2

Time Complexity: O(n4)
Space Complexity: O(1)

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :