Continue learning
Practice related DSA questions in the same topic.
The input has distinct values from zero through n inclusive, with one number missing. Identify that number.
The input has distinct values from zero through n inclusive, with one number missing. Identify that number.
def solve(values, target): result = len(values) i = 0 while (i < len(values)): result = ((result ^ i) ^ values[i]) i = (i + 1) return resultCancel the expected index and supplied value together.
Cancel the expected index and supplied value together.
Pair expected roll-call cards with attended cards; the unmatched card remains.
XOR every expected number and every supplied number. All values present in both groups cancel.
O(n) time · O(1) auxiliary space
Worked example · {"values":[3,0,1],"target":0} ⇒ 2
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[0],"target":0} ⇒ 1
The inclusive upper endpoint n also belongs to the expected set.
Would an arithmetic sum also work for this bounded input?