Problem p03262 (python)

Fixed submission:
n,x = map(int, raw_input().split())
lis = sorted(map(int, raw_input().split()))
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b%a,a)
if n == 1:
    val = abs(lis[0]-x)
    print val
    exit()
val = 0
#print lis
for i in xrange(n):
    lis[i] -= x
    val = gcd(abs(lis[i]), val)
print val

Buggy submission (verdict: Runtime Error):
n,x = map(int, raw_input().split())
lis = sorted(map(int, raw_input().split()))
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b%a,a)
if n == 1:
    val = abs(lis[0]-x)
    print val
    exit()
import bisect
#print lis
idx = bisect.bisect_left(lis, x)
#print idx
if idx == 0:
    val = lis[0] - x
    flag = 0
    lmao = -1
    for i in range(1, n):
        if (lis[i] - x) % val != 0:
            flag = 1
            lmao = i
            break
    if flag == 0:
        print val
    else:
        factors = []
        flag1 = 0
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                for j in xrange(n):
                    if abs(lis[j]-x) % i != 0:
                        flag1 = 1
                        break
                if flag1==0:
                    print i
                    break
elif idx == n:
    val = x - lis[-1]
    flag = 0
    lmao = -1
    for i in range(n-2, -1, -1):
        if (x - lis[i]) % val != 0:
            lmao = i
            flag = 1
            break
    if flag == 0:
        print val
    else:
        factors = []
        flag1 = 0
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                for j in xrange(n):
                    if abs(lis[j]-x) % i != 0:
                        flag1 = 1
                        break
                if flag1==0:
                    print i
                    break
else:
    val = min(lis[idx]-x, x-lis[idx-1])
    flag = 0
    lmao = -1
    for i in range(idx, n, 1):
        if (lis[i]-x)%val != 0:
            flag= 1
            lmao = i
            break
    for i in range(idx-1, -1, -1):
        if (x-lis[i])%val != 0:
            lmao = i
            flag = 1
            break
    if flag == 0:
        print val
    else:
        factors = []
        flag1 = 0
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                for j in xrange(n):
                    if abs(lis[j]-x) % i != 0:
                        flag1 = 1
                        break
                if flag1==0:
                    print i
                    break


#print bisect.bisect_left(lis, 38)

Buggy submission (verdict: Runtime Error):
n,x = map(int, raw_input().split())
lis = sorted(map(int, raw_input().split()))
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b%a,a)
if n == 1:
    val = abs(lis[0]-x)
    print val
    exit()
import bisect
#print lis
idx = bisect.bisect_left(lis, x)
#print idx
if idx == 0:
    val = lis[0] - x
    flag = 0
    lmao = -1
    for i in range(1, n):
        if (lis[i] - x) % val != 0:
            flag = 1
            lmao = i
            break
    if flag == 0:
        print val
    else:
        factors = [] 
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                factors.append(i)
        for i in xrange(len(factors)):
            flag1 = 0
            for j in xrange(n):
                if abs(lis[j]-x) % factors[i] != 0:
                    flag1 = 1
                    break
            if flag1 == 0:
                print factors[i]
                break
elif idx == n:
    val = x - lis[-1]
    flag = 0
    lmao = -1
    for i in range(n-2, -1, -1):
        if (x - lis[i]) % val != 0:
            lmao = i
            flag = 1
            break
    if flag == 0:
        print val
    else:
        factors = [] 
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                factors.append(i)
        for i in xrange(len(factors)):
            flag1 = 0
            for j in xrange(n):
                if abs(lis[j]-x) % factors[i] != 0:
                    flag1 = 1
                    break
            if flag1 == 0:
                print factors[i]
                break
else:
    val = min(lis[idx]-x, x-lis[idx-1])
    flag = 0
    lmao = -1
    for i in range(idx, n, 1):
        if (lis[i]-x)%val != 0:
            flag= 1
            lmao = i
            break
    for i in range(idx-1, -1, -1):
        if (x-lis[i])%val != 0:
            lmao = i
            flag = 1
            break
    if flag == 0:
        print val
    else:
        factors = [] 
        for i in range(min(x, lis[lmao]), 0, -1):
            if x % i == 0 and lis[lmao] % i == 0:
                factors.append(i)
        for i in xrange(len(factors)):
            flag1 = 0
            for j in xrange(n):
                if abs(lis[j]-x) % factors[i] != 0:
                    flag1 = 1
                    break
            if flag1 == 0:
                print factors[i]
                break


#print bisect.bisect_left(lis, 38)