Added logic for test-4. I still need a test case to tweak the condition.