Apply "buy X get Y" discount on N products

1k Views Asked by At

Consider a product is added to a cart with quantity of N and "Buy X and get y" offer is applied i.e. Buy two get one free.

If n = 3 then I could simply apply a discount rate of 33.33 on each product. But my problem occurs when there are more than 3.

If n = 4 then number of free products would still be 1 and I would have to pay full price for 4th product, but if I apply the above method I would be applying discount on the 4th product as well which should not be the case.

This continues for n = 4,5,6,7,8,9,.....

I need a formula to correctly calculate the price with the scheme applied.

Thank you.

Edit: Edited the question as it was not clear. Sorry.

X would be the number of products that needs to be purchased for the offer to apply and N would be the number of products that are actually in the cart.

1

There are 1 best solutions below

5
On BEST ANSWER

The simple answer is if one orders $n$ of the product you get $\lfloor \frac n3 \rfloor$ free and pay for $n-\lfloor \frac n3 \rfloor$