/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { { List<String> initialCollection = new ArrayList<String>(); for(int i = 0; i < 31; i++) { initialCollection.add("" + i); } List<List<String>> resultCollections = new ArrayList<>(); int remainder = initialCollection.size() % 5; int minimumCollectionSize = initialCollection.size() / 5; for (int i = 0; i < 5; i++) { int startIndex = i * minimumCollectionSize; int endIndex = (i + 1) * minimumCollectionSize; if (i < remainder) { startIndex += i; endIndex += i + 1; } else { startIndex += remainder; endIndex += remainder; } //System.out.println(startIndex + "/" + endIndex); List<String> collectionPart = new ArrayList<>(initialCollection.subList(startIndex, endIndex)); resultCollections.add(collectionPart); } } }
Standard input is empty
7/ [0, 1, 2, 3, 4, 5, 6] 6/ [7, 8, 9, 10, 11, 12] 6/ [13, 14, 15, 16, 17, 18] 6/ [19, 20, 21, 22, 23, 24] 6/ [25, 26, 27, 28, 29, 30] [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30]]