+{"link": "https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation", "name": "Find Positive Integer Solution for a Given Equation", "difficulty": "Easy", "statement": "<div><p>Given a function <code>f(x, y)</code> and a value <code>z</code>, return all positive integer pairs <code>x</code> and <code>y</code> where <code>f(x,y) == z</code>.</p>\n\n<p>The function is constantly increasing, i.e.:</p>\n\n<ul>\n\t<li><code>f(x, y) < f(x + 1, y)</code></li>\n\t<li><code>f(x, y) < f(x, y + 1)</code></li>\n</ul>\n\n<p>The function interface is defined like this: </p>\n\n<pre>interface CustomFunction {\npublic:\n // Returns positive integer f(x, y) for any given positive integer x and y.\n int f(int x, int y);\n};\n</pre>\n\n<p>For custom testing purposes you're given an integer <code>function_id</code> and a target <code>z</code> as input, where <code>function_id</code> represent one function from an secret internal list, on the examples you'll know only two functions from the list. </p>\n\n<p>You may return the solutions in any order.</p>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> function_id = 1, z = 5\n<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]\n<strong>Explanation:</strong> function_id = 1 means that f(x, y) = x + y</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> function_id = 2, z = 5\n<strong>Output:</strong> [[1,5],[5,1]]\n<strong>Explanation:</strong> function_id = 2 means that f(x, y) = x * y\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= function_id <= 9</code></li>\n\t<li><code>1 <= z <= 100</code></li>\n\t<li>It's guaranteed that the solutions of <code>f(x, y) == z</code> will be on the range <code>1 <= x, y <= 1000</code></li>\n\t<li>It's also guaranteed that <code>f(x, y)</code> will fit in 32 bit signed integer if <code>1 <= x, y <= 1000</code></li>\n</ul>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass CustomFunction\n{\npublic:\n // Returns f(x, y) for any given positive integers x and y.\n // Note that f(x, y) is increasing with respect to both x and y.\n // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n int f(int x, int y);\n};\n\nclass Solution\n{\npublic:\n vector<vector<int>> findSolution(CustomFunction &customfunction, int z)\n {\n\n vector<vector<int>> res;\n for (int x = 1; x <= 1000; x++)\n {\n for (int y = 1; y <= 1000; y++)\n {\n int val = customfunction.f(x, y);\n if (val == z)\n {\n vector<int> r = {x, y};\n res.push_back(r);\n break;\n }\n if (val > z)\n {\n break;\n }\n }\n }\n\n return res;\n }\n};"}
0 commit comments