Given two integers N and K, the task is to find the smallest positive integer X satisfing the equation:
(X / K) * (X % K) = N
Examples:
Input: N = 6, K = 3
Output: 11
Explanation:
For X = 11, (11 / 3) * (11 % 3) = 3 * 2 = 6
Therefore, the following equation satisfies.Input: N = 4, K = 6
Output: 10
Explanation:
For X = 10, (10 / 6) * (10 % 6) = 1 * 4 = 4
Therefore, the following equation satisfies.
Approach:
The idea is to observe that, since (X / K) * (X % K) = N, therefore, N will be divisible by p = X % K, which is less than K. Therefore, for all i in the range [1, K) try all values of p where:
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find out the smallest // positive integer for the equation int findMinSoln(int n, int k) { // Stores the minimum int minSoln = INT_MAX; // Iterate till K for (int i = 1; i < k; i++) { // Check if n is divisible by i if (n % i == 0) minSoln = min(minSoln, (n / i) * k + i); } // Return the answer return minSoln; } // Driver Code int main() { int n = 4, k = 6; cout << findMinSoln(n, k); } |
Java
// Java Program to implement // the above approach import java.util.*; class GFG{ // Function to find out the smallest // positive integer for the equation static int findMinSoln(int n, int k) { // Stores the minimum int minSoln = Integer.MAX_VALUE; // Iterate till K for (int i = 1; i < k; i++) { // Check if n is divisible by i if (n % i == 0) minSoln = Math.min(minSoln, (n / i) * k + i); } // Return the answer return minSoln; } // Driver Code public static void main(String[] args) { int n = 4, k = 6; System.out.println(findMinSoln(n, k)); } } // This code is contributed by Ritik Bansal |
Python3
# Python3 program to implement # the above approach import sys # Function to find out the smallest # positive integer for the equation def findMinSoln(n, k): # Stores the minimum minSoln = sys.maxsize; # Iterate till K for i in range(1, k): # Check if n is divisible by i if (n % i == 0): minSoln = min(minSoln, (n // i) * k + i); # Return the answer return minSoln; # Driver Code if __name__ == '__main__': n = 4; k = 6; print(findMinSoln(n, k)); # This code is contributed by amal kumar choubey |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find out the smallest // positive integer for the equation static int findMinSoln(int n, int k) { // Stores the minimum int minSoln = int.MaxValue; // Iterate till K for (int i = 1; i < k; i++) { // Check if n is divisible by i if (n % i == 0) minSoln = Math.Min(minSoln, (n / i) * k + i); } // Return the answer return minSoln; } // Driver Code public static void Main(String[] args) { int n = 4, k = 6; Console.WriteLine(findMinSoln(n, k)); } } // This code is contributed by amal kumar choubey |
10
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Only integer with positive value in positive negative value in array
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count possible permutations of given array satisfying the given conditions
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Check if elements of an array can be arranged satisfying the given condition
- Count valid pairs in the array satisfying given conditions
- Generate N integers satisfying the given conditions
- Count of pairs satisfying the given condition
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Split N as the sum of K numbers satisfying the given conditions
- Maximum possible sum of squares of stack elements satisfying the given properties
- Find the smallest positive number which can not be represented by given digits
- Print first k digits of 1/n where n is a positive integer
- Minimum positive integer to divide a number such that the result is an odd
- Minimum positive integer required to split the array equally
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

