ম্যাক্সিমাম সাবঅ্যারে সাম বের করার কোডঃ
Input
10
4 -5 4 -3 4 4 -4 4 -5
Output:
maximum sum=9 between 3 to 8 position
Input:
4
-2 -3 -5
Output:
Maximum sum is negative or zero
Input:
maximum sum=6 between 2 to 2 position
///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
long n,i,mx=0,j,ar[20005]={0},fst=1,scnd=-1,p=1,sum=0;
cin>>n;
for(i=1;i<n;i++)
{
cin>>ar[i];
}
for(i=1;i<n;i++)
{
sum+=ar[i];
if(sum<0)
{
sum=0;
p=i+1;
}
else if(sum>mx)
{
mx=sum;
fst=p;
scnd=i;
}
else if(sum==mx&&i-p>scnd-fst)
{
mx=sum;
fst=p;
scnd=i;
}
}
if(mx<=0)
{
printf("Maximum sum is negative or zero\n");
}
else
{
printf("Maximum sum is =%ld is between stops %ld and %ld\n",mx,fst,scnd);
}
}
}
Input
10
4 -5 4 -3 4 4 -4 4 -5
Output:
maximum sum=9 between 3 to 8 position
Input:
4
-2 -3 -5
Output:
Maximum sum is negative or zero
Input:
3 -1 6Output:
maximum sum=6 between 2 to 2 position
///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
long n,i,mx=0,j,ar[20005]={0},fst=1,scnd=-1,p=1,sum=0;
cin>>n;
for(i=1;i<n;i++)
{
cin>>ar[i];
}
for(i=1;i<n;i++)
{
sum+=ar[i];
if(sum<0)
{
sum=0;
p=i+1;
}
else if(sum>mx)
{
mx=sum;
fst=p;
scnd=i;
}
else if(sum==mx&&i-p>scnd-fst)
{
mx=sum;
fst=p;
scnd=i;
}
}
if(mx<=0)
{
printf("Maximum sum is negative or zero\n");
}
else
{
printf("Maximum sum is =%ld is between stops %ld and %ld\n",mx,fst,scnd);
}
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন