code link. What I’ve done is first check for the impoosible cases. for that I am checking whether another element is present or not in the +d range. If not I am adding. And for the remaining left indexes I just fill it upto the last first (i.e. to ‘x’) and then start from the last greedily.