πŸ’‘ Deepak and his journey

why is this code giving wrong answer the output seems to be fine ?https://ide.codingblocks.com/s/99338
I have tried using long long int also .

@aganunay hey anunay this approach will not work
as this question have very large input.
I am suggesting you a simple approach
1 take input n
2 make three vectors C(n),L(n),M(i)
the return type of vectors is long long
3 apply a loop from i=0 to n-1
take input for the first vectors
i.e cin>>C[i];
4 In this loop a conditional statement required
if(i==0)
M[i]=C[i];
else
M[i]=min(M[i-1],C[i]);
end of loop
5 apply a loop from i=0 to n-1
take input for the third vector L[i];
end of loop
6 take two variable
petrol_available=0
final_cost=0
7 Again apply a loop from i=0 to n-1
final_cost+=L[i]*M[i];
end of loop
8 print final_cost

1 Like

@jaiskid sir thank you very much the code worked
but the only difference between the code suggested by you and my code was the use of vector and since i was using dynamic allocation for arrays then why did my code fail and yours passed since the time complexity in my opinion remained same ?

@aganunay ok let consider this for fun If your problem is done then open one of these test case and try out with both data structure do check in both the cases i.e vector as well array an then discuss me about which one fit best. and you will get to why I am using vectors

I have written the code according to your method but it isn’t working for most of testcases.Here’s the link