Minimize cost to make X and Y equal by given increments | C ++ 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
#include <iostream> NEW_LINE using namespace std ; int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } int main ( ) { int x = 5 , y = 17 ; cout << minimumCost ( x , y ) << endl ; }