# 888. Fair Candy Swap

• Time:
• Space:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public: vector fairCandySwap(vector& A, vector& B) { int diff = (accumulate(begin(A), end(A), 0) - accumulate(begin(B), end(B), 0)) / 2; unordered_set set{begin(B), end(B)}; for (int a : A) if (set.count(a - diff)) return {a, a - diff}; throw; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public int[] fairCandySwap(int[] A, int[] B) { int diff = (IntStream.of(A).sum() - IntStream.of(B).sum()) / 2; Set set = new HashSet<>(); for (int b : B) set.add(b); for (int a : A) if (set.contains(a - diff)) return new int[] {a, a - diff}; throw new IllegalArgumentException(); } } 
 1 2 3 4 5 6 7 8 class Solution: def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]: diff = (sum(A) - sum(B)) // 2 B = set(B) for a in A: if a - diff in B: return [a, a - diff]