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.

**Examples:**

Input :n = 4, k = 6Output :10Explanation :(10 % 6) * (10 / 6) = (4) * (1) = 4 which is equal to nInput :n = 5, k = 5Output :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.

## C++

`// CPP Program to find the minimum ` `// positive X such that the given ` `// equation holds true ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// This function gives the required ` `// answer ` `int` `minimumX(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `ans = INT_MAX; ` ` ` ` ` `// Iterate over all possible ` ` ` `// remainders ` ` ` `for` `(` `int` `rem = k - 1; rem > 0; rem--) { ` ` ` ` ` `// it must divide n ` ` ` `if` `(n % rem == 0) ` ` ` `ans = min(ans, rem + (n / rem) * k); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 6; ` ` ` `cout << minimumX(n, k) << endl; ` ` ` ` ` `n = 5, k = 5; ` ` ` `cout << minimumX(n, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the minimum ` `// positive X such that the given ` `// equation holds true ` `class` `Solution ` `{ ` `// This function gives the required ` `// answer ` `static` `int` `minimumX(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `ans =Integer.MAX_VALUE; ` ` ` ` ` `// Iterate over all possible ` ` ` `// remainders ` ` ` `for` `(` `int` `rem = k - ` `1` `; rem > ` `0` `; rem--) { ` ` ` ` ` `// it must divide n ` ` ` `if` `(n % rem == ` `0` `) ` ` ` `ans = Math.min(ans, rem + (n / rem) * k); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code to test above function ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `4` `, k = ` `6` `; ` ` ` `System.out.println( minimumX(n, k)); ` ` ` ` ` `n = ` `5` `; k = ` `5` `; ` ` ` `System.out.println(minimumX(n, k)); ` ` ` `} ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the minimum positive ` `# x such that the given equation holds true ` ` ` `# This function gives the required answer ` `def` `minimumX(n, k): ` ` ` ` ` ` ` `ans ` `=` `10` `*` `*` `18` ` ` ` ` `# Iterate over all possible remainders ` ` ` `for` `i ` `in` `range` `(k ` `-` `1` `, ` `0` `, ` `-` `1` `): ` ` ` `if` `n ` `%` `i ` `=` `=` `0` `: ` ` ` `ans ` `=` `min` `(ans, i ` `+` `(n ` `/` `i) ` `*` `k) ` ` ` `return` `ans ` ` ` `# Driver Code ` `n, k ` `=` `4` `, ` `6` ` ` `print` `(minimumX(n, k)) ` ` ` `n, k ` `=` `5` `, ` `5` ` ` `print` `(minimumX(n, k)) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the minimum ` `// positive X such that the given ` `// equation holds true ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// This function gives the required ` `// answer ` `static` `int` `minimumX(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `ans =` `int` `.MaxValue; ` ` ` ` ` `// Iterate over all possible ` ` ` `// remainders ` ` ` `for` `(` `int` `rem = k - 1; rem > 0; rem--) { ` ` ` ` ` `// it must divide n ` ` ` `if` `(n % rem == 0) ` ` ` `ans = Math.Min(ans, rem + (n / rem) * k); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code to test above function ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `n = 4, k = 6; ` ` ` `Console.WriteLine( minimumX(n, k)); ` ` ` ` ` `n = 5; k = 5; ` ` ` `Console.WriteLine(minimumX(n, k)); ` ` ` `} ` `} ` `//This code is contributed by Sachin. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the minimum ` `// positive X such that the given ` `// equation holds true ` ` ` `// This function gives the required ` `// answer ` `function` `minimumX(` `$n` `, ` `$k` `) ` `{ ` ` ` `$ans` `= PHP_INT_MAX; ` ` ` ` ` `// Iterate over all possible ` ` ` `// remainders ` ` ` `for` `(` `$rem` `= ` `$k` `- 1; ` `$rem` `> 0; ` `$rem` `--) ` ` ` `{ ` ` ` ` ` `// it must divide n ` ` ` `if` `(` `$n` `% ` `$rem` `== 0) ` ` ` `$ans` `= min(` `$ans` `, ` `$rem` `+ ` ` ` `(` `$n` `/ ` `$rem` `) * ` `$k` `); ` ` ` `} ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` `$n` `= 4 ; ` `$k` `= 6 ; ` ` ` `echo` `minimumX(` `$n` `, ` `$k` `), ` `"\n"` `; ` ` ` `$n` `= 5 ; ` `$k` `= 5 ; ` ` ` `echo` `minimumX(` `$n` `, ` `$k` `) ; ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10 26

**Time Complexity : ** O(k), where k is the given positive integer.

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:

- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k
- Find an index such that difference between product of elements before and after it is minimum
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Find minimum number K such that sum of array after multiplication by K exceed S
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum positive integer to divide a number such that the result is an odd
- Break a number such that sum of maximum divisors of all parts is minimum
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum value of N such that xor from 1 to N is equal to K
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Ways to form n/2 pairs such that difference of pairs is minimum
- Minimum K such that every substring of length atleast K contains a character c
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum value to be added to X such that it is at least Y percent of N
- Pick points from array such that minimum distance is maximized

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.