# Problem 4 - Timeout Error

``````def find_unsorted_subarray(nums):
l = 0
r = len(nums)-1
max_len = 0
while l<r:
if nums[l+1]<nums[l]:
# l+=1
break
l+=1
if l<r:
while r>l:
if nums[r]>nums[r-1]:
break
r-=1
max_ele = max(nums[l:r+1])
min_ele = min(nums[l:r+1])
start = 0
end = len(nums)-1
while l>start and r<end:
if nums[l-1]>min_ele:
l-=1
if nums[r+1]<max_ele:
r+=1
max_len = r-l
return max_len
``````

Why am I getting timeout error for test case:
87
0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9

@m_a_x_i_m_u_s

I understand that the test case given on platform is big. So I tried my best to create a small test case that gives the same error. Go ahead and test your code with the following inputs and correct the code. On the face of it i see too many issues with your code.

Input1 : [2,6,9,9,8,8,4,4,8,9,15]
Output : 7
Expected : 8

Input2 : [2, 1]
Output : 0
Expected : 2

Input2 : [1,2,6,5,5,1,1,5,3,3,10]
Output : 8
Expected : 9

I donâ€™t want to give away the bug easily, so debug with these cases and find the issue yourself and then fix it and submit it.

Thanks @Sumanth_Uppala. Let me check and get back on this.

@m_a_x_i_m_u_s Were you able to resolve this?