1 test case is not passing please tell mistake.
Https://ide.codingblocks.com/s/215490
Given an array of size n with 0s and 1s , flip at most k 0s to get the longest possible subarray of 1s.
Input Format
First Line : n, size of array and k Second Line : n space separated numbers (0s or 1s)
Constraints
n <= 10^5 0 <= k <= n
@Adarshrajpandey
I tried your code
#include <iostream>
using namespace std;
int maxLenSubarray(int* &arr,int n,int k)
{
int l=0,r=0;
int maxLen=0,zero_count=0;
int ml=0,mr=0; //max length left and right
while(r<n)
{
if(arr[r]==1)
r++;
else {
zero_count++;
r++;
while(zero_count>k)
{
if(arr[l]==0)
zero_count--;
l++;
}
}
if(maxLen < (r-l) )
{
maxLen=r-l;
ml=l;
mr=r;
}
}
for(int i=ml;i<ml+maxLen;i++)
{
arr[i]=1;
}
return maxLen;
}
int main() {
int n,k;
cin>>n>>k;
int *arr=new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int len=maxLenSubarray(arr,n,k);
cout<<len<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
This passes all test cases.
I hope this resolves your doubt
it is different from what u have sent
sir i have written that code by seeing someone’s other code to check why my code was failing
I have asked doubt on the link that i have given
Your code also seems to be correct, it passes 4 test cases out of 5. I checked the out put for which it is giving WA, in that case also your output seems to match with the correct sol.
bhaiya, that’s why i checked it using others code, because this had happend with me 2 times before also.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.