<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 {
//     int cnt = 0 ;
//     int totalSum ;
//     int n ;
//     vector<vector<vector<int>>> dp ;
//     map<pair<int,pair<int,int>>,int> mpp ;
//     bool helper(vector<int>& nums , int sum , int elecnt , int index){
//         if(index >= n){
//             if(elecnt == 0 || n-elecnt == 0) return false;
//             double av1 =  (double)sum/(double)elecnt ;
//             double av2 =  (double)(totalSum-sum)/(double)(n-elecnt);

//             if(av1 == av2){
//                 return true ;
//             }
//             return false;
//         }

//         if(dp[sum][elecnt][index] != -1) return dp[sum][elecnt][index] ;
        
//         bool o1 =  helper(nums , sum+nums[index] , elecnt+1 , index+1);
//         o1 |= helper(nums , sum , elecnt , index+1);
//         return dp[sum][elecnt][index] = o1 ;;
//     }

//     bool solver(vector<int>& nums , int index , int t , int x){
//         if(x == 0 && t == 0) return true ;
//         if(x < 0 || t < 0) return false ;
//         if(index >= n ) return false ;

//         if(mpp.find({index , {t , x}}) != mpp.end()) return mpp[{index , {t,x}}];
//         bool take = solver(nums , index+1 , t-nums[index] , x-1);
//         bool skip = solver(nums , index+1 , t , x);

//         return mpp[{index , {t,x}}] = (take|skip) ;
//     }
// public:
//     bool splitArraySameAverage(vector<int>& nums) {
//         n = nums.size() ;
//         totalSum = 0 ;
//         for(auto val : nums) totalSum += val ;

//         for(int x =1 ;x<=n/2;x++){
            
//             if((totalSum*x)%n == 0){
//                 int t = (totalSum*x)/n ;
//                 if(solver(nums , 0 , t , x)){
//                     return true ;
//                 }
//             }
//         }

//         return false ;









//         // dp = vector<vector<vector<int>>>(totalSum+1 , vector<vector<int>>(31 , vector<int>(31 , -1)));

//         // return helper(nums , 0 , 0 , 0);
//     }
// };




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