Algorithm


Problem Name: 888. 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.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
        int sumA(0), sumB(0), half;
        unordered_set < int>s;
        for(auto& x: A) sumA += x;
        for(auto& x: B) sumB += x, s.insert(x);
        half = (sumA + sumB) / 2;
        for(auto& x: A) if(s.count(half - (sumA - x))) return {x, half - (sumA - x)};
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
aliceSizes = [1,1], bobSizes = [2,2]

Output

x
+
cmd
[1,2]

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        int sumA = 0;
        int sumB = 0;
        
        Set < Integer> set = new HashSet<>();
        
        for (int num : A) {
            sumA += num;
            set.add(num);
        }
        
        for (int num : B) {
            sumB += num;
        }
        
        int diff = (sumA - sumB)/2;
        
        int[] ans = new int[2];
        for (int num : B) {
            if (set.contains(num + diff)) {
                ans[0] = num + diff;
                ans[1] = num;
                break;
            }
        }
            
        return ans;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
aliceSizes = [1,1], bobSizes = [2,2]

Output

x
+
cmd
[1,2]

#3 Code Example with Javascript Programming

Code - Javascript Programming


const fairCandySwap = function(aliceSizes, bobSizes) {
  let sum = 0
  for(let e of aliceSizes) sum += e
  for(let e of bobSizes) sum -= e
  // sum > 0, alice > bob
  // sum < 0, alice < bob
  sum /= 2
  const set = new Set()
  for(let e of aliceSizes) set.add(e)
  for(let e of bobSizes) {
    if(set.has(e + sum)) return [e + sum, e]
  }
  return [0]
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
aliceSizes = [1,2], bobSizes = [2,3]

Output

x
+
cmd
[1,2]

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def fairCandySwap(self, A, B):
        """
        :type A: List[int]
        :type B: List[int]
        :rtype: List[int]
        """
        a, b = set(A), set(B)
        diff =(sum(A) - sum(B)) // 2
        for c in B:
            if c + diff in a:
                return [c + diff, c]
Copy The Code & Try With Live Editor

Input

x
+
cmd
aliceSizes = [1,2], bobSizes = [2,3]

Output

x
+
cmd
[1,2]

#5 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;
using System.Linq;

namespace LeetCode
{
    public class _0888_FairCandySwap
    {
        public int[] FairCandySwap(int[] A, int[] B)
        {
            var different = (B.Sum() - A.Sum()) / 2;

            var set = new HashSet < int>(B);
            foreach (var num in A)
            {
                if (set.Contains(num + different))
                    return new int[] { num, num + different };
            }
            return null;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
aliceSizes = [2], bobSizes = [1,3]

Output

x
+
cmd
[2,3]
Advertisements

Demonstration


Previous
#887 Leetcode Super Egg Drop Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#889 Leetcode Construct Binary Tree from Preorder and Postorder Traversal Solution in C, C++, Java, JavaScript, Python, C# Leetcode