Shafaet's blog

By Shafaet, history, 3 years ago,

I invite you to take part in HackerRank HourRank 19 starting on April 2nd 2017. The contest duration is 1 hours. There will be three algorithmic problems of various difficulty.

The chief author of this round is torquecode. The chief tester is pkacprzak. Thanks to Piotr Gajowiak and wild_hamster for pre-solving the problems and giving valuable opinions.

The problems will have subtasks to make them interesting for everyone. I strongly recommend to read all the problems.

The contestants are ranked by score. If two contestants get the same score, the person who reached the score first is ranked higher.

Happy Coding!

• +16

 » 3 years ago, # |   +3 Wow a 1 hour event again.......Thanks for the info :0 Hoping for better rating this time .
 » 3 years ago, # |   +5 For the last question,instead of removing each edge and calculating the diameters of the 2 trees obtained..it would be sufficient to just remove edges incident on the center of the tree and which are part of diameter(ie only 2 edges). Although I'm not aware of the proof,I'm quite sure that this should also be correct.
•  » » 3 years ago, # ^ |   +9 If I understood your idea correctly, your solution would print 9 for this tree, but answer is 10.
•  » » 3 years ago, # ^ | ← Rev. 6 →   0 Yes!The problem is very similar to this.
 » 3 years ago, # |   0 Third problem is similar to a COCI problem which I think is the reason why people solved it so quickly.
•  » » 3 years ago, # ^ | ← Rev. 4 →   +5 Are your certain that's correct? What would be your answer for this tree?UPD: Don't know how to post images :-(
•  » » » 3 years ago, # ^ |   0 You probably meant to post this as a reply to shubhamgoyal__.
•  » » » » 3 years ago, # ^ |   0 Indeed. I am sorry for my mistake.
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Also, reducing this problem to calculating the longest path in every subtree (also for the subtrees "going up") is pretty easy, and the latter problem is incredibly standard and well known... So I would guess some of the fastest contestants just copied their solutions from some other problem which required the same thing, not necessarily the one you mentioned. I couldn't remember any of those problems though, had to implement it again :P
•  » » » 3 years ago, # ^ |   +6
 » 3 years ago, # |   0 Can someone explain how to solve the second problem.
•  » » 3 years ago, # ^ |   0 Read Nim
•  » » 3 years ago, # ^ |   +5 let indexes are 0..n-1. 1. s[b] XOR s[b+1] XOR ... XOR s[e] == s[0] XOR s[1] XOR ... XOR s[n-1] = stotalxor = const. 2. s[b] XOR s[b+1] XOR ... XOR s[e] == ( s[0] XOR s[2] .. XOR s[b-1]) XOR ( s[0] ... s[e] ) = = v[b] XOR v[e+1] 3. for each 0 <= e < n need find how many b <= e, that v[b] XOR v[e+1] == stotalxor. or v[b] = stotalxor XOR v[e+1]. where v[i] = s[0] xor s[2] ... s[i-1] . v[i] - easyliy computeted, because v[0] = 0, v[i] = v[i-1] xor s[i-1], for i > 0. and v[i] <= 2^17. Let xorcnt[ i ] - number of that j, v[j] = i. xorcnt[i] - also easyly computed. xorcnt[v[0]] = 1; ans = 0; for(int i= 1; i <= n; ++i){ ans += xorcnt[ v[ i ] ^ sxortotal ]; xorcnt[ v[i] ] ++; } cout << ans; 
•  » » » 3 years ago, # ^ |   0 well if i got you right finding all combinations of v[b] ^ v[e] == stotalxor is the main idea of solution?
•  » » » » 3 years ago, # ^ | ← Rev. 3 →   0 Nim game: first player can win if only if xor sum s[] elements is not zero.But, our case need count winning situations of second player, so need achieve xor sum of s[] elements to zero, after remove s[b]...s[e] elements.i.e.s[0] xor s[1] xor ... xor s[b-1] xor s[e+1] xor ... s[n-1] == 0.add xor to both sides to (s[b] xor s[b+1] xor .. s[e]) — givesstotalxor = xor_sum(s[i], i= 0..n-1) = 0 xor xor_sum(s[j], j = b..e) = xor_sum(s[j], b..e).xor_sum(s[j], j = b..e) = xor_sum(s[j], j = 0..b-1) xor xor_sum(s[j], j = 0..e)Let v[k] = xor_sum(s[j], j =0..k-1);so need find that (b,e) pairs (0 <= b <= e < n ), that v[b] ^ v[e+1] == stotalxor.if we know e, so can calculate number of b, that 0 <= b <= e, and v[b] ^ v[e+1] == stotalxor. because, v[b] = stotalxor ^ v[e+1]. if we store number of v[i] , 0<=i<=e in xocrcnt[] array, numberof b is xorcnt[ stotalxor ^ v[e+1] ].