-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContinuousOnes.java
39 lines (32 loc) · 1.02 KB
/
ContinuousOnes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* https://www.techiedelight.com/find-index-0-replaced-get-maximum-length-sequence-of-continuous-ones/
*/
class ContinuousOnes {
public static int findIndexOfZero(int[] array) {
if (array == null) {
throw new IllegalArgumentException();
}
int maxCount = 0;
int maxIndex = -1;
int currentCount = 0;
int previousIndex = -1;
for (int i = 0; i < array.length; ++i) {
if (array[i] == 1) {
currentCount++;
} else {
currentCount = i - previousIndex;
previousIndex = i;
}
if (currentCount > maxCount) {
maxCount = currentCount;
maxIndex = previousIndex;
}
}
return maxIndex;
}
public static void main(String[] args) {
int[] array = {0, 0, 1, 0, 1, 1, 1, 0, 1, 1};
int index = ContinuousOnes.findIndexOfZero(array);
System.out.println("Index to be replaces : " + index);
}
}