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:
9
hidden implementations of CustomFunction
, along with a way to generate an answer key of all valid pairs for a specific z
.function_id
(to determine which implementation to test your code with), and the target z
.findSolution
and compare your results with the answer key.Accepted
.
Example 1:
Input: function_id = 1, z = 5 Output: [[1,4],[2,3],[3,2],[4,1]] Explanation: The hidden formula for function_id = 1 is f(x, y) = x + y. The following positive integer values of x and y make f(x, y) equal to 5: x=1, y=4 -> f(1, 4) = 1 + 4 = 5. x=2, y=3 -> f(2, 3) = 2 + 3 = 5. x=3, y=2 -> f(3, 2) = 3 + 2 = 5. x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
Example 2:
Input: function_id = 2, z = 5 Output: [[1,5],[5,1]] Explanation: The hidden formula for function_id = 2 is f(x, y) = x * y. The following positive integer values of x and y make f(x, y) equal to 5: x=1, y=5 -> f(1, 5) = 1 * 5 = 5. x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
Constraints:
1 <= function_id <= 9
1 <= z <= 100
f(x, y) == z
will be in the range 1 <= x, y <= 1000
.f(x, y)
will fit in 32 bit signed integer if 1 <= x, y <= 1000
.This problem involves finding all pairs of positive integers (x, y) that satisfy a given equation f(x, y) == z
, where f
is a hidden monotonically increasing function. The challenge lies in the unknown nature of f
. Two efficient solutions are presented below.
This approach iterates through possible values of x
and uses binary search to find the corresponding y
for each x
.
Algorithm:
x
from 1 to z
(since f
is monotonically increasing, if x
exceeds z
, it's impossible to find a y
that satisfies the equation).x
, we perform a binary search on the interval [1, z]
to find a y
such that f(x, y) == z
. The monotonicity of f
guarantees that binary search will work efficiently.Time Complexity: O(z log z). The outer loop iterates z
times, and the binary search takes O(log z) time for each iteration.
Space Complexity: O(1). We only use a constant amount of extra space.
This solution leverages the monotonicity of f
to efficiently explore the solution space using two pointers.
Algorithm:
x = 1
and y = 1000
(the maximum possible value of y).f(x, y)
with z
:
f(x, y) < z
, increment x
(since increasing x
will increase the result of f
).f(x, y) > z
, decrement y
(since decreasing y
will decrease the result of f
).f(x, y) == z
, add (x, y)
to the result list, then increment x
and decrement y
to continue searching.x
exceeds 1000 or y
becomes 0.Time Complexity: O(z). In the worst case, we might iterate through all possible values of x and y, but this is linear in the range of possible values.
Space Complexity: O(1). We only use a constant amount of extra space.
Solution 1 (Enumeration + Binary Search):
import bisect
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
ans = []
for x in range(1, z + 1):
y = 1 + bisect_left(range(1, z + 1), z, key=lambda y: customfunction.f(x, y))
if customfunction.f(x, y) == z:
ans.append([x, y])
return ans
Solution 2 (Two Pointers):
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
ans = []
x, y = 1, 1000
while x <= 1000 and y > 0:
t = customfunction.f(x, y)
if t < z:
x += 1
elif t > z:
y -= 1
else:
ans.append([x, y])
x += 1
y -= 1
return ans
The Two Pointers solution is generally preferred due to its better average-case time complexity, although in the worst case they can perform similarly. Both solutions demonstrate how to effectively utilize the monotonicity property of the unknown function. Remember that you'll need a CustomFunction
class definition (provided by LeetCode) to run these code snippets.