Three-Speaker Truth Consistency
Problem statement
Three speakers A, B, and C each make one statement. For this exercise, assume every option assigns a Boolean truth value to each speaker's statement.
An option is an integer mask from 0 through 7. Bit 0 gives A's assigned truth value, bit 1 gives B's, and bit 2 gives C's. A set bit means true; an unset bit means false.
The 3 x 8 matrix statements supplies the truth table of the statements: statements[s][mask] is the truth value of speaker s's statement in the world represented by mask, with speakers ordered A, B, C. These supplied tables define the statements; you do not need to parse natural language.
An option is consistent when both conditions hold:
- For every speaker
s, the supplied statement truthstatements[s][mask]equals bitsofmask. - Not all three statements are true, so
mask = 7is excluded. All three statements may be false.
Given statements and the ordered array options, return the zero-based index of the first consistent option. Return -1 if no offered option is consistent. Duplicate options are allowed and keep their original positions.
Function
firstConsistentOption(statements: int[][], options: int[]) → intExamples
Example 1
statements = [[0,0,1,1,0,0,1,1],[0,1,0,1,0,1,0,1],[0,0,0,0,0,0,0,0]]options = [7,2,3,0]return = 2The supplied tables require A and B to have the same truth value, while C is false. Masks 3 and 0 are consistent. Mask 3 appears first in the offered list, at index 2.
Example 2
statements = [[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]]options = [7,6,3]return = -1Only mask 7 agrees with the three constant-true statements. It is excluded because all three truth bits are 1, so no offered option qualifies.
Example 3
statements = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]options = [0]return = 0Mask 0 makes all three statements false. This satisfies the requirement that not all statements are true, so the option at index 0 qualifies.
Constraints
statements.length = 3and every row has length8.- Every table entry is
0or1. 0 <= options.length <= 1000.0 <= options[i] <= 7.