You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.

Input & Output

Example 1 — Basic Division Chain
$ Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.0,0.5,-1.0,1.0,-1.0]
💡 Note: a/b = 2.0, b/c = 3.0, so a/c = (a/b) × (b/c) = 2.0 × 3.0 = 6.0. b/a = 1/(a/b) = 0.5. a/e is undefined (-1.0). a/a = 1.0. x/x is undefined since x doesn't appear in equations.
Example 2 — Single Equation
$ Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.5,2.0,-1.0,-1.0]
💡 Note: Only one equation a/b = 0.5. Direct query a/b = 0.5. Reverse b/a = 1/0.5 = 2.0. a/c and x/y are undefined.
Example 3 — Multiple Connected Components
$ Input: equations = [["a","b"],["c","d"]], values = [1.0,1.0], queries = [["a","c"],["b","d"],["b","a"],["d","c"]]
Output: [-1.0,-1.0,1.0,1.0]
💡 Note: Two separate components: {a,b} and {c,d}. No path between different components, so a/c and b/d are -1.0. Within components: b/a = 1/1.0 = 1.0, d/c = 1/1.0 = 1.0.

Constraints

  • 1 ≤ equations.length ≤ 20
  • equations[i].length == 2
  • 1 ≤ Ai.length, Bi.length ≤ 5
  • values.length == equations.length
  • 0.0 < values[i] ≤ 20.0
  • 1 ≤ queries.length ≤ 20
  • queries[i].length == 2
  • 1 ≤ Cj.length, Dj.length ≤ 5
  • Ai, Bi, Cj, Dj consist of lowercase English letters and digits.

Visualization

Tap to expand
Evaluate Division: From Equations to Graph QueriesInput Equationsa/b = 2.0b/c = 3.0Weighted GraphabcQuery Resultsa/c = ?Answer: 6.0DFS Path Findinga → b (×2.0) → c (×3.0) = 2.0 × 3.0 = 6.0Time: O(Q × (V + E)) | Space: O(V + E)
Understanding the Visualization
1
Input Equations
Given equations like a/b = 2.0, b/c = 3.0 with their values
2
Graph Construction
Build weighted graph with bidirectional edges
3
Query Processing
Use DFS to find paths and calculate results
Key Takeaway
🎯 Key Insight: Division relationships form a weighted graph where DFS finds multiplication paths between variables
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
185.0K Views
Medium Frequency
~25 min Avg. Time
4.5K 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