Help Needed I don't it is again and agin giving MLE optimisation needed


#1
bool mycomp(vector<int> &a,vector<int> &b)
{
    if(a.size()<b.size())
    {
        return true;
    }
    else if(a.size()>b.size())
    {
        return false;
    }
    else
    {
        for(int i=0;i<a.size();i++)
        {
            if(a[i]<b[i])
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return true;
    }
}

bool check(int ind,int sum,int element,vector<int> &A,vector<int> &res,vector<vector<vector<int>>> dp)
{
    if(element==0)
    {
        return sum==0;
    }
    
    if(ind>=A.size())
    {
        return false;
    }
    
    if(dp[sum][ind][element]!=-1)
    {
        return dp[sum][ind][element];
    }
    
    if(A[ind]<=sum)
    {
        res.push_back(A[ind]);
        if(check(ind+1,sum-A[ind],element-1,A,res,dp))
        {
            return dp[sum][ind][element] = true;
        }
        res.pop_back();
    }
    
    if(check(ind+1,sum,element,A,res,dp))
    {
        return dp[sum][ind][element] = true;
    }
    
    return dp[sum][ind][element] = false;
}

vector<vector<int> > Solution::avgset(vector<int> &A) {
     sort(A.begin(),A.end());
    
    int sum =0;
    for(int i: A)
    {
        sum+=i;
    }
    
    vector<vector<vector<int>>> dp(sum+1,vector<vector<int>>(A.size(),vector<int>(A.size(),-1)));
    int n= A.size();
    vector<vector<int>> ret;
    
    for(int i=1;i<=n-1;i++)
    {
        if((sum*i)%n == 0)
        {
            int ss = i*sum;
            ss/=n;
            vector<int> res;
            
            if(check(0,ss,i,A,res,dp))
            {
                //process the array;
                sort(res.begin(),res.end());
                if(ret.size()==0)
                {
                    ret.push_back(res);    
                }
                else 
                {
                    if(mycomp(ret[0],res))
                    {
                        
                    }
                    else
                    {
                        ret.pop_back();
                        ret.push_back(res);
                    }
                }
                
            }
        }
    }
    
    if(ret.size()==0)
    {
        return {};    
    }
    else
    {
        vector<int> ans1;
        
        for(auto i: ret)
        {
            ans1=i;
            break;
        }
        
        vector<int> r2;
        int index=0;
        for(int i=0;i<A.size();i++)
        {
            if(index<ans1.size())
            {
                if(ans1[index]==A[i])
                {
                    index++;
                    continue;
                }
                else
                {
                    r2.push_back(A[i]);
                }
            }
            else{
                r2.push_back(A[i]);
            }
        }
        
        return {ans1,r2};
        
    }
    
}