He had 50 cookies
He ate 50% of the remaining cookies.
He had 15 cookies left
15 = 50% so 30 = 100%
He had 30 left after giving cookies to his friend.
50 - 30 = 20
He gave 20 cookies to his friend.
20 is 40% of the total amount of cookies.
He gave his friend 40% of the cookies