Skip to main content
Medium Hash Map / Set Medium frequency

4Sum II

Open on LeetCode

Approach Summary

Store all pairwise sums of A+B in a HashMap. Count how many C+D values equal the negation of a stored sum.

How to Recognize This Pattern

  • Four arrays, each of length n — O(n²) must beat O(n⁴)
  • Meet-in-the-middle: A+B sums vs -(C+D) sums

Complexity Analysis

Time Complexity

O(n²)

Space Complexity

O(n²)

Tags

Array Hash Table

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee