<p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>

<p>Notice that the solution set must not contain duplicate triplets.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong> 
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 3000</code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

#include <cassert>
#include <compare>
#include <ranges>
#include <span>
#include <unordered_set>
#include <utility>
#include <vector>

struct addends {
    int n0, n1, n2;
    bool operator==(addends const&) const noexcept = default;
    std::strong_ordering operator<=>(addends const&) const noexcept = default;
};

std::size_t hash_combine(std::size_t lhs, std::size_t rhs) {
    return lhs ^ rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);
}

namespace std {
template <>
struct hash<addends>
{
    std::size_t operator()(addends const& a) const {
        return hash_combine(
            hash_combine(std::hash<int>{}(a.n0), std::hash<int>{}(a.n1)), std::hash<int>{}(a.n2));
    }
};
} // namespace std

class Solution {
public:

    std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
        std::ranges::sort(nums);
        std::unordered_set<addends> set;
        int sz = size(nums);
        for (auto i = 0; i < sz - 2; ++i) {
            int n0 = nums[i];
            auto vec = twoSum(span(&nums[i + 1], sz - i - 1), -n0);
            for (auto [n1, n2]: vec)
                set.insert(addends(n0, n1, n2));
        }
        std::vector<std::vector<int>> result;
        result.reserve(size(set));
        for (auto [n0, n1, n2]: set) {
            result.push_back({n0, n1, n2});
        }
        return result;
    }

private:
    
    std::vector<std::pair<int,int>> twoSum(std::span<int> nums, int target) {
        std::vector<std::pair<int,int>> vec;
        assert(size(nums) >= 2);
        int first = 0;
        int last = size(nums) - 1;
        do {
            auto sum = nums[first] + nums[last];
            if (sum < target) ++first;
            else if (sum > target) --last;
            else {
                vec.emplace_back(nums[first], nums[last]);
                ++first;
                --last;
            }
        } while (first < last);
        return vec;
    }

};
199149