You have to choose the minimum length of sub array such that the sum of that sub array is maximum possible. It is given that all the element are greater than equal to zero.
Since all the number are positive the minimum length sub array such that the sum of that sub array is maximum is the array starting from the leftmost positive element greater than 0 and ending at the rightmost positive element greater than 0.
If all the elements of the array are 0 then we will choose a sub array of length 1 as the maximum possible sum is 0.
while(low<n&&a[low]==0)low++; while(high>=0&&a[high]==0)high--; if(low<high)cout<<high-low+1<<"\n"; else cout<<"1\n"; } return 0;