-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMaxCont1s.java
53 lines (41 loc) · 1.11 KB
/
MaxCont1s.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package TwoPointers;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 16/10/18
* Time - 12:21 PM
*/
public class MaxCont1s {
public ArrayList<Integer> maxone(ArrayList<Integer> A, int B) {
int wL = 0, wR = 0;
int bestL = 0, bestR = 0;
int zeroCount= 0;
while(wR < A.size()){
if(zeroCount <= B){
if(A.get(wR).equals(0)){
zeroCount++;
}
wR++;
}
if(zeroCount > B){
if(A.get(wL).equals(0)){
zeroCount--;
}
wL++;
}
if((wR-wL) > (bestR-bestL)){
bestL = wL;
bestR = wR;
}
}
ArrayList<Integer> r = new ArrayList<>();
for(int i=bestL;i<bestR;i++){
r.add(i);
}
return r;
}
public static void main(String[] args) {
System.out.println(new MaxCont1s().maxone(new ArrayList<>(Arrays.asList(1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0)), 4));
}
}