Solution and pseudocode.