Two Pointer || JAVA
IntuitionThe goal is to find all unique quadruplets in the array that sum up to the target. Since a brute force approach would involve iterating through all com
# Intuition
The goal is to find all unique quadruplets in the array that sum up to the target. Since a brute force approach would involve iterating through all combinations, which is inefficient, we can use a combination of sorting and the two-pointer technique to reduce the time complexity. Sorting the array helps identify and skip duplicates efficiently while maintaining order for the two-pointer traversal.

# Approach
1. **Sort the Array:** Begin by sorting the input array to simplify the process of handling duplicates and allow the use of two-pointer techniques.
2. **Iterate through the Array:**
   - Use two nested loops (`i` and `j`) for the first two numbers of the quadruplet.
   - Skip duplicate values for both `i` and `j` to avoid repeating the same quadruplets.
3. **Two-Pointer Technique:**
   - For each pair of numbers chosen by `i` and `j`, use two pointers (`left` and `right`) to find the remaining two numbers.
   - Adjust the pointers based on the sum:
     - If the sum equals the target, add the quadruplet to the result and move both pointers inward while skipping duplicates.
     - If the sum is less than the target, move the `left` pointer to increase the sum.
     - If the sum is greater than the target, move the `right` pointer to decrease the sum.
4. **Avoid Overflow:** Use `long` for the sum to handle cases where the numbers are near `Integer.MAX_VALUE` or `Integer.MIN_VALUE`.
5. **Return Results:** Convert the results into a list of lists and return them.

# Complexity
- **Time complexity:** `O(n^3)`
  - Sorting takes `O(nlog n)`.
  - The nested loops (`i`, `j`) and two-pointer traversal take \(O(n^3)\) in total.
- **Space complexity:** \(O(1)\)
  - Apart from the result list, no significant additional space is used.

# Code
```java
import java.util.*;

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 4) {
            return result; // Return empty list if not enough elements
        }

        Arrays.sort(nums); // Sort the array for easier duplicate handling

        int n = nums.length;
        for (int i = 0; i < n - 3; i++) {
            // Skip duplicate values for the first number
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            for (int j = i + 1; j < n - 2; j++) {
                // Skip duplicate values for the second number
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                int left = j + 1;
                int right = n - 1;

                while (left < right) {
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];

                    if (sum == target) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));

                        left++;
                        right--;

                        // Skip duplicate values for the third and fourth numbers
                        while (left < right && nums[left] == nums[left - 1]) left++;
                        while (left < right && nums[right] == nums[right + 1]) right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }

        return result;
    }
}

['Array', 'Two Pointers', 'Sorting', 'Java']