Considering the first problem, You guys have come up with brilliant answers which is taking me more than 20 minutes to understand. Did not someone gave a link to an easier answer so that a novice like me could understand.
Add all the element in the array subtract 1000 * 1001 / 2. The answer is the duplicate. Hope I am right
That is wrong.
- Let say X represent the duplicate number.
- And Y represent the missing number.
- 1 + 2 + + .... + 1000 + X - Y = Z
- Z represent the sum of array.
- So X - Y = Z - (1000*1001/2)
- Then, the result of your answer is the difference between the missing number and duplicate number.