<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> if it is possible to divide this array into <code>k</code> non-empty subsets whose sums are all equal.</p>

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

<pre>
<strong>Input:</strong> nums = [4,3,2,3,5,2,1], k = 4
<strong>Output:</strong> true
<strong>Explanation:</strong> It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 3
<strong>Output:</strong> false
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 16</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li>The frequency of each element is in the range <code>[1, 4]</code>.</li>
</ul>

class Solution
{
public:
    bool canPartitionKSubsets(vector<int> &nums, int k)
    {
        int n = nums.size();
        int sum = accumulate(nums.begin(), nums.end(), 0);
        // if the size of the array is too small,
        // or the sum of the array does not evenly divide into k parts,
        // then return false
        if (n < k || sum % k != 0)
            return false;

        // if any of the values are too large than we cannot complete it
        for (int num : nums)
            if (num > sum / k)
                return false;

        // just optimization trick because big values speak larger in way :)
        sort(nums.begin(), nums.end(), greater<int>());
        return backtrack(nums, sum / k, 0, 0, 0, k);
    }

private:
    unordered_map<int, int> dp;

    bool backtrack(vector<int> &nums, int target, int start, int sum, int mask, int k)
    {
        if (start >= nums.size())
            return false;
        if (sum > target)
            return false;
        // Since the total sum of the array is divisible by k,
        // the remaining elements must also form a subset with the target sum.
        // Therefore, if we have successfully partitioned into k-1 subsets,
        // the remaining elements naturally form the last subset with the required sum.
        if (k == 1)
            return true;
        // if we have used this exact sequence of elements before then return the cached result
        if (dp.count(mask))
            return dp[mask];
        // start a new search back from the beginning,
        // because we don't know which elements we will pick,
        // and we have marked the ones that we already used in the bitmask
        // the order does not matter beacuse we are picking subsets and not subarrays
        // also reset the sum
        if (sum == target)
            return dp[mask] = backtrack(nums, target, 0, 0, mask, k - 1);

        for (int i = start; i < nums.size(); i++)
        {
            // don't use same element twice
            if (mask & (1 << i))
                continue;

            // try including the current element in the sum
            if (backtrack(nums, target, i + 1, sum + nums[i], mask | (1 << i), k))
                return dp[mask] = true;
        }
        return dp[mask] = false;
    }
};
19495