Find The One

Is it possible to design an algorithm that uses constant space for the following function?

Input: 2 n + 1 2n + 1 non-negative integers; every integer appears twice, except one that appears only once.

Output: The integer that appears exactly once.


Details and Assumptions: Assume that storing each integer takes constant amount of space regardless of the number of bits in the integer.

Yes No

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

The solution to this problem relies on the properties of \oplus (XOR).

We know that \oplus gives a zero whenever both operands are equal. a a = 0 a \oplus a = 0

Also, 0 0 is the identity of the \oplus . So, a 0 = 0 a = a a \oplus 0 = 0 \oplus a = a

Now, suppose the integers given to us are x 1 , x 2 x n x_1, x_2 \cdots x_n . Now, because \oplus is commutative, we can assume, without loss of generality, that x n x_n is the unique integer and x 1 = x 2 , x 3 = x 4 , x 5 = x 6 , x_1 = x_2, x_3 = x_4, x_5 = x_6, \cdots .

Then, we are assured that x 1 x 2 x 3 x n = x n x_1 \oplus x_2 \oplus x_3 \cdots x_n = x_n

So, we can just use an accumulator to \oplus all the values that are fed in:

1
2
3
4
5
acc = 0
for i = 1 to length(list):
   input element
   acc = acc ^ element
return acc

Look, your algorithm stores the list, instant O(n) space.

the rest of the proof... You need to relearn how to prove algorithmic complexity man... "WLOG" can be used for correctness, not complexity.

Easy example: list = (2, 1, 2) Absolutely everything in your proof breaks down.

Derek Modzelewski - 4 years, 8 months ago

Log in to reply

That was a little rude, but I hope you understand that this supposed to be something like an online algorithm. I edited the solution for clarity.

Agnishom Chattopadhyay - 4 years, 8 months ago

Log in to reply

Then you absolutely can't assume the order you get the elements... 'a (xor) b' isn't even defined for a,b /= 0

I may have been rude, but your solution is still fundamentally incorrect.

Derek Modzelewski - 4 years, 8 months ago

Log in to reply

@Derek Modzelewski

Then you absolutely can't assume the order you get the elements... 'a (xor) b' isn't even defined for a,b /= 0

It doesn't matter what the order of the elements are, right?

Agnishom Chattopadhyay - 4 years, 8 months ago

Log in to reply

@Agnishom Chattopadhyay If you don't define 'a (xor) b', you can't stipulate commutativity. However, instead of the set operation 'xor' you should've just used bitwise xor, included a short explanation that bits can be dealt with independently (and so all inputs to xor are 0 or 1), then you can claim commutativity and the rest of your argument follows. Since you use bitwise xor in your code, your code is correct.

Sorry I got confused by your invalid use of xor, disregard all my previous comments.

Derek Modzelewski - 4 years, 8 months ago
Ishan Sheth
Nov 10, 2016

just keep doing xor operation of all the 2n+1 numbers

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...