 Coder's Cat

## LeetCode: Find Positive Integer Solution for a Given Equation

2020-12-18

Given a function  f(x, y) and a value z, return all positive integer pairs x and y where f(x,y) == z.

The function is constantly increasing, i.e.:

The function interface is defined like this:

For custom testing purposes you’re given an integer function_id and a target z as input, where function_id represent one function from an secret internal list, on the examples you’ll know only two functions from the list.

You may return the solutions in any order.

Example 1:

Explanation: function_id = 1 means that f(x, y) = x + y

## Brute force Solution

Enumerate all the pairs of `x` and `y`, add the pair into final answer if the result is ‘z’.

## Binary-search Solution

Notice the precondition: The function is constantly increasing:

We can set `x` to 1 and `y` to 1000, this is the middle of all possible results, then we adjust `x` and `y` according to the result.
This solution is an algorithm of binary-search.

Preparing for an interview? Checkout this!

Join my Email List for more insights, It's Free!😋