-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxConsecutiveOnes.java
More file actions
33 lines (27 loc) · 875 Bytes
/
maxConsecutiveOnes.java
File metadata and controls
33 lines (27 loc) · 875 Bytes
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
import java.util.Scanner;
public class maxConsecutiveOnes {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number");
char number[] = in.next().toCharArray();
System.out.println("Enter the number of flips: ");
int flips = in.nextInt();
int maxOnes = 0;
int replacements = 0;
int start = 0;
for (int end = 0; end < number.length; end++) {
if (number[end] == '0') {
replacements++;
}
while (replacements > flips) {
if (number[start] == '0') {
replacements--;
}
start++;
}
maxOnes = Math.max(maxOnes, end - start + 1);
}
System.out.println(maxOnes);
in.close();
}
}