import java.util.*; import java.lang.*; import java.io.*; 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 totalSize = initialCollection.size(); int parts = 5; int baseSize = totalSize / parts; int remainder = totalSize % parts; int startIndex = 0; for (int i = 0; i < parts; i++) { int endIndex = startIndex + baseSize + (i < remainder ? 1 : 0); List<String> part = new ArrayList<>(initialCollection.subList(startIndex, endIndex)); resultCollections.add(part); startIndex = endIndex; } // Print the results for (List<String> part : resultCollections) { } } }
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]