Could you please tell me why it is not passing other test cases

Here is my code:

def merge(l,low,m,high):
n1=m+1-low
n2=high-m
l1=[0](n1)
l2=[0]
(n2)
for i in range(0,n1):
l1[i]=l[low+i]

for j in range(0,n2):
    l2[j]=l[m+1+j]

i,j,k=0,0,low
while i<n1 and j<n2 :
    if l1[i]<l2[j]:
        l[k]=l1[i]
        i +=1
    else:
        l[k]=l2[j]
        j +=1
    
    k +=1
    
while i<n1:
    l[k]=l1[i]
    i +=1
    k +=1 
while j<n2:
    l[k]=l2[j]
    j+=1 
    k+=1 

def mergesort(l,low,high):
if low<high:
m=int((low+(high-1))/2)
mergesort(l,low,m)
mergesort(l,m+1,high)
merge(l,low,m,high)

n=int(input(""))
l=[int(l) for l in input().split()]
mergesort(l,0,n-1)

for i in range(n):
print(l[i],end=" ")

Hi Bhavika,
Request you to share the code through the Coding Blocks IDE (cb.lk/ide). Python code with messed up indentation is much harder to debug. It would be a lot easier for me to help you if you could share the code through the mentioned platform.
Just paste the code at cb.lk/ide, save it, and share the generated URL.

Here it is: https://ide.codingblocks.com/s/235687

The logic of your code is fine. The problem is TLE (Time Limit Exceeded). The code is not fast enough to complete within the time limits.
Checkout this snippet:


Here, I replaced the following section of your code:

l1=[0]*(n1)
l2=[0]*(n2)
for i in range(0,n1):
    l1[i]=l[low+i]

for j in range(0,n2):
    l2[j]=l[m+1+j]

with this:

l1=l[low:m+1]
l2=l[m+1:high+1]

Python’s in-built list slicing is much faster than for loops and hence the TLE is resolved.
Also, to get the mid index, just do this in python:

mid = (low+high) // 2

‘//’ operator does a floor division and returns an int (No casting needed).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.