# How to cover elements with minimum amount of elements

I'm trying to create a game but I am having some difficulties in coming up with a suitable algorithm for my problem. I have elements from 1 to n and I am trying to cover all of the elements using the minimum amount of elements as possible. Each element can cover an amount of elements to the left (x direction) and an amount of elements to the right direction (y direction) I'm trying to get all of them connected, meaning that if i pick one element z, each pair of adjacent elements i and i+1 should be covered from z.

Let me explain with an example:

I have placed n=7 boxes here (the boxes are i=0,1,2,3,4,5,6)

In each box i have put the range it can cover from the x and y list i provided, for example, in box 0, we can see that it covers element from 0 to 1, box 1 can cover element 0,1,2,3, box 2 can cover from 1-3, etc.

The goal is to find the minimum amount of elements in order to cover the entire 1 to n elements.

This is an example:
If n= 7
x= 0 1 1 2 1 1 1 [how many elements you can cover to the left]
y= 1 2 1 2 1 1 0 [how many elements you can cover to the right]

#as you can see you can cover 1 element to the left from element 2, and 2 elements to the right from element 2


To clarify, the reason it is 0 in the first x element, is because you can cover 0 elements to the left, and 0 in the last y element because you can cover 0 elements to the right. So from each element i, you can cover everything from i-xi and i+yi.

Answer: we could pick elements 2, 4 and 7 to cover all of the 7 elements (there are many other combinations). So the minimum would be 3 which is the right answer for this input

The reason why I dont pick element 2 and 6,(one might think they cover all of them) is because that would leave a gap between element 4 and 5 where they are not connected. So the minimum is not 2, but 3

What algorithm can solve this? I have looked at interval scheduling, as well as vertex cover, but vertex cover would not solve that just because you can cover for example element 6 from element 4, it does not imply vice versa. (if you look at the example) I have looked at dynamic programming and greedy algorithms, but they do not seem to solve the connecting issue, and would give the output of 2 and not 3, so Im a but clueless in how to tackle this so I am hoping that there is someone out there that can help me solve this, any advice, recommendations, youtube clips, pages and such are highly appreciated if you dont know or have the time to help Thank you in advance. If there are any clarifications needed let me know.

A solution i tried: Lets say i merge the two lists x and y to create an upper and lower bound we will get (0,1) (0,3) (1,3) (1,5) (3,5) (4,6) (5,6) meaning that index 0 can cover from 0 to 1, index 1 can cover from 0 to 3 etc. if i start from the left, and pick each square that covers a new element, in this case it would be correct, because it would pick 1,3 and 5. but what if i have the edgecase (0,1) (0,3) (1,3) (1,5) (3,5) (4,6) (0,6) then i would start from the left, and pick 1,3,5, when in fact, the right answer would be to only pick 6 (since it covers all of the elements)

• I don't understand what "if i pick one element z, each pair of adjacent elements i and i+1 should be covered from z" means. Can you edit your question to rephrase that? I don't know what "covered from z" means.
– D.W.
May 26 at 6:36
• I have provided an example, Im hoping it clarifies the constraints May 27 at 15:29

Create the set of intervals $$[3i - 3 \, x[i] - 2, 3 \, 3i + y[i] - 4]$$, i.e., the left endpoint of the $$i$$th interval is $$3i - 3 \, x[i]+1$$ and the right endpoint is $$3i + y[i] - 1$$. Now look for a minimal cover of all integers $$1,2,3,\dots,3n-4$$ using these intervals. THe solution to that will be a solution to your problem as well. You can find the minimal cover very efficiently, in particular in $$O(n \log n)$$ time, using the method in https://stackoverflow.com/q/293168/781723.