Geek collects the balls | Practice | GeeksforGeeks

There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The balls in first road are given in an array a and balls in the second road in an array b. The buckets on both roads are kept in such a way that they are sorted according to the number of balls in them. Geek starts from the end of the road which has the bucket with a lower number of balls(i.e. if buckets are sorted in increasing order, then geek will start from the left side of the road).
Geek can change the road only at a point of intersection i.e. a point where buckets have the same number of balls on two roads. Help Geek collect the maximum number of balls.
 
Example 1:
Input: 
N = 5, M = 5
a[] = {1, 4, 5, 6, 8}
b[] = {2, 3, 4, 6, 9}
Output: 29
Explanation: The optimal way to get the 
maximum number of balls is to start from 
road 2. Get 2+3. Then switch at intersection 
point 4. Get 4+5+6. Then switch at intersection
point 6. Get 9. Total = 2+3+4+5+6+9 = 29.
Example 2:
Input:
N = 3, M = 3
a[] = {1, 2, 3}
b[] = {4, 5, 6}
Output: 15

 

Your Task:
You do not need to read input or print anything. Your task is to complete the function maxBalls() which takes N, M, a[] and b[] as input parameters and returns the maximum number of balls that can be collected.
 
Expected Time Complexity: O(N+M)
Expected Auxililary Space: O(1)
 
Constraints:
1 ≤ N, M ≤ 103
1 ≤ a[i], b[i] ≤ 106  
0
abhisheksinghyadav05052 weeks ago
I have written my code in python and I am not able to pass this test case:
Possibly your code doesn't work correctly for multiple test-cases (TCs).The first test case where your code failed:Test Cases Passed: 1 / 23Input:  
6 6 
1 8 8 9 11 12
 6 6 8 9 10 14
And Your Code's output is: 
53
Its Correct output is: 
61
can somebody plzz help me with this..
My Python code's link: 
https://practice.geeksforgeeks.org/viewSol.php?subId=963fe3930e4108e165d2151383c7c653&pid=705421&user=abhisheksinghyadav0505
0
arghasahu1 month ago
My clean Java code with Greedy approach,int maxBalls(int n, int m, int[] a, int[] b) {       int i = 0,j = 0, s1 = 0, s2 = 0, c1, c2;       while(i < n && j < m) {           if (a[i] < b[j]) {               s1 += a[i++];           }           else if (a[i] > b[j]) {               s2 += b[j++];           }           else {               int x = a[i], oldS1 = s1, oldS2 = s2;               for (c1 = 0; i < n && a[i] == x; ++c1, ++i);               for (c2 = 0; j < m && b[j] == x; ++c2, ++j);
               s1 = oldS1 + ((c1 > 1 && c2 > 1)? (c1 + c2 - 2) * x: c1 * x);               s1 = Math.max(s1, oldS2 + (c1 + c2 - 1) * x);                              s2 = oldS2 + ((c1 > 1 && c2 > 1)? (c1 + c2 - 2) * x: c2 * x);               s2 = Math.max(s2, oldS1 + (c1 + c2 - 1) * x);           }       }       for (; i < n; ++i) {           s1 += a[i];       }       for (; j < m; ++j) {           s2 += b[j];       }       return Math.max(s1, s2);   }
+1
smartsuman252 months ago
My C++ code for greedy approach:
 
int maxBalls(int n, int m, vector<int> a, vector<int> b){
        int i=0,j=0;
        int s1=0,s2=0;
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        while(i<n || j<m){
            if(i<n && j<m){
                if(a[i]<b[j]){
                    s1 += a[i];
                    i++;
                }
                else if(a[i]>b[j]){
                    s2 += b[j];
                    j++;
                }
                else {
                    int x = a[i];
                    int c1 = 0,c2 = 0;
                    while(a[i++]==x)c1++;
                    while(b[j++]==x)c2++;
                    i--,j--;
                    
                    if(s1>s2){
                        s2 = s1 + (c1+c2-1)*x;
                        if(c1>1)s1 += (c1+c2-2)*x;
                        else s1 += x;
                    }
                    else{
                        s1 = s2 + (c1 + c2 -1)*x;
                        if(c2>1)s2 += (c1+c2-2)*x;
                        else s2 += x;
                    }
                }
            }
            else if(i<n){
                s1 += a[i];
                i++;
            }
            else if(j<m){
                s2 += b[j];
                j++;
            }
        }
        return max(s1,s2);
    }
-1
shivam1706206 months ago
Static int maxBalls(int N, int M, int a[], int b[])   {       // code here       int n=N;       int m=M;       long first=0,second=0;       int res=0;     int i=0,j=0;     while(i<n&& j<m)     {         if(a[i]<b[j]){             first+=a[i++];         }         else if(a[i]>b[j])         {             second+=b[j++];         }         else         {             res+=Math.max(first,second)+a[i];             first=0;             second=0;             int temp=a[i];             ++i;             temp=b[j];             ++j;             while(i<n && a[i]==temp)             res+=a[i++];             while(j<m && b[j]==temp)             res+=b[j++];         }     }     while(i<n)     {         first+=a[i++];     }     while(j<m)     {        second+=b[j++];     }     res+=Math.max(first,second);     return res;          }
-2
shivam1706206 months ago
  int asum=0;       int bsum=0;       int i=0;       int j=0;       int ans=0;       int m=M;       int n=N;       while(i<n && j<m)       {           while((i+1)<n && a[i]==a[i+1])           {               asum+=a[i];               i++;           }           while((j+1)<m && b[j]==b[j+1])           {               bsum+=b[j];               j++;           }                      if(a[i]==b[j])           {               ans+=max(asum,bsum)+a[i];               i++;               j++;               asum=0;               bsum=0;           }           else           {               if(a[i]<b[j])               {                   asum+=a[i];                   i++;               }               else               {                   bsum+=b[j];                   j++;               }           }       }              for(;i<n;i++)       {           asum+=a[i];       }              for(;j<m;j++)       {           bsum+=b[j];       }              ans+=max(asum,bsum);       return ans;   }
0
shivam1706206 months ago
whats wrong with the solution
 
 int maxBalls(int N, int M, vector<int> a, vector<int> b){       // code here       int sum1=0;       int sum2=0;       int l1=0;       int l2=0;       int ans=0;       while(l1<N && l2<M)       {                                             while(  (l1+1)<N && a[l1]==a[l1+1] )              {                  sum1+=a[l1];                  l1++;              }                            while( (l2+1)<M && b[l2]==b[l2+1] )              {                  sum2+=b[l2];                  l2++;              }                                       if(a[l1]==b[l2])           {            ans+=max(sum1,sum2);               ans+=a[l1];               l1++;               l2++;                             sum1=0;               sum2=0;           }           else           {               if(a[l1]<b[l2])               {                   sum1+=a[l1];                   l1++;               }               else               {                   if(a[l1]>b[l2])                   {                       sum2+=b[l2];                       l2++;                   }               }           }       }           while(l1<N)    {        sum1+=a[l1];        l1++;    }       while(l2<M)       {           sum2+=b[l2];           l2++;       }            ans+=max(sum1,sum2);              return ans;          } 
0
Rishabh saxena8 months ago
Rishabh saxena
6 61 8 8 9 11 126 6 8 9 10 14how its correct answer is 61, I think its should be 53.
-1
ANAS MALVAT8 months ago
ANAS MALVAT
    int i = 0;    int j = 0;    int cnt1 = 0;    int cnt2 = 0;    int ret = 0;    while(i < l1 and j < l2)    {        if(A[i] == B[j]) {            int chk = A[i];            while(A[i] == chk and i < l1)            {                cnt1 += A[i++];            }            while(B[j] == chk and j < l2)            {                cnt2 += B[j++];            }            ret +=  max(cnt1,cnt2);            cnt1 = 0;            cnt2 = 0;        }        else if(A[i] < B[j]){            cnt1 += A[i++];        }        else if(B[j] < A[i]){            cnt2 += B[j++];        }    }    while(i < l1)    {        cnt1 += A[i++];    }    while(j < l2)    {        cnt2 += B[j++];    }    return ret + max(cnt1,cnt2);     }
0
Swathi Kedarasetty9 months ago
Swathi Kedarasetty
My AC code in C++. Similar to merge procedure in merge sort.
0
Swathi Kedarasetty
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.