My brute force solution,that worked


#1

#include
#include

int Solution::solve(int A, vector &B) {
int sum = accumulate(B.begin(), B.end(), 0);
int size = B.size();

if(sum%3!=0)
    return 0;
    
sum/=3;

int first_sum=0, second_sum=0, first, second, temp_sum=0, first_time=1;
int partition=0, temp_partition=0;

for(int i=0; i<size; i++){
    
        first_sum+=B[i];
        if(i>=(size-2))
            return 0;
            
            
        if(first_sum==sum){
            first=i;
            break;
        }
        
}

for(int i=first+1; i<size; i++){
    
        second_sum+=B[i];
        if(i>=(size-1))
            return 0;
            
        if(second_sum==sum){
            second=i;
            break;
        }
        
}
temp_sum=second_sum;

while(first<=(size-3)){
    
    if(first_time!=1){
        
        first_sum+=B[first];
        
        if(first_sum!=sum){
            
            first++;
            continue;
            
        }
        else{
            
            if(first<second){
                
                partition+=temp_partition+1;
                //cout<<"partition inner"<<partition<<endl;
                first++;
                continue;
                
            }
            else{
                temp_sum=0;
                
                for(int j=first+1; j<=(size-2); j++){
                    
                    temp_sum+=B[j];
                    if(temp_sum==sum){
                        second=j;
                        break;
                    }

                }                    

                temp_sum=0;
                
                temp_partition=0;
            }
            
        } 
        
    }
    //cout<<" first "<<first<<endl;
   //cout<<" second "<<second<<endl;        
    for(int j=second+1; j<=(size-2); j++){
        
        temp_sum+=B[j];
        if(temp_sum==sum)
            temp_partition++;
        
    }
    
    partition+=temp_partition+1;
    //cout<<"partition "<<partition<<endl;
    first++;
    first_time=0;
}

return partition;

}