solved problem seven. runtime O(4n)