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.
Explanation: function_id = 1 means that f(x, y) = x + y
Brute force Solution
Enumerate all the pairs of
y, add the pair into final answer if the result is ‘z’.
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
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!😋