Sort by

recency

|

409 Discussions

|

  • + 0 comments

    Only 2 test cases (0,4)are passing all other are failing , can't think where is it going wrong public static int solve(Stack f,Stack s,int len , int cs,int max){ int n = f.peek()>=s.peek()?s.peek():f.peek(); // System.out.println(f.peek()); // System.out.println(s.peek()); // System.out.println("n:"+n); if(cs+n a, List b) { // Write your code here Stack first = new Stack<>(); for(int i =a.size()-1;i>=0;i--){ first.push(a.get(i)); } // System.out.println;

    Stack<Integer> Second = new Stack<>();
    for(int i =b.size()-1;i>=0;i--){
        Second.push(b.get(i));
    }
    int len = 0;
    int currsumm = 0;
    return solve(first,Second,len,currsumm,maxSum);
    
    }
    

    }

  • + 0 comments
    import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int g = sc.nextInt();
            for (int tc = 0; tc < g; tc++) {
                int n = sc.nextInt();
                int m = sc.nextInt();
                int x = sc.nextInt();
                int[] a = readArray(sc, n);
                int[] b = readArray(sc, m);
    
                System.out.println(solve(a, b, x));
            }
    
            sc.close();
        }
    
        static int[] readArray(Scanner sc, int size) {
            int[] result = new int[size];
            for (int i = 0; i < result.length; i++) {
                result[i] = sc.nextInt();
            }
            return result;
        }
    
        static int solve(int[] a, int[] b, int x) {
            int lengthB = 0;
            int sum = 0;
            while (lengthB < b.length && sum + b[lengthB] <= x) {
                sum += b[lengthB];
                lengthB++;
            }
    
            int maxScore = lengthB;
            for (int lengthA = 1; lengthA <= a.length; lengthA++) {
                sum += a[lengthA - 1];
    
                while (sum > x && lengthB > 0) {
                    lengthB--;
                    sum -= b[lengthB];
                }
    
                if (sum > x) {
                    break;
                }
    
                maxScore = Math.max(maxScore, lengthA + lengthB);
            }
            return maxScore;
        }
    }
    
  • + 1 comment
    def twoStacks(maxSum, a, b):
        # Write your code here
        i = j = s = 0
        
        while i < len(a) and s + a[i] <= maxSum: 
            s += a[i]
            i += 1
            
        n = maxn = i
        i -= 1
        
        while j < len(b):
            if s + b[j] <= maxSum:
                s += b[j]
                j += 1
                n += 1
                maxn = max(maxn, n)
            elif i >= 0:
                   s -= a[i]
                   i -= 1
                   n -= 1
            else:
                break
        
        return maxn
    
  • + 1 comment

    I found the solution on programmingoneonone

  • + 0 comments
    def twoStacks(maxSum, a, b):
        Totalsum = 0
        countS1 = 0 
        countS2 = 0
        result = 0
        for ele in a:
            if Totalsum + ele > maxSum:
                break
            Totalsum = Totalsum + ele
            countS1 = countS1 + 1
        for ele in b:
            Totalsum = Totalsum + ele
            countS2 = countS2 + 1
            while Totalsum > maxSum and countS1 > 0:
                Totalsum = Totalsum - a[countS1-1]
                countS1 = countS1 -1
            if Totalsum <= maxSum:
                result = max((countS1 + countS2), result)
        return result