time and space complexity added