Search⌘ K
AI Features

Evaluate Division

Explore how to apply the union-find algorithm to evaluate division queries between variable pairs using given equations and values. Understand the problem constraints, devise an optimal approach, and prepare for coding solutions that handle real number divisions and impossible cases.

Statement

We are given three arrays:

  1. equations: Here, each equations[i] represents a pair of variables [a[i], b[i]], where each a[i] or b[i] is a string that represents a single variable.

  2. values: This array contains real numbers that are the result values when the first variable in equations[i] is divided by the second. For example, if equations[i] = ["m", "n"] and values[i] = 2.0, it means that m / n = 2.0.

  3. queries: Here, each queries[i] represents a pair of variables [c[i], d[i]], where each c[i] or d[i] is a string that represents a single variable. The answer to each query must be calculated as c[i] / d[i].

Given these arrays, find the result of each queries[i] by dividing the first variable with the second. To answer all the queries correctly, use the given equations and values. If it's impossible to determine the answer to any query based on the given equations and values, return 1.0-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.

Constraints:

  • 11 \leq equations.length 20\leq 20

  • equations[i].length ==2== 2

  • 11 \leq a[i].length, b[i].length 5\leq 5

  • values.length ==== equations.length

  • 0.0<0.0 < values[i] 20.0\leq 20.0

  • 11 \leq queries.length 20\leq 20

  • queries[i].length ==2== 2

  • 11 \leq c[j].length, d[j].length 5\leq 5

  • a[i], b[i], c[j], d[j] consist of lower case English letters and digits.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Evaluate Division

1.

What is the correct output if the following values are given as input?

equations = [[“a”, “b”]]

values = [1.5]

queries = [[“a”, “b”], [“c”, “b”], [“x”, “x”]]

A.

[1.5, 1.0, -1.0]

B.

[1.5, -1.0, -1.0]

C.

[1.5, -1.0, 1.0]


1 / 2

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground.

We have left the solution to this challenge as an exercise for you. The optimal solution to this problem runs in O((m+n).logn) time and takes O(n) space. You may try to translate the logic of the solved puzzle into a coded solution.

Java
usercode > Solution.java
/*
⬅️ We have provided a UnionFind.java file under the "Files" tab
of this widget. You can use this file to build your solution.
*/
import java.util.*;
public class Solution {
public static double[] evaluateEquations(List<List<String>> equations, double[] v, List<List<String>> queries){
// Replace this placeholder return statement with your code
return new double[]{};
}
}
Evaluate Division