problem 3 for multiple cases