We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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'
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all 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'