Count of quadruplets with given sum | Set 3
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: Two different approaches to solve this problem has been discussed in Set 1 and Set 2 of this article. Here, an approach using the binary search will be discussed. Pick any two arrays and calculate all the possible pair sums and store them in a vector. Now, pick the other two arrays and calculate all possible sums and for every sum say tempSum, check whether sum – temp exists in the vector created earlier (after sorting it) using the binary search. Below is the implementation of the above approach:
CPP
// 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) { vector< int > sum1; vector< int >::iterator it; vector< int >::iterator it2; int cnt = 0; // Take every possible pair sum // from the two arrays for ( int i = 0; i < n1; i++) { for ( int j = 0; j < n2; j++) { // Push the sum to a vector sum1.push_back(arr1[i] + arr2[j]); } } // Sort the sum vector sort(sum1.begin(), sum1.end()); // Calculate the pair sums from // the other two arrays for ( int i = 0; i < n3; i++) { for ( int j = 0; j < n4; j++) { // Calculate the sum int temp = arr3[i] + arr4[j]; // Check whether temp can be added to any // sum stored in the sum1 vector such that // the result is the required sum if (binary_search(sum1.begin(), sum1.end(), value - temp)) { // Add the count of such values from the sum1 vector it = lower_bound(sum1.begin(), sum1.end(), value - temp); it2 = upper_bound(sum1.begin(), sum1.end(), value - temp); cnt = cnt + ((it2 - sum1.begin()) - (it - sum1.begin())); } } } return cnt; } // Driver code int main() { int arr1[] = { 0, 2 }; int n1 = sizeof (arr1) / sizeof (arr1[0]); int arr2[] = { -1, -2 }; int n2 = sizeof (arr2) / sizeof (arr2[0]); int arr3[] = { 2, 1 }; int n3 = sizeof (arr3) / sizeof (arr3[0]); int arr4[] = { 2, -1 }; int n4 = sizeof (arr4) / sizeof (arr4[0]); int sum = 0; cout << countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum); return 0; } |
Java
import java.util.Arrays; import java.util.Vector; import java.util.*; // Function to return the count // of the required quadruplets class Main{ public static int countQuadruplets( int [] arr1, int n1, int [] arr2, int n2, int [] arr3, int n3, int [] arr4, int n4, int value) { Vector<Integer> sum1 = new Vector<>(); int cnt = 0 ; // Take every possible pair sum // from the two arrays for ( int i = 0 ; i < n1; i++) { for ( int j = 0 ; j < n2; j++) { // Push the sum to a vector sum1.add(arr1[i] + arr2[j]); } } // Sort the sum vector sum1.sort( null ); // Calculate the pair sums from // the other two arrays for ( int i = 0 ; i < n3; i++) { for ( int j = 0 ; j < n4; j++) { // Calculate the sum int temp = arr3[i] + arr4[j]; // Check whether temp can be added to any // sum stored in the sum1 vector such that // the result is the required sum if (Collections.binarySearch(sum1, value - temp) >= 0 ) { // Add the count of such values from the sum1 vector int it = Collections.binarySearch(sum1, value - temp); int it2 = it + 1 ; while (it2 < sum1.size() && sum1.get(it2).equals(value - temp)) { it2++; } cnt = cnt + (it2 - it); } } } return cnt; } // Driver code public static void main(String[] args) { int [] arr1 = { 0 , 2 }; int n1 = arr1.length; int [] arr2 = { - 1 , - 2 }; int n2 = arr2.length; int [] arr3 = { 2 , 1 }; int n3 = arr3.length; int [] arr4 = { 2 , - 1 }; int n4 = arr4.length; int sum = 0 ; System.out.println(countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum)); } } |
Python3
from typing import List import bisect def countQuadruplets(arr1: List [ int ], n1: int , arr2: List [ int ], n2: int , arr3: List [ int ], n3: int , arr4: List [ int ], n4: int , value: int ) - > int : sum1 = [] # Take every possible pair sum from the two arrays for i in range (n1): for j in range (n2): # Append the sum to the list sum1.append(arr1[i] + arr2[j]) # Sort the sum list sum1.sort() cnt = 0 # Calculate the pair sums from the other two arrays for i in range (n3): for j in range (n4): # Calculate the sum temp = arr3[i] + arr4[j] # Check whether temp can be added to any sum stored in the sum1 # list such that the result is the required sum index = bisect.bisect_left(sum1, value - temp) if index ! = len (sum1) and sum1[index] = = value - temp: # Add the count of such values from the sum1 list it = index it2 = it + 1 while it2 < len (sum1) and sum1[it2] = = value - temp: it2 + = 1 cnt + = (it2 - it) return cnt # Driver code if __name__ = = "__main__" : arr1 = [ 0 , 2 ] n1 = len (arr1) arr2 = [ - 1 , - 2 ] n2 = len (arr2) arr3 = [ 2 , 1 ] n3 = len (arr3) arr4 = [ 2 , - 1 ] n4 = len (arr4) sum = 0 print (countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum )) # This code is contributed by Prince Kumar |
C#
// C# implementation of the approach using System; using System.Collections.Generic; public class GFG { // Function to return the count // of the required quadruplets public static int CountQuadruplets( int [] arr1, int n1, int [] arr2, int n2, int [] arr3, int n3, int [] arr4, int n4, int value) { List< int > sum1 = new List< int >(); int cnt = 0; // Take every possible pair sum // from the two arrays for ( int i = 0; i < n1; i++) { for ( int j = 0; j < n2; j++) { // Push the sum to a vector sum1.Add(arr1[i] + arr2[j]); } } // Sort the sum vector sum1.Sort(); // Calculate the pair sums from // the other two arrays for ( int i = 0; i < n3; i++) { for ( int j = 0; j < n4; j++) { // Calculate the sum int temp = arr3[i] + arr4[j]; // Check whether temp can be added to any // sum stored in the sum1 vector such that // the result is the required sum if (sum1.BinarySearch(value - temp) >= 0) { // Add the count of such values from the sum1 vector int it = sum1.BinarySearch(value - temp); int it2 = it + 1; while (it2 < sum1.Count && sum1[it2] == (value - temp)) { it2++; } cnt = cnt + (it2 - it); } } } return cnt; } // Driver code public static void Main( string [] args) { int [] arr1 = { 0, 2 }; int n1 = arr1.Length; int [] arr2 = { -1, -2 }; int n2 = arr2.Length; int [] arr3 = { 2, 1 }; int n3 = arr3.Length; int [] arr4 = { 2, -1 }; int n4 = arr4.Length; int sum = 0; Console.WriteLine(CountQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum)); } } // This code is contributed by Aman Kumar. |
Javascript
// Function to return the count // of the required quadruplets function countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, value) { let sum1 = []; // Take every possible pair sum // from the two arrays for (let i = 0; i < n1; i++) { for (let j = 0; j < n2; j++) { // Push the sum to an array sum1.push(arr1[i] + arr2[j]); } } // Sort the sum array sum1.sort((a, b) => a - b); let cnt = 0; // Calculate the pair sums from // the other two arrays for (let i = 0; i < n3; i++) { for (let j = 0; j < n4; j++) { // Calculate the sum let temp = arr3[i] + arr4[j]; // Check whether temp can be added to any // sum stored in the sum1 array such that // the result is the required sum if (binarySearch(sum1, value - temp) >= 0) { // Add the count of such values from the sum1 array let it = binarySearch(sum1, value - temp); let it2 = it + 1; while (it2 < sum1.length && sum1[it2] === value - temp) { it2++; } cnt = cnt + (it2 - it); } } } return cnt; } // Binary search function to search for an element in the array function binarySearch(arr, x) { let l = 0, r = arr.length - 1; while (l <= r) { let m = Math.floor((l + r) / 2); if (arr[m] === x) { return m; } else if (arr[m] < x) { l = m + 1; } else { r = m - 1; } } return -1; } // Driver code let arr1 = [0, 2]; let n1 = arr1.length; let arr2 = [-1, -2]; let n2 = arr2.length; let arr3 = [2, 1]; let n3 = arr3.length; let arr4 = [2, -1]; let n4 = arr4.length; let sum = 0; console.log(countQuadruplets(arr1, n1, arr2, n2, arr3, n3, arr4, n4, sum)); |
2
Time Complexity: O(n*log(n)+m*log(n)) where n=n1*n2 and m=n3*n4
Auxiliary Space: O(n)
Please Login to comment...