Jan 11, 2009

We've been given an array which contains triplets of various integers. Only one number is occurring just once. Find out that number.

We've been given an array which contains triplets of various integers. Only one number is occurring just once. Find out that number.
Suppose A[] = {3,5,2,2,5,3,7,3,2,5}
Find out the optimal algorithm to find out the only element which doesn't have a triplet/duplicate.
Convert each number to base-3 representation and do a digit-wise addition modulo 3 of the numbers. The result will be the number we are looking for.
Say the inputs were: [ 7, 8, 7, 7 ]
In base 3: [ 021, 022, 021, 021 ]
000 + 021 =021
021 + 022 =010
010 + 021 =001
001 + 021 =022
022
And 022 = 8 is the answer.

No comments:

Post a Comment