Added the logic for Problem2