Fair Candy Swap - Complete Solution Guide
Fair Candy Swap is LeetCode problem 888, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th box of candy that Alice has and bobSizes[j] is the number of candies of the j th 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 i
Detailed Explanation
The Fair Candy Swap problem requires finding one candy box each for Alice and Bob to exchange, such that after the exchange, both have the same total amount of candy. You are given two arrays, `aliceSizes` and `bobSizes`, representing the sizes of candy boxes Alice and Bob possess, respectively. The goal is to return an array `[x, y]`, where `x` is the size of the candy box Alice gives to Bob, and `y` is the size of the candy box Bob gives to Alice.
Solution Approach
The solution involves calculating the sum of candies for Alice and Bob, then iterating through Alice's candy sizes and using the formula `y = (sum_bob - sum_alice + 2 * x) / 2` to find the corresponding candy size Bob must exchange. A hash set stores Bob's candy sizes, which allows for O(1) average time complexity lookups to determine if `y` exists within Bob's collection. If the value exists in Bob's set, the pair `[x, y]` is the answer, and the algorithm terminates.
Step-by-Step Algorithm
- Step 1: Calculate the sum of candy sizes for Alice (`sum_alice`) and Bob (`sum_bob`).
- Step 2: Create a hash set (`set_bob`) containing all the candy sizes in Bob's collection. This allows for efficient O(1) lookup.
- Step 3: Iterate through each candy size `x` in Alice's collection (`aliceSizes`).
- Step 4: For each `x`, calculate the required candy size `y` from Bob using the formula `y = (sum_bob - sum_alice + 2 * x) // 2` (integer division is crucial here!).
- Step 5: Check if `y` exists in the `set_bob`. If it does, then `x` and `y` are the solution.
- Step 6: If `y` is found in `set_bob`, return the array `[x, y]`. The problem states a solution is guaranteed so no further iteration is needed.
- Step 7: The C solution is different. It uses a nested for loop, resulting in a higher Time Complexity of O(n*m).
Key Insights
- Insight 1: The core idea is to determine the difference in the total candy amounts and find the values needed to equalize them. We need to find an `x` in Alice's set and `y` in Bob's set such that `sum_alice - x + y = sum_bob - y + x`.
- Insight 2: The equation can be simplified to `y = (sum_bob - sum_alice + 2 * x) / 2`. This formula is crucial for finding the required candy value from Bob given a specific candy value from Alice.
- Insight 3: Using a set (or hash table) to store Bob's candy sizes enables efficient lookup, which improves the algorithm's performance from O(n*m) to O(n+m).
- Insight 4: The problem statement guarantees that at least one solution exists. Therefore, once a valid pair (x, y) is found, we can return it immediately.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(m)
Topics
This problem involves: Array, Hash Table, Binary Search, Sorting.
Companies
Asked at: Odoo, Swiggy.