Amazon Interview Question
Software Engineer / Developersyes, u r correct.
we can do it in O(n) without extra space by doing XOR on all numbers. Final result of XOR is the odd number( the number with out pair).
XORing is an excellent idea and operations at bit level are faster, so they solution is as elegant as it gets.
thi solution wont work if the odd occurring number is 0. isn't it?
For ex: 2, 6, 4 ,4, 0, 2, -1,6,-1
Let MIN be smallest and MAX be largest element in array A. Counting Sort needs to create an extra array[MAX-MIN]. This algorithm would run in O(N) but takes too much memory.
A friend of mine gave me a very clever solution today. We need to compare the numbers in the binary level.
Assume that this is an array of integers (32 bits). This algorithm would run in
O(N * 32) worse case.
1. For each element in the Array, if the least significant bit is 1, increment a counter. Also keep track of the Lastest_Index that has the LSB equals 1.
2. If the counter is 1, then you know that the odd occuring number is at location Latest_Index.
3. Perform Step 1 but check the Next Bit.
This is a very clever solution.
tks. I'd like to add that another application of this XOR solution is in GUI applications. When you are drawing a line, e.g., when you press the mouse and move around, use XOR bgcolor with current draw color once to draw a temporary line, and twice to cancel the temporary drawing. this would eliminate the need to refresh the entire dirty rectangle which may include a lot of other drawings.
there is another clever app of XOR. can anyone think of it?
How about this..
Create a set of nine buckets 1 to 9. Let each bucket act has a head to a linked list.
As each number is scanned, store it in the bucket according to its first digit. If you encounter a number that already exists in the bucket then remove it from the bucket.
At the end, when you scan the buckets, you will have a number that has not been repeated.
Does this sound like a good solution? I guess it would be O(n) too.
For modified quicksort, if the pivot is a duplicate, then the split could still be balanced and would be ambiguous.
Counterexample:
Unique element on the left
odd + odd = even
Other duplicates on the right
even + even = even
Hence, this conflicts with the case in which the pivot is the unique element.
XOR all the numbers ..
..
The duplicates cancel each other out and give 0 ..
Single element XOR 0 gives the element
Question does not say the array is sorted... so its not necessary the adjacent number will cancel out to 0... you need to sort first (o[n log n]) and then you can do the XOR...
That's wrong. There is no need to sort, as if they are duplicated, they will eventually cancel the other out while we are traversing the entire array.
Sorting is *not* needed.
Here is a one-liner in Python:
>>> a = 2*range(10)
>>> a.append(77777)
>>> random.shuffle(a)
>>> a
[1, 9, 3, 2, 5, 6, 7, 7, 9, 77777, 0, 8, 1, 3, 8, 4, 0, 2, 4, 5, 6]
>>> reduce(lambda x,y: x^y,a)
77777
public class Test{
public static void main(String args[]){
int a[] = {600,100,200,300,400,300,400,500,600,100,200};
int value = 0;
for (int i=0;i<a.length;i++){
value = value ^ a[i] ;
}
System.out.println("Value is "+value);
}
}
//Output is 500
A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:
0101
XOR 0011
= 0110
Hope it helps
Well the XOR solution works for sure , I am just a bit curious , suppose if there are numbers that are present odd number of times (apart from the one that is not duplicated) , then the XOR solution might not work , and is there a way of solving that problem ,( I suppose that duplicate does not mean the number being replicated even number of times.)
This was a hard problem until Khoa mentioned thinking about the numbers on a binary level. Since each number XOR'ed by itself will clear it's set bits, you can go through the array XORing each number and end up with the number without a pair.
- TheChronoTrigger February 03, 2006Consider the example:
2,3,2,4,3
0010 // 2
0011 // 3
---- // XOR'ed
0001 // result
0010 // 2
---- // XOR'ed
0011 // result: 3 (the odd number so far)
0100 // 4
---- // XOR'ed
0111 // result
0011 // 3
---- // XOR'ed
0100 // result 4 (The odd number in the sequence)