Show problems as
Classic problem #18MediumArrayTwo PointersSorting

4Sum

Real-world version: Four-Card Bias Groups
Tracked in this browser only
Read 4Sum on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A test bench stacks probe cards to cancel out a bias. You are given the integer array offsets, holding one signed bias figure per card in the tray, and an integer target.

Report every group of four cards whose bias figures total target. A group picks four distinct tray positions, so no card may be counted twice inside one group.

Two groups are the same group when they hold the same four figures with the same repeats, whichever tray positions supplied them; report such a group once only. Write the four figures of a group in non-decreasing order. The groups themselves may come back in any order. Return an empty list when no four cards total target.

Examples

Example 1

Input
offsets = [1, 5, 3, 6], target = 15
Output
[[1, 3, 5, 6]]

All four cards must be used, and 1 + 5 + 3 + 6 is 15, so the single group is the whole tray written in non-decreasing order.

Example 2

Input
offsets = [-6, -1, 0, 1, 6, 7], target = 0
Output
[[-6, -1, 0, 7], [-6, -1, 1, 6]]

Two distinct groups of figures reach 0 here; either ordering of the two groups is accepted.

Example 3

Input
offsets = [9, 9, 9], target = 27
Output
[]

Only three cards sit in the tray, so no group of four exists and the report is empty.

Constraints

  • 1 <= offsets.length <= 200
  • -10^9 <= offsets[i] <= 10^9
  • -10^9 <= target <= 10^9

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def bias_quartets(offsets: list[int], target: int) -> list[list[int]]:
Java
public List<List<Integer>> biasQuartets(int[] offsets, int target)