Maximize the Sum Through Two Arrays

You are given two sorted arrays of distinct integers, ARR1 and ARR2. If there is a common element in both arrays, you can switch from one array to the other.

Your task is to determine a path through the intersections (i.e., common integers) of ARR1 and ARR2, which results in the maximum possible sum, and return that maximum sum.

Input:
ARR1 = [1, 5, 10, 15, 20] ARR2 = [2, 4, 5, 9, 15]
Output:
56
Example:

For example, in the arrays given, the common elements are 5 and 15.

Start with ARR2 and sum up values until you reach 5 (sum = 11), then switch to ARR1 at 10 and continue until 15 (new sum = 36), then continue with ARR1 alone to add 20 (final sum = 56). The path is 2 -> 4 -> 5 -> 10 -> 15 -> 20.

Input:

The first line contains an integer 'T', representing the number of test cases. Each test case follows:
- The first line includes two space-separated integers, 'N' and 'M', denoting the sizes of 'ARR1' and 'ARR2'.
- The second line contains 'N' space-separated integers denoting the values in 'ARR1'.
- The third line contains 'M' space-separated integers denoting the values in 'ARR2'.

Output:

Output the maximum sum value for each test case on a new line.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N, M ≤ 10^4
  • 1 ≤ ARR1[i], ARR2[j] ≤ 10^5
  • Time Limit: 1 second
Note:
You do not need to print anything; this has already been managed. Implement the given function to achieve the desired results.
AnswerBot
4mo

Given two sorted arrays, find the path through common elements for maximum sum.

  • Start with the array containing the smaller first common element

  • Switch arrays at common elements to maximize the sum

  • Conti...read more

Help your peers!
Select
Add answer anonymously...

Eternal Limited Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Write a SQL query to find the X percentile of students.
A Software Developer Intern was asked Q2. Convert a Binary Tree to a Sum Tree Given a binary tree of integers, convert the...read more
A Software Developer Intern was asked Q3. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
Eternal Limited Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits