Problem:
 Score : 500 points    Problem Statement In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.  Takahashi checked and found that the time gap (defined below) between the local times in his city and the i -th person's city was D_i hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.
Here, we are using 24 -hour notation.
That is, the local time in the i -th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.  Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.  Find the maximum possible value of s .      Constraints  1 \leq N \leq 50  0 \leq D_i \leq 12  All input values are integers.         Input Input is given from Standard Input in the following format:  N  D_1  D_2  ...  D_N       Output Print the maximum possible value of s .        Sample Input 1 3
7 12 8      Sample Output 1 4  For example, consider the situation where it is 7 , 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.       Sample Input 2 2
11 11      Sample Output 2 2       Sample Input 3 1
0      Sample Output 3 0  Note that Takahashi himself is also a participant.

Solution (Python):
from itertools import permutations, combinations,combinations_with_replacement,product
n = int(input())
d = list(map(int,input().split()))
time = [0 for i in range(13)]
for i in range(n):
    time[d[i]] += 1
    if d[i] == 0:
        print(0)
        exit()

kouho = [0]
doti = []
for i in range(13):
    if time[i] >= 3:
        print(0)
        exit()
    elif time[i] == 2:
        kouho.append(i)
        kouho.append(24-i)
    elif time[i] == 1:
        doti.append((i,24-i))


ans = 0
for i in range(2**len(doti)):
    kar = []
    for j in range(len(doti)):
        kar.append(doti[j][i>>j&1])
    c = kouho[:] + kar[:]
    now = 12
    p = list(combinations(c,2))
    for a , b in p:
        now = min(now,min(abs(a-b),24-abs(a-b)))
        
    ans = max(ans,now)
print(ans)

Solution (Python):
def main():
    n = int(input())
    D = list(map(int,input().split()))
    D.sort()

    T = [0]
    ans = 24
    for i,d in enumerate(D):
        if i%2 == 0:
            t = d
        else:
            t = 24 - d
        T.append(t)

    T.sort()
    MIN = 24
    for i in range(n):
        diff = T[i+1] - T[i]
        MIN = min(diff,MIN)

    print(MIN)

main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.append(0)
from collections import Counter
ca=Counter(a)
if max(ca.values())>2 or ca[0]>1 or ca[12]>1:
  print(0)
  exit()
elif ca[12]>0 and ca[11]>0:
  print(1)
  exit()
a.sort()
ans=a[0]
for i in range(2**n):
  aa=[0]
  for j in range(n):
    if (i>>j)&1:
      aa.append((24-a[j+1])%24)
    else:
      aa.append(a[j+1]%24)
  tmp=float('inf')
  aa.sort()
  for j in range(n):
    tmp=min(tmp,aa[j+1]-aa[j])
  tmp=min(tmp,24-aa[-1])
  
  ans=max(ans,tmp)
print(ans)

Solution (Python):
N = int(input())
D = list(map(int,input().split()))
Ta = [0]*13
Ta[0] = 1
for i in range(N):
    Ta[min(D[i],24-D[i])] += 1

if max(Ta)>=3:
    print(0)
elif Ta[0]>=2:
    print(0)
elif Ta[12]>=2:
    print(0)
else:
    L1 = []
    L2 = []
    for i in range(13):
        if Ta[i] ==1:
            if i==0 or i==12:
                L2.append(i)
            else:
                L1.append(i)
        elif Ta[i]==2:
            L2.append(i)
            L2.append(24-i)
    NL1 = len(L1)
    from itertools import product,combinations
    LS = list(product([0,1], repeat=NL1))
    NLS = len(LS)
    out = 0
    for i in range(NLS):
        L = LS[i]
        L3 = L2[:]
        score = 100
        for j in range(NL1):
            if L[j]==1:
                L3.append(L1[j])
            elif L[j]==0:
                L3.append(24-L1[j])
        CS = list(list(combinations(L3,2)))
        NCS = len(CS)
        for k in range(NCS):
            score = min(score,min(abs(CS[k][0]-CS[k][1]),24-abs(CS[k][0]-CS[k][1])))
        out=max(out,score)

    print(out)

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

N = int(readline())
D = list(map(int,readline().split()))

c = [0]*13
c[0] = 1
for d in D:
    c[d] += 1

if c[0] >= 2 or c[12] >= 2:
    print(0)
    exit()
for i in range(1,12):
    if c[i] >= 3:
        print(0)
        exit()

bitsearchlist = []
notbitsearch = [False]*24
notbitsearch[0] = True
if c[12]:
    notbitsearch[12] = True


for i in range(1,12):
    if c[i] == 1:
        bitsearchlist.append(i)
    elif c[i] == 2:
        notbitsearch[i] = True
        notbitsearch[24-i] = True

def count(List):
    anss = INF
    ret = INF
    for b in notbitsearch:
        if b:
            anss = min(anss,ret)
            ret = 0
        else:
            ret += 1
    anss = min(anss,ret)
    anss += 1
    return anss



L = len(bitsearchlist)

if L == 0:
    print(count(notbitsearch))
    exit()

ans = 0
for i in range(2**L):
    for j in range(L):
        if ((i >> j) & 1):
            notbitsearch[bitsearchlist[j]] = True
        else:
            notbitsearch[24-bitsearchlist[j]] = True
    ans = max(ans,count(notbitsearch))
    for j in range(L):
        if ((i >> j) & 1):
            notbitsearch[bitsearchlist[j]] = False
        else:
            notbitsearch[24-bitsearchlist[j]] = False

print(ans)

Solution (Python):
import sys
from collections import Counter
from itertools import product, combinations
from copy import deepcopy

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    D = list(map(int, input().split()))

    cnt = Counter(D)
    S_init = [0]
    T = []
    for k, v in cnt.items():
        if k == 0:
            print(0)
            exit()
        elif k == 12:
            if v >= 2:
                print(0)
                exit()
            else:
                S_init.append(k)
        else:
            if v == 1:
                T.append(k)
            elif v == 2:
                S_init.append(k)
                S_init.append(24 - k)
            else:
                print(0)
                exit()

    res = 0
    for pattern in product([0, 1], repeat=len(T)):
        S = deepcopy(S_init)
        for idx, p in enumerate(pattern):
            if p == 1:
                S.append(24 - T[idx])
            else:
                S.append(T[idx])
        score = f_inf
        for i, j in combinations(S, 2):
            gap = abs(i - j)
            score = min(score, gap, 24 - gap)
        res = max(res, score)

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
map_D = dict()
map_D[0] = 1
for d in D:
    if d in map_D:
        map_D[d] += 1
    else:
        map_D[d] = 1

new_D = []
is_zero = False
for (k, v) in map_D.items():
    
    if ((k == 0 or k == 12) and v >= 2) and v >= 3:
        is_zero = True
        break
    else:
        for i in range(v):
            new_D.append(k)

new_D.sort()
final_D = []
for i in range(len(new_D)):
    if i % 2 == 0:
        final_D.append(new_D[i])
    else:
        final_D.append(24 - new_D[i])

ans = 100
for i in range(len(final_D) - 1):
    for j in range(i + 1, len(final_D)):
        ans = min(ans, min(24 - abs(final_D[i] - final_D[j]), abs(final_D[i] - final_D[j])))

if is_zero:
    print(0)
else:
    print(ans)

Solution (Python):
from collections import deque

n = int(input())
d = list(map(int, input().split()))
d.append(0)
d.sort()

deq = deque()

for i in range(n + 1):
    if i & 1:
        deq.append(d[i])
    else:
        deq.appendleft(-d[i])

ans = abs(deq[0] + 24 - deq[n])
for i in range(n):
    ans = min(ans, deq[i + 1] - deq[i])

print(ans)

Solution (Python):
N=int(input())
D=[0]+list(map(int,input().split()))
D.sort()
for i in range(1,N+1):
  if i&1:
    D[i]=(24-D[i])%24
D.sort()
P=24-D[N]
for i in range(N):
  P=min(P,D[i+1]-D[i])
print(P)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))

arr.sort()

pep = [0 for i in range(24)]
pep[0] = 1

lr =True
for a in arr:
    if a== 0:
        pep[0] += 1
    else:
        if pep[a] > 1:
            pep[24-a] += 1
        elif pep[24-a] > 1:
            pep[a] += 1
        else:
            if lr:
                pep[a] += 1
                lr = not lr
            else:
                pep[24-a] += 1
                lr = not lr

ans = 1e9
for i in range(24):
    if pep[i] > 1:
        ans = 0
        break
    for j in range(i+1,24):
        if pep[i] > 0 and pep[j] > 0:
            d = min(j-i,24-(j-i))
            if ans > d:
                ans = d

print(ans)

Solution (Python):
import sys
from itertools import product
from copy import deepcopy

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    D = list(map(int, input().split()))

    cnt = [0] * 13
    cnt[0] = 1
    for d in D:
        cnt[d] += 1

    P = []
    init_T = []
    for i in range(13):
        if i == 0 or i == 12:
            if cnt[i]:
                if cnt[i] == 1:
                    init_T.append(i)
                else:
                    print(0)
                    exit()
        else:
            if cnt[i]:
                if cnt[i] == 1:
                    P.append(i)
                elif cnt[i] == 2:
                    init_T.append(i)
                    init_T.append(24 - i)
                else:
                    print(0)
                    exit()

    res = 0
    for pattern in product([0, 1], repeat=len(P)):
        T = deepcopy(init_T)
        for idx, p in enumerate(pattern):
            if p == 0:
                T.append(P[idx])
            else:
                T.append(24 - P[idx])

        min_gap = f_inf
        for i in range(len(T)):
            for j in range(i + 1, len(T)):
                gap = abs(T[i] - T[j])
                min_gap = min(min_gap, min(gap, 24 - gap))

        res = max(res, min_gap)

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):



from collections import Counter
N = int(input())
*A, = map(int, input().split())

INF = 1 << 30
C = Counter(A)
if N >= 24 or 0 in C:
    print(0)
    exit()

dt = []
nt = []
for k, v in C.items():
    if v > 2:
        print(0)
        exit()
    if v == 2:
        dt.append(k)
        dt.append(24-k)
    if v == 1:
        nt.append(k)

tmp = min(A)
for i, t in enumerate(dt):
    for s in dt[i+1:]:
        if abs(t-s) < tmp:
            tmp = abs(t-s)
ans = 0

M = len(nt)
if M:
    for bit in range(1 << M):
        D = [0]*M
        for i in range(M):
            if bit >> i & 1:
                D[i] = nt[i]
            else:
                D[i] = 24-nt[i]

        mn = tmp
        for j, d in enumerate(D):
            for e in D[j+1:]:
                if abs(d-e) < mn:
                    mn = abs(d-e)
            for t in dt:
                if abs(d-t) < mn:
                    mn = abs(d-t)

        if mn != INF and mn > ans:
            ans = mn
else:
    ans = tmp

print(ans)

Solution (Python):
def dist(a,b):
    return min(abs(a-b),24-abs(a-b))
from itertools import groupby, accumulate, product, permutations, combinations
from copy import copy
def solve():
    N = int(input())
    D = list(map(int, input().split()))
    c = [0]*13
    for i in range(N):
        c[D[i]]+=1
    choices = []
    dicided = [0]
    if c[12]>1:
        return 0
    if c[12]==1:
        dicided.append(12)
    if c[0]>0:
        return 0
    for i in range(1,12):
        if c[i]>2:
            return 0
        if c[i]==1:
            choices.append([i,24-i])
        if c[i]==2:
            dicided.append(i)
            dicided.append(24-i)
    ans = 0
    for p in product([0,1],repeat=len(choices)):
        lis = copy(dicided)
        for i in range(len(choices)):
            lis.append(choices[i][p[i]])
        m = 24
        for i in range(len(lis)):
            for j in range(len(lis)):
                if i!=j:
                    m = min(m,dist(lis[i],lis[j]))
        ans = max(ans,m)
    return ans
print(solve())

Solution (Python):
N = int(input())
D = sorted(list(map(int,input().split())))
for i in range(0,N,2):
    D[i] = 24-D[i]
D.insert(0,0)
dmin = 24
for i in range(N):
    for j in range(i+1,N+1):
        a = D[i]
        b = D[j]
        if a>b:
            a,b = b,a
        d = b-a
        if d>12:
            d = 24-d
        dmin = min(dmin,d)
print(dmin)

Solution (Python):
from collections import *

N = int(input())
D = list(map(int, input().split()))
cnt = Counter(D)
l = []
T = [0]

for k, v in cnt.items():
    if v>=3:
        print(0)
        exit()
    
    if v==1:
        l.append(k)
    else:
        T.append(k)
        T.append(24-k)

ans = 0

for i in range(1<<len(l)):
    t = T[:]
    
    for j in range(len(l)):
        if (i>>j)&1:
            t.append(l[j])
        else:
            t.append(24-l[j])
    
    t.sort()
    d = t[0]+24-t[-1]
    ans_cand = min(d, 24-d)
    
    for i in range(len(t)-1):
        d = t[i+1]-t[i]
        ans_cand = min(ans_cand, min(d, 24-d))
    
    ans = max(ans, ans_cand)

print(ans)

Solution (Python):
n = int(input())
D = list(map(int, input().split()))
from collections import Counter
C = Counter(D)


A = [0]
C = list(C.items())
C.sort()

for i in range(len(C)):
    k, v = C[i]
    if k != 0 and k != 12:
        if v == 1:
            A.append(k*(-1)**i)
        elif v == 2:
            A.append(k)
            A.append(-k)
        else:
            print(0)
            exit()
    else:
        if v == 1:
            A.append(k*(-1)**i)
        else:
            print(0)
            exit()


m = len(A)
ans = 10**18
for i in range(m-1):
    for j in range(i+1, m):
        temp = min(abs(A[i]-A[j]), 24-abs(A[i]-A[j]))
        ans = min(ans, temp)
print(ans)

Solution (Python):
import collections
import itertools

def dfs(used:set, newi:int, i:int):
    global D, N, iters
    new1 = newi
    new2 = (24-newi) * (newi!=0)
    if new1 not in used:
        used.add(new1)
        if i+1 == N:
            iters.append(list(used))
        else:
            dfs(used, D[i+1], i+1)
        used.remove(new1)
    if new2 not in used:
        used.add(new2)
        if i+1 == N:
            iters.append(list(used))
        else:
            dfs(used, D[i+1], i+1)
        used.remove(new2)

def main():
    global D, N, iters
    N = int(input())
    D = list(map(int, input().split()))
    D.sort()
    iters = []
    used = set([0])
    dfs(used, D[0], 0)
    if len(iters) == 0:
        print(0)
        return
    maxd = 0
    for tmp in iters:
        pairs = itertools.combinations(tmp, 2)
        mind = 25
        for x, y in pairs:
            mind = min(mind, min(abs(x-y), 24-abs(x-y)))
        maxd = max(maxd, mind)
    print(maxd)

if __name__ == "__main__":
    main()

Solution (Python):
import collections
n=int(input())
s=list(map(int,input().split()))
if 0 in s:
  print(0)
  quit()
s=collections.Counter(s)
l1=[0]
l2=[]
for x in s:
  if s[x]>2:
    print(0)
    quit()
  elif s[x]>1:
    l2.append(x)
    l2.append(24-x)
  else:
    l1.append(x)
x=len(l1)
an=0
for i in range(2**x):
  memo=[0]*x
  for j in range(x):
    if ((i >> j) & 1):
      memo[j]=l1[j]
    else:
      memo[j]=24-l1[j] 
  ans=12
  memo=memo+l2
  for k in range(len(memo)):
    for m in range(k+1,len(memo)):
      ans=min(ans,min(abs(memo[k]-memo[m]),24-abs(memo[k]-memo[m])))
  an=max(ans,an)
print(an)

Solution (Python):
n = int(input())
d = list(map(int, input().split()))
d.sort()
d_a = [v if i % 2 == 0 else 24-v for i, v in enumerate(d)]
d_b = [v if i % 2 == 1 else 24-v for i, v in enumerate(d)]
d_a.append(0)
d_b.append(0)
d_a.sort()
d_b.sort()
res_a = 24
res_b = 24
for i in range(n):
    res_a = min(res_a, d_a[i+1]-d_a[i])
    res_b = min(res_b, d_b[i+1]-d_b[i])
res_a = min(res_a, 24-d_a[-1]+d_a[0])
res_b = min(res_a, 24-d_b[-1]+d_b[0])
res = max(res_a, res_b)
if res == 24:
    res = 0
print(res)

Solution (Python):
n = int(input())
d = [int(i) for i in input().split()]
x = [0] * 13
for i in d:
    x[i] += 1
x[0] += 1

if max(x) > 2:
    print(0)
    exit()
ans = 0
for i in range(1 << 13):
    a = []
    for j in range(13):
        if x[j]==1:
            if (i >> j) & 1 == 1:
                a.append(j)
            else:
                a.append(24 - j)
        elif x[j] == 2:
            a.append(j)
            a.append(24 - j)
    aa = 10**18
    for j in range(len(a)):
        for k in range(j + 1, len(a)):
            d = abs(a[j] - a[k])
            aa = min(aa, min(d, 24-d))
    ans = max(ans, aa)
print(ans)

Solution (Python):
n = int(input())
ds = list(map(int, input().split()))

from collections import defaultdict
d = defaultdict(int)

d[0] += 1
for d_ in ds:
    d[d_] += 1
    if (d_ == 12 or d_ == 0) and d[d_] == 2:
        print(0)
        exit()
    elif d[d_] == 3:
        print(0)
        exit()

def calcmin(tmp):
    n = len(tmp)
    ret = 1 << 50
    for i in range(n):
        for j in range(i+1,n):
            ret = min(ret, abs(tmp[i]-tmp[j]),24-abs(tmp[i]-tmp[j]))
    return ret

two = []
x = []

for k in d.keys():
    if d[k] == 2 or k == 12:
        two.append(k)
        two.append(24-k)
    elif d[k] == 1:
        x.append(k)

two = list(set(two))
n = len(x)
ans = 0
for i in range(1 << n):
    tmp = x[:]
    for j in range(n):
        if i & (1 << j):
            tmp[j] = 24-x[j]
    
    tmp.extend(two)
    ans = max(calcmin(tmp), ans)

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



from itertools import combinations

def main():
	n=II()
	d=LI()
	d.sort()
	t=[0]*25
	t[0]=1
	t[24]=1

	for i in range(n):
		if i%2==0:
			t[d[i]]+=1
		else:
			t[24-d[i]]+=1

	if max(t)>=2:
		print(0)
		exit(0)

	ans=12
	cnt=0
	for i in range(1,25):
		if t[i]==0:
			cnt+=1
		else:
			ans=min(ans,cnt+1)
			cnt=0

	print(ans)


if __name__ == "__main__":
	main()

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
d.sort()
t=[0,24]
for i in range(n):
  if i%2==1:t.append(d[i])
  else:t.append((24-d[i])%24)
t.sort()
print(min(t[i]-t[i-1]for i in range(1,len(t))))

Solution (Python):
import math



def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            

def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))











n = int(input())

aa = list(map(int, input().split()))
t = [0 for i in range(13)]
a=[]

for i in aa:
    if(i==0):
        print(0)
        exit()
    if(t[i]<=1):
        t[i]+=1
        a.append(i)
    elif(t[i]==2):
        print(0)
        exit()

n = len(a)
if(t[1]>=1):
    print(1)
    exit()

b = [0 for i in range(25)]
ans = 0
count = 0

for i in range(2**(n-1)):
    count +=1
    for j in range(25):
        b[j]=0
    b[0]=1
    b[24]=1
    for j in range(n):
        if((i>>j) & 1):
            b[24-a[j]] += 1
        else:
            b[a[j]] += 1
    minim = 24
    prev = -24
    for j in range(0,25):
        if(b[j]==1):
            minim = min(minim, j-prev)
            prev = j
        elif(b[j]>=2):
            minim = 0
            break
    ans = max(ans,minim)
    

print(ans)

Solution (Python):
n = int(input())
ns = list(map(int, input().split()))
ns = sorted(ns)
ans = [0] * 24
ans[0] = 1
for i in range(n):
    if i % 2 == 0:
        ans[ns[i]] += 1
    else:
        ans[(24 - ns[i]) % 24] += 1
for i in ans:
    if i >= 2:
        print(0)
        exit()
ans = ans[1:]
print(1+min(len(i) for i in ''.join(map(str, ans)).split('1')))

Solution (Python):
n = int(input())
D = list(map(int, input().split()))

if n == 1:
    print(D[0])
    exit()

C = [0]*13
C[0] = 1
for i in range(n):
    C[D[i]] += 1

D = []
E = []
for i in range(13):
    if i != 0:
        if C[i] >= 3:
            print(0)
            exit()
        elif C[i] == 2:
            D.append(i)
            D.append(-i)
        elif C[i] == 1:
            E.append(i)
        else:
            continue
    else:
        if C[i] >= 2:
            print(0)
            exit()
        elif C[i] == 1:
            D.append(i)
        else:
            continue

p = len(D)
q = len(E)

import copy
if q == 0:
    ans = 25
    for i in range(p-1):
        for j in range(i+1, p):
            ans = min(ans, min(abs(D[i]-D[j]), 24-abs(D[i]-D[j])))
    print(ans)
else:
    ans = []
    for i in range(2**q):
        D_ = copy.copy(D)
        for j in range(q):
            if (i >> j)&1:
                D_.append(E[j])
            else:
                D_.append(-E[j])
        p = len(D_)
        temp = 25
        
        for i in range(p-1):
            for j in range(i+1, p):
                temp = min(temp, min(abs(D_[i]-D_[j]), 24-abs(D_[i]-D_[j])))
        ans.append(temp)
    print(max(ans))

Solution (Python):
n = int(input())
D = list(map(int, input().split()))

if n == 1:
    print(D[0])
    exit()

C = [0]*13
C[0] = 1
for i in range(n):
    C[D[i]] += 1

D = []
E = []
for i in range(13):
    if i != 0:
        if C[i] >= 3:
            print(0)
            exit()
        elif C[i] == 2:
            D.append(i)
            D.append(-i)
        elif C[i] == 1:
            E.append(i)
        else:
            continue
    else:
        if C[i] >= 2:
            print(0)
            exit()
        elif C[i] == 1:
            D.append(i)
        else:
            continue

p = len(D)
q = len(E)

import copy
if q == 0:
    ans = 25
    for i in range(p-1):
        for j in range(i+1, p):
            ans = min(ans, min(abs(D[i]-D[j]), 24-abs(D[i]-D[j])))
    print(ans)
else:
    ans = []
    for i in range(2**q):
        D_ = copy.copy(D)
        for j in range(q):
            if (i >> j)&1:
                D_.append(E[j])
            else:
                D_.append(-E[j])
        p = len(D_)
        temp = 25
        
        for i in range(p-1):
            for j in range(i+1, p):
                temp = min(temp, min(abs(D_[i]-D_[j]), 24-abs(D_[i]-D_[j])))
        ans.append(temp)
    print(max(ans))

Solution (Python):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)

    count = Counter(D)
    items = count.items()
    times = []
    times_def = []

    for k, v in items:
        if k == 0:
            if v >= 2:
                print(0)
                return
            else:
                times_def.append(0)

        elif k == 12:
            if v >= 2:
                print(0)
                return
            else:
                times_def.append(12)

        elif v == 2:
            times_def.append(24 - k)
            times_def.append(k)

        elif v >= 3:
            print(0)
            return

        else:
            times.append(k)
            continue
    answer = []
    for i in range(2 ** (len(times)) + 1):
        d = []
        ans = INF
        for j in range(len(times)):
            if (i >> j) & 1:
                d.append(24 - times[j])
            else:
                d.append(times[j])
        t = times_def + d
        
        
        for a in range(N):
            for b in range(a + 1, N + 1):
                diff = min(abs(t[a] - t[b]), 24 - (abs(t[a] - t[b])))
                if diff < ans:
                    ans = diff
        answer.append(ans)
    print(max(answer))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import copy
from collections import Counter

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    D = list(map(int, input().split())) + [0]
    c = Counter(D)
    times = []
    undecided = []
    for k, v in c.items():
        if k == 0 or k == 12:
            if v > 1:
                print(0)
                return
            else:
                times.append(k)
                continue

        if v > 2:
            print(0)
            return
        elif v == 2:
            times.append(k)
            times.append(24 - k)
        else:
            undecided.append(k)

    ans = 0
    n = len(undecided)
    for pattern in range(2 ** n):
        tmp = copy.deepcopy(times)
        for i in range(n):
            if pattern & (1 << i):
                tmp.append(undecided[i])
            else:
                tmp.append(24 - undecided[i])

        L = len(tmp)
        diff = INF
        for i in range(L):
            for j in range(i + 1, L):
                d = abs(tmp[i] - tmp[j])
                d = min(d, 24 - d)

                if d < diff:
                    diff = d

        if diff > ans:
            ans = diff

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
d = list(map(int,input().split()))
d.sort()
for i,j in enumerate(d):
    if i%2==0:
        d[i] = 24-j
d.sort()
ans = d[0]
for i in range(1,n):
    tmp = d[i]-d[i-1]
    ans = min(ans,tmp)

ans = min(ans,24-d[-1])
print(ans)

Solution (Python):
from collections import deque
def main():
    n = int(input())
    d = list(map(int, input().split()))
    d.sort()
    q = []
    q = deque(q)
    q.append(0)
    ans = float("inf")
    for v in d:
        if q[0]-(-v) < v-q[-1]:
            ans = min(ans, v-q[-1])
            q.append(v)
        else:
            ans = min(ans, q[0]-(-v))
            q.appendleft(-v)
    ans = min(ans, q[0]-(-12) + 12-q[-1])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):




import math
import sys
import bisect
import heapq  
from collections import deque

inf = 10 ** 15
mod = 10 ** 9 + 7

n = int(input())
d = list(map(int, input().split()))
Dlist = [0 for i in range(12)]
for num in d:
    if num == 0:
        print(0)
        sys.exit()
    Dlist[num-1]+=1
    if Dlist[num-1] == 3:
        print(0)
        sys.exit()
judge = [0]
unknown = []
if Dlist[11] >= 2:
    print(0)
    sys.exit()
elif Dlist[11] == 1:
    judge.append(12)
for i,num in enumerate(Dlist[1:11]):
    if num == 2:
        judge.append(i+2)
        judge.append(24-i-2)
    if num == 1:
        unknown.append(i+1)
nujeng = len(unknown)
ans = 0
for i in range(2**nujeng):
    tmp = []
    for num in judge:
        tmp.append(num)
    for j in range(nujeng):
        dnow = unknown[j]
        if ((i >> j) & 1):
            tmp.append(dnow+1)
        else:
            tmp.append(24-dnow-1)
    dic = 50
    if tmp:
        tmp.sort()
        pre = -inf
        for num in tmp:
            if num-pre < dic:
                dic = num-pre
            pre = num
        if 24-tmp[-1] < dic:
            dic = 24-tmp[-1]
        ans = max(dic, ans)
print(ans)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))

"""
愚直に解くと
・時差が0,12以外の人は、D, 24-Dの2パターンの時間になりうる
・なので、2^N全部試す
で、普通にTLE。

例えばある時差Dの人が
（１）一人の場合：時刻Dとする
（２）二人の場合：Dと24-Dで振り分ける
で、最小の時差を減らさない動きができる
一方で
（３）三人以上の場合：Dと24-Dの少なくとも一方が２人以上いることになるから、時差の最小値が0になる

なので、時差Dが一人の時は、Dと24-Dで２通り試して、時差Dが二人の時は両方に入れる、３人以上は問答無用で0になる、みたいにするとよさげ
あるいは最初はDとして、次は24-Dとして～みたいなのを繰り返す。
"""

from collections import Counter
from collections import defaultdict

c = Counter(D)
c[0] += 1

for i in range(13):
    if i == 0 or i == 12:
        if c[i] >= 2:
            print(0)
            exit()
    else:
        if c[i] >= 3:
            print(0)
            exit()

D.sort()

d = defaultdict(int)
d[24] += 1
is_am = True
for t in D:
    if is_am:
        d[t] += 1
        is_am = False
    else:
        d[24 - t] += 1
        is_am = True


ans = min(D)
times = list(d.keys())
times.sort()
for a,b in zip(times, times[1:]):
    ans = min(ans, b-a)

print(ans)