Minimize the cost of buying the Objects | Java implementation of the approach ; Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
class GFG { static int totalPay ( int totalItems , int priceOfOneItem , int N , int M ) { int freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; int amount = actual * priceOfOneItem ; return amount ; } public static void main ( String [ ] args ) { int T = 12 , P = 8 ; int N = 2 , M = 1 ; System . out . print ( " Amount ▁ = ▁ " + totalPay ( T , P , N , M ) ) ; } }