If we have a array 10 intergers, that contains all integers from 1 to 10 except for one interger which is zero, then what is the best method to find the missing number.
Eg arr[10] = { 1, 2, 3, 4, 5, 0, 7, 8, 9, 10}
Answer:
The sum of n contiguous integers is (n* (n + 1) ) / 2.
The sum of 10 contiguous integers is (10 * 11) / 2 = 55.
The actual sum of integers in arr = 1+2+3+4+5+0+7+8+9+10 = 49.
The missing number is 55 - 49 = 6.
This method is easier when compared to setting the bit position for each number and then checking which bit is not set.
Complexity: SIMPLE.
Subscribe to:
Post Comments (Atom)
.jpg)
No comments:
Post a Comment