given n numbers, with number ranged from 1 to n-1, there is only one duplication, how to find it out?
since there is only one duplication, there will be two situations, one is there is only one number, another is numbers are from 1 to n-1 and one of it is duplicate.
for second situation, you can use sum of all numbers - (n-1)*(n-1+1)/2 to instantly figure out the number.
with n-1 duplications, just check to see if first 3 numbers are same.
No comments:
Post a Comment