Check for Contradictions in Equations - Problem
Check for Contradictions in Equations
You are given a
Your task is to determine if there exists a contradiction in the given equations. A contradiction occurs when the same division relationship can be derived in multiple ways that yield different results.
Example: If we have
Goal: Return
Note: When comparing floating-point numbers, two values are considered equal if their absolute difference is less than
You are given a
2D array of strings called equations and an array of real numbers called values. Each equation equations[i] = [Ai, Bi] represents a division relationship where Ai / Bi = values[i].Your task is to determine if there exists a contradiction in the given equations. A contradiction occurs when the same division relationship can be derived in multiple ways that yield different results.
Example: If we have
a/b = 2.0 and b/c = 3.0, we can derive that a/c = 6.0. If another equation directly states a/c = 5.0, this creates a contradiction.Goal: Return
true if there is a contradiction, false otherwise.Note: When comparing floating-point numbers, two values are considered equal if their absolute difference is less than
10-5. Input & Output
example_1.py ā Basic Contradiction
$
Input:
equations = [["a","b"],["b","c"],["a","c"]]
values = [2.0,3.0,5.0]
āŗ
Output:
true
š” Note:
From a/b = 2.0 and b/c = 3.0, we can derive a/c = 6.0. But the direct equation states a/c = 5.0, which creates a contradiction since 6.0 ā 5.0.
example_2.py ā No Contradiction
$
Input:
equations = [["a","b"],["b","c"],["a","c"]]
values = [2.0,3.0,6.0]
āŗ
Output:
false
š” Note:
From a/b = 2.0 and b/c = 3.0, we derive a/c = 6.0. The direct equation a/c = 6.0 is consistent, so no contradiction exists.
example_3.py ā Disconnected Components
$
Input:
equations = [["a","b"],["c","d"]]
values = [2.0,3.0]
āŗ
Output:
false
š” Note:
The equations represent two separate relationships (a/b = 2.0 and c/d = 3.0) with no connection between them. No contradictions can arise from disconnected components.
Visualization
Tap to expand
Understanding the Visualization
1
Model as Graph
Each variable becomes a node, each equation creates weighted edges
2
Union Components
Use weighted Union-Find to efficiently group related variables
3
Check Consistency
When connecting already-linked variables, verify ratio matches
4
Detect Contradiction
Return true immediately if any inconsistency is found
Key Takeaway
šÆ Key Insight: Contradictions occur when the same relationship can be computed through different paths with different results - this is efficiently detected using weighted Union-Find structure.
Time & Space Complexity
Time Complexity
O(n·α(n))
Union-Find operations with inverse Ackermann function
ā Linear Growth
Space Complexity
O(n)
Parent and weight arrays for Union-Find structure
ā” Linearithmic Space
Constraints
- 1 ⤠equations.length ⤠100
- equations[i].length == 2
- 1 ⤠Ai.length, Bi.length ⤠5
- values.length == equations.length
- 0.0 < values[i] ⤠20.0
- Ai and Bi consist of lowercase English letters and digits
- Floating point precision: Use 10-5 as epsilon for comparison
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code