I was recently asked in a job interview to find the max sum across two arrays with given constraints. The question was phrased differently, but it boiled down to what I said above. Nothing was said about the elements being distinct, or the arrays being sorted.
Example:
A = [2000, 3000, 4000, 5000]
B = [6000, 5000, 4000, 1000]
Constraint: A[i] + B[j] <= 7000
Expected Result: (A[0], B[1]), (A[1], B[2])
Note that this isn't the same question as Find the pair across 2 arrays with kth largest sum because of the constraint.
A brute force solution is to take the cartesian product of the 2 arrays, calculate the sum of each pair, filter out the ones greater than 7000, sort, and take the top values that are equal. The time complexity is O(n2). Can we do better?