Problem Statement
The inhabitants of a certain magical country are known to be clever wizards. Their King decided to test how clever they really were.
He took 3 hats (2 white and 1 black) and put 2 smart wizards around a table. He put a white hat on one of the wizards and a black hat on the other. Each wizard could see what hat was on his neighbor, but could not see what hat was on his own head. The wizards knew that there were 2 white hats and 1 black hat. The King asked: "Do you know what hat is on your head?", and the wizard with the white hat answered: "Yes". It's clear how he knew: he had seen the black hat on his neighbor, and knowing that there was only 1 black hat, concluded that his own hat was white.
The King then decided to complicate their task a bit, and he put white hats on both the wizards. He asked: "Do you know what hat is on your head?", and both wizards answered together: "No". He asked again: "Do you know what hat is on your head?", and both wizards then answered: "Yes, we know. We have white hats on our heads!". How did they find out? When the question was first asked, the first wizard had seen the white hat on his neighbor, but didn't know the color of his own hat, so he answered "No". After hearing his neighbor's answer to the same question, he realized that his neighbor was in the same situation. Therefore, both wizards knew that they were wearing white hats when the question was asked a second time.
Let's consider a more general scenario. You are given an
Definition
- Class:
- Wizards
- Method:
- questions
- Parameters:
- int, int[], int[]
- Returns:
- int
- Method signature:
- int questions(int wizards, int[] hats, int[] hatsOnWizards)
- (be sure your method is public)
Constraints
- wizards will be between 1 and 75, inclusive.
- hats will contain between 2 and 5 elements, inclusive.
- Each element of hats will be between 1 and 15, inclusive.
- hatsOnWizards and hats will contain the same number of elements.
- Each element of hatsOnWizards will be between 0 and the corresponding element in hats, inclusive.
- All elements of hatsOnWizards will sum up to wizards.
Examples
2
{1,2}
{1,1}
Returns: 1
The first example from the problem statement.
2
{1,2}
{0,2}
Returns: 2
The second example from the problem statement.
2
{2,2}
{0,2}
Returns: -1
No wizard will ever know the color of his own hat.
18
{7,8,9}
{5,4,9}
Returns: 3
32
{9,9,9,9}
{8,8,8,8}
Returns: 4
7
{4, 3, 3}
{1, 3, 3}
Returns: 1
6
{4, 3, 3}
{0, 3, 3}
Returns: -1
2
{5, 1, 1}
{0, 1, 1}
Returns: -1
7
{4, 3, 3}
{3, 2, 2}
Returns: 3
7
{4, 3, 3}
{4, 2, 1}
Returns: 4
50
{15, 15, 15, 15, 15}
{10, 10, 10, 10, 10}
Returns: -1
50
{15, 15, 15, 15, 15}
{13, 12, 11, 10, 4}
Returns: -1
50
{15, 15, 15, 15, 15}
{14, 14, 12, 10, 0}
Returns: -1
75
{15, 15, 15, 15, 15}
{15, 15, 15, 15, 15}
Returns: 1
70
{15, 15, 15, 15, 15}
{14, 14, 14, 14, 14}
Returns: 5
50
{15, 15, 15, 15, 15}
{13, 13, 13, 10, 1}
Returns: -1
49
{15,15,15,15,15}
{13, 13, 13, 10, 0}
Returns: -1
50
{15,15,15,15,15}
{13, 13, 13, 11, 0}
Returns: -1
48
{15,15,15,15,15}
{13, 13, 13, 9, 0}
Returns: -1
39
{8, 8, 11, 8, 14}
{5, 8, 11, 4, 11}
Returns: 8
32
{6, 4, 5, 12, 15}
{6, 4, 5, 9, 8}
Returns: 4
26
{5, 9, 6, 9, 3}
{5, 8, 6, 4, 3}
Returns: 2
34
{11, 4, 10, 15, 11}
{6, 4, 6, 7, 11}
Returns: -1
25
{6, 5, 8, 6, 3}
{6, 4, 8, 4, 3}
Returns: 2
36
{7, 8, 7, 6, 15}
{4, 7, 6, 6, 13}
Returns: 6
35
{11, 12, 12, 11, 11}
{11, 4, 6, 9, 5}
Returns: -1
43
{4, 12, 13, 7, 13}
{4, 6, 13, 7, 13}
Returns: 1
33
{6, 5, 8, 8, 8}
{6, 5, 8, 6, 8}
Returns: 1
24
{4, 5, 5, 6, 10}
{4, 4, 5, 5, 6}
Returns: 3
36
{8, 8, 11, 8, 14}
{4, 8, 11, 3, 10}
Returns: 10
30
{6, 4, 5, 12, 15}
{6, 4, 5, 8, 7}
Returns: 5
24
{5, 9, 6, 9, 3}
{5, 7, 6, 3, 3}
Returns: 3
30
{11, 4, 10, 15, 11}
{5, 4, 5, 6, 10}
Returns: -1
23
{6, 5, 8, 6, 3}
{6, 3, 8, 3, 3}
Returns: 3
32
{7, 8, 7, 6, 15}
{3, 6, 5, 6, 12}
Returns: 9
30
{11, 12, 12, 11, 11}
{10, 3, 5, 8, 4}
Returns: -1
42
{4, 12, 13, 7, 13}
{4, 5, 13, 7, 13}
Returns: 1
32
{6, 5, 8, 8, 8}
{6, 5, 8, 5, 8}
Returns: 1
20
{4, 5, 5, 6, 10}
{3, 3, 5, 4, 5}
Returns: -1
1
{1,1}
{0,1}
Returns: -1
34
{12,14,3,6,11}
{5,14,3,6,6}
Returns: 13
27
{10, 6, 8, 4, 7 }
{7, 5, 5, 3, 7 }
Returns: 6
51
{10, 14, 11, 12, 13 }
{10, 10, 9, 11, 11 }
Returns: 6
63
{15, 15, 15, 15, 15 }
{12, 12, 12, 12, 15 }
Returns: 10
22
{12, 13, 14, 15, 7 }
{5, 5, 2, 5, 5 }
Returns: -1
59
{13, 14, 15, 15, 15 }
{8, 10, 12, 14, 15 }
Returns: 10
55
{12, 13, 14, 15, 15 }
{11, 11, 11, 11, 11 }
Returns: 11
56
{15, 14, 13, 12, 15 }
{12, 11, 12, 8, 13 }
Returns: 11
33
{15, 10, 10, 5, 4 }
{13, 8, 8, 3, 1 }
Returns: 10
18
{1, 7, 8, 9, 1 }
{0, 5, 4, 9, 0 }
Returns: 9