@ankit0038 Because the equation we get is X = sz * (sum of two subarrays)
Here we calculate all possible sum of subarrays of size sz and that is stored in the freq array.
From the above equation, the we need to find a pair whose sum is X/sz. That is the reason why we store all sums and calculate the other.
To simplify, let us take an equation a = b + c
We have b’s to know c’s we do a-b right, same idea
Here’s the catch
what if we take array A : 2 3 5
then subarray(such that Bij=Ai+Aj) will be
4 5 7
5 6 8
7 8 10
In this e.g.,
considering the cell (1,1) & (3,3) as edge we get side length = 3
sum of subarray with these two cell at edge = (1+2+3+1+2+3) * 3 = 36 = X
but in actual the sum is 4+5+7+5+6+8+7+8+10 = 60 , hence it may contradict
as X not equal to sum of subarray
please guide me if i got the above e.g. in wrong aspect(probably my misconception)!
Yeah, did not try that. It might have worked. The next thought actually that came to me was of array so went with that as it was surely decreasing the complexity.
Haha, yes. Even I jumped to an array after I got TLE. Anyways, I just wanted to know if doing what I suggested could avoid TLE.
Yesterday in codeforces Div 3, I used unordered_map which gave TLE. I switched to a regular map and it passed in 200 ms. So is it always good to just use map for everything?
Problem D. And no custom Hash.
The problem actually does not require a map, or a frequency table. One could just store the reminders, sort it, and count the occurrences as we pass. However, I first used a frequency array (yep, did not see the constraint for k), and I got MLE. So the next instinct was to use an unordered_map, which gave TLE .
My advice would be to always use custom hash with unordered maps. Even if your solution passes pretests, in uphacking phase someone can hack your solution. The problem D can actually be done in O(1) extra space so you don’t need anything.