First program:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,taken,i,j,ans=0,t,num,k;
cin>>n>>t;
vector<int>a(n);
for(i=0;i<n;++i)
cin>>a[i];
i=0;j=0;
for(;i<n;++i)
{
if(i>j)
{
j=i;
k=0;
taken=0;
}
for(;j<n;++j)
{
if(taken+a[j]<=t)
{
taken+=a[j];
k++;
}
else
break;
}
ans=max(ans,k);
taken-=a[i];
k--;
}
cout<<ans;
return 0;
}
Second program:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,taken,i,j,ans=0,t,num,k;
cin>>n>>t;
vector<int>a(n);
for(i=0;i<n;++i)
cin>>a[i];
for(i=0;i<n;++i)
{
if(i>j)
{
j=i;
k=0;
taken=0;
}
for(j=0;j<n;++j)
{
if(taken+a[j]<=t)
{
taken+=a[j];
k++;
}
else
break;
}
ans=max(ans,k);
taken-=a[i];
k--;
}
cout<<ans;
return 0;
}
it seems these 2 programs give 2 diffrent outputs,here is a testcase to check it out yourself: 4 5 3 1 2 1
the first one outputs "3" while the second outputs "1991243264 " on codeforces tests while the 2 programs give the same output on ideone..any help?