Find Positive Integer Solution for a Given Equation - Math - Easy - LeetCode
💻 coding

Find Positive Integer Solution for a Given Equation - Math - Easy - LeetCode

2 min read 367 words
2 min read
ShareWhatsAppPost on X
  • 1The function f(x, y) is strictly increasing in both x and y, ensuring unique positive integer solutions.
  • 2The algorithm iteratively adjusts x and y to find all pairs where f(x, y) equals the target z.
  • 3The solution guarantees that all pairs are within the range of 1 to 1000 for both x and y.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The function f(x, y) is strictly increasing in both x and y, ensuring unique positive integer solutions."

Find Positive Integer Solution for a Given Equation - Math - Easy - LeetCode

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.:

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 positive integer f(x, y) for any given positive integer x and y. int f(int x, int y); }; For custom testing purposes you're given an integer function_id and a target z as input, where function_id represents one function from a 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:

Input: function_id = 1, z = 5 Output: [[1,4],[2,3],[3,2],[4,1]] Explanation: function_id = 1 means that f(x, y) = x + y Example 2:

Input: function_id = 2, z = 5 Output: [[1,5],[5,1]] Explanation: function_id = 2 means that f(x, y) = x * y

Constraints:

1 <= function_id <= 9 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

/*
 * // This is the custom function interface.
 * // You should not implement it, or speculate about its implementation
 * public class CustomFunction {
 * // Returns f(x, y) for any given positive integers x and y.
 * // Note that f(x, y) is increasing with respect to both x and y.
 * // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
 * public int f(int x, int y);
 * };
 */

public class Solution {
 public IList<IList<int>> FindSolution(CustomFunction customfunction, int z) {
 var res = new List<IList<int>>();
 int x = 1;
 int y = 1000;
 while (x <= 1000 && y > 0) {
 int v = customfunction.f(x, y);
 if (v > z){
 --y; 
 }
 else if (v < z){
 ++x; 
 }
 else{
 var arr = new List<int>();
 arr.Add(x++);
 arr.Add(y--);
 res.Add(arr);
 }
 }
 return res;
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 2 October 2020 · 2 min read · 367 words

Part of AskGif Blog · coding

You might also like

Find Positive Integer Solution for a Given Equation - Math - Easy - LeetCode | AskGif Blog