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.
Organizing Containers of Balls
Organizing Containers of Balls
Sort by
recency
|
626 Discussions
|
Please Login in order to post a comment
Here is problem solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-organizing-containers-of-balls-problem-solution.html
Python code(Easy)
def organizingContainers(container): # Write your code here n = len(container) row_sums = [sum(row) for row in container] col_sums = [sum(container[i][j] for i in range(n)) for j in range(n)] if sorted(row_sums) == sorted(col_sums): return "Possible" else: return "Impossible"
Solution in C#, first thing I did was realize that there is very little sorting necessary in this problem. What we have to do is find the sums of the x rows and the sums of the y columns. Once we've done that we sort both arrays and compare the arrays to ensure they match 1-to-1. If they do not, they are impossible to sort otherwise they are possible. I left my debugging code for brevity.
Essentially this swap sort method only works if each bucket has enough storage in it to hold the maximum count of a given item. (i.e. if there are two items of type A, there must be a corresponding bucket with two slots) this must be true for each item.
c++ solution:
here is my solution