-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximize_Number_of_1s.cpp
52 lines (44 loc) · 998 Bytes
/
Maximize_Number_of_1s.cpp
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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
// m is maximum of number zeroes allowed to flip
// n is size of array
int findZeroes(int arr[], int n, int m) {
int s=0,e=0;
int zeroCount=0;
int mx=0;
for(int e=0;e<n;e++){
if(arr[e]==0)
zeroCount++;
if(zeroCount>m){
if(arr[s]==0)
zeroCount--;
s++;
}
mx = max(mx,e-s+1);
}
return mx;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, i, m;
cin >> n;
int arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
}
cin >> m;
Solution ob;
auto ans = ob.findZeroes(arr, n, m);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends