Cant find this question.We are given an array,we have to find maximum sum of consecutive elements but the maximum sum limit is given.For ex- in array 7 3 5 6,and maximum allowed sum is 9,so the answer should be 8 is given.The only thing i find on internet is maximum possible sum,but i want to find the limited sum
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,a[100],dp[100];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
for(int i=0;i<n&&sum<=m;i++)
{
int j=0;
sum=sum+a[i];
if(sum>m)
{
dp[j]=sum-a[i];
}
else dp[j]=sum;
j++;
sum=0;
}
sort(dp,dp+n);
cout<<dp[n-2];
return 0;
}