<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) {
        // We can do it via DP, in a knapsack fashion.
        // If it's smaller than our target sum, we can take it.
        int sumOfArr = accumulate(begin(nums), end(nums), 0);
        if (sumOfArr % k != 0) {
            return false;
        }

        // We can do a small optimization here, to sort the numbers in descending order.
        // We'll try the larger numbers first, so it can fail faster.
        sort(begin(nums), end(nums), greater<int>());

        // We can't use the same numbers if we've added them to a used subset already, so we can keep track of used ones.
        // vector<bool> used(nums.size(), false);

        // Our targetSum should be sum of arr / k, as we want to find k subsets with equal sum.\

        // To further optimize, we can use a string bitmask. 0's will represent not taken, 1's will represent taken.
        // We can use this mask as our key for memoization.
        string mask(nums.size(), '0');
        unordered_map<string, bool> memo;
        return backtrack(nums, 0, k, 0, sumOfArr/k, mask, memo);
    }

    bool backtrack(vector<int> &nums, int idx, int k, int currSum, int targetSum, string &mask, unordered_map<string, bool> &memo) {
        // If we're able to make k subsets, then we can return true.
        if (k == 0) {
            return true;
        }

        // If our current sum is over target, then we can stop searching, it's no longer valid answer
        if (currSum > targetSum) {
            return false;
        }

        // If we've already computed for this subset, return it
        if (memo.find(mask) != memo.end()) {
            return memo[mask];
        }

        // We have found 1 valid subset which sums up to target sum, so we can decrement k, and search again.
        // This time, with different numbers, as the used subsets will no longer be available.
        // In the case our targetSum == current sum, then we can decrement k, as we've found a valid solution.
        // We will need to reset our running sum and start the search from beginning again.
        if (currSum == targetSum) {
            return memo[mask] = backtrack(nums, 0, k-1, 0, targetSum, mask, memo);
        }

        // Try all digits in nums
        for(int i = idx; i < nums.size(); ++i) {
            // If we haven't used this digit, we can try taking it.
            if (mask[i] == '0') {
                mask[i] = '1';
                if (backtrack(nums, i+1, k, currSum + nums[i], targetSum, mask, memo)) {
                    return true;
                }
                mask[i] = '0';
            }
        }
        return memo[mask] = false;
    }
};
63990