Evaluate Division Problem
Evaluate Division Problem — ExecCode Hard DSA Practice
Solve the Evaluate Division problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Description Problem You are given a list of pairwise equations that define division relationships between variables. Each equation is of the form A B k, meaning A / B = k, where A and B are variable names (strings) and k is a positive real number. After the equations, you are given a list of queries; each query asks for the result of X / Y. If the value can be determined from the given equations, return it as a floating-point number; otherwise return -1.0. Input Format The input is provided as a single stdin blob with the following structure: - The first line contains two integers E and Q: the number of equations and the number of queries. The next E lines each contain an equation in the form: A B k, where A and B are strings (variable names without spaces) and k is a floating-point number (A / B = k). The next Q lines each contain a query in the form: X Y, asking for X / Y. Example input layout: 3 3 A B 2.0 B C 3.0 A C B A A E Output Format Output Q lines. For each query, print the computed floating-point value (as a decimal) if X / Y can be determined from the equations, or -1.0 if it cannot. The output must match the order of queries. Constraints Variable names are non-empty st…
Examples
Input {"data": "2 2\na b 0.5\nb a 2.0\na b\nb a"}; Output [0.5,2.0]. Input {"equations": [["a", "b"], ["b", "c"]], "values": [2.0, 3.0], "queries": [["a", "c"], ["b", "a"]]}; Output [6.0,0.5]. Input {"equations": [["a0", "b0"], ["b0", "c0"], ["c0", "d0"], ["d0", "e0"]], "values": [2.0, 3.0, 4.0, 5.0], "queries": [["a0", "e0"], ["c0", "a0"], ["b0", "missing"]]}; Output [120.0,0.16666666666666666,-1.0]
Constraints
1<=N<=10^5
Practice Evaluate Division free on ExecCode. Browse DSA problems, topic map, and placement guides.