Adding solution 1 with test cases.