Problem Statement
Some people are sitting in a row. Each person came here from some country. You have a theory that people from the same country are all sitting together. You decided to test this theory. You asked each person the same question: "How many people from your country (including you) are here?"
You are given a
If all the answers are consistent with your theory, return the number of different countries that are present. (Given that all answers are consistent with the theory, the number of countries can always be uniquely determined.) Otherwise, return -1.
Definition
- Class:
- CountryGroup
- Method:
- solve
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int solve(int[] a)
- (be sure your method is public)
Constraints
- The number of elements in a will be between 1 and 100, inclusive.
- All elements of a will be between 1 and 100, inclusive.
Examples
{2,2,3,3,3}
Returns: 2
These answers are consistent with your theory. The first two people are from one country, the other three are from a different country. Thus, there are two countries and the correct return value is 2.
{1,1,1,1,1}
Returns: 5
Here, each person comes from a different country.
{3,3}
Returns: -1
These answers do not correspond to your theory. In particular, they are not even correct: there are only two people but each of them claims that there are three people from their country.
{4,4,4,4,1,1,2,2,3,3,3}
Returns: 5
{2,1,2,2,1,2}
Returns: -1
These answers do not correspond to your theory. It is possible that these are people from four different countries, but even if that were the case, we can tell that people from some countries are not sitting together. For example, consider the leftmost person. According to her answer, there are two people from her country. However, the person sitting next to her cannot be from the same country.
{100}
Returns: -1
{1}
Returns: 1
{2}
Returns: -1
{2,2,2,2,4,4,4,4}
Returns: 3
{5,4,3,2,1}
Returns: -1
{1,2,2,1}
Returns: 3
{3,1,3,3}
Returns: -1
{7,7,7,7,7,7,7,7}
Returns: -1
{1,2,2,1,1,1,2,2,2,2,1,1,1,1,2,2,1,1,3,3,3,1,1,1,2,2,3,3,3,1,1,1,1,1}
Returns: 25
{3,2,2}
Returns: -1
{1,2,3,4,5}
Returns: -1
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: 100
{10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10}
Returns: 10
{99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}
Returns: -1
{49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51}
Returns: 2
{2,2,4,4,4,4,4,4,4,4,3,3,3,2,2,1,4,4,4,4,4,4,4,4,1,2,2,3,3,3,4,4,4,4,1,5,5,5,5,5,4,4,4,4,2,2,1,3,3,3,2,2,5,5,5,5,5,4,4,4,4,3,3,3,4,4,4,4,5,5,5,5,5,2,2,5,5,5,5,5,5,5,5,5,5,3,3,3,2,2,3,3,3,4,4,4,4}
Returns: 31
{5,5,5,5,5,1,1,2,2,1,2,2,1,1,1,2,2,5,5,5,5,5,4,4,4,4,5,5,5,5,5,1,5,5,5,5,5,1,5,5,5,5,5,2,2,4,4,4,4,5,5,5,5,5,4,4,4,4,1,3,3,3,1,4,4,4,4,1,1,1,5,5,5,5,5,3,3,3,2,2,1,5,5,5,5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3,5,5,5,5,5,4,4,4,4,5,5,5,5,5,1,1,5,5,5,5,5}
Returns: 30
{5,5,5,5,5,4,4,4,4,5,5,5,5,5,5,5,5,5,5,2,2,3,3,3,1,3,3,3,2,2,1,5,5,5,5,5,2,2,3,3,3,2,2,2,2,5,5,5,5,5,5,5,5,5,5,2,2,3,3,3,5,5,5,5,5,1,2,2,1,5,5,5,5,5,2,2,1,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,1}
Returns: 32
{4,4,4,4,2,2,1,4,4,4,4,1,1,1,1,2,2,1,4,4,4,4,1,4,4,4,4,2,2,5,5,5,5,5,1,2,2,2,2,5,5,5,5,5,2,2,4,4,4,4,4,4,4,4,3,3,3,1,3,3,3,5,5,5,5,5,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,4,4,4,4,1,5,5,5,5,5,4,4,4,4,4,4,4,4}
Returns: 37
{3,3,3,3,3,3,3,3,3,4,4,4,4,1,3,3,3,4,4,4,4,4,4,4,4,2,2,3,3,3,4,4,4,4,5,5,5,5,5,1,4,4,4,4,1,3,3,3,1,2,2,4,4,4,4,1,3,3,3,3,3,3,3,3,3,5,5,5,5,5,5,5,5,5,5,2,2,1,3,3,3,1,1,1,3,3,3,3,3,3,4,4,4,4,1,3,3,3,2,2}
Returns: 37
{5,5,5,5,5,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,10,10,10,10,10,10,10,10,10,10,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,6,6,6,6,6,6,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16}
Returns: 7
{3,3,3,1,9,9,9,9,9,9,9,9,9,1,6,6,6,6,6,6,6,6,6,6,6,6,5,5,5,5,5,7,7,7,7,7,7,7,1,11,11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,10,10,10,2,2}
Returns: 12
{1,11,11,11,11,11,11,11,11,11,11,11,1,5,5,5,5,5,11,11,11,11,11,11,11,11,11,11,11,3,3,3,4,4,4,4,5,5,5,5,5,2,2,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,6,6,6,6,6,6,1}
Returns: 12
{12,12,12,12,12,12,12,12,12,12,12,12,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,2,2,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,1,5,5,5,5,5,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31}
Returns: 7
{7,7,7,7,7,7,7,5,5,5,5,5,10,10,10,10,10,10,10,10,10,10,1,1,7,7,7,7,7,7,7,3,3,3,1,7,7,7,7,7,7,7,5,5,5,5,5,2,2,8,8,8,8,8,8,8,8,5,5,5,5,5}
Returns: 13
{47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,5,5,5,5,5,3,3,3,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,2,2}
Returns: 6
{2,2,9,9,9,9,9,9,9,9,9,4,4,4,4,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,2,2,5,5,5,5,5,6,6,6,6,6,6,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22}
Returns: 8
{25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11,11,11,11,11,11,11,11,2,2,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,8,8,8,8,8,8,8,8}
Returns: 6
{4,4,4,4,1,1,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,1,12,12,12,12,12,12,12,12,12,12,12,12,3,3,3,5,5,5,5,5,6,6,6,6,6,6,2,2,2,2}
Returns: 11
{9,9,9,9,9,9,9,9,9,1,2,2,1,2,2,2,2,4,4,4,4,2,2,6,6,6,6,6,6,6,6,6,6,6,6,1,1,5,5,5,5,5,1,5,5,5,5,5,1,1}
Returns: 17
{4,4,4,4,4,4,4,4,10,10,10,10,10,10,10,10,10,10,3,3,3,1,8,8,8,8,8,8,8,8,4,4,4,4,10,10,10,10,10,10,10,10,10,10,5,5,5,5,5,1}
Returns: 10
{2,2,5,5,5,5,5,2,2,1,12,12,12,12,12,12,12,12,12,12,12,12,6,6,6,6,6,6,4,4,4,4,2,2,5,5,5,5,5,2,2,4,4,4,4,2,2,1,3,3,3,3,3,3,1,3,3,3,4,4,4,4}
Returns: 18
{3,3,3,6,6,6,6,6,6,6,6,6,6,6,6,9,9,9,9,9,9,9,9,9,1,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,2,2,7,7,7,7,7,7,7,5,5,5,5,5,3,3,3,3,3,3,8,8,8,8,8,8,8,8,6,6,6,6,6,6,4,4,4,4,3,3,3}
Returns: 15
{1,8,8,8,8,8,8,8,8,12,12,12,12,12,12,12,12,12,12,12,12,3,3,3,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,5,5,5,5,5,7,7,7,7,7,7,7,4,4,4,4,3,3,3,3,3,3,1,5,5,5,5,5,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15}
Returns: 13
{3,3,3,1,2,2,5,5,5,5,5,6,6,6,6,6,6,5,5,5,5,5,3,3,3,5,5,5,5,5,1,1,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,2,2,4,4,4,4,1}
Returns: 14
{5,5,5,5,5,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,2,2,14,14,14,14,14,14,14,14,14,14,14,14,14,14,5,5,5,5,5,2,2,2,2,3,3,3,13,13,13,13,13,13,13,13,13,13,13,13,13}
Returns: 9
{3,3,3,3,3,3,1,1,1,4,4,4,4,1,2,2,1,6,6,6,6,6,6,7,7,7,7,7,7,7,1,2,2,5,5,5,5,5,10,10,10,10,10,10,10,10,10,10,1,2,2,2,2}
Returns: 18
{1,1,12,12,12,12,12,12,12,12,12,12,12,12,5,5,5,5,5,7,7,7,7,7,7,7,7,7,7,7,7,7,7,4,4,4,4,7,7,7,7,7,7,7,4,4,4,4,2,1,13,13,13,13,13,13,13,13,13,13,13,13,13,3,3,3,5,5,5,5,5}
Returns: -1
{8,8,8,8,8,8,8,8,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,13,13,13,13,13,13,13,13,13,13,13,13,13,2,2,13,13,13,13,13,13,13,13,13,13,13,13,13,4,4,4,4,7,7,7,7,7,7,7,13,13,13,13,13,13,13,13,13,13,13,13,13}
Returns: 8
{3,3,3,9,9,9,9,9,9,9,9,9,1,2,2,4,4,4,4,2,2,6,6,6,6,6,6,4,4,4,4,6,6,6,6,6,6,4,4,4,4,5,5,5,5,5,1,2,2,4,4,4,4,4,4,4,4,7,7,7,7,7,7,7,4,4,4,4,5,5,5,5,5,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,2,2}
Returns: 20
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}
Returns: -1
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}
Returns: 1
{4, 4, 4, 4, 1, 1, 2, 2, 3, 3, 3 }
Returns: 5
{2, 1 }
Returns: -1
{2, 2, 1, 1, 3, 3, 3, 2, 2 }
Returns: 5
{2, 1, 1 }
Returns: -1
{3, 3, 3, 1, 3, 3, 3 }
Returns: 3
{3, 1, 3 }
Returns: -1
{2, 2, 2, 2 }
Returns: 2
{3, 3 }
Returns: -1
{1, 2 }
Returns: -1
{1, 2, 2, 2 }
Returns: -1
{3, 3, 3, 3, 3, 3 }
Returns: 2
{2, 2, 2 }
Returns: -1
{2 }
Returns: -1
{2, 2, 3, 3, 3 }
Returns: 2
{4, 3, 4, 4 }
Returns: -1
{2, 1, 2 }
Returns: -1
{2, 3 }
Returns: -1
{4, 1, 1, 4, 1, 1 }
Returns: -1
{4, 4, 4, 3 }
Returns: -1
{2, 1, 2, 2, 1, 1 }
Returns: -1
{3, 1, 2, 3, 2, 3, 1 }
Returns: -1
{2, 2, 1, 2, 2 }
Returns: 3
{1 }
Returns: 1
{1, 2, 2, 3, 2, 3, 3, 2 }
Returns: -1
{1, 2, 2, 1 }
Returns: 3
{2, 1, 2, 1 }
Returns: -1