Its not exactly clean and not sure if its what you are trying to acomplish, but I did a Script where you can set up the number of product to get 1 free. Like the BOGO Script from the examples. I think you could change the code to suit your needs. Hope it helps :P
If you have any question Ill be happy to help :D
This is great... except again it only loops 1 time over each line item. It doesn't loop over the line item... then the newly created line items and continue until there are no more possible groups of 3 or 2
As an example...