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

1. Member
Join Date
Mar 2010
Posts
18
Rep Power
0

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

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.

Map<String, List<String>> boxBallMap = new LinkedHashMap<String, List<String>>();

Data in the above map is like this -

Java Code:
```	{box1=[blue, red, orange]}
{box2=[blue, red]}
{box3=[blue, red, orange]}```
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 -

Java Code:
```	[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}]```
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.

Java Code:
`{box1=blue, box2=blue, box3=orange}`
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? bcoz 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 -

Java Code:
```	List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue");
List<String> balls3 = Arrays.asList("red", "blue", "orange");

Map<String, List<String>> maps = new LinkedHashMap<String, List<String>>();
maps.put("box1", balls1);
maps.put("box2", balls2);
maps.put("box3", balls3);

List<Map<String, String>> mappingss = generateMappings(maps);
System.out.println(mappingss);```

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

Java Code:
```    private static List<Map<String, String>> generateMappings(Map<String, List<String>> map) {

return null;
}```
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)

2. ## Re: How to generate alternate color of balls combination for 3 boxes?

Originally Posted by raihan26
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)
Which part of that is giving you trouble? What have you tried so far?

3. Member
Join Date
Mar 2010
Posts
18
Rep Power
0

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

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

Java Code:
```public List<Map<String, String>> createMappings(List<String> boxes, List<String> balls) {
List<Map<String, String>> result = new ArrayList<Map<String, String>>();
for(int i = 0; i < balls.size(); i++) {
Map<String, String> row = new HashMap<String,String>();
for(int j = 0; j < boxes.size(); j++) {
String box = boxes.get(j);
int ballIndex = (j + i) % balls.size();
String ball = balls.get(ballIndex);
row.put(box, ball);
}
}
return result;
}```