Created
February 16, 2020 08:17
-
-
Save jatinsharrma/b5b40f80271cd433d10eb7a0ee4819c0 to your computer and use it in GitHub Desktop.
Organizing Containers of Balls (Hackerrank)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#For question refer : https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem | |
''' logic | |
Suppose there are two container A and B and there are two type of balls in each container (qunatity may varry) | |
Type0 Type 1 | |
A contains [ 1 , 1 ] | |
B contains [ 1 , 1 ] | |
First i am calculating total no of balls of each type. | |
In above example Type0 balls are 2 and Type1 balls are 2 | |
Then i am picking up each type balls one by one and checking if any container can accomodate that quantity of balls (note: i am not adding extra balls to any of the container by this i am trying to satisfy swap property). | |
How i am checking that some contianer can accomadate balls? I'll explain | |
There are 2 balls of type0 in total. In container A there is one ball of Type0, subtract it from total number of balls. Difference will yield 1 | |
Now count total number of balls in contaner A and subtract Type0 balls from it. This difference will yield 1 | |
If both the difference yields same value then that container can accomodate that type of balls. | |
Similarly i am find container for each type of balls | |
''' | |
def organizingContainers(container): | |
count = [0]*len(container[0]) | |
for i in container: | |
temp = 0 | |
for j in i: | |
count[temp]+=j | |
temp+=1 | |
temp = 0 | |
for i in count: | |
flag = False | |
for j in container: | |
color = i - j[temp] | |
left = sum(j)-j[temp] | |
if color == left: | |
flag = True | |
if flag == False: | |
return "Impossible" | |
return "Possible" | |
print(organizingContainers([[1,1],[1,1]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment