Given a sequence of binary numbers (0, 1) of size ‘n’ in which every 0 represent a decrement to the sum and every 1 represents the increment to the sum. You have to find the minimum possible value of ‘k’ such that the sum of the first ‘k’ elements is greater than the sum of remaining elements.
Answers
Answered by
6
I don't know about this question tested
Answered by
2
Answer:
vector<int>vec={1,0,0,1,0,0,1};
int count1=0,count0=0,lsum=0,rsum=0;
for(int i=0;i<vec.size();i++)
{
if(vec[i]==1)count1++;
else count0++;
}
if(count1-count0<0){cout<<0;return 0;}
for(int i=0;i<vec.size();i++)
{
if(vec[i]==1)
{
lsum+=1;
count1--;
rsum=-1*count0+count1;
}
else
{
lsum-=1;
count0--;
rsum=-1*count0+count1;
}
if(lsum>rsum)
{
cout<< i+1;
}
}
Explanation:
Similar questions