#include<bits/stdc++.h>
using namespace std;
int maxLength(int a[], int n, int x)
{
int sum = 0, c = 0;
for(int i=1;i<=n;i++)
{
if((sum + a[i]) <= x)
{
sum += a[i];
c++;
}
else if(sum != 0)
sum = sum - a[i-c] + a[i];
}
return c;
}
int main()
{
int n, x;
cout << "Enter Array Element Number :: ";
cin >> n;
int a[n+1];
cout << "Enter Array Element :: \n";
for(int i=1;i<=n;i++)
cin >> a[i];
cout << "Enter Height Value :: ";
cin >> x;
cout << "\nMaximum Length is :: " << maxLength(a, n, x) << "\n";
return 0;
}
Comments
Post a Comment