CONDEL - Editorial

Actually I didn’t undertstand can you please elaborate . In my opinion my code is working fine under the given constraints . Can you project me any testcase where my code might be failing

What’s wrong in this code somebody help.
CodeChef: Practical coding for everyone

What he says is, you are using int. But the solution requires the usage of long. So, it is causing WA.

1 Like

It would be great if anyone helps me out with my submission. It is giving WA. I’m not able to figure it out.
https://www.codechef.com/viewsolution/44062991
Here’s the submission link

I even tried without the long long, but still got WA

Your code fails in first test-case :
1
5 3
1 0 1 0 1
Expected O/P :
3
Your O/P :
4

Hey, i am able to pass all the sample cases but still, it showing me wrong and can anyone plz tell me is there any special case I’m missing or there is any mistake in my logic?

#include
using namespace std;

int main() {
long long int t;
cin>>t;
while(t>0){
long long int n;cin>>n;long long int k;cin>>k; long long int arr[n]; long long int count=0;long long int b=0;long long int sum=0;long long int minsum=1000000;
for(long long int i=0;i<n;i++){
cin>>arr[i];
if(i==0)b=arr[0];
if(i<k){sum=sum+arr[i];}
else{

     if(sum<minsum)minsum=sum;
     sum=sum-b;b=arr[i];sum=sum+arr[i];
     
     
 }
     if(arr[i]==1)count++;
     
 }

 
 if(count==0)cout<<0<<endl;
else if(count==1)cout<<1<<endl;
else if(k==n)cout<<count*(count+1)/2<<endl;
else{
    

    cout<<(minsum*(minsum+1)/2)+(count-minsum)<<endl;
    
}
 t--;}
return 0;

}

Because you are not using Sliding window technique

Plz have a closer look to my code, while taking the input iam considering all possible windows.

Can anyone help me in my code debugging .I am gtting WA on it although the logic and dataypes are correct .
Code

#include<bits/stdc++.h>
#define ll long long
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl ‘\n’
using namespace std;
const int N=1e5+5;

void solve() {
ll n,k,sum=0,mini=1e9,cnt=0;
cin>>n>>k;
ll arr[n];
for(ll i=0;i<n;i++){
cin>>arr[i];
if(arr[i]==1)
cnt++;
}
for(ll i=0;i<k;i++)
sum+=arr[i];
// cout<<sum;
if(n==k){
ll ans=(cnt*(cnt+1))/2;
cout<<ans<<endl;
return;

}
else{
   for(ll i=k;i<n;i++){
       sum+=arr[i]-arr[i-k];
       mini=min(sum,mini);
   }
   ll left=cnt-mini;
   ll ans=mini*(mini+1)/2;
   cout<<ans+left<<endl;
   return;

}

}

int32_t main()
{
ios;
int test;
test=1;
// int cas=1;
// factorial(N-1);
// InvFactorial(N-1);
// sieve();
// SieveOfEratosthenes(1e5);
cin>>test;

  while(test--)
  {
     solve();
  }
   

return 0;
}

A minor update:

else{
   mini = sum; // Here is the update.
   for(ll i=k;i<n;i++){
       sum+=arr[i]-arr[i-k];
       mini=min(sum,mini);
   }
   ll left=cnt-mini;
   ll ans=mini*(mini+1)/2;
   cout<<ans+left<<endl;
   return;
}

Apart from this, I don’t find any bug.

Thanks brother! It worked.

1 Like

hey can anyone help me with my code,I’m able to pass the sample test cases but wrong and as a result during submission.

     #include <iostream>
using namespace std;

int main()
{
  long long int t;
  cin >> t;
  while (t > 0)
  {
    long long int n;
    cin >> n;
    long long int k;
    cin >> k;
    long long int arr[n];
    long long int count = 0;
    long long int b = 0;
    long long int sum = 0;
    long long int minsum = 1000000000;
    for (long long int i = 0; i < n; i++)
    {
      cin >> arr[i];
      if (i == 0)
        b = arr[0];
      if (i < k)
      {
        sum = sum + arr[i];
      }
      else
      {

        if (sum < minsum)
          minsum = sum;
        sum = sum - b;
        b = arr[i - k];
        sum = sum + arr[i];
      }
      if (arr[i] == 1)
        count++;
    }

    if (count == 0)
      cout << 0 << endl;
    else if (count == 1)
      cout << 1 << endl;
    else if (k == n)
      cout << count * (count + 1) / 2 << endl;
    else
    {

      cout << (minsum * (minsum + 1) / 2) + (count - minsum) << endl;
    }
    t--;
  }
  return 0;
}

This is my code . Very simple and sweet.
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
ll a[n],count1=0;
for(ll i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
count1++;
}
// if(k==1 || count1==n)
// cout<<count1<<endl;
if(count1==0)
cout<<0<<endl;
else
{
ll max=200000,newj=0,i=0,j=0,res=0;

		for(ll i=0;i<k;i++)
		{
			res+=a[i];
		}
				
		ll cursum=res;
		for(ll i=k;i<n;i++)
		{
			cursum+=a[i]-a[i-k];
			res=min(res,cursum);
		}
		ll ans=(((res)*(res+1))/2)+(count1-res);
		cout<<ans<<endl;
		
	}
}

}

Can anyone please tell me Why this is giving an error ? All sample test cases are passing

https://www.codechef.com/viewsolution/45000984