-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-positive-integer-solution-for-a-given-equation.html
62 lines (50 loc) · 2.62 KB
/
find-positive-integer-solution-for-a-given-equation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<p>Given a callable function <code>f(x, y)</code> <strong>with a hidden formula</strong> and a value <code>z</code>, reverse engineer the formula and return <em>all positive integer pairs </em><code>x</code><em> and </em><code>y</code><em> where </em><code>f(x,y) == z</code>. You may return the pairs in any order.</p>
<p>While the exact formula is hidden, the function is monotonically increasing, i.e.:</p>
<ul>
<li><code>f(x, y) < f(x + 1, y)</code></li>
<li><code>f(x, y) < f(x, y + 1)</code></li>
</ul>
<p>The function interface is defined like this:</p>
<pre>
interface CustomFunction {
public:
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
int f(int x, int y);
};
</pre>
<p>We will judge your solution as follows:</p>
<ul>
<li>The judge has a list of <code>9</code> hidden implementations of <code>CustomFunction</code>, along with a way to generate an <strong>answer key</strong> of all valid pairs for a specific <code>z</code>.</li>
<li>The judge will receive two inputs: a <code>function_id</code> (to determine which implementation to test your code with), and the target <code>z</code>.</li>
<li>The judge will call your <code>findSolution</code> and compare your results with the <strong>answer key</strong>.</li>
<li>If your results match the <strong>answer key</strong>, your solution will be <code>Accepted</code>.</li>
</ul>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> function_id = 1, z = 5
<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]
<strong>Explanation:</strong> The hidden formula for function_id = 1 is f(x, y) = x + y.
The following positive integer values of x and y make f(x, y) equal to 5:
x=1, y=4 -> f(1, 4) = 1 + 4 = 5.
x=2, y=3 -> f(2, 3) = 2 + 3 = 5.
x=3, y=2 -> f(3, 2) = 3 + 2 = 5.
x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> function_id = 2, z = 5
<strong>Output:</strong> [[1,5],[5,1]]
<strong>Explanation:</strong> The hidden formula for function_id = 2 is f(x, y) = x * y.
The following positive integer values of x and y make f(x, y) equal to 5:
x=1, y=5 -> f(1, 5) = 1 * 5 = 5.
x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= function_id <= 9</code></li>
<li><code>1 <= z <= 100</code></li>
<li>It is guaranteed that the solutions of <code>f(x, y) == z</code> will be in the range <code>1 <= x, y <= 1000</code>.</li>
<li>It is also guaranteed that <code>f(x, y)</code> will fit in 32 bit signed integer if <code>1 <= x, y <= 1000</code>.</li>
</ul>