Whats wrong in the code

#include
#include
using namespace std;
int Water_stored(int a[],int n, int k){
int cs=0;
for(int j=1;j<n-1;j++){
if(a[j]<k){
a[j]=k-a[j];
cs+=a[j];
}else{
a[j]=0;
cs+=a[j];
}
}cout<<cs;
}
int main() {

int n;
cin>>n;
int a[1000]={0};
for(int i=0; i<n; i++){
	cin>>a[i];
}cout<<endl;
int k=2;
Water_stored(a,n,k);


return 0;

}

Hi @neelrai906,
u need to modify ur code a bit. here u r taking k=2 which is not correct for all the cases. try building a logic which doesnt use a constant variables like k=2.
One of the test case on which ur code fail can be :
n=4, array={2,4,2,4}, here output should be 2(between 2nd and 4th index) but ur code gives 0 as output.
Hope dis resolves ur doubt.
if u still need some help, u can post ur doubts here.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.