Works for first test, but extreme cases are not working