// https://leetcode.com/problems/binary-prefix-divisible-by-5/description/
public static List<Boolean> prefixesDivBy5(int[] nums) {
List<Boolean> result = new ArrayList<>();
long currNum = 0; // pay attention here, it should be long
for (int num : nums) {
currNum = currNum * 2 + num;
System.out.println(currNum);
result.add((currNum % 5) == 0);
}
return result;
}
Also
Binary Input as Array
If the binary is given as an array of integers (e.g., [1, 0, 1, 1]
):
public class BinaryArrayToInt {
public static void main(String[] args) {
int[] binary = {1, 0, 1, 1};
int result = 0;
for (int bit : binary) {
result = result * 2 + bit;
}
System.out.println("The integer value is: " + result);
}
}