Pregunta de entrevista de Amazon

Given an array of 100 integers where every integer from 1-101 occurs once, except for one. Find the missing integer.

Respuestas de entrevistas

Anónimo

15 jun 2012

Let a = XOR of all elements in array and b = XOR of all numbers from 1 to 100. The final result is a XOR b

4

Anónimo

11 jul 2016

XOR all numbers in the array + till closest power of 2 - 1(in this case 127). Output will be the missing number. i.e. 1 ^ 2 ^ 3 ....... ^ 101 ^ 102...... ^ 127 = missing number

Anónimo

3 dic 2010

sum of number from 1 to n is n(n+1)/2 So Sum from 1 to 101 = (101*102)/2 = 5151 So missing number is 5151 - (sum of all elements in the given array)

2