I've met a quite interesting problem and I thought it would be a nice task also on the code golf.
Unfortunately, it is currently being held as unclear.
However, the problem is for me very simple and clear. To my best knowledge, I reacted and made clear all reactions in comments, and by improving the post. The question didn't get up, neither down votes.
Considering
- the numerous reactions about the details,
- the lack of up and also down votes,
- while the problem is quite simple and clear in my mind,
I think some major problem still could exist, which could make the post still unclear. But, by answering all comments and improving the post according them, I don't have more idea.
I think maybe the math-focused part makes the post partially offtopic. On this reason, I've made a MathSE question and as it is visible in the answer, it has a very quick, simple and nice algorithm. This post is linked into the question.
@hvd pointed to a problem about the exact formulation of the required algorithmic complexity. I think it was fixed in the post.
Comparing the current version of the post to its first, I see major differences. Any idea, what else to do?