The main goal of this problem is to calculate average velocities for $Q$ queries.

If you write a brute force solution (e.g. you go to $L...R$ indexes, calculate the sum of the velocities and then calculate average), the time complexity of our solution will be $O(Q|L-R|)$ and you will get "Time Limit Exceeded".

Advanced solvers will easily solve this problem by using Segment Tree, but it can be done without it as well.

Lets think of an optimized way: How can you skip the $L...R$ iteration part? For the $i^{th}$ element of the array, if you replace it with the sum till the $i^{th}$ element of the array, then you can get the sum of given range $(L, R)$ by simply calculating $sum[R] - sum[L-1]$. Now our sum calculating part complexity becomes $O(1)$ and overall complexity reduces to $O(Q)$.

You will definitely get Accepted now.

Caution: Check your code with these values of $L$: $1$, $2$

Statistics

83% Solution Ratio
Nowshin_SEarliest, Oct '20
nusuBotFastest, 0.0s
Jame.boyLightest, 3.0 MB
abid1729Shortest, 387B
Toph uses cookies. By continuing you agree to our Cookie Policy.