Skip to content

15august.cpp #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Hasheditz opened this issue Aug 15, 2023 · 0 comments
Closed

15august.cpp #1

Hasheditz opened this issue Aug 15, 2023 · 0 comments

Comments

@Hasheditz
Copy link
Owner

15-08-23

//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends

class Solution {
public:
int maxOnes(int a[], int n)
{
int zero = 0, one = 0, max_flips = 0;

    for (int i = 0; i < n; ++i) {
        if (a[i] == 0)
            zero++;
        else{
            zero--;
            one++;
        }

        if (zero < 0)
            zero = 0;

        max_flips = max(zero, max_flips);
    }
    return max_flips + one;
}

};

//{ Driver Code Starts.
int main()
{
int t; cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n+5];
for(int i=0;i<n;i++)
cin>>a[i];
Solution ob;
cout<< ob.maxOnes(a, n) <<endl;
}
return 0;
}

// } Driver Code Ends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant