Problem Description
Given n people with statements about each other where a good person always tells the truth and a bad person may lie or tell the truth, determine the maximum possible number of good people that can exist such that all statements made by assumed good people are consistent.
Key Insights
- Enumerate every possible assignment of good (truth-tellers) and bad (potential liars) using bit masking.
- For every assumed assignment, only consider the statements from people assumed as good.
- For a person i assumed good, their statement about person j must be consistent: if statements[i][j] == 1 then j must be good; if it equals 0 then j must be bad.
- Since n is at most 15, a brute-force approach over all 2^n assignments (maximum of 32768 possibilities) is feasible.
Space and Time Complexity
Time Complexity: O(2^n * n^2) – we iterate through all possible 2^n assignments and, for each, check up to n people making n statements each. Space Complexity: O(n) – extra space used for iteration and helper variables.
Solution
We solve the problem by using bit manipulation to iterate over each possible configuration (mask) that assigns each person as either good (bit set) or bad (bit not set). For each configuration, we verify if it is valid by ensuring that for every person i assumed good (bit is set), their statements about every other person j align with the assumed configuration: statement 1 must correspond to j being good; statement 0 must correspond to j being bad; statements with value 2 can be ignored. We track the maximum count of good people across all valid configurations.