-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlip Bits.java
More file actions
26 lines (25 loc) · 856 Bytes
/
Flip Bits.java
File metadata and controls
26 lines (25 loc) · 856 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
//https://practice.geeksforgeeks.org/problems/flip-bits0240/1
class Solution {
public static int maxOnes(int a[], int n) {
// Your code goes here
int maxCount=0;
int count = 0;
int countOne = 0;
for(int i=0;i<n;i++){
if(a[i]==1){
countOne++; //count number of 1's
}
if(a[i]==0){
count++; //count of flips required
}else{
count--; //if element is 1 then it should be zero and not to be counted in the flip's count or in the result
}
if(count>0 && count > maxCount){
maxCount = count;//update if count is maximum
}else if(count<0){
count = 0;// if negative count then discard
}
}
return maxCount + countOne;
}
}