My code show s wrong answer but work well on ide

#include
#include
using namespace std;

int kadane(int a[],int n)
{
int msum=0,tsum=0;
for(int i=0;i<n;++i)
{
tsum+=a[i];
msum=max(tsum,msum);
if(tsum<0)
tsum=0;
}

 return msum;

}

int main()
{
int t,n,a[1000];
cin>>t;
cin>>n;

int csum=0;

for(int i=0;i<t;++i)
{

  for(int i=0;i<n;++i)
        cin>>a[i];

  for(int i=0;i<n;++i)
        csum+=a[i];

  int msum=kadane(a,n);

  for(int i=0;i<n;++i)
        a[i]=-a[i];

  int nsum=kadane(a,n);

  int temp=csum + nsum;

  if(msum>temp)
    cout<<msum;
  else
    cout<<temp;

}

return 0;
}

I have edited your code… Try to submit the code now…

code is submitted successfully but how we know ,when to use long long int

When the constraints are large enough so that they cannot be solved using integers range, we use long long int values…

1 Like