Grouping is Good (Code Overflow 1.2)

Question Link:-CodeChef: Practical coding for everyone
I checked the other solutions i didnt got what they are doing .can some one please help the solution for it.