AlgoMaster-io/leetcode-solutions

misleading solution to the question single-number-iii

Closed this issue · 1 comments

The solution to the problem single-number-iii which involves bit manipulation is potentially wrong while calculating the rightmost set bit
int rightmost_set_bit = xor_all & -xor_all; this errors out for a vector like [1,1,0,INT_MIN] complaining

Line 10: Char 37: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:19:37

Consider updating this section to something like

int rightmostBit = 1;
while ((xorAll & rightmostBit) == 0) {
    rightmostBit <<= 1;
}

Hey, sorry for the late reply. Thanks for pointing it out.

I have updated the code that fixes this issue.