aboutsummaryrefslogtreecommitdiff
path: root/set_mismatch.cpp
blob: 8c216bcea28401b9914f1ad6a85f288296d8d81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "leetcode.h"

class Solution {
public:
  vector<int> findErrorNums(vector<int> &nums) {
    int p = 0, ac1 = 0, ac2 = 0;
    for (uint i = 0; i < nums.size(); ++i)
      p ^= (i + 1) ^ nums[i];
    p &= -p;
    for (uint i = 0; i < nums.size(); ++i) {
      ((nums[i] & p) == 0) ? ac1 ^= nums[i] : ac2 ^= nums[i];
      (((i + 1) & p) == 0) ? ac1 ^= i + 1 : ac2 ^= i + 1;
    }
    for (auto n : nums)
      if (n == ac1)
        return {ac1, ac2};
    return {ac2, ac1};
  }
};