TopCoder problem "TrueStatements" used in SRM 378 (Division I Level One , Division II Level Two)



Problem Statement

    Professor Smith teaches a logic class. One day, he writes some statements on the blackboard:
Exactly a of these statements are true.
Exactly b of these statements are true.
Exactly c of these statements are true.
.
.
.

Each of a, b, c and so on is a number. He then asks the class how many of the statements are true.

You will be given a int[] statements, containing the numbers written in Professor Smith's statements. Return the number of the statements that are true. If there is more than one possible answer, return the largest one. If there are no possible answers, return -1.

 

Definition

    
Class:TrueStatements
Method:numberTrue
Parameters:int[]
Returns:int
Method signature:int numberTrue(int[] statements)
(be sure your method is public)
    
 

Constraints

-statements will contain between 1 and 50 elements, inclusive.
-Each element of statements will be between 0 and 50, inclusive.
 

Examples

0)
    
{0, 1, 2, 3}
Returns: 1
The 2nd statement is true (there is one true statement) and the others are false.
1)
    
{0}
Returns: -1
This is the Epimedes paradox: if the statement is true then it claims to be false, but if it is false then it must be true.
2)
    
{0, 3, 1, 3, 2, 3}
Returns: 3
One solution is that the 3rd statement is the only true statement. However, it is also possible that the 2nd, 4th and 6th statements are true (all of which say that 3 statements are true), and the largest solution must be returned.
3)
    
{1, 1}
Returns: 0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8390

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10798&pm=8390

Writer:

bmerry

Testers:

PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Problem categories:

Brute Force