Puzzle: Your friend thinks of an N bit string that will refer to it as a code. The task is to guess the code by asking your friend a series of queries. In each query, you can provide your friend with an N bit string of your choice, and your friend will tell you the number of bits in your string that coincides with the corresponding bits in the code. For example, if the code is 01011 and your query string is 11001, then the answer will be three because the two strings have the same bits on the second, third, and fifth positions. Devise an algorithm that can identify any N bit code in no more than N query.
Let the N bit binary code string be b1b2….bn, it can be identified using the following N queries:
Query 1 – 000…00
Query 2 – 100…00
Query 3 – 110…00
Query N – 11….10
- The answer to the first query a1, gives the total number of 0s in the code string being identified. Let a2 be the result to the second question.
Since the query bit strings in the first two questions differs only by one bit, hence, the number of correct bits in them will also differ by one, that will help to identify the first bit b1 of the code for us as follows:
- If a1 < a2, b1 = 1
- If a1 > a2, b1 = 0
For example, for code 01011, a1 = 2 and a2 = 1, therefore b1 = 0.
- Repeating the same argument for the next N – 2 bits of the string makes it possible to identify the bits, b2, …, bN – 1 of the code.
- The last bit bN can be found by using the total number of 0s in the code determined by the first query: if this number is equal to the number of zeroes in the code’s first N – 1 bits, which are now known, then bn = 1, else bn = 0.
Example: Let N = 3 and the code which your friend thought be “110”.
The code can be guessed in 3 queries.
- Query 1 = “000”: The answer to the query is 1 as only the third-bit matches in the string “110” and “000”. The resultant code bit string has a total of 1 zero.
- Query 2 = “100”: The answer to the query is 2 as only two-bit matches in the string “100” and “110”. Since in this query changing only the first bit from the last query and the resultant code bit increased so the first bit of code string will be ‘1’.
- Query 3 = “110”: The answer to the query is as since three-bit matches. So now since we changed only the first bit from the last query and the answer increased so the second bit of code string will be ‘1’.
From the above, we know that two of the bits are ‘1’. And we have 1 zero in the code string so the third bit will be a ‘0’. Therefore, code string = “110”
- Puzzle 41 | (Guess Color of Hat)
- Puzzle | Guess the shape
- Puzzle | Guess the total number of coins
- Puzzles | (Age, Time and Distance, Guess the Word, Mystery)
- Puzzle 15 | (Camel and Banana Puzzle)
- Puzzle 24 | (10 Coins Puzzle)
- Puzzle 27 | (Hourglasses Puzzle)
- Puzzle 28 | (Newspaper Puzzle)
- Puzzle 29 | (Car Wheel Puzzle)
- Puzzle 31 | (Minimum cut Puzzle)
- Puzzle 33 | ( Rs 500 Note Puzzle )
- Puzzle 34 | (Prisoner and Policeman Puzzle)
- Puzzle 36 | (Matchstick Puzzle)
- Puzzle 38 | (Tic Tac Toe Puzzle)
- Puzzle 39 | (100 coins puzzle)
- Puzzle 51| Cheryl’s Birthday Puzzle and Solution
- Puzzle 81 | 100 people in a circle with gun puzzle
- Puzzle 85 | Chain Link Puzzle
- Puzzle | Elevator Puzzle
- Puzzle | 3 Priests and 3 devils Puzzle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.