Maximize Sum by Choosing from Two Arrays

Quick Overview

Given two equal-length integer arrays and a count k, pick one value at every index so that exactly k picks come from the first array and the total is as large as possible. It tests greedy reasoning under an exact-count constraint, an exchange argument for correctness, and overflow-safe summation.

Maximize Sum by Choosing from Two Arrays

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given two integer arrays `r1` and `r2`, both of length `n`, and an integer `k`. At every index `i` (0-indexed) you must pick exactly one value: either `r1[i]` or `r2[i]`. Across all `n` picks, exactly `k` must come from `r1` and the remaining `n - k` must come from `r2`. Return the maximum possible sum of the picked values. ### Function Signature ```python def maximize_sum(r1: list[int], r2: list[int], k: int) -> int: ``` ### Rules - `r1` and `r2` always have the same length `n`. - The count constraint is exact. If `k` picks from `r1` lower the total, you still have to make them. - Return the sum as a plain integer. ### Constraints - `1 <= n <= 10^5` - `0 <= k <= n` - `-10^9 <= r1[i], r2[i] <= 10^9` ### Examples ```text Input: r1 = [5, 1, 3], r2 = [2, 8, 4], k = 2 Output: 16 ``` Take `r1[0] = 5`, `r2[1] = 8` and `r1[2] = 3`. That uses `r1` twice and `r2` once, for 5 + 8 + 3 = 16. The other two ways to use `r1` twice give 5 + 1 + 4 = 10 and 2 + 1 + 3 = 6. ```text Input: r1 = [10, 20, 30], r2 = [1, 2, 3], k = 3 Output: 60 ``` Since `k = n`, every pick comes from `r1`: 10 + 20 + 30 = 60. ```text Input: r1 = [1, 2], r2 = [10, 20], k = 1 Output: 21 ``` One pick has to come from `r1` even though both `r1` values are smaller. Taking `r1[0]` gives 1 + 20 = 21 and taking `r1[1]` gives 10 + 2 = 12, so the answer is 21.

Overview: Given two equal-length integer arrays and a count k, pick one value at every index so that exactly k picks come from the first array and the total is as large as possible. It tests greedy reasoning under an exact-count constraint, an exchange argument for correctness, and overflow-safe summation.

Microsoft logo
Microsoft
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given two integer arrays r1 and r2, both of length n, and an integer k. At every index i (0-indexed) you must pick exactly one value: either r1[i] or r2[i]. Across all n picks, exactly k must come from r1 and the remaining n - k must come from r2.

Return the maximum possible sum of the picked values.

Function Signature

def maximize_sum(r1: list[int], r2: list[int], k: int) -> int:

Rules

  • r1 and r2 always have the same length n .
  • The count constraint is exact. If k picks from r1 lower the total, you still have to make them.
  • Return the sum as a plain integer.

Constraints

  • 1 <= n <= 10^5
  • 0 <= k <= n
  • -10^9 <= r1[i], r2[i] <= 10^9

Examples

Input: r1 = [5, 1, 3], r2 = [2, 8, 4], k = 2
Output: 16

Take r1[0] = 5, r2[1] = 8 and r1[2] = 3. That uses r1 twice and r2 once, for 5 + 8 + 3 = 16. The other two ways to use r1 twice give 5 + 1 + 4 = 10 and 2 + 1 + 3 = 6.

Input: r1 = [10, 20, 30], r2 = [1, 2, 3], k = 3
Output: 60

Since k = n, every pick comes from r1: 10 + 20 + 30 = 60.

Input: r1 = [1, 2], r2 = [10, 20], k = 1
Output: 21

One pick has to come from r1 even though both r1 values are smaller. Taking r1[0] gives 1 + 20 = 21 and taking r1[1] gives 10 + 2 = 12, so the answer is 21.