Count of ways to choose 4 unique position elements one from each Array to make sum at most K
Given four arrays A[], B[], C[], D[] and an integer K. The task is to find the number of combinations of four unique indices p, q, r, s such that A[p] + B[q] + C[r] + D[s] ≤ K.
Examples:
Input: A = {2, 3}, B = {5, 2}, C = {0}, D = {1, 2}, K = 6
Output: 3
Explanation: The following are the required combinations:
{2, 2, 0, 1}, {2, 2, 0, 2}, {3, 2, 0, 1}Input: A = {1, 1}, B = {0}, C = {0}, D = {0}, K = 1
Output: 2
Naive approach: The brute force would be to build the sum of all combinations of four numbers, using four nested loops, and count how many of those sums are at most K.
Time Complexity: O(N4) where N is the maximum size among those four arrays
Auxiliary Space: O(1)
Efficient Approach: Improve the above method by using Divide and Conquer and Binary Search. Follow the steps mentioned below to solve the problem:
- Generate all possible pair combinations for A, B, and C, D.
- Assume each array has length n, then we will have two arrays, each with length n*n. Let it be merge1 and merge2.
- Sort one of the merge array, let’s say merge2.
- Iterate through the unsorted merge1 array and find how many elements from merge2 can be paired up with a sum less than or equal to K. It can easily be done by using binary search.
Below is the implementation of the above method.
C++
// C++ to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to get the number of combinations int fourSumLessThanK(vector< int >& A, vector< int >& B, vector< int >& C, vector< int >& D, int K) { vector< int > merge1; vector< int > merge2; int res = 0; for ( int i : A) { for ( int j : B) { // Merging A and B into merge1 merge1.push_back(i + j); } } for ( int i : C) { for ( int j : D) { // Merging C and D into merge2 merge2.push_back(i + j); } } // Sorting merge2 sort(merge2.begin(), merge2.end()); // Looping through unsorted merge1 for ( int i : merge1) { int l = 0, r = merge2.size() - 1; int pos = -1; // Binary search to find how many // Element from merge2 can be paired // With merge1 element with sum less // Than or equal to K while (l <= r) { int mid = l + (r - l) / 2; if (merge2[mid] + i <= K) { pos = mid; l = mid + 1; } else { r = mid - 1; } } // Adding the number // Of pairs in the result res += pos + 1; } return res; } // Driver Code int main() { vector< int > A = { 2, 3 }; vector< int > B = { 5, 2 }; vector< int > C = { 0 }; vector< int > D = { 1, 2 }; int K = 6; // Function call cout << fourSumLessThanK(A, B, C, D, K); return 0; } |
Java
// Java code for the above approach import java.util.*; class GFG { // Function to get the number of combinations static int fourSumLessThanK( int A[], int B[], int C[], int D[], int K) { List<Integer> merge1= new ArrayList<Integer>(); List<Integer> merge2= new ArrayList<Integer>(); int res = 0 ; for ( int i = 0 ; i < A.length; i++) { for ( int j = 0 ; j < B.length; j++) { // Merging A and B into merge1 merge1.add(A[i] + B[j]); } } for ( int i = 0 ; i < C.length; i++) { for ( int j = 0 ; j < D.length; j++) { // Merging C and D into merge2 merge2.add(C[i] + D[j]); } } // Sorting merge2 Collections.sort(merge2); // Looping through unsorted merge1 for ( int i = 0 ; i < merge1.size(); i++) { int l = 0 , r = merge2.size() - 1 ; int pos = - 1 ; // Binary search to find how many // Element from merge2 can be paired // With merge1 element with sum less // Than or equal to K while (l <= r) { int mid = l + (r - l) / 2 ; if (merge2.get(mid) + merge1.get(i) <= K) { pos = mid; l = mid + 1 ; } else { r = mid - 1 ; } } // Adding the number // Of pairs in the result res += pos + 1 ; } return res; } // Driver Code public static void main (String[] args) { int A[] = { 2 , 3 }; int B[] = { 5 , 2 }; int C[] = { 0 }; int D[] = { 1 , 2 }; int K = 6 ; System.out.println(fourSumLessThanK(A, B, C, D, K)); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python code for the above approach # Function to get the number of combinations def fourSumLessThanK(A, B, C, D, K): merge1 = []; merge2 = []; res = 0 ; for i in range ( len (A)): for j in range ( len (B)): # Merging A and B into merge1 merge1.append(A[i] + B[j]); for i in range ( len (C)): for j in range ( len (D)): # Merging C and D into merge2 merge2.append(C[i] + D[j]); # Sorting merge2 merge2.sort() # Looping through unsorted merge1 for i in range ( len (merge1)): l = 0 ; r = len (merge2) - 1 ; pos = - 1 ; # Binary search to find how many # Element from merge2 can be paired # With merge1 element with sum less # Than or equal to K while (l < = r): mid = (l + r) / / 2 ; if (merge2[mid] + merge1[i] < = K): pos = mid; l = mid + 1 ; else : r = mid - 1 ; # Adding the number # Of pairs in the result res = res + pos + 1 ; return res; # Driver Code A = [ 2 , 3 ]; B = [ 5 , 2 ]; C = [ 0 ]; D = [ 1 , 2 ]; K = 6 ; # Function call print (fourSumLessThanK(A, B, C, D, K)); # This code is contributed by Potta Lokesh |
C#
// C# code for the above approach using System; using System.Collections; class GFG { // Function to get the number of combinations static int fourSumLessThanK( int []A, int []B, int []C, int []D, int K) { ArrayList merge1 = new ArrayList(); ArrayList merge2 = new ArrayList(); int res = 0; for ( int i = 0; i < A.Length; i++) { for ( int j = 0; j < B.Length; j++) { // Merging A and B into merge1 merge1.Add(A[i] + B[j]); } } for ( int i = 0; i < C.Length; i++) { for ( int j = 0; j < D.Length; j++) { // Merging C and D into merge2 merge2.Add(C[i] + D[j]); } } // Sorting merge2 merge2.Sort(); // Looping through unsorted merge1 for ( int i = 0; i < merge1.Count; i++) { int l = 0, r = merge2.Count - 1; int pos = -1; // Binary search to find how many // Element from merge2 can be paired // With merge1 element with sum less // Than or equal to K while (l <= r) { int mid = l + (r - l) / 2; if (( int )merge2[mid] + ( int )merge1[i] <= K) { pos = mid; l = mid + 1; } else { r = mid - 1; } } // Adding the number // Of pairs in the result res += pos + 1; } return res; } // Driver Code public static void Main () { int []A = { 2, 3 }; int []B = { 5, 2 }; int []C = { 0 }; int []D = { 1, 2 }; int K = 6; Console.WriteLine(fourSumLessThanK(A, B, C, D, K)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript to implement the above approach // Function to get the number of combinations const fourSumLessThanK = (A, B, C, D, K) => { let merge1 = []; let merge2 = []; let res = 0; for (let i in A) { for (let j in B) { // Merging A and B into merge1 merge1.push(A[i] + B[j]); } } for (let i in C) { for (let j in D) { // Merging C and D into merge2 merge2.push(C[i] + D[j]); } } // Sorting merge2 merge2.sort(); // Looping through unsorted merge1 for (let i in merge1) { let l = 0, r = merge2.length - 1; let pos = -1; // Binary search to find how many // Element from merge2 can be paired // With merge1 element with sum less // Than or equal to K while (l <= r) { let mid = l + parseInt((r - l) / 2); if (merge2[mid] + merge1[i] <= K) { pos = mid; l = mid + 1; } else { r = mid - 1; } } // Adding the number // Of pairs in the result res += pos + 1; } return res; } // Driver Code let A = [2, 3]; let B = [5, 2]; let C = [0]; let D = [1, 2]; let K = 6; // Function call document.write(fourSumLessThanK(A, B, C, D, K)); // This code is contributed by rakeshsahni </script> |
3
Time Complexity: O(N2 * logN)
Auxiliary Space: O(N2)
Please Login to comment...