Target interview question

to write logic gor given problem