|
| 1 | +# Find the Missing Number |
| 2 | +# You are given a list of n-1 integers and these integers are in |
| 3 | +# the range of 1 to n. There are no duplicates in list. One of |
| 4 | +# the integers is missing in the list. Write an efficient code |
| 5 | +# to find the missing integer. |
| 6 | + |
| 7 | + |
| 8 | +def missing(arr): |
| 9 | + size = len(arr) |
| 10 | + a1 = arr[0] |
| 11 | + a2 = 1 |
| 12 | + for i in range(1, size): |
| 13 | + a1 = a1 ^ arr[i] |
| 14 | + for i in range(2, size+2): |
| 15 | + a2 = a2 ^ i |
| 16 | + |
| 17 | + return a1 ^ a2 |
| 18 | + |
| 19 | + |
| 20 | +if __name__ == '__main__': |
| 21 | + |
| 22 | + a = [1, 2, 4, 5, 6] |
| 23 | + |
| 24 | + miss = missing(a) |
| 25 | + print(miss) |
0 commit comments