Find Positive Integer Solution for a Given Equation - Problem
You are given a mysterious black box function f(x, y) that takes two positive integers and returns a positive integer. Your mission is to reverse engineer this function by finding all pairs of positive integers (x, y) where f(x, y) == z for a given target value z.

Here's what you know about the function:
β€’ The exact formula is hidden from you
β€’ The function is monotonically increasing in both dimensions:
  - f(x, y) < f(x + 1, y) (increasing as x increases)
  - f(x, y) < f(x, y + 1) (increasing as y increases)

Goal: Return all positive integer pairs [x, y] where f(x, y) == z. The pairs can be returned in any order.

Example: If f(x, y) = x + y and z = 3, then the answer would be [[1,2], [2,1]] because f(1,2) = 3 and f(2,1) = 3.

Input & Output

example_1.py β€” Basic Function
$ Input: function_id = 1, z = 5 # Hidden function: f(x, y) = x + y
β€Ί Output: [[1,4], [2,3], [3,2], [4,1]]
πŸ’‘ Note: The function f(x, y) = x + y, so we need to find all positive integer pairs where x + y = 5. The valid pairs are (1,4), (2,3), (3,2), and (4,1).
example_2.py β€” Multiplicative Function
$ Input: function_id = 2, z = 5 # Hidden function: f(x, y) = x * y
β€Ί Output: [[1,5], [5,1]]
πŸ’‘ Note: The function f(x, y) = x * y, so we need to find all positive integer pairs where x * y = 5. Since 5 is prime, only (1,5) and (5,1) work.
example_3.py β€” No Solutions
$ Input: function_id = 1, z = 1 # Hidden function: f(x, y) = x + y
β€Ί Output: []
πŸ’‘ Note: The function f(x, y) = x + y, but since x and y must be positive integers (β‰₯ 1), the minimum value of x + y is 2. Therefore, there are no solutions for z = 1.

Visualization

Tap to expand
Smart Grid Navigation: Two Pointers Approach2345← START HERE34564567βœ“ Green: f(x,y) < 4 (target)β†’ Yellow: f(x,y) = 4 (found!)βœ— Red: f(x,y) > 4 (eliminate)Movement Strategy:β€’ If value > target: Move LEFT (eliminate right region)β€’ If value < target: Move DOWN (eliminate upper region)β€’ If value = target: Found solution!
Understanding the Visualization
1
Start Smart
Begin at the top-right corner (small x, large y) where we have the best information
2
Compare & Decide
If f(x,y) > target: move left (smaller y). If f(x,y) < target: move down (larger x)
3
Eliminate Regions
Each move eliminates an entire row or column from consideration
4
Collect Solutions
When f(x,y) = target, record the solution and continue searching
Key Takeaway
🎯 Key Insight: By starting from a strategic corner and using the monotonic property, each comparison eliminates an entire row or column, reducing time complexity from O(n²) to O(n).

Time & Space Complexity

Time Complexity
⏱️
O(X Γ— Y)

Where X and Y are the upper bounds for x and y coordinates. In worst case, this could be O(zΒ²) if the function grows slowly

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables, not counting the output array

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ function_id ≀ 9
  • 1 ≀ z ≀ 100
  • f(x, y) is monotonically increasing
  • 1 ≀ x, y ≀ 1000
  • The function will return an integer in the range [1, 109]
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
52.3K Views
Medium Frequency
~25 min Avg. Time
1.8K 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