Find minimum x such that ( x % k ) * ( x / k ) == n | This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver code to test above function
function minimumX ( n , k ) { let ans = Number . MAX_VALUE ; for ( let rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = Math . min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } let n = 4 , k = 6 ; document . write ( minimumX ( n , k ) + " " ) ; n = 5 , k = 5 ; document . write ( minimumX ( n , k ) ) ;