888. Fair Candy Swap LeetCode Solution
In this guide, you will get 888. Fair Candy Swap LeetCode Solution with the best time and space complexity. The solution to Fair Candy Swap problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Fair Candy Swap solution in C++
- Fair Candy Swap solution in Java
- Fair Candy Swap solution in Python
- Additional Resources

Problem Statement of Fair Candy Swap
Alice and Bob have a different total number of candies. You are given two integer arrays aliceSizes and bobSizes where aliceSizes[i] is the number of candies of the ith box of candy that Alice has and bobSizes[j] is the number of candies of the jth box of candy that Bob has.
Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.
Return an integer array answer where answer[0] is the number of candies in the box that Alice must exchange, and answer[1] is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists.
Example 1:
Input: aliceSizes = [1,1], bobSizes = [2,2]
Output: [1,2]
Example 2:
Input: aliceSizes = [1,2], bobSizes = [2,3]
Output: [1,2]
Example 3:
Input: aliceSizes = [2], bobSizes = [1,3]
Output: [2,3]
Constraints:
1 <= aliceSizes.length, bobSizes.length <= 104
1 <= aliceSizes[i], bobSizes[j] <= 105
Alice and Bob have a different total number of candies.
There will be at least one valid answer for the given input.
Complexity Analysis
- Time Complexity:
- Space Complexity:
888. Fair Candy Swap LeetCode Solution in C++
class Solution {
public:
vector<int> fairCandySwap(vector<int>& aliceSizes, vector<int>& bobSizes) {
const int diff = (accumulate(aliceSizes.begin(), aliceSizes.end(), 0) -
accumulate(bobSizes.begin(), bobSizes.end(), 0)) /
;
const unordered_set<int> bobSizesSet{bobSizes.begin(), bobSizes.end()};
for (const int aliceSize : aliceSizes) {
const int target = aliceSize - diff;
if (bobSizesSet.contains(target))
return {aliceSize, target};
}
throw;
}
};
/* code provided by PROGIEZ */
888. Fair Candy Swap LeetCode Solution in Java
class Solution {
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
final int diff = (Arrays.stream(aliceSizes).sum() - Arrays.stream(bobSizes).sum()) / 2;
Set<Integer> bobSizesSet = Arrays.stream(bobSizes).boxed().collect(Collectors.toSet());
for (final int aliceSize : aliceSizes) {
final int target = aliceSize - diff;
if (bobSizesSet.contains(target))
return new int[] {aliceSize, target};
}
throw new IllegalArgumentException();
}
}
// code provided by PROGIEZ
888. Fair Candy Swap LeetCode Solution in Python
class Solution:
def fairCandySwap(
self,
aliceSizes: list[int],
bobSizes: list[int],
) -> list[int]:
diff = (sum(aliceSizes) - sum(bobSizes)) // 2
bobSizesSet = set(bobSizes)
for aliceSize in aliceSizes:
target = aliceSize - diff
if target in bobSizesSet:
return [aliceSize, target]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.