Here’s the problem : “Emma plays a game where she piles up two stacks using cuboids varying in height. Emma’s sister belma removes one or more cuboids at the top of the two stacks such that their height remains the same. Find the maximum possible height of the two stacks such that all of the stacks are exactly the same height and return the height. if theres no possibility print “Not possible”.”
I tried a stack based solution which seems to be correct but it giving me wrong answer can anyone help me with my code.
cin >> n >> m;
a = 0;
b = 0;
stack<lli>sta, stb;
bool flag = false;
for(lli i=0; i<n; i++){
cin >> k;
a += k;
sta.push(k);
}
for(lli i=0; i<m; i++){
cin >> k;
b += k;
stb.push(k);
}
while(!sta.empty() && !stb.empty()){
if(a == b){
flag = true;
break;
}
if(a > b){
a -= sta.top();
sta.pop();
}
if(b>a){
b -= stb.top();
stb.pop();
}
}
if(flag){
cout << a ;
}
else{
cout << "Not possible";
}