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.
This is fairly simple when you think it through. You don't actually go through each swap, as it only requires whether it's possible or not. The working logic is below:
1.The balls in the containers are not moved around, but just swapped. This means, the number of balls in each container will always remain the same.
2.Also, each container must only have balls with the same number, the numbers of balls of each type must match the number of balls in containers. For example, if there are 3 containers containing 3, 6, 9 balls, then there must be 3 balls of one type, 6 balls of another type and 9 balls of the other type.
3.As a result, when you work out the numbers of balls in each container and the numbers of balls of each type, these two numbers must match to be "Possible". Otherwise, it is "Impossible"
My working python 3 solution is below:
# The balls can only be swapped, meaning the number of balls in each container will remain unchanged.# Therefore, the numbers of balls in containers must be aligend with the numbers of balls of each type# e.g. if there are 3 containers with 3, 6, 9 balls, then # types of balls must be 3, 6, 9 (does not have to be ordered)q=int(input().strip())fora0inrange(q):n=int(input().strip())M=[]forM_iinrange(n):M_t=[int(M_temp)forM_tempininput().strip().split(' ')]M.append(M_t)rowSums=[]colSums=[]# Get sums of each row (number of balls in each container)foriinrange(n):rowSums.append(sum(M[i]))# Get sums of each column (number of balls of each type)foriinrange(n):colSum=0forjinrange(n):colSum+=M[j][i]colSums.append(colSum)# if sorted sums are the same, print Possible# otherwise, print Impossibleifsorted(rowSums)==sorted(colSums):print("Possible")else:print("Impossible")
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →
This is fairly simple when you think it through. You don't actually go through each swap, as it only requires whether it's possible or not. The working logic is below:
1.The balls in the containers are not moved around, but just swapped. This means, the number of balls in each container will always remain the same.
2.Also, each container must only have balls with the same number, the numbers of balls of each type must match the number of balls in containers. For example, if there are 3 containers containing 3, 6, 9 balls, then there must be 3 balls of one type, 6 balls of another type and 9 balls of the other type.
3.As a result, when you work out the numbers of balls in each container and the numbers of balls of each type, these two numbers must match to be "Possible". Otherwise, it is "Impossible"
My working python 3 solution is below: