LeetCode Problem

How to Solve Fair Candy Swap

This problem asks for identifying one candy box from Alice and one from Bob to swap so that both have the same total candies. Using the array scanning plus hash lookup pattern ensures we compute the target difference efficiently and check for matching pairs without redundant comparisons. The approach is direct, leveraging sums and set membership to find the valid exchange quickly.

GhostInterview Help

Need help with Fair Candy Swap without spending extra time grinding it?

GhostInterview can read Fair Candy Swap from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #888Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Easy
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem asks for identifying one candy box from Alice and one from Bob to swap so that both have the same total candies. Using the array scanning plus hash lookup pattern ensures we compute the target difference efficiently and check for matching pairs without redundant comparisons. The approach is direct, leveraging sums and set membership to find the valid exchange quickly.

Problem Statement

Alice and Bob each have several boxes of candies represented as integer arrays aliceSizes and bobSizes, where each element denotes the number of candies in a box. The total candies for Alice and Bob differ, and they want to swap exactly one box each to balance their totals.

Return an array answer where answer[0] is the number of candies in the box Alice should give, and answer[1] is the number of candies in the box Bob should give. Multiple valid answers may exist, but at least one guaranteed solution exists for every input.

Examples

Example 1

Input: aliceSizes = [1,1], bobSizes = [2,2]

Output: [1,2]

Example details omitted.

Example 2

Input: aliceSizes = [1,2], bobSizes = [2,3]

Output: [1,2]

Example details omitted.

Example 3

Input: aliceSizes = [2], bobSizes = [1,3]

Output: [2,3]

Example details omitted.

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.

Solution Approach

Calculate total sums and target difference

Compute the sum of candies for Alice and Bob. The difference between their totals divided by two identifies the required delta for a fair swap. This delta guides which pairs to check and eliminates unnecessary comparisons.

Use hash set for constant-time lookup

Store all Bob's candy box sizes in a hash set. For each Alice box, check if adding the delta equals any value in Bob's set. This avoids O(n*m) scans and leverages the hash table to find a valid swap efficiently.

Return the first valid swap found

Iterate through Alice's boxes and use the hash set check. Once a matching Bob box is identified, return the pair immediately. The guarantee of a solution ensures that this first match is valid, maintaining linear time complexity.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n + m) due to summing arrays and iterating once over Alice's boxes with constant-time hash lookups. Space complexity is O(m) to store Bob's boxes in a hash set for fast membership checking.

What Interviewers Usually Probe

  • Checks if you recognize the pattern of array scanning plus hash lookup.
  • Tests whether you handle differences in total sums correctly.
  • Looks for use of constant-time data structures to avoid nested loops.

Common Pitfalls or Variants

Common pitfalls

  • Trying nested loops without using hash set, resulting in O(n*m) complexity.
  • Miscomputing the delta by not dividing the total difference by 2.
  • Returning invalid pairs or assuming multiple swaps are needed instead of exactly one.

Follow-up variants

  • Swapping multiple boxes to balance totals instead of just one.
  • Allowing unequal exchanges but minimizing difference after swap.
  • Input arrays with duplicate candy counts requiring handling in the hash set.

How GhostInterview Helps

  • GhostInterview identifies the array scanning plus hash lookup pattern immediately for Fair Candy Swap.
  • Provides step-by-step guidance on computing sums, delta, and hash set lookups efficiently.
  • Highlights the exact early return strategy to find a valid swap without scanning unnecessary pairs.

Topic Pages

FAQ

What is the core pattern used in Fair Candy Swap?

The problem uses array scanning combined with hash lookup to identify a pair of boxes whose swap equalizes totals.

Why do we divide the total difference by two?

Dividing by two calculates the exact delta that each swap must correct to balance Alice's and Bob's totals.

Can there be multiple valid answers?

Yes, multiple valid swaps can exist, and any one can be returned because at least one solution is guaranteed.

What if arrays contain duplicates?

Duplicates are handled naturally since the hash set stores distinct Bob box values, ensuring the correct match is found.

What is the time complexity using the hash lookup approach?

The approach runs in O(n + m) time, iterating over Alice's array and using constant-time lookups in Bob's hash set.

GhostInterview Solver

Need direct help with Fair Candy Swap instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Fair Candy Swap from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.