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 Positive Integer Solution INPUT f(x, y) = hidden Monotonically Increasing function_id = 1 z = 5 Search Space 1 2 3 4 5 ... x: 1 to z, y: 1 to z f(x,y) < f(x+1,y) f(x,y) < f(x,y+1) Find all (x,y) where f(x,y)==z ALGORITHM STEPS 1 Initialize Pointers x=1 (start), y=z (end) 2 Two Pointer Search While x<=z and y>=1 3 Compare f(x,y) with z Adjust x or y accordingly 4 Collect Solutions When f(x,y)==z, add pair Decision Logic: if f(x,y) == z: add [x,y], x++, y-- if f(x,y) < z: x++ (need larger) if f(x,y) > z: y-- (need smaller) FINAL RESULT Valid Pairs Found: [1, 4] f(1,4)=5 [2, 3] f(2,3)=5 [3, 2] f(3,2)=5 [4, 1] f(4,1)=5 Output: [[1,4],[2,3],[3,2],[4,1]] Complexity: Time: O(z) | Space: O(1) Key Insight: The monotonic property allows two-pointer technique: start with x=1, y=z (opposite corners). If f(x,y) is too small, increase x. If too large, decrease y. This avoids O(z^2) brute force and achieves O(z) time by eliminating rows/columns with each comparison. TutorialsPoint - Find Positive Integer Solution for a Given Equation | Optimal Two-Pointer Approach
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