Given two positive integers n and k. Find minimum positive integer x such that the (x % k) * (x / k) == n, where % is the modulus operator and / is the integer division operator.
Input : n = 4, k = 6
Explanation : (10 % 6) * (10 / 6) = (4) * (1) = 4 which is equal to n
Input : n = 5, k = 5
Output : 26
Approach: The problem has been solved using the value of K in the Set-1. In this article, we will use the factor method to solve the above problem. Given below are the steps to solve the above problem.
- Since the equation is of the form a*b = N, so a and b will be the factors of N.
- Iterate from 1 to sqrt(N) to get all the factors.
- The factors i and n/i can be either of A or B.
- If i is A and n/i is B then the number will be i*k + (n/i). We can check if this number is the one by comparing it with N, if it is then it is a number which satisfies the equation.
- If i is B and n/i is A then the number will be (n/i)*k + (i). We can check if this number is the one by comparing it with N, if it is then it is a number which satisfies the equation.
- Obtain all the numbers which satisfies the equation and print the smallest among them.
Below is the implementation of the above approach.
Time Complexity : O(sqrt(N)), where N is the given positive integer.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Divide two integers without using multiplication, division and mod operator | Set2
- Find minimum x such that (x % k) * (x / k) == n
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find the minimum value of X for an expression
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find minimum GCD of all pairs in an array
- Find minimum sum of factors of number
- Find two numbers with the given LCM and minimum possible difference
- Find the minimum permutation of A greater than B
- Find the subarray of size K with minimum XOR
- Find the maximum possible value of the minimum value of modified array
- Find the minimum value of the given expression over all pairs of the array
- Find the minimum possible health of the winning player
- Find the minimum cost to cross the River
- Find the minimum number possible by changing at most one digit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.