Adapted original solution into a more general solution for both 2a and 2b.