Just add them all up, and subtract the total you would expect if only 1001 numbers were used from that.
Just add them all up, and subtract the total you would expect if only 1001 numbers were used from that. Eg: Input: 1,2,3,2,4 => 12 Expected: 1,2,3,4 => 10 Input - Expected => 2.
3 @leppie: To hold the calculated sum, but honestly I don't know exactly what the OP meant by extra storage. In any case, I like your answer. – Brian Rasmussen Apr 9 '10 at 7:42 4 @Brian, the interviewer probably meant "don't use a hash table or an array"... I'm pretty sure O(1) storage, especially a single variable, would be satisfactory.
– Michael Aaron Safyan Apr 9 '10 at 7:44 7 +1, although your example is the wrong way around. – Aistina Apr 9 '10 at 7:46 6 the methord works perfectly fine. But the example should have been something like ( 1,3,2,4,2=>12 ) - (1+2+3+4 => 10) = 2 – SysAdmin Apr 9 '10 at 7:56 4 @Franci Penov: I am not sure interview questions are supposed to scale :) – Brian Rasmussen Apr 9 '10 at 8:04.
Update 2: Some people think that using XOR to find the duplicate number is a hack or trick. To which my official response is: "I am not looking for a duplicate number, I am looking for a duplicate pattern in an array of bit sets. And XOR is definitely suited better than ADD to manipulate bit sets".
:-) Update: Just for fun before I go to bed, here's "one-line" alternative solution that requires zero additional storage (not even a loop counter), touches each array element only once, is non-destructive and does not scale at all :-) printf("Answer : %d\n", array0 ^ array1 ^ array2 ^ // continue typing... array999 ^ array1000 ^ 1 ^ 2 ^ // continue typing... 999^ 1000 ); Note that the compiler will actually calculate the second half of that expression at compile time, so the "algorithm" will execute in exactly 1002 operations. And if the array element values are know at compile time as well, the compiler will optimize the whole statement to a constant. :-) Original solution: Which does not meet the strict requirements of the questions, even though it works to find the correct answer.It uses one additional integer to keep the loop counter, and it accesses each array element three times - twice to read it and write it at the current iteration and once to read it for the next iteration.
Well, you need at least one additional variable (or a CPU register) to store the index of the current element as you go through the array. Aside from that one though, here's a destructive algorithm that can safely scale for any N up to MAX_INT. For (int I = 1; I.
7 +1 for leetness – larsm Apr 9 '10 at 8:25 1 A non-destructive method would be to maintain an accumulator on the side... would also make it more readable I think. – Matthieu M. Apr 9 '10 at 8:32 2 @Matthiey M.
- but a non-destructive solution would require additional storage and thus violate the requirements of the problem. – Franci Penov Apr 9 '10 at 8:40 1 +1, Absolutely Brilliant! :-) – missingfaktor Apr 9 '10 at 8:56 2 Nice answer +1 but now my brain hurts!
– leppie Apr 9 '10 at 9:16.
Add all the numbers together. The final sum will be the 1+2+...+1000+duplicate number.
To paraphrase Francis Penov's solution. The (usual) problem is: given an array of integers of arbitrary length that contain only elements repeated an even times of times except for one value which is repeated an odd times of times, find out this value. The solution is: acc = 0 for I in array: acc = acc ^ I Your current problem is an adaptation.
The trick is that you are to find the element that is repeated twice so you need to adapt solution to compensate for this quirk. Acc = 0 for I in len(array): acc = acc ^ I ^ arrayi Which is what Francis' solution does in the end, although it destroys the whole array (by the way, it could only destroy the first or last element...) But since you need extra-storage for the index, I think you'll be forgiven if you also use an extra integer... The restriction is most probably because they want to prevent you from using an array. It would have been phrased more accurately if they had required O(1) space (1000 can be seen as N since it's arbitrary here).
I've posted Python one-liner based on your answer stackoverflow. Com/questions/2605766/… – J.F. Sebastian Apr 10 '10 at 5:18.
A non destructive version of solution by Franci Penov. This can be done by making use of the XOR operator. Lets say we have an array of size 5: 4, 3, 1, 2, 2 Which are at the index: 0, 1, 2, 3, 4 Now do an XOR of all the elements and all the indices.
We get 2, which is the duplicate element. This happens because, 0 plays no role in the XORing. The remaining n-1 indices pair with same n-1 elements in the array and the only unpaired element in the array will be the duplicate.Int i; int dupe = 0; for(i=0;i.
Add all numbers. The sum of integers 1..1000 is (1000*1001)/2. The difference from what you get is your number.
If you know that we have the exact numbers 1-1000, you can add up the results and subtract 500500 (sum(1, 1000)) from the total. This will give the repeated number because sum(array) = sum(1, 1000) + repeated number.
Well, there is a very simple way to do this... each of the numbers between 1 and 1000 occurs exactly once except for the number that is repeated.... so, the sum from 1....1000 is 500500. So, the algorithm is: sum = 0 for each element of the array: sum += that element of the array number_that_occurred_twice = sum - 500500.
No extra storage requirement (apart from loop variable). Int length = (sizeof array)/(sizeof array0); for(int i=1; I.
You are assuming the array is sorted. Bad assumption. – leppie Apr 9 '10 at 7:46 2 @leppie: how come?
I didn't assume anything. And it actually uses any extra space like other answers suggest. – N 1.
1 Apr 9 '10 at 7:59 1 How is he assuming that? – Dennis Zickefoose Apr 9 '10 at 8:05 Although I do have issue with the premise. It requires an extra two ints.
– Dennis Zickefoose Apr 9 '10 at 8:06 @Dennis: well loop variable has to be there, and length to make it generic. – N 1. 1 Apr 9 '10 at 8:20.
Int sumRemaining(int* remaining, int count) { if (!count) { return 0; } return remaining0 + sumRemaining(remaining + 1, count - 1); } printf("duplicate is %d", sumRemaining(array, 1001) - 500500); Edit: tail call version int sumRemaining(int* remaining, int count, int sumSoFar) { if (!count) { return sumSoFar; } return sumRemaining(remaining + 1, count - 1, sumSoFar + remaining0); } printf("duplicate is %d", sumRemaining(array, 1001, 0) - 500500).
This requires linear stack space, so that is definitely cheating. – Dennis Zickefoose Apr 9 '10 at 8:21 1 Throw in another argument and you can tail call optimize it. – cobbal Apr 9 '10 at 8:27.
One line solution in Python arr = 1,3,2,4,2 print reduce(lambda acc, (i, x): acc ^ I ^ x, enumerate(arr), 0) # -> 2 Explanation on why it works is in @Matthieu M. 's answer.
1, well done: even though it's not a code golf, using python's built-in loops is faster :) – Matthieu M. Apr 10 '10 at 12:43.
A triangle number T(n) is the sum of the n natural numbers from 1 to n. It can be represented as n(n+1)/2. Thus, knowing that among given 1001 natural numbers, one and only one number is duplicated, you can easily sum all given numbers and subtract T(1000).
The result will contain this duplicate. For a triangular number T(n), if n is any power of 10, there is also beautiful method finding this T(n), based on base-10 representation: n = 1000 s = sum(GivenList) r = str(n/2) duplicate = int( r + r ) - s.
N = 1000 s = sum(GivenList) r = str(n/2) duplicate = int( r + r ) - s.
Improvement of Fraci's answer based on the property of xor'ing consecutive values: int result = xor_sum(N); for (i = 0; I.
I support the addition of all the elements and then subtracting from it the sum of all the indices but this won't work if the number of elements is very large.i.e. It will cause a buffer overflow! So I hav devised this algorithm which may be will reduce the chances of a buffer overflow to a lage extent.
For i=0 to n-1 begin: diff = ai-i; dup = dup + diff; end //where dup is the duplicate element.. but by this method I won't be able to find out the index at which the duplicate element is present! For that I need to traverse the array another time which is not desirable.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.