# 0018. 4Sum

<https://leetcode.com/problems/4sum>

## Description

Given an array `nums` of `n` integers, return *an array of all the **unique** quadruplets* `[nums[a], nums[b], nums[c], nums[d]]` such that:

* `0 <= a, b, c, d < n`
* `a`, `b`, `c`, and `d` are **distinct**.
* `nums[a] + nums[b] + nums[c] + nums[d] == target`

You may return the answer in **any order**.

**Example 1:**

```
**Input:** nums = [1,0,-1,0,-2,2], target = 0
**Output:** [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
```

**Example 2:**

```
**Input:** nums = [2,2,2,2,2], target = 8
**Output:** [[2,2,2,2]]
```

**Constraints:**

* `1 <= nums.length <= 200`
* `-109 <= nums[i] <= 109`
* `-109 <= target <= 109`

## ac

```java
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        return helper(nums, 0, 4, target);
    }

    private List<List<Integer>> helper(int[] nums, int start, int ksum, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        // exit
        if (start >= nums.length) return res;

        if (ksum == 2) {
            int l = start, r = nums.length - 1;
            while (l < r) {
                int sum = nums[l] + nums[r];
                if (sum == target) {
                    List<Integer> tmp = new ArrayList<Integer>();
                    tmp.add(nums[l]);
                    tmp.add(nums[r]);
                    res.add(tmp);
                    // skip duplicate
                    while (l < r && nums[l] == nums[l+1]) l++;
                    while (l < r && nums[r] == nums[r-1]) r--;
                    l++;
                    r--;
                } else if (sum < target) {
                    l++;
                } else {
                    r--;
                }
            }
            return res;
        }

        // recursion
        for (int i = start; i <= nums.length - ksum; i++) {
            if (i > start && nums[i] == nums[i-1]) continue; // skip duplicate
            // child result
            List<List<Integer>> child = helper(nums, i+1, ksum-1, target-nums[i]);
            for (List<Integer> list : child) {
                list.add(nums[i]);
            }
            res.addAll(child);
        }

        return res;
    }
}

/*
sort, recursion till 2sum
*/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/0018-4sum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
