Check for Contradictions in Equations - Problem
Check for Contradictions in Equations

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
Currency Exchange Network AnalogyUSDEURGBP1.20.80.7Contradiction DetectionDirect rates:• USD → EUR: 1.2• USD → GBP: 0.8• EUR → GBP: 0.7 (given)Derived rate:• EUR → GBP: 0.8/1.2 = 0.670.7 ≠ 0.67 → CONTRADICTION!Algorithm Steps1. Model currencies as graph nodes, exchange rates as weighted edges2. Use Union-Find to group connected currencies and track relative rates3. When adding new rate between connected currencies, check for consistency
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

n
2n
āœ“ Linear Growth
Space Complexity
O(n)

Parent and weight arrays for Union-Find structure

n
2n
⚔ 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
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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