Hi,
I am getting WA in this problem, but my solution passes all test cases given. My logic was to use one cycle to start from 0th element and then 1st element and so on…, and then calculate sum using another cycle and then compare the sum with maxi if it greater than maxi and less than m then update the maximum.
using namespace std;
int main()
{
int n, m;
vector <int> arr;
cin>>n>>m;
for(int k = 0;k < n;k++)
{
int temp;
cin>>temp;
arr.push_back(temp);
}
int maxi = 0;
for(unsigned int i=0;i<arr.size();i++)
{
int total = 0;
for(unsigned int j=i;j<arr.size();j++)
{
total += arr[j];
if (total > maxi && total <= m)
{
maxi = total;
}
}
}
cout<<maxi<<endl;
}
This is my code.