Maximum Good People Based on Statements - Problem

Imagine you're a detective investigating a group of suspects where some always tell the truth (good people) and others might lie (bad people). Your job is to determine the maximum number of people who could be truthful based on their statements about each other.

You are given an n x n matrix statements where:

  • statements[i][j] = 0: Person i claims person j is bad
  • statements[i][j] = 1: Person i claims person j is good
  • statements[i][j] = 2: Person i makes no statement about person j

Key Rule: Good people always tell the truth, while bad people can either lie or tell the truth. No one makes statements about themselves (statements[i][i] = 2).

Goal: Find the maximum number of people who can simultaneously be good without creating any contradictions in their statements.

Input & Output

example_1.py โ€” Basic Case
$ Input: statements = [[2,1,2],[1,2,2],[2,0,2]]
โ€บ Output: 2
๐Ÿ’ก Note: Person 0 says person 1 is good, and person 1 says person 0 is good. They could both be telling the truth. Person 1 also says person 2 is bad, which is consistent if person 2 is indeed bad. Maximum good people: persons 0 and 1.
example_2.py โ€” All Good Impossible
$ Input: statements = [[2,0],[0,2]]
โ€บ Output: 1
๐Ÿ’ก Note: Person 0 says person 1 is bad, and person 1 says person 0 is bad. They can't both be good (good people tell the truth). At most one can be good, so the answer is 1.
example_3.py โ€” Single Person
$ Input: statements = [[2]]
โ€บ Output: 1
๐Ÿ’ก Note: With only one person who makes no statements about others, they can be good. The maximum is 1.

Constraints

  • n == statements.length == statements[i].length
  • 2 โ‰ค n โ‰ค 15
  • statements[i][j] is 0, 1, or 2
  • statements[i][i] == 2 (no self-statements)
  • At least one person can always be considered good

Visualization

Tap to expand
๐Ÿ•ต๏ธ Truth Detective ProcessStep 1: GenerateAll possiblecombinations oftruthful people2^n possibilitiesStep 2: ValidateCheck if truthfulpeople's statementsare consistentO(nยฒ) per checkStep 3: CountValid scenarios:How many peopleare truthful?Track maximumExample Investigation: 3 SuspectsScenario: Suspects 0,1 Truthfulโœ“ Person 0: "Person 1 is good" โ†’ TRUEโœ“ Person 1: "Person 0 is good" โ†’ TRUEโœ“ Person 1: "Person 2 is bad" โ†’ TRUEScenario: All 3 Truthfulโœ— Person 1: "Person 2 is bad"โœ— But Person 2 is supposed to be goodโœ— CONTRADICTION!All Other ScenariosCheck remaining combinations...โ€ข Only person 0 truthfulโ€ข Only person 1 truthful, etc.๐ŸŽฏ Investigation ResultMaximum truthful suspects: 2(Persons 0 and 1 can both be truthful)
Understanding the Visualization
1
List All Possibilities
Create every possible combination of who could be truthful vs. untruthful
2
Test Each Scenario
For each possibility, check if all truthful people's statements are consistent
3
Count Valid Witnesses
When statements align, count how many people are truthful in that scenario
4
Find Maximum
Return the largest number of truthful people from any valid scenario
Key Takeaway
๐ŸŽฏ Key Insight: Since n โ‰ค 15, we can efficiently check all 2^n possible truth assignments using bitmasks. Each valid assignment where all truthful people's statements are consistent gives us a candidate answer, and we return the maximum count found.
Asked in
Meta 8 Google 6 Microsoft 4 Amazon 3
24.6K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen