Find Positive Integer Solution for a Given Equation - Problem

Given a callable function f(x, y) with a hidden formula and a value z, reverse engineer the formula and return all positive integer pairs x and y where f(x,y) == z.

You may return the pairs in any order. While the exact formula is hidden, the function is monotonically increasing, i.e.:

  • f(x, y) < f(x + 1, y)
  • f(x, y) < f(x, y + 1)

The function interface is defined like this:

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);
};

We will judge your solution as follows: The judge has a list of 9 hidden implementations of CustomFunction, along with a way to generate an answer key of all valid pairs for a specific z. The judge will receive two inputs: a function_id (to determine which implementation to test your code with), and the target z. The judge will call your findSolution and compare your results with the answer key.

Input & Output

Example 1 — Simple Addition Function
$ Input: function_id = 1, z = 5
Output: [[1,4],[2,3],[3,2],[4,1]]
💡 Note: Assuming f(x,y) = x + y, we need pairs where x + y = 5. Valid positive integer pairs are (1,4), (2,3), (3,2), (4,1).
Example 2 — Product Function
$ Input: function_id = 2, z = 6
Output: [[1,6],[2,3],[3,2],[6,1]]
💡 Note: Assuming f(x,y) = x * y, we need pairs where x * y = 6. Valid positive integer pairs are (1,6), (2,3), (3,2), (6,1).
Example 3 — Large Target
$ Input: function_id = 1, z = 100
Output: [[1,99],[2,98],...[99,1]]
💡 Note: For f(x,y) = x + y with target 100, there are 99 valid pairs from (1,99) to (99,1).

Constraints

  • The function f is defined for you
  • 1 ≤ z ≤ 100
  • It's guaranteed that the solutions of f(x, y) == z will be on the range 1 ≤ x, y ≤ 1000
  • It's also guaranteed that f(x, y) will fit in 32-bit signed integer if 1 ≤ x, y ≤ 1000

Visualization

Tap to expand
Find All Pairs (x,y) Where f(x,y) = zHidden Functionf(x,y) monotonic ↗Targetz = 5(1,4)(2,3)(3,2)(4,1)Search efficiently using monotonic propertyOutput: [[1,4],[2,3],[3,2],[4,1]]
Understanding the Visualization
1
Input
Hidden monotonic function f(x,y) and target value z
2
Process
Search grid efficiently using monotonic property
3
Output
All positive integer pairs where f(x,y) = z
Key Takeaway
🎯 Key Insight: The monotonic property allows us to eliminate entire rows/columns during search, making the solution much more efficient than brute force
Asked in
Google 15 Facebook 12 Amazon 8
25.0K Views
Medium Frequency
~15 min Avg. Time
892 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