We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>usingnamespacestd;intmain(){intg;cin>>g;for(inta0=0;a0<g;a0++){intn;intm;intx;cin>>n>>m>>x;vector<int>a(n);for(inta_i=0;a_i<n;a_i++){cin>>a[a_i];}vector<int>b(m);for(intb_i=0;b_i<m;b_i++){cin>>b[b_i];}intsum=0,count=0,temp=0,i=0,j=0;while(i<n&&sum+a[i]<=x){//considering only first stack and calculating countsum+=a[i];i++;}count=i;while(j<m&&i>=0){//now adding one element of second stack at a time and subtracting the top element of first stack and calculating the count sum+=b[j];j++;while(sum>x&&i>0){i--;sum-=a[i];}if(sum<=x&&i+j>count)count=i+j;}cout<<count<<endl;}return0;}
Game of Two Stacks
You are viewing a single comment's thread. Return to all comments →
My c++ solution