mzaferbooshi96's blog

By mzaferbooshi96, history, 2 hours ago, In English

They love to play chess together. They play n From chess matches. None of them end in a draw. You should compare the number of times the letter "A" is repeated with the letter "D". If the letter "A" is more, type "Anton",If the letter "D" is repeated more than once. type "Danik",If they are tied, type "Friendship".

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by mzaferbooshi96 (previous revision, new revision, compare).

»
72 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Another interesting problem from you, although much harder than the previous one. I will assume that the input is a string.

We can solve this with Divide and Conquer in a similar way that we would construct a segment tree. Let's create an array $$$a$$$, where $$$a_i$$$ is $$$1$$$ if $$$s_i$$$ is 'A', otherwise $$$a_i$$$ is $$$-1$$$. We can recursively calculate the difference between sum of the array by calculating the sum of the left part and the right part. Merging the result is trivial: $$$sum_{L, R} = sum_{L, M} + sum_{M + 1, R}$$$, where $$$M = \lfloor \frac{L + R}{2} \rfloor$$$.

If the sum of the array is positive, the answer is Anton, if it is negative, the answer is Danik, and if it is $$$0$$$ the answer is Friendship.

Time complexity: $$$O(n)$$$

Proof of time complexity: At avery level of the recursion where $$$L \neq R$$$, we split into two more recursions. We can model the recurrence as $$$T(n) = 2 \cdot T(\frac{n}{2}) + O(1)$$$. By the [Master Theorem](https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)) the time complexity is $$$O(n)$$$.

My implementation