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