You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.
You are given a list of n-1 integers and these integers are in the range
of 1 to n. There are no duplicates in list. One of the integers is
missing in the list. Write an efficient code to find the missing
integer.
#Note : the numbers are not in sorted order
Algorithm :
#Note : the numbers are not in sorted order
Algorithm :
1. Get the sum of numbers
total = n*(n+1)/2
2 Subtract all the numbers from sum and
you will get the missing number.
Note : Time complexity is O(n);
Algorithm : Using EX-OR
Here we can use same concept of finding which element is odd number times in the given list.
1) XOR all the array elements, let the result of XOR be X1. 2) XOR all numbers from 1 to n, let XOR be X2. 3) XOR of X1 and X2 gives the missing number.
Comments
Post a Comment