Can anyone tell me the approach to this problem?

Can anyone please help me why my code for this problem is wrong: