retr0coderxyz's blog

By retr0coderxyz, history, 5 years ago, In English

Hello community,

Recently I cam across 4 questions(A hiring challenge from Hacker Earth which is over now!) to which I could not find solutions. I tried my best during contest to come with solutions but I was unable to do so. It would be great if you guys could help me provide a solution for these problems(also a generic solution for similar kind of problems) Would really help me a lot. I dont remember the exact problem but I would try to provide the best description to my knowledge.

Problem 1:

We have to distribute N coins to S students such that each gets distinct number of coins while maximizing S. We have to find S. For eg. if N is 5 then S is 2 because (2,3). Its not mandatory to distribute all N coins.

My Idea

Problem 2:

Swapping the segments: given array of size n and q queries with each query containing l1,r1,l2,r2 A[l1:r1] should be swapped with A[l2,r2] After Q queries we should print the final array.

My Idea

Problem 3:

Given a string s we need to find two subsequence s1=s2 and maximize |s1+s2|. For eg: aabababb the answer is 6 s1=aab and s2=aab

My Idea

Problem 4:

Given a character for each node. and given a graph(its guarenteed to be a tree). Q queries with each query giving x y. We have to find for each query a consider a simple path from x->y find the most frequent character along the path. For eg.

6
a b a s q n
(n-1 edges)
1 2
1 3
2 6
3 4
3 5
2
2 3  => a
2 6  => b(print lexicographically smaller in case of tie)
My Idea

Thank you for reading patiently guys, I tried so much to come up with a solution but couldnt so any help would be greatly appreciated guys.

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

| Write comment?
»
5 years ago, # |
  Vote: I like it +8 Vote: I do not like it

If I correctly got you.

  • problem #1: max $$$S$$$ with condition $$$N >= \frac{S*(S+1)}{2}$$$
  • problem #2: looks like treaps but if $$$n$$$ and $$$q$$$ are small enough (for $$$O(n(n+q))$$$ solution) then we can use linked lists
  • problem #3: easy with suffix automation but if $$$|s|$$$ is small enough (for $$$O(|s|^2)$$$ solution) we can use trie of all string suffixes (meanwhile, can $$$s1$$$ and $$$s2$$$ overlap?)
  • problem #4: since we have only 26 different node values we can precalculate the count of each letter on the path from the tree root to each node (by means of simple DFS) and then on the query x y we can find lca(x,y) and then check the frequencies of the letters $$$'a'\dots'z'$$$ separately