Partition Problem

https://www.interviewbit.com/problems/partitions/ problem link…
This ques is totally going from over my head
can u explain following code logic where i commented
https://ide.codingblocks.com/s/478909

hello @joshisanjay8467

cnt is holding how many times sum/3 occured till i (not including i).
so whener u find s==total sum/3 u increment its(cnt) value by 1.

now whenever ur s(prefix sum) = two_third of total sum
that means remaining part has sum =1/3 of total sum.
so all we need is how many times total sum/3 occured before current point.
we will consider it as left partition and current point as right parition.
that is why we are doing ans+=cnt;
if still u face any issue then check my implementation

int Solution::solve(int A, vector<int> &B){
    int sum=0,su=0,ways=0;
    for(auto k : B)
        sum+=k;
        if(sum%3)
        return 0;
    map<int,int> mp;
    for(int i=0;i<B.size()-1;i++){
        su+=B[i];
        
        if(sum-su==sum/3){
            ways+=mp[su-sum/3];
        }
        mp[su]++;
    }
    if(sum==0){
        ways=mp[0];
        ways=ways*(ways-1)/2;
    }
    return ways;
    
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.