I am unable to understand this editorial from codemonk series.
Please explain in detail with an example.
Thank you.
I am unable to understand this editorial from codemonk series.
Please explain in detail with an example.
Thank you.
I didn’t read the editorial.
But I will share my solution.
If for each i from 0 to (n-k) the sum is <=X,
Else
long long n,x;
cin>>n>>x;
long long p[n]={0};
for(int i=0;i<n;i++)
{
long long temp;
cin>>temp;
if(i==0)
p[0]=temp;
else p[i]+=p[i-1]+temp;
}
long long l=1,r=n,mid=-1;
long long ans=-1;
while(l<=r)
{
mid=(l+r)>>1;
bool possible=true;
for(int i=0;i<=(n-mid);i++)
{
long long sum=(i>0)?(p[i+mid-1]-p[i-1]):(p[i+mid-1]);
if(sum>x)
{
possible=false;
break;
}
}
if(possible)
{
ans=mid;
l=mid+1;
}
else
{
r=mid-1;
}
}
cout<<ans<<endl;
Total time complexity: O(nlgn)
If you still have any doubt, feel free to ask.
Thank you so much. I was stuck on this problem for a long time. Now it makes a lot more sense.