Skip to content

Instantly share code, notes, and snippets.

@jonsie
Created March 9, 2025 20:30
Show Gist options
  • Select an option

  • Save jonsie/a10fdbc1adff15321ab0fab1e2065115 to your computer and use it in GitHub Desktop.

Select an option

Save jonsie/a10fdbc1adff15321ab0fab1e2065115 to your computer and use it in GitHub Desktop.
binary list thingy
import java.util.ArrayList;
import java.util.List;
public class BinaryListProcessor {
public static List<Integer> processBinaryList(List<Integer> binaryList) {
List<Integer> addList = new ArrayList<>();
List<Integer> 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<Integer> 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<Integer> result = processBinaryList(binaryList);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment