D. Final Algorithm and Flowchart