Tag Archives: tree

Hacckerrank Weekly #4 // problems 1-3

Palindrome Index

The first problem was posted on Monday. The statement says that you're give a string s of n \leq 10^5 letters. The task is to find the index of a letter in the string such that after removing that letter s is a palindrome. It's guaranteed that such an index exists. One test file consists of at most 20 test cases.


There are two cases. The string s can be already a palindrome or not.

It's easy to check, that if s is already a palindrome, then after removing the n / 2-th letter, s remains a palindrome.

If s is not a palindrome, then there exists an index i such that s_i \neq s_{n - 1 - i}. Let k be the smallest such index.

Again, it's easy to see that we have to remove either s_{k} or s_{n - 1 - k}, because after removing any s_j for k < j < n - 1 - k, s_{k} will be compared with the same letter that it was compared in the original s and the result of that comparison is still negative. In addition, it makes no sense to remove any a_j for j < k or j > n - 1 - k because with that action we won't achieve anything positive.


a := s without s_{k}

b := s without the s_{n - 1 - k}

From the problem statement, we know that at least one of a and b is a palindrome. If a is a palindrome, then the result is k. In the other case, the result is n - 1 - k.

The time complexity of that solution is O(n).


Algorithmic Crush

In Tuesday problem, you're given an array c[1..n], where n \leq 10^7. Initially c_i := 0 for each i.

Next, there are m \leq 2 \cdot 10^5 queries. Each query consists of 3 integers a, b, k, and it adds k to every c_i where a \leq i \leq b. Your task is to return the maximal value in the array after performing these m operations. The full problem statement is here.


At the first sight it seems like a segment tree problem, but it would be an overkill here. The crucial thing there is that we only have to compute a maximal value once, so we can first "perform" all queries and then calculate the result. The next simple observation is that we can only consider elements c_i for which there is at least one query where a or b equals i. That observation led us to a solution based on sorting.

Let's thing of each query as of two event. Each event has it's own index and value. For the query <a, b, k> the first event is to add a value k to the index a and the second event is to subtract a value k from the index b. After that, we can sort these events. We say that event1 < event2 iff. the index of event1 is less than the index of event2 or these indices are equal and event1 is the opening event i.e. it adds a value. Next we can process all the events from left to right keeping track of the current value and the maximum value. We update the maximum value if the current value is greater than the maximum value collected so far.

The total time complexity of that method is O(m \cdot \log m) and it's worth to mention, that it doesn't depend on n at all.

Lucy and Flowers

Wednesday problem is more difficult, but not so much :) While the original problem statement has some story behind it, the real task is the following:

You're given n \leq 5000 distinct integers. The task is to count how many distinct Binary Search Trees (BST) can be created by picking any non-empty subset of these integers. Because the result can be very large, you have to compute it modulo 10^9 + 9, but in order to provide a clear solution I'll omit that. There are at most 5000 testcases in one test file.


There are two crucial observations. If you pick two different subsets, the trees created from these subsets are different. On the other hand, if you pick two subsets with the same number of elements, the number of different trees created from each subset is the same, because only ranks of elements matter, not their relative values.

Let dp[k] be the number of different BST that can be created from k integers and assume that we can compute that value.

Then using two above facts, the result can be computed as:

\sum\limits_{k = 1}^n = \binom {n} {k} \cdot dp[k]

because we can compute the number of different BST for each subset separately and that number is the same for subsets of the same size.

But how to compute dp[n] for any n?

If we have n elements, we can put any of them in the root of a tree. If we put the k-th smallest of them, k - 1 smallest elements have to be in the left subtree of the root and n - k largest elements have to be in the right subtree. The number of possible different BST as a left subtree is dp[k - 1] and it's dp[n - k] for the right subtree. This led us to the recursive formula:

dp[n] = \sum\limits_{k = 1}^{n} dp[k - 1] \cdot dp[n - k]

The base case is:

dp[0] = 1

because there is only one empty BST.

In the full solution, we precompute dp table and binomial coefficients \binom {n} {k}. These two precomputations take O(n^2) time. After that, we can compute every testcase in O(n) time which gives O(n^2 + t \cdot n) time, where t is the number of testcases.