Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 -


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-

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic