class Solution
{
public:
	vector<vector<int>> fourSum(vector<int> &nums, int target)
	{
		long long l_target = target;
		sort(nums.begin(), nums.end());
		vector<vector<int>> results;
		int N = nums.size();
		for (int i = 0; i < N - 3; i++)
		{
			if (i > 0 && nums[i] == nums[i - 1])
				continue;
			for (int j = i + 1; j < N - 2; j++)
			{
				if (j > i + 1 && nums[j] == nums[j - 1])
					continue;
				for (int k = j + 1, l = N - 1; k < l; k++)
				{
					if (k > j + 1 && nums[k] == nums[k - 1])
						continue;
					while (k < l &&
						   (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
					{
						l--;
					}
					if (k >= l)
					{
						break;
					}
					if ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0)
					{
						results.emplace_back(
							vector<int>({nums[i], nums[j], nums[k], nums[l]}));
					}
				}
			}
		}
		return results;
	}
};