Amazon Interview Question
Software Engineer / Developers"you have unsorted array[n] elements. the numbers in the array[n] occurs event times except one number occurs odd time. So, write an algorithm to find that number. Also explain its complexity too. (time and space) both. (On ALGORITHM)"
You can do it in O(n). The idea is that if you multiply the numbers and divide when it is divisible by the current element, at the end, all even numbers will cancel out, leaving the odd number only:
int number = 1;
For each element i in the array:
if number is divisible by i [i.e. if( number mod (a[i] == 0) ]
then number = number / a[i];
else number = number * a[i];
return number;
"you have unsorted array[n] elements. the numbers in the array[n] occurs event times except one number occurs odd time. So, write an algorithm to find that number. Also explain its complexity too. (time and space) both. (On ALGORITHM)"
- Anonymous August 04, 2011Sort and XOR the elements the even elements will cancel out.