Array

Count Zero-Sum Quadruplets Across Four Arrays

Counting quadruplets that sum to zero across four arrays is a classic interview question, and a recent post on techiedelight.com lays out a clean solution. The trick is to stop thinking about four loops and start thinking about two pairs.

The algorithm reduces the brute-force O(n to the fourth) approach down to O(n squared) by leaning on a hash map of pair sums.

The short version:

  • The goal is to count combinations where one element from each of four arrays adds up to zero.
  • The naive brute force needs four nested loops and runs in O(n to the fourth).
  • The hash-map approach runs in O(n squared) time and O(n squared) extra space.
  • The idea is to record every pair sum from the first two arrays, then look up its opposite from the last two.
  • The post includes working code in C++, Java, and Python.

How the algorithm works

The post splits the problem in half. Whenever the sum of some pair from the first two arrays equals the negative of a pair from the last two arrays, you have a valid quadruplet. Build a hash map from the first two arrays where each key is a pair sum and each value is how often that sum appeared. Then walk the last two arrays, compute each pair sum, and check whether the map contains its negation. Every match is a valid quadruplet, and you add the stored count to a running total.

This collapses the search dramatically. With n elements per array, you generate n squared pair sums up front and probe n squared sums on the right side. Total work stays quadratic instead of blowing up to quartic.

Code shape in all three languages

The three implementations are structurally identical. In C++, the post uses an unordered_map<int, int>. In Java, it uses a HashMap<Integer, Integer> with getOrDefault to keep the counting loop tidy. In Python, it uses a plain dict with setdefault for the same purpose. Each main routine feeds in a small example set, computes the count, and prints it.

The Java and Python versions are slightly more compact thanks to getOrDefault and setdefault, but the logic is unchanged. There is no preprocessing, no sorting, and no clever pruning, which is part of the appeal in an interview setting.

Our analysis

The most important lesson is the technique, not the algorithm itself. Whenever you see "find combinations across k arrays", look for a way to halve the search space. Hash maps of pair sums are a workhorse for four-sum and similar problems, and the same idea extends to k-sum with minor adjustments.

The trade-off is space. You are trading O(n to the fourth) time for O(n squared) extra memory, which is almost always worth it, but worth flagging in an interview setting. If the inputs are huge or memory-bound, sorting plus a two-pointer sweep may be a better fit.

Worked example

Say you have four lists of transaction amounts and want to flag sets of four transactions that net to zero, useful for detecting offsetting bookkeeping entries. Run the algorithm across all four lists, count the matches, and you have a fast first-pass filter before doing any deeper analysis.

What to watch next

If you want to extend this, look at the related problems the post links to: finding quadruplets with a given target sum, and printing all such quadruplets. Both build on the same pair-sum map idea, with output requirements that change the data structure choices.

Who should care: developers prepping for technical interviews, students learning hash-map patterns, and engineers who need a quick counting primitive for multi-array analytics.

Key takeaways

  • A hash map of pair sums turns a quartic brute force into a quadratic algorithm.
  • The technique generalises to other multi-array combination problems.
  • The trade-off is O(n squared) extra space, which is usually acceptable.

Frequently asked questions

What is the time complexity of the algorithm?

According to techiedelight.com, the solution runs in quadratic time and uses quadratic extra memory relative to the array size.

Why does the algorithm work?

It splits the quadruplet equation in half, counting all pair sums from the first two arrays and matching each pair sum from the last two against its negation.

Which languages does the post cover?

The post includes complete implementations in C++, Java, and Python.

Can this algorithm be extended to print all quadruplets instead of counting them?

The post links to a related problem that does exactly that, building on the same pair-sum map technique.

What sample input does the post use?

The post demonstrates with small integer arrays including values like 0, -1, 1, 2, 4, and -4 to verify the count.

Related