Find minimum x such that ( x % k ) * ( x / k ) == n | Java 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
class Solution { static int minimumX ( int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = Math . min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } 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 ) ) ; } }