712393. Maximum Sum Combination

Medium

Maximum Sum Combination

Summary

The problem is to find the maximum sum combination of elements from two arrays. The goal is to select one element from each array such that their sum is maximized.

Detailed Explanation

To solve this problem, we can use a greedy approach. First, sort both arrays in descending order. Then, iterate through both arrays simultaneously and select the pair with the maximum sum at each step. This ensures that we always pick the largest possible combination.

Here's a step-by-step breakdown of the solution:

  1. Sort both arrays in descending order.
  2. Initialize two pointers, one for each array, to point to their respective first elements.
  3. Iterate through both arrays simultaneously:
    • At each step, calculate the sum of the current pair of elements.
    • If the sum is greater than the maximum sum found so far, update the maximum sum and store the current pair.
  4. Return the maximum sum and the corresponding pair of elements.

Time complexity: O(n log n) due to sorting Space complexity: O(1)

+--------+
|  A    |
|  [5,3]  |
+--------+
|  B    |
|  [2,8]  |
+--------+

In the above diagram, A and B are the input arrays. The arrows represent the sorting of the arrays in descending order.

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static int findMaximumSum(int[] A, int[] B) {
        Arrays.sort(A);
        Arrays.sort(B);

        int sum = 0;
        int i = A.length - 1;
        int j = B.length - 1;

        while (i >= 0 && j >= 0) {
            sum += A[i] + B[j];
            i--;
            j--;
        }

        return sum;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.