<p>You are given an integer array <code>nums</code>.</p>

<p>You should move each element of <code>nums</code> into one of the two arrays <code>A</code> and <code>B</code> such that <code>A</code> and <code>B</code> are non-empty, and <code>average(A) == average(B)</code>.</p>

<p>Return <code>true</code> if it is possible to achieve that and <code>false</code> otherwise.</p>

<p><strong>Note</strong> that for an array <code>arr</code>, <code>average(arr)</code> is the sum of all the elements of <code>arr</code> over the length of <code>arr</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6,7,8]
<strong>Output:</strong> true
<strong>Explanation:</strong> We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5.
</pre>

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

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

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 30</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    bool splitArraySameAverage(vector<int>& nums) {
        // we can solve this problem using
        // meet in the middle and math
        int n = nums.size();
        int totalSum = accumulate(nums.begin(), nums.end(), 0);

        // split the array into two halves
        vector<int> left(nums.begin(), nums.begin()+n/2);
        vector<int> right(nums.begin()+n/2, nums.end());

        // find all the possible subset sums of the left part
        vector<vector<int>> leftSums(left.size()+1);

        for(int bitmask=0; bitmask<(1<<left.size()); bitmask++) {
            int setbits = __builtin_popcount(bitmask);
            int sum = 0;

            for(int i=0; i<left.size(); i++) {
                if(bitmask & (1<<i)) sum += left[i];
            }

            leftSums[setbits].push_back(sum);
        }

        // find all the possible subset sums of the right part
        // and add them to hash sets
        vector<unordered_set<int>> rightSums(right.size()+1);

        for(int bitmask=0; bitmask<(1<<right.size()); bitmask++) {
            int setbits = __builtin_popcount(bitmask);
            int sum = 0;

            for(int i=0; i<right.size(); i++) {
                if(bitmask & (1<<i)) sum += right[i];
            }

            // multiply sum with the size of nums
            // before inserting it into the hashset
            // for easier checking in the future
            rightSums[setbits].insert(n * sum);
        }

        // go through each sum in leftSums and check if there
        // are subsets that satisfy the condition
        for(int i=0; i<=left.size(); i++) {
            for(int sum : leftSums[i]) {
                // check for all possible sizes of the subset
                for(int l=max(i, 1); l<i+right.size(); l++) {
                    int leftlen = i;
                    int rightlen = l - i;

                    // check if the required value exists
                    // in the hashset of the right part
                    if(rightSums[rightlen].count((totalSum * l) - (n * sum))) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
};
30213