> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/4-sum.md).

# 4 Sum

Given an array `S` of `n` integers, are there elements `a, b, c, and d` in `S` such that `a + b + c + d = target`? Find all unique quadruplets in the array which gives the sum of target.

> **Note:**
>
> * Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
> * The solution set must not contain duplicate quadruplets.

**Example :**\
Given array `S = {1 0 -1 0 -2 2}`, and `target = 0`\
A solution set is:

```
    (-2, -1, 1, 2)
    (-2,  0, 0, 2)
    (-1,  0, 0, 1)
```

Also make sure that the solution set is lexicographically sorted.\
`Solution[i] < Solution[j] iff Solution[i][0] < Solution[j][0] OR (Solution[i][0] == Solution[j][0] AND ... Solution[i][k] < Solution[j][k])`

```java
public class Solution {
    public ArrayList<ArrayList<Integer>> fourSum(ArrayList<Integer> A, int target) {
        Collections.sort(A);
        // changing ArrayList to Integer array
        Integer[] num = new Integer[A.size()];
        num = A.toArray(num);
        HashSet<String> hashSet = new HashSet<>();
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        for (int i = 0; i + 3 < num.length; i++) {
            for (int j = i + 1; j + 2 < num.length; j++) {
                int k = j + 1;
                int l = num.length - 1;
                while (k < l) {
                    int sum = num[i] + num[j] + num[k] + num[l];
                    if (sum > target)
                        l--;
                    else if (sum < target)
                        k++;
                    else if (sum == target) {
                        ArrayList<Integer> temp = new ArrayList<Integer>();
                        temp.add(num[i]);
                        temp.add(num[j]);
                        temp.add(num[k]);
                        temp.add(num[l]);
                        String str = Integer.toString(num[i]) + " " + Integer.toString(num[j]) + " "
                                + Integer.toString(num[k]) + " " + Integer.toString(num[l]);
                        if (!hashSet.contains(str)) {
                            hashSet.add(str);
                            result.add(temp);
                        }
                        k++;
                        l--;
                    }
                }
            }
        }
        return result;
    }
}
```


---

# 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, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/4-sum.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
