Algorithms | Searching | Question 6

You are given a list of 5 integers and these integers are in the range from 1 to 6. There are no duplicates in list. One of the integers is missing in the list. Which of the following expression would give the missing number.
^ is bitwise XOR operator.
~ is bitwise NOT operator.

Let elements of list can be accessed as list[0], list[1], list[2], list[3], list[4]
(A) list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4]
(B) list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6
(C) list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4] ^ 1 ^ 2 ^ 3 ^ 4 ^ 5
(D) ~(list[0] ^ list[1] ^ list[2] ^ list[3] ^ list[4])


Answer: (B)

Explanation: XOR of all list elements and numbers from 1 to 6 gives the missing number. See this for details

Quiz of this Question

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.