import java.util.Random;
public class Main {
public static void printIntegerArray(int[] arr) {
if (arr == null)
for (int i = 0; i < arr.length; i++) {
System.
out.
printf("%2d", arr
[i
]); }
}
public static int[] generateRandomIntegerArray
(Random random,
int size,
int minValue, int maxValue) {
if (random == null || size <= 0 || maxValue < minValue)
return null;
int delta = maxValue - minValue + 1;
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = random.nextInt(delta) + minValue;
}
return arr;
}
public static void printMaxLongZeroGroup(int[] arr) {
if (arr == null)
int maxLength = 0, startMaxIndex = -1;
int counter = 0, startIndex = 0;
boolean isZeroStarted = false;
for (int i = 0; i < arr.length; i++) {
if (isZeroStarted) {
if (arr[i] == 0) {
counter++;
} else {
isZeroStarted = false;
if (counter > maxLength) {
maxLength = counter;
startMaxIndex = startIndex;
}
}
} else {
if (arr[i] == 0) {
isZeroStarted = true;
startIndex = i;
counter = 0;
}
}
}
if (counter > maxLength) {
maxLength = counter;
startMaxIndex = startIndex;
}
if (maxLength > 0) {
int endMaxIndex = startMaxIndex + maxLength;
System.
out.
printf("Max zero sequence length: %d, indexes: %d - %d\n",
maxLength, startMaxIndex, endMaxIndex);
} else {
System.
out.
println("No zero sequences."); }
}
public static void main
(String[] args
) { for (int i = 0; i < 7; i++) {
int[] a = generateRandomIntegerArray(random, 39, 0, 1);
printIntegerArray(a);
printMaxLongZeroGroup(a);
}
}
}
aW1wb3J0IGphdmEudXRpbC5SYW5kb207CgpwdWJsaWMgY2xhc3MgTWFpbiB7CglwdWJsaWMgc3RhdGljIHZvaWQgcHJpbnRJbnRlZ2VyQXJyYXkoaW50W10gYXJyKSB7CgkJaWYgKGFyciA9PSBudWxsKQoJCQl0aHJvdyBuZXcgTnVsbFBvaW50ZXJFeGNlcHRpb24oKTsKCQlmb3IgKGludCBpID0gMDsgaSA8IGFyci5sZW5ndGg7IGkrKykgewoJCQlTeXN0ZW0ub3V0LnByaW50ZigiJTJkIiwgYXJyW2ldKTsKCQl9CgkJU3lzdGVtLm91dC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0 0 0 0 1 0 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0
Max zero sequence length: 4, indexes: 13 - 17
1 1 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 0 0 1 0 1 1 0 1 1 0 0 0
Max zero sequence length: 2, indexes: 9 - 11
0 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 0 0 0
Max zero sequence length: 2, indexes: 5 - 7
0 1 1 1 1 1 1 1 0 0 0 1 0 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0
Max zero sequence length: 4, indexes: 23 - 27
1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 0 0 0 1 1 1 1 0 0 1 0 0
Max zero sequence length: 4, indexes: 25 - 29
1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0
Max zero sequence length: 5, indexes: 33 - 38