Minimize cost to make X and Y equal by given increments | Java program for the above approach ; Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
import java . util . * ; class GFG { static int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } static int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } public static void main ( String [ ] args ) { int x = 5 , y = 17 ; System . out . print ( minimumCost ( x , y ) + "NEW_LINE"); } }