Quote:
Could you give any hints as to how I can improve the time complexity of the code?
Your code runtime complexity is O(n²) and memory complexity is O(2n) because of temp array.
With the method of OG (with division), the runtime complexity is O(n) and memory complexity is O(n).
Since the only thing you do with temp array is printing the value, you don't need the array, once you got the product, print it.
Your code runtime complexity will be O(n²) and memory complexity will be O(n).
If you really need to replace the values of
str
, it is a little more complicated, but a variable with partial product is enough to do it.