# Leetcode – Fair Candy Swap Solution in Python

Spread the love

Number problems, especially those that involve arrays, often present a unique mix of mathematical insight and algorithmic thought. The “Fair Candy Swap” problem on Leetcode is one such challenge. It involves two arrays and the objective to make their sums equal by swapping just one element from each array. In this comprehensive article, we’ll dive deep into understanding the problem, breaking down its core components, and building a Python-based solution.

## Problem Statement

Alice and Bob have candy bars of different sizes. They want to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. Return an integer array answer where:

• answer[0] is the size of the candy bar that Alice must exchange.
• answer[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them.

### Constraints:

• 1 <= A.length <= 1000
• 1 <= B.length <= 1000
• 1 <= A[i], B[i] <= 100000
• It is guaranteed that Alice and Bob have different total amounts of candy.
• It is guaranteed there exists an answer.

### Examples:

1. Input: A = [1,1], B = [2,2] Output: [1,2]
2. Input: A = [1,2], B = [2,3] Output: [1,2] or [2,3]

## Analysis

Given the problem statement, we can deduce that the difference between the total candies of Alice and Bob will always be even.

To equalize the total candies between Alice and Bob, they need to cover half of their total difference by swapping.

For a swap to be valid:

• sum(A) - x + y = sum(B) - y + x Where x belongs to Alice’s candies and y belongs to Bob’s candies.

From the equation above, we can deduce:

• y = x + (sum(B) - sum(A)) / 2

This equation gives us a clear direction for the solution. Once we have x, we can easily determine the corresponding y using the above formula.

## Solution Strategy

1. Calculate Differences: First, find the total candies with Alice and Bob. Then, calculate the difference.
2. Iterate and Find the Pair: For each candy size x in Alice’s candies, compute the potential size of y that Bob should swap. If y is present in Bob’s candies, then this pair of x and y is a valid answer.

### Python Code:

def fairCandySwap(A: List[int], B: List[int]) -> List[int]:
# Calculate the total candies with Alice and Bob
sumA, sumB = sum(A), sum(B)

# Set for faster look-up
setB = set(B)

# Calculate the difference
diff = (sumB - sumA) // 2

# Find the valid pair
for x in A:
if x + diff in setB:
return [x, x + diff]

## Complexity Analysis

1. Time Complexity: Creating the set for Bob’s candies is O(M), where M is the size of array B. Then, we iterate through Alice’s candies, which takes O(N), where N is the size of array A. In total, the time complexity is O(N + M).
2. Space Complexity: The additional space is used for setB, which is O(M).

## Conclusion

The Fair Candy Swap problem beautifully demonstrates how mathematical deduction can simplify seemingly complex problems. By transforming the problem statement into a mathematical equation, the solution path becomes clear and algorithmically feasible.

This problem emphasizes the importance of understanding the underlying patterns and relationships within data, allowing us to harness mathematical insights for algorithmic solutions. It also serves as a great example of how array manipulation, combined with mathematical prowess, can address intriguing real-world-like problems efficiently.