Any tutorial on this problem?

Problem link: Contest Page | CodeChef
Any tutorial or can you share your idea on this problem?

You can just go greedily from the first friend…
We can move the first friend to the right and last friend to the left (irrespective of other friend’s position)
And from the second friend check if he is exactly at distance K from the friend standing before him
if yes, skip this friend(don’t move him)
else if distance is less than K, moving the current friend to the right, will still keep the current friend and the previous friend grouped, so just move the current friend to the right
else if check whether moving the current friend one step to the left will group him with the previous friend, if yes move him to the left
else, just move the current friend to the right

After making all these changes, calculate the total friend groups
(There will be at least one friend group)

1 Like