Skip to content

Commit

Permalink
Merge pull request Rishabh062#13 from gayatri517/gayatri517_b2
Browse files Browse the repository at this point in the history
flip bits code added in C++ folder
  • Loading branch information
Rishabh062 authored Oct 1, 2021
2 parents c96bea1 + 4518b08 commit 4da6685
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions C++/flip_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*Problem Statement: You are given an array of integers ARR[] of size N consisting of zeros and ones. You have to select a subset and flip bits of that subset.
You have to return the count of maximum one’s that you can obtain by flipping chosen sub-array at most once.
A flip operation is one in which you turn 1 into 0 and 0 into 1.
Time Complexity : O(n)
*/

#include<bits/stdc++.h>
using namespace std;
int flipBits(vector<int> v, int n)
{
int ones=0;
for(int i=0;i<n;i++)
{
if(v[i]==1)
{
ones++;
}
}

for(int i=0;i<n;i++)
{
if(v[i]==1)
{
v[i]=-1;
}
else
{
v[i]=1;
}
}
int curr=0,max_sum=0;
for(int i=0;i<n;i++)
{
curr+=v[i];
max_sum = max(curr, max_sum);
if(curr<0)
{
curr=0;
}
}
return max_sum+ones;
}
int main()
{
int test;
cout<<"Enter total testcases: "<<endl;
cin>>test;
while(test--)
{
int n;
cout<<"Enter total: "<<endl;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++)
{
int t;
cin>>t;
v.push_back(t);
}
cout<<flipBits(v,n)<<endl;
}
return 0;
}

0 comments on commit 4da6685

Please sign in to comment.