• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to generate alternate color of balls combination for 3 boxes?  RSS feed

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project in which I have three box (as of now) and each box will have some color of balls

So I am storing them in a Map of String and List of String as mention below.



Data in the above map is like this -



**ProblemStatement:-**

Basis on the above input, I need to return a mapping which will be List<Map<String, String>>, let's say for above input, below mapping would be return as an output -



Here if you see, each row has alternate color of balls for each box - meaning blue for box1, red for box2, orange for box3 in first row. I cannot have same color of balls in each row. So this combination is not possible as it has same color of balls for two boxes in one row.



And also, in the second row, I won't use those balls which have been used in the first row for that box. In second row, box1 has red why? because blue was already used in the first row for box1 and box3 has blue and no box2 in second row.

The output combination is getting generated basis on the input being passed as shown above.

I started with the below code -



Below is my method in which the crux of logic should be there


Below algorithm might work but still not sure how should I fit this in the code -

- sort the boxes by the number of balls they have in it (ascending, from the smallest to the largest box).
- while there are colors left
- loop over the sorted list of boxes
- in each iteration pick a color from the box (if there is one left), that is not already picked in the current iteration (of the while loop)


This is what I have tried but this only works for when number of balls is same in each box-


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!