Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4398 |

@sanjay05 | 3762 |

@mantu_singh | 3654 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 899 |

@akhileshkumar562002 | 862 |

@pradeeppatidar1999 | 852 |

@kanewilliamson123 | 850 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@wallflowe101 | 770 |

Complete Leaderboard |

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 ie- 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 ≤ 10^{3}

1 ≤ a[i], b[i] ≤ 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Geek collects the balls

...