Organizing Containers of Balls

  • + 6 comments

    Only after re-reading my code, I realised that the variable M is not used at all(Edit-removing it now). LOL. Anyway array a[] stores the number of balls in each container. Array b[] stores the number of balls in each type. The number of balls in each container will be intact no matter how many swaps we do because of +1 -1. So all we need to check is that if there are any type of balls with the same quantity of that of each box. So after some swaps we can store all those balls in that container. Similarly for all containers. We should also note that once a ball kind is mapped to a container, we should not consider it again so I swapped it to the ith position and each time I am starting the inner loop from 'i'. In case no match is found, inner loop runs fully and j==n. So I break and print 'Impossible', else 'Possible'