added reference to SO post
solved problem 4.
solved problem 5. algorithm sucks though.