What is the runtime error in this code? please help
What is the runtime error in this code
here is my code, please help
In my code , i sort the array accordance with second array and making the ith as heightest try to calculate the answer for all the other whose second array value is less
@anand_54 your are not using the concept according to the optimised approach as done by using combinatorics and bit ?
I think there is no need of combinatorics because we have to calculate for each pair of i,j
check this code:
can you explain the logic behind this solution .
lets say we are considering a pair (x,y) of soldiers who communicate
assume x<y, so |x-y| = (y-x)
now we have 2 cases.
Case 1: b >= c
in this case the answer will simply be (y-x)*b, so total cost for this case is
sigma (y(i) - x) *b
simplifying further, we get sigma (y) b - x b (number of soldiers having less bombs than current soldier)
To answer this one, we require 2 things:
- number of soldiers lying ahead having less bombs
- sum of their positions
this can be answered with segment tree / fenwick tree.
Case 2: b < c
total cost for this case is
sigma [(y(i) - x) * c(i)]
simplifying:
sigma [y(i) c(i)] - sigma [x c(i)]
= sigma [y(i) c(i)] - x (sigma c(i) )
the first term can be found out by precomputing all y(i)*c(i) values, and proceeding in a similar way
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.