solution for 3-6.cpp

My solution probably worked, but was not optimal.