Finding out the duplicate element in an array

前端 未结 2 2199
生来不讨喜
生来不讨喜 2020-11-30 19:47

There is an array of size n and the elements contained in the array are between 1 and n-1 such that each element occurs once and just one element occurs more than once. We

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 20:11

    Adding the elements is perfectly fine you just have to take mod(%) of the intermediate aggregate when calculating the sum of the elements and the expected sum. For the mod operation you can use something like 2n. You also have to fix the value after substraction.

提交回复
热议问题