SQRSUBMA - Editorial

@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

no that would be (1+2+3+1+2+3) * 3 = 36.

https://www.codechef.com/viewsolution/34862345

some one please help on this WA and one TLE also…

i explained this code above :point_up_2:

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, Got it! Thanks buddy.

for the given example

that sum would be calculated as ((2 + 3 + 5) + (2 + 3 + 5)) * 3 = 60.

my submission.

You are using a map. Use a frequency array. The TL was strict to weed out the map solutions

1 Like

Glad to help you :slight_smile:

Try using unordered map…they might give AC

I tried that in the contest, using custom hash function. It didn’t work.

Did you try inserting only sums that are less than x? That significantly reduces the map size, and hence reduces collision probability.

1 Like

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.

1 Like

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?

Did you use unordered map with custom hash??? and also which question ??

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 :slightly_smiling_face:.

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.

1 Like

Yes :P. I should add it to my snippets. Thanks!

I used map and it passed in Problem D (Div.3)!

Yess! That’s what I finally did, but two penalties for using array (MLE) and unordered_map (TLE).

1 Like

Oh…!

1 Like