is it only used to display the maximum sum of subarray?Can’t we display the subarray that yields the maximum sum?
Kadane's algorithm
@Senjuti256
kadanes algo with subarray …!!
#include
using namespace std;
int main()
{
int a[1000];
int temp_sum = 0 ,maxsum=0,i,j,n;
int left = 0, right = -1;
cin>>n;
for(i = 0 ; i < n ; i++)
cin>>a[i];
for(int j = 0 ; j <n ; j ++)
{
if(temp_sum < 0) // if adding negative no. making temp_sum negative than has to leave that subarray ..
{
temp_sum = 0;
left = j ;
}
temp_sum+=a[j]; // making temparory sum
if(temp_sum>maxsum) // update the max sum
{
maxsum=temp_sum;
right = j;
}
}
cout<<"\n max posssible sum is : " <<maxsum<<" for the given array .."<<endl;
for(int i = left ; i <=right ; i++)
cout<<a[i]<<" , ";
return 1;
}
dont forget to hit like and mark resolved if cleared 