import java.util.ArrayList; import java.util.List; public class BinaryListProcessor { public static List processBinaryList(List binaryList) { List addList = new ArrayList<>(); List resultList = new ArrayList<>(); for (int index = 0; index < binaryList.size(); index++) { int value = binaryList.get(index); // ignore the last index since we do a look ahead below, avoid out of bounds error if (index != binaryList.size() - 1) { addList.add(value); // check the next value if (binaryList.get(index + 1) == 1) { resultList.add(addList.size()); addList.remove(0); if (!addList.isEmpty()) { resultList.addAll(addList); addList.clear(); } } } } return resultList; } public static void main(String[] args) { List binaryList = new ArrayList<>(); // Example input binaryList.add(1); binaryList.add(1); binaryList.add(0); binaryList.add(0); binaryList.add(1); binaryList.add(0); binaryList.add(0); binaryList.add(0); binaryList.add(1); binaryList.add(0); binaryList.add(0); binaryList.add(0); binaryList.add(0); binaryList.add(1); binaryList.add(1); List result = processBinaryList(binaryList); System.out.println(result); } }