Rain water harvesting

sir i wrote this code. it is showing correct answer after compilation but when i submit it, it shows that 3 test cases are not passed out of 10. it also shows time limit exceeded with the following message -

Dang! Your program was too slow to compute the answer to one or more test cases. Think - is there a more efficient way to arrive at the solution than brute force?

please somebody help me nd tell me what i have done wrong. my code is this -

hello @pushkar24sharma
ur soution time complexity is O(n^2) thats why it is givng tle for some cases.

try to think of of O(n) approach .

1 Like

@aman212yadav sir okay i got it.
i designed this new code and it also got accepted with a green tick on every possible test case.
but i have one doubt in it.

in the following code if we declare a[1000000], l[1000000], r[1000000] arrays globally that is just above main then only it gives correct answer.

if we declare a[1000000], l[1000000], r[1000000] as local arrays that is inside int main function then it shows error and does not compute the answer.

does declaring array globally or locally is different??

please look into it. this is the code -

yeah global scope allows to declare large size arrays compared to function scope(i.e on stack)

1 Like