Only 3 test case are passing in rain harvesting problem

i have implented the logic by watching youtube video of prateek bhaiya https://youtu.be/Uog2Jmyb3iY.
But only 3 test cases are passing i dont know whats wrong need help…

Here is the code: #include #include using namespace std; int rain(int a[],int n) { int count=0; int left[100],right[100]; int leftmax; int rightmax; for(int i=0;i<n;i++) { leftmax=a[i]; rightmax=a[i]; for(int j=i;j>=0;j–) { if(a[j]>leftmax ||i==j) { leftmax=a[j]; } } left[i]=leftmax; for(int k=i;k<n;k++) { if(a[k]>rightmax ||i==k) { rightmax=a[k]; } } right[i]=rightmax; } for(int i=0;i<n;i++) { int minimum=min(left[i],right[i]); //cout<<minimum<<","<<endl; count=(minimum-a[i])+count; } cout<<count; return 0; } int main() { int n; int a[1000]; cin>>n; for(int i=0;i<n;i++) { cin>>a[i]; } rain(a,n); return 0; }

hello @mdhammad139
pls share ur code using cb ide.

ok let me see where the option is

@mdhammad139
a) go to this link -> https://ide.codingblocks.com/
b) paste ur code there
c) do ctrl + s
d) a link will be generated in ur search bar
share that link with me

uploaded it bhaiya have a look

@mdhammad139
bro ur logic is O(n^2) that is why it is giving tle .

refer this->
An element of an array can store water if there are higher bars on left and right. We can find the amount of water to be stored in every element by finding the heights of bars on the left and right sides. The idea is to compute the amount of water that can be stored in every element of the array. For example, consider the array {3, 0, 0, 2, 0, 4}, we can store two units of water at indexes 1 and 2, and one unit of water at index 2.

Pre-compute highest bar on left and right of every bar in O(n) time. Then use these pre-computed values to find the amount of water in every array element.

code for ur reference ->

ok bhaiya so orange exclamation mark here means time limit exceeded?

click on that mark , it will show what exaclty u r getting.

i guessed it by seeing the constraints

nothing happening by clicking on that mark but it’s says wrong answer in the submission tab with partial score of 30

it is showing run error because of small size of array.
click on any orange mark and then see area of compile message there u will see its meaning

I have clicked that but it show nothing on the area of compile message

image

I have increased the array size to 10000 now only 3 cases are not passing

now u need to optimise ur logic

okay bhaiya I’ll see it

I have improvised my logic to order of n still I’m not able to pass 3 cases

@mdhammad139
check now (i just changed the array declration)->