Hello guys,

I m trying to do this program where I used bisect_left, bisect_right for upperbound and lowerbound binary search, but the program seems not pass all the test cases, please kindly help me to understand what’s wrong.

I tried debugging for almost 5 Hr yesterday, couldn’t do so, also this is my first post.

Thanks regards.

Below is the code:

import sys

from bisect import bisect_left, bisect_right

# upper bound search on V

# use bisect_left (also useful to find first

# occurence of a no)

# finding greatest value smaller than X

def BinarySearchUpperBound(a, x):

i = bisect_left(a, x)

if i:

return (i-1)

else:

return -1

# lower bound search on W

def BinarySearchLowerBound(a, x):

i = bisect_right(a, x)

if i:

return (i-1)

else:

return -1

t, n, m = map(int, input().split())

contest = []

for i in range(t):

contest.append(list(map(int, input().split())))

V = list(map(int, input().split()))

W = list(map(int, input().split()))

minSum = sys.maxsize

V.sort()

W.sort()

for i in contest:

j = BinarySearchUpperBound(V, i[0])

k = BinarySearchLowerBound(W, i[1])

if j != -1 and k != -1:

minSum = min(minSum, (W[k]-V[j])+1)

print(minSum)