Leetcode 924 Solution

This article provides solution to leetcode question 924 (fair-candy-swap)

https://leetcode.com/problems/fair-candy-swap

Solution

class Solution {
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
        int sum1 = 0;
        for (auto num: A)
            sum1 += num;

        int sum2 = 0;
        for (auto num: B)
            sum2 += num;

        bool bm[1000001] = {false};
        for (auto num: B)
            bm[num] = true;

        int dist = (sum2 - sum1) / 2;
        for (auto num: A)
        {
            int target = num + dist;
            if (0 <= target && target <= 100000)
                if (bm[target])
                    return {num, target};
        }

        return {};
    }
};