forked from Rishabh062/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Rishabh062#13 from gayatri517/gayatri517_b2
flip bits code added in C++ folder
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |