Exchange One Box Each to Equalize Two Candy Totals
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
### Problem
Each value in `aliceSizes` or `bobSizes` is the candy count in one box owned by that person. Exchange exactly one box from each person so their new total candy counts are equal. Return `[Alice_box_size,Bob_box_size]`. At least one valid exchange exists; when several work, return any one.
```cpp
class Solution {
public:
vector<int> fairCandySwap(vector<int>& aliceSizes, vector<int>& bobSizes);
};
```
### Examples
**Example 1:**
```text
Input: aliceSizes = [1,4], bobSizes = [2,5]
Output: [1,2]
Explanation: Totals5 and7 both become6. Exchanging4 and5 would also work.
```
**Example 2:**
```text
Input: aliceSizes = [4], bobSizes = [2,6]
Output: [4,6]
Explanation: The exchange changes totals4 and8 to6 and6.
```
### Constraints
- `1 <= aliceSizes.length,bobSizes.length <= 10^4`.
- `1 <= aliceSizes[i],bobSizes[j] <= 10^5`.
- The original candy totals differ.
- At least one valid exchange exists.
### Contract considerations
The original totals are different, and at least one answer is guaranteed. Either valid pair in the first example is acceptable.
```hint Read the boundary condition
Write the two totals after a hypothetical exchange, then solve for the required partner size.
```
### What a Strong Answer Covers
The original totals are different, and at least one answer is guaranteed. Either valid pair in the first example is acceptable.
### Follow-up Questions
Optional practice extension, separate from the original task: How could you report every distinct valid size pair instead of one?
Overview: Exchange One Box Each to Equalize Two Candy Totals. Each value in aliceSizes or bobSizes is the candy count in one box owned by that person.
Exchange One Box Each to Equalize Two Candy Totals
Microsoft
Jun 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Problem
Each value in aliceSizes or bobSizes is the candy count in one box owned by that person. Exchange exactly one box from each person so their new total candy counts are equal. Return [Alice_box_size,Bob_box_size]. At least one valid exchange exists; when several work, return any one.
class Solution {
public:
vector<int> fairCandySwap(vector<int>& aliceSizes, vector<int>& bobSizes);
};
Examples
Example 1:
Input: aliceSizes = [1,4], bobSizes = [2,5]
Output: [1,2]
Explanation: Totals5 and7 both become6. Exchanging4 and5 would also work.