Maximum Good People Based on Statements - Problem

There are two types of persons:

  • The good person: The person who always tells the truth.
  • The bad person: The person who might tell the truth and might lie.

You are given a 0-indexed 2D integer array statements of size n x n that represents the statements made by n people about each other. More specifically, statements[i][j] could be one of the following:

  • 0 which represents a statement made by person i that person j is a bad person.
  • 1 which represents a statement made by person i that person j is a good person.
  • 2 represents that no statement is made by person i about person j.

Additionally, no person ever makes a statement about themselves. Formally, we have that statements[i][i] = 2 for all 0 <= i < n.

Return the maximum number of people who can be good based on the statements made by the n people.

Input & Output

Example 1 — Basic Consistency Check
$ Input: statements = [[2,1,2],[1,2,2],[2,2,2]]
Output: 3
💡 Note: Person 0 says person 1 is good, person 1 says person 0 is good, person 2 makes no statements. All three can be good consistently since their statements don't contradict. Maximum good people = 3.
Example 2 — Contradictory Statements
$ Input: statements = [[2,0],[0,2]]
Output: 1
💡 Note: Person 0 says person 1 is bad, person 1 says person 0 is bad. They can't both be good (would contradict each other). Maximum good people = 1.
Example 3 — Single Person
$ Input: statements = [[2]]
Output: 1
💡 Note: Only one person exists, no statements about others. That person can be good. Maximum good people = 1.

Constraints

  • n == statements.length == statements[i].length
  • 2 ≤ n ≤ 15
  • statements[i][j] is either 0, 1, or 2
  • statements[i][i] == 2

Visualization

Tap to expand
Maximum Good People Based on Statements INPUT statements[n][n] matrix j=0 j=1 j=2 i=0 2 1 2 i=1 1 2 2 i=2 2 2 2 Legend: 1 = says good 0 = says bad 2 = no statement 3 People: P0 P1 P2 good good ALGORITHM STEPS 1 Enumerate All Subsets Try all 2^n combinations of good/bad assignments 2 Validate Each Subset Check if good people's statements are consistent 3 Consistency Check If good says j is good, j must be in good set 4 Track Maximum Keep max count of valid good person subsets Valid Subsets for n=3: {0,1}: P0 says P1 good [OK] P1 says P0 good [OK] VALID! count=2 {0,1,2}: P0 no stmt on P2 P1 no stmt on P2 But inconsistent! FINAL RESULT Maximum Good People Found: 2 Best Valid Assignment: P0 GOOD P1 GOOD P2 BAD says good says good Verification: P0 (good) says P1 is good [OK] P1 (good) says P0 is good [OK] Key Insight: Use bitmask enumeration (2^n subsets) to try all possible good/bad person assignments. For each subset, validate that every "good" person's statements match the assignment. Time Complexity: O(2^n * n^2) where n is number of people. Space: O(1) extra. TutorialsPoint - Maximum Good People Based on Statements | Optimal Bitmask Solution
Asked in
Google 15 Meta 12 Amazon 8
18.5K Views
Medium Frequency
~25 min Avg. Time
485 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