-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlip.java
More file actions
47 lines (37 loc) · 1.1 KB
/
Flip.java
File metadata and controls
47 lines (37 loc) · 1.1 KB
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
import java.util.ArrayList;
public class Flip {
public static int[] flip(String A) {
int l = A.length();
int emp[] = new int[0];
int ans[] = new int[2];
int left=0, right=0;
int currSum=0,max=0;
for(int i=0; i<l; i++){
if(A.charAt(i) == '1'){
currSum = currSum-1;
}
if(A.charAt(i) == '0')
currSum = currSum + 1;
if(currSum > max){
max = currSum;
right=i;
ans[0] = left+1;
ans[1] =right+1;
}
if(currSum < 0){
currSum = 0;
left =i+1;
}
}
if(max == 0)
return emp;
return ans;
}
public static void main(String args[]){
int a[]={};
String s = "0011101";
a=flip(s);
for(int i=0; i<a.length; i++)
System.out.print(a[i]+" ");
}
}