MISSP - Editorial

Problem Link - Chef and Dolls

Problem Statement

Chef is a fan of pairs and likes things that come in pairs, including his doll collection. One day, he realizes there is an odd number of dolls, meaning one doll is missing from its pair. Help Chef find the type of the missing doll.

Approach

The code uses the XOR operation to find the missing doll in the collection. The XOR operation has a property where identical numbers cancel each other out (i.e., x ^ x = 0). Thus, when we XOR all the elements of the array, the result will be the number that appears only once, as all paired elements will cancel each other. This approach ensures that we find the missing doll efficiently.

Time Complexity

O(N), where N is the number of dolls.

Space Complexity

O(1), as no extra space is used except for a few variables.