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 Output :10 Explanation : (10 % 6) * (10 / 6) = (4) * (1) = 4 which is equal to n Input : n = 5, k = 5 Output : 26
Naive Solution : A simple approach is to run a while loop until we find a solution which satisfies the given equation, but this would be very slow.
Efficient Solution : The key idea here is to notice that the value of (x % k) lies in the range [1, k – 1]. (0 is not included, since we can’t divide n by (x % k) when it is zero). Now, we need to find the largest possible number in the range that divides n and hence the given equation becomes x = (n * k) / (x % k) + (x % k).
Note : (x % k) is added to the answer since for the current value of modulus (x % k), it must not be contradicting that on one hand x is such that the remainder upon dividing by k is (x % k) and on the other x is (n * k) / (x % k) whose remainder is simply zero when we divide this value by k.
Below is the implementation of the above approach.
Time Complexity : O(k), where k is the given positive integer.
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Find minimum sum of factors of number
- Find minimum area of rectangle with given set of coordinates
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum possible digit sum after adding a number d
- Find minimum speed to finish all Jobs
- Find the maximum possible value of the minimum value of modified array
- Find minimum number of coins that make a given value
- Find the minimum difference between Shifted tables of two numbers
- Find element using minimum segments in Seven Segment Display
- Find minimum number of Log value needed to calculate Log upto N
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Program to find minimum number of lectures to attend to maintain 75%
- Find minimum moves to reach target on an infinite line
- Find minimum operations needed to make an Array beautiful