Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int twoStack(int x, int[] a, int[] b) {
- int sum = 0, maxBucket=0;
- int bj = 0, ai = 0;
- while(ai < a.length && (sum+a[ai]) <= x ){
- sum+=a[ai++];
- maxBucket++; // add max buckets from 1st stack , sum <=x
- }
- while(bj < b.length){ // add each element from b stack
- sum+=b[bj++];
- while(sum>x && ai > 0){// while sum is larger
- sum-=a[--ai]; // keep removing element from a stack from top
- }
- if(sum<=x && ai+bj>maxBucket){// if ai+aj > maxBucket
- maxBucket= ai+bj; // maxbucket is ai+aj
- }
- }
- return maxBucket;
- }
Add Comment
Please, Sign In to add comment