<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:

// //dp[sum] is a bitmask here jo ki store karta hai in what all lengths is this sum available 
//     bool splitArraySameAverage(vector<int>& A) {
//         if(A.size()<=1) {
//             return false;
//         }
//         int n = A.size();
//         int Sum = 0;
//         for(auto x: A) Sum += x;
        
//         int dp[Sum+1];
//         memset(dp, 0, sizeof dp);
//         dp[A[0]] = 2;
//         for(int i=1; i<n; i++) {
//             for(int s=Sum-A[i]; s>=0; s--) {
//                 if(dp[s])
//                     dp[s+A[i]] |= (dp[s]<<1);
//             }
//             dp[A[i]] |= 2;
//         }
//         for(int len=1; len<n; len++) {
//             if( (Sum*len)%n == 0 && ((1<<len) & dp[Sum*len/n])) {
//                 return true;
//             }
//         }
//         return false;
//     }
// };
//alternate simple soln

unordered_map<string,bool>mp;

int solve(int ind,int s1,int siz,vector<int>&v)
{
    if(ind>=v.size())
        return s1==0&&siz==0;
    
    if(siz==0)
        return s1==0;
    
    string k=to_string(ind)+" "+to_string(s1)+" "+to_string(siz);

    if(mp.find(k)!=mp.end())
        return mp[k];
    
    mp[k]=false;

    if(v[ind]<=s1)
        mp[k]|=solve(ind+1,s1-v[ind],siz-1,v);
    
    if(mp[k])
        return 1;

    return mp[k]|=solve(ind+1,s1,siz,v);
}


class Solution {
public:
    bool splitArraySameAverage(vector<int>& v) {
        //s1+s2=s
        //n1+n2=n
        //s1/n1=s2/n2 --> s2=(n-n1)s1/n1
        //s1=s*n1/n

        mp.clear();

        int s=0;

        for(auto i:v)
            s+=i;
        
        int n=v.size();
        
        for(int i=1;i<n;++i)
        {
            if((s*i)%n==0)
            {
                int s1=s*i;
                s1/=n;
                
                if(solve(0,s1,i,v))
                    return true;
            }
        }
        return false;
    }
};
105661