Rainwater harvesting code error

this code is giving some segmentation fault . can you tell me what is wrong in the code ? please tell also the basic cause of occurrence of this fault in any program.

hello @Raghavsingla522
common reason for this error is either u r accessing invalid index of array or u have allocated too much memory in ur program.

in ur code u are accessing invalid index because ur array size is 100 but n can be greater than that .
to rectify it check maximum value of n and declare ur array greater than or equal to that.

i have rectified that error as you suggested but now it is giving wrong output. what is going wrong
?

image
modify size here as well.

for left loop start i from 1.
for right loop start i from n-2

still not getting the desired output .

the formula should be
image

also only single test case is given but u are running while loop for t times.

ur updated code ->

ya it gets executed now . thanks. :+1: