I’m not a nodejs guy, but I just tried your code (after fixing the errors - left_arr and right_arr should be leftArr and rightArr respectively), and it looks like outputting the results one by one with console.log takes up the bulk of the time.
Other solutions seem to be combining all output into one string and dumping that out i.e.
console.log(final_array.join('\n'));
This is much faster in my tests, though I don’t know whether it’s fast enough. Frankly, I’d just use the built-in sort method, personally
@ssjgz thanks for helping, that was the only issue that you pointed out. But I wanted to know that loop was giving the time complexity O(n) and same “join” will be giving then why my solution was not working?
@sweta787 I suppose the problem link is Turbo Sort.
If you look at the constraints in the problem: the value of 0 \leq a[i] \leq 10^6 where a[i] is element at the i^{th} location, so instead of using traditional comparison-based sorting algorithm like Merge Sort and Quick-Sort, you can use Linear Bound Sorting Algorithm as all the data in the array are integers. For example, you can use Counting Sort to solve the problem which will solve the problem in O(max(a[i])) which in this problem statement is O(10^6) in worst-case scenario.
For understanding Counting Sort you can refer to the following link: Counting Sort Algorithm Tutorial.