VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-quadruplets-with-sum-k-from-given-array/

⇱ 4 Sum - Count quadruplets with given sum in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

4 Sum - Count quadruplets with given sum in an array

Last Updated : 23 Jul, 2025

Given an array arr[] and a target value, the task is to find the count of quadruplets present in the given array having sum equal to the given target. 

Examples:

Input: arr[] = [1, 5, 3, 1, 2, 10], target = 20
Output: 1
Explanation: Only quadruplet satisfying the conditions is arr[1] + arr[2] + arr[4] + arr[5] = 5 + 3 + 2 + 10 = 20.

Input: arr[] = [1, 1, 1, 1, 1], target = 4
Output: 5
Explanation:
arr[0] + arr[1] + arr[2] + arr[3] = 4
arr[0] + arr[1] + arr[3] + arr[4] = 4
arr[0] + arr[1] + arr[2] + arr[4] = 4
arr[0] + arr[2] + arr[3] + arr[4] = 4
arr[1] + arr[2] + arr[3] + arr[4] = 4

Input: arr = [4, 3, -13, 3], target = -3
Output: 1
Explanation: There is only 1 quadruplet with sum = -3, that is [4, 3, -13, 3].

Naive Approach - O(n^4) Time and O(1) Space

The idea is to generate all possible combinations of length 4 from the given array. For each quadruplet, if the sum equals target, then increment the counter by 1.


Output
5

Time Complexity: O(n4), where n is the size of the given array.
Auxiliary Space: O(1)

Better Approach - O(n^3) Time and O(n) Space

The idea is to use Hash Map or Dictionary. For every pair (arr[i], arr[j]) where j > i, we simply use the logic of counting pairs for the subarray from (j + 1) to end of the array for target equals to given target - arr[i] - arr[j].

  • Initialize the counter count with 0 to store the number of quadruplets.
  • Traverse the given array over the range [0, n - 3) using the variable i. For each element arr[i], traverse the array again over the range [i + 1, n - 2) using the variable j and do the following:
    • Find the value of the remaining sum(say rem) as (target - arr[i] - arr[j]).
    • Count the number of pairs with sum = (target - arr[i] - arr[j]) in the subarray arr[j+1...n-1] and add it to count.
  • After the above steps, print the value of count as the result.

Output
5

Time complexity: O(n3), where n is the size of the given array.
Auxiliary Space: O(n)

Efficient Approach - O(n^2) Time and O(n^2) Space

The idea is similar to the above approach using hashing. In this approach, we fix the 3rd element, then find and store the frequency of sums of all possible first two elements of any quadruplet of the given array. Follow the below steps to solve the problem:

  • Initialize count = 0 and a hash map or dictionary to store count of all possible sums of the first two elements of possible quadruplets.
  • Iterate over the array with arr[i] as the third element of the quadruplet.
  • For each arr[i], loop j from [i + 1... n - 1] to find the fourth element and check if (target - arr[i] - arr[j]) exists in the hash map. If it does, add its frequency to count.
  • After iterating over all possible fourth elements, traverse the array arr[] from j = 0 to i - 1 and increment the frequency of all sums arr[i] + arr[j]. This is needed because when we move to the next i, we need to look for possible pairs that come before i.
  • Repeat the above steps for each element arr[i] and return count as the total number of quadruplets with the given target sum.

Output
5

Time Complexity: O(n2), where n is the size of the given array.
Auxiliary Space: O(n2), as we are storing sum of all possible pairs in the hash map.

Comment