• + 1 comment

    The code idea is correct but we don't need to perform three O(n) operations for same stuff. The time complexity will remain O(n) but if taken for large array length, there will be slight difference in time in microseconds probably compared to that with one or two O(n) time complexity program. We mostly don't care for space complexity (just for single list), thus, we may use it.

    REDUCE COMPLEXITY TO WHATEVER YOU CAN!!