https://www.hackerrank.com/contests/world-codesprint-6/challenges/beautiful-3-set
I can't quite understand how to solve the problem. Although there is an editorial it's really poorly written as it only explains how to get an upperbound for the number of sets but it doesn't explain why that upperbound is achievable and how to come up with a formula for the numbers that satisfy the bound.
Thanks in advance.
I too cant understand the editorial.During the contest I wasted a lot of time on this problem trying to model it into some sort of matching problem.Does a matching solution exist for this problem?
IMHO,no. General case for this problem is Rainbow Matching which is NP-hard. But, I've read somewhere in the web that for perfect bipartite graphs lower bound (2 * n) / 3 exists.
The simplest and the best proof of this is to show by example.
Actually, there are a lot of formulas. I can show how i came up with my solution.
At first, I decided to take first set as
0 1 2 ... k-1
.Secondly, I started to think about the difference between adjacent elements, sum of triple such differences should be equal to zero. And after a little reasoning, i decided to take second set of differences as
-(x+1), +x, -(x+1), +x...
, so the third set will be+x, -(x+1), +x, -(x+1)
, where . After that, i had only to adjust the coefficients forx
and the first element of the second set.Also, there is a very simple solution with a cyclic shift. It looks like:
0 1 2 3 4 5 6 7 8
4 5 6 7 8 0 1 2 3
8 6 4 2 0 7 5 3 1
Thanks, I got it.
This is why it is achievable