Function giving solution for problme 2