Problem Statement
You have been given a sealed box full of objects. Each object in the box is either red or blue; in addition, each object is either a cube or a sphere. You will be given some information about the contents of the box. Among the things you might be told are:
known[0]: The total number of objects in the box.
known[1]: The number of red objects in the box.
known[2]: The number of blue objects in the box.
known[3]: The number of cubes in the box.
known[4]: The number of spheres in the box.
known[5]: The number of red cubes in the box.
known[6]: The number of red spheres in the box.
known[7]: The number of blue cubes in the box.
known[8]: The number of blue spheres in the box.
You are given a
You will also be given an integer, target, indicating the information you would like to find out. Using the above example, if target = 3, it would mean that you would like to know how many cubes are in the box. You would return 20, since every object is either a cube or a sphere.
Given known and target, return an integer representing the number of objects of the type indicated by target, where target has the same meaning as the indices of known. There will always be one and exactly one possible value for the target which is consistent with the information in known. In other words, you will be able to uniquely determine the target value, given known. Furthermore, you know that the total number of objects in the box cannot exceed 100, and the number of any particular type of object (red cube, red sphere, blue cube, or blue sphere) cannot exceed 25.
Definition
- Class:
- ObjectCounter
- Method:
- getCount
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int getCount(int[] known, int target)
- (be sure your method is public)
Notes
- There will be at most 25 red cubes, 25 red spheres, 25 blue cubes, and 25 blue spheres in the box. Usually, you would try to figure out some set of equations relating to the different pieces of data. However, given that you have a computer handy, there are probably simpler ways to do it.
Constraints
- There will be exactly one possible object count for the target which is consistent with known.
- known will contain exactly 9 elements.
- Element 0 of known will be between -1 and 100, inclusive.
- Elements 1, 2, 3, and 4 of known will each be between -1 and 50, inclusive.
- Elements 5, 6, 7, and 8 of known will each be between -1 and 25, inclusive.
- target will be between 0 and 8, inclusive.
- The data in known will not contradict itself, given the restrictions imposed above.
Examples
{50,-1,-1,-1,30,-1,-1,-1,-1}
3
Returns: 20
This is the example from above. You know that there are 50 objects in the box, 30 of which are spheres. You want to know how many cubes there are. The only possible value that will work for the number of cubes (target = 3) is 20. Any other choice c will cause 30 + c to not equal 50.
{-1,25,-1,35,-1,15,-1,-1,0}
0
Returns: 45
You know that there are 25 red objects, 35 cubes, 15 red cubes, and 0 blue spheres in the box. You want to know the total number of objects in the box. Since there are 25 red objects, and 15 of those are cubes, the other 10 must be spheres. In addition, since there are 35 cubes, 15 of which are red, the other 20 must be blue. We now know that there are 15 red cubes, 10 red spheres, 20 blue cubes, and 0 blue spheres in the box. Therefore, there are 15+10+20+0=45 total objects in the box.
{-1,-1,-1,-1,-1,-1,-1,17,-1}
7
Returns: 17
The only thing you know is that there are 17 blue cubes in the box. However, the number of blue cubes is exactly what you want to know.
{0,-1,-1,-1,-1,-1,-1,-1,-1}
2
Returns: 0
If there are no objects in the box, then there are no blue objects in the box, either.
{1,-1,-1,-1,-1,-1,-1,-1,1}
1
Returns: 0
{1,-1,-1,-1,-1,-1,-1,-1,1}
2
Returns: 1
{1,-1,-1,-1,-1,-1,-1,-1,1}
5
Returns: 0
{100,-1,-1,-1,-1,-1,-1,-1,-1}
3
Returns: 50
{100,-1,-1,-1,-1,-1,-1,-1,-1}
7
Returns: 25
{98,-1,-1,-1,-1,-1,24,-1,24}
5
Returns: 25
{-1,15,22,-1,-1,-1,-1,22,-1}
8
Returns: 0
{68,26,-1,-1,-1,22,4,22,-1}
4
Returns: 24
{-1,36,-1,-1,37,-1,16,14,-1}
3
Returns: 34
{-1,-1,-1,27,-1,-1,0,10,0}
4
Returns: 0
{54,32,-1,-1,24,-1,12,-1,-1}
8
Returns: 12
{-1,37,14,-1,25,-1,15,-1,10}
2
Returns: 14
{40,17,-1,-1,-1,-1,2,-1,-1}
0
Returns: 40
{-1,-1,-1,44,-1,-1,1,-1,-1}
6
Returns: 1
{-1,-1,-1,-1,-1,7,-1,4,-1}
3
Returns: 11
{-1,-1,49,48,-1,-1,18,-1,25}
5
Returns: 24
{91,-1,44,48,-1,-1,22,-1,-1}
7
Returns: 23
{-1,10,38,-1,17,7,-1,-1,-1}
4
Returns: 17
{-1,-1,-1,-1,31,4,6,15,-1}
2
Returns: 40
{-1,8,36,-1,-1,8,-1,12,-1}
6
Returns: 0
{-1,-1,-1,13,-1,13,1,-1,-1}
1
Returns: 14
{-1,23,-1,36,-1,-1,-1,-1,7}
3
Returns: 36
{-1,13,-1,32,-1,13,0,-1,9}
4
Returns: 9
{-1,42,-1,31,37,-1,-1,8,-1}
1
Returns: 42
{24,-1,-1,-1,11,-1,4,12,-1}
5
Returns: 1
{23,-1,11,3,20,0,-1,-1,-1}
6
Returns: 12
{-1,16,24,-1,-1,7,-1,10,-1}
4
Returns: 23
{-1,28,-1,-1,35,-1,-1,-1,-1}
4
Returns: 35
{-1,27,-1,-1,23,-1,21,22,2}
1
Returns: 27
{68,-1,-1,-1,-1,25,-1,-1,-1}
0
Returns: 68
{-1,-1,-1,15,31,5,-1,-1,-1}
5
Returns: 5
{88,48,-1,40,-1,-1,-1,-1,23}
5
Returns: 23
{-1,29,24,-1,22,-1,13,-1,9}
5
Returns: 16
{46,22,-1,21,-1,-1,-1,-1,24}
2
Returns: 24
{16,-1,-1,-1,-1,-1,-1,-1,-1}
0
Returns: 16
{59,34,-1,12,-1,-1,-1,0,-1}
6
Returns: 22
{-1,-1,-1,-1,14,-1,-1,-1,-1}
4
Returns: 14
{40,-1,21,13,-1,4,-1,-1,-1}
1
Returns: 19
{-1,44,-1,29,42,21,-1,-1,19}
0
Returns: 71
{-1,19,-1,-1,-1,-1,-1,5,-1}
1
Returns: 19
{-1,38,21,36,-1,-1,15,-1,-1}
4
Returns: 23
{-1,-1,-1,17,-1,-1,19,16,16}
6
Returns: 19
{-1,46,-1,-1,-1,25,21,6,-1}
7
Returns: 6
{49,-1,27,37,-1,-1,0,-1,-1}
3
Returns: 37
{60,41,-1,-1,-1,21,-1,-1,13}
7
Returns: 6
{-1,35,7,-1,19,-1,-1,-1,7}
3
Returns: 23
{-1,24,23,-1,26,-1,-1,-1,9}
2
Returns: 23
{46,-1,-1,-1,24,11,-1,-1,-1}
0
Returns: 46
{-1,-1,27,-1,-1,-1,-1,-1,-1}
2
Returns: 27
{-1,22,-1,22,-1,-1,-1,-1,-1}
1
Returns: 22
{33,-1,-1,-1,-1,-1,-1,24,8}
1
Returns: 1
{73,-1,-1,-1,40,19,17,-1,-1}
6
Returns: 17
{-1,-1,-1,-1,-1,-1,5,-1,-1}
6
Returns: 5
{-1,19,-1,24,20,-1,5,-1,-1}
8
Returns: 15
{-1,-1,-1,19,-1,1,2,-1,-1}
7
Returns: 18
{-1,42,-1,-1,23,-1,-1,-1,-1}
1
Returns: 42
{ 0, -1, -1, -1, -1, -1, -1, -1, -1 }
2
Returns: 0
{ -1, -1, -1, -1, 0, -1, -1, -1, -1 }
8
Returns: 0
{ 23, -1, 11, 3, 20, 0, -1, -1, -1 }
6
Returns: 12
{ 100, -1, -1, -1, -1, -1, -1, -1, -1 }
3
Returns: 50
{ 100, -1, -1, -1, -1, -1, -1, -1, -1 }
7
Returns: 25
{ 100, -1, -1, -1, -1, -1, -1, -1, -1 }
8
Returns: 25
{ -1, 25, -1, 35, -1, 15, -1, -1, 0 }
0
Returns: 45
{ 100, -1, -1, -1, -1, -1, -1, -1, -1 }
2
Returns: 50
{ 7, -1, -1, -1, -1, -1, 3, 4, -1 }
1
Returns: 3
{ 20, 10, -1, -1, -1, -1, -1, -1, -1 }
2
Returns: 10
{ 50, 0, -1, -1, -1, -1, -1, -1, -1 }
3
Returns: 25
{ -1, 50, -1, -1, -1, -1, -1, -1, -1 }
5
Returns: 25
{ -1, -1, -1, -1, 20, 17, -1, 12, -1 }
0
Returns: 49
{ -1, -1, -1, -1, -1, 5, 10, -1, -1 }
1
Returns: 15
{ -1, 0, -1, -1, -1, -1, -1, -1, -1 }
5
Returns: 0
{ 3, -1, -1, -1, -1, 1, 1, 1, -1 }
8
Returns: 0
{ 10, 5, -1, -1, -1, -1, -1, -1, -1 }
2
Returns: 5
{ 25, -1, -1, -1, -1, -1, 25, -1, -1 }
3
Returns: 0
{ 0, -1, -1, -1, -1, -1, -1, -1, -1 }
4
Returns: 0
{ 0, -1, -1, -1, -1, -1, -1, -1, -1 }
1
Returns: 0
{ 20, -1, 0, -1, -1, 5, -1, -1, -1 }
4
Returns: 15
{ 100, -1, -1, -1, -1, -1, -1, -1, -1 }
1
Returns: 50
{ 5, 0, -1, -1, -1, -1, -1, -1, -1 }
5
Returns: 0
{ 1, -1, -1, -1, -1, -1, 1, -1, -1 }
5
Returns: 0
{ -1, -1, -1, -1, -1, -1, 10, -1, 10 }
4
Returns: 20
{ 1, -1, -1, -1, -1, -1, -1, -1, 1 }
5
Returns: 0
{ -1, -1, -1, -1, -1, -1, -1, 25, 25 }
2
Returns: 50