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):
N = int(input())
D = list(map(int, input().split()))
ans = 0
lb, ub = -1, min(D) + 1
while ub - lb > 1:
    m = (lb + ub) // 2
    group = [0] * N
    for k in range(N):
        if group[k] != 0:
            continue
        group[k] = 1
        stack = [k]
        while stack:
            i = stack.pop()
            for j in range(N):
                if i == j or group[j] != 0:
                    continue
                diff = abs(D[i]-D[j])
                if min(diff, 24-diff) < m:
                    group[j] = group[i] * -1
                    stack.append(j)
    ok = True
    for i in range(N):
        for j in range(i):
            diff = abs(D[i]*group[i]-D[j]*group[j])
            if min(diff, 24-diff) < m:
                ok = False
                break
        if not ok:
            break
    if not ok:
        ub = m
    else:
        lb = m
print(lb)

Solution (Python):


def saiki(num,lis):
    if num==13:
        
        temp=10**9
        lis.sort()
        for i in range(len(lis)-1):
            temp=min(temp,abs(lis[i+1]-lis[i]))
        if temp!=10**9:
            ans.append(temp)
        
    else:
        if dp[num]>2:
            print(0)
            exit()
        elif dp[num]==2:
            saiki(num+1,lis+[num]+[24-num])
            saiki(num+1,lis+[num]+[num])
            saiki(num+1,lis+[24-num]+[24-num])
        elif dp[num]==1:
            saiki(num+1,lis+[num])
            saiki(num+1,lis+[24-num])
        else:
            saiki(num+1,lis)            


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

dp=[0]*13

for d in D:
    dp[d]+=1

ans=[]
saiki(0,[0,24])

print(max(ans))

Solution (Python):
def main():
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    h = [0 for _ in range(13)]
    for d in D:
        h[d] += 1
    if h[0] >= 2 or h[12] >= 2:
        return 0
    for i in range(1, 12):
        if h[i] >= 3:
            return 0

    res = 0
    for i in range(2**11):
        L = [0]
        for j in range(1, 12):
            if h[j] == 2:
                L.append(j)
                L.append(24-j)
            if h[j] == 1:
                if i & 2**(j-1) != 0:
                    L.append(j)
                else:
                    L.append(24-j)
        if h[12] == 1:
            L.append(12)
        L.append(24)
        L.sort()
        tres = 25
        for i in range(len(L)-1):
            tres = min(tres, L[i+1]-L[i])
        res = max(res, tres)
    return res


print(main())

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

def gap(s, t):
    d = abs(s-t)
    return min(d, 24 - d)

N = int(input())
*D, = map(int, input().split())
cnt = Counter(D + [0])
time_definite = []
time_indefinite = []
for k, v in cnt.items():
    if (k != 0 and v >= 3) or (k == 0 and v >= 2):
        ans = 0; break
    else:
        if v == 2:
            time_definite.append(k); time_definite.append(-k)
        else:
            if k == 0: time_definite.append(k)
            else: time_indefinite.append(k)
else:
    ans = 0
    def_min = 12
    for t1, t2 in combinations(time_definite, 2):
        def_min = min(def_min, gap(t1, t2))
    cand = len(time_indefinite)
    for bit in range(1 << cand):
        time_temp = [t if (bit & (1 << i)) else -t for i, t in enumerate(time_indefinite)]
        indef_min = 12
        for t1, t2 in combinations(time_temp, 2):
            indef_min = min(indef_min, gap(t1, t2))
        temp = min(def_min, indef_min)
        for t1, t2 in product(time_temp, time_definite):
            temp = min(temp, gap(t1, t2))
        ans = max(ans, temp)
print(ans)

Solution (Python):
def f(a, b):
    d = abs(a-b)
    return min(d, 24-d)

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

Solution (Python):
from itertools import product
from collections import Counter
def main():
    N,*D=map(int, open(0).read().split())
    C=Counter(D)
    ans=min(D)
    for i in range(1,12):
        if i in C and C[i]>=3:
            ans=0
    if 12 in C and C[12]>=2:
        ans=0
    if ans:
        X1=[k for k,v in C.items() if v==1]
        X2=[k for k,v in C.items() if v==2]
        Y=[False]*24
        for x in X2:
            Y[x]=Y[-x]=True
        res=0
        for p in product((True, False), repeat=len(X1)):
            Z=Y.copy()
            for t,x in zip(p,X1):
                if t:
                    Z[x]=True
                else:
                    Z[-x]=True
            res2=12
            I=[i for i,p in enumerate(Z) if p]
            for i in I:
                for j in I:
                    if i==j: continue
                    res2=min(res2,abs(i-j))
            res=max(res,res2)
        ans=min(ans,res)
    print(ans)

if __name__ == "__main__":
    main()

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

C = Counter(D)

P = [0] 
if C[0] > 0:
  print(0)
  exit(0)
  
hidarif = True
for i in range(1,13):
  if C[i] == 0:
    continue
  if C[i] > 2:
    print(0)
    exit(0)
  if C[i] == 2:
    P.append(i)
    P.append(24-i)
    continue
  
  P.append(i if hidarif else 24-i)
  hidarif = not hidarif
  
P.sort()

ans = 24
for i in range(0,len(P)):
  compare = 0 if i == len(P) - 1 else i+1
  ans = min(ans, abs(P[compare] - P[i]))
  
print(ans)

Solution (Python):
N = int(input())
D = [int(d) for d in input().split()]

D.append(0)
D.sort()
for i in range(1, N+1):
    if i%2 == 0:
        D[i] = 24-D[i]
D.sort()
ans = min(D[-1], 24-D[-1])
for i in range(N):
    ans = min(ans, D[i+1]-D[i])
print(ans)

Solution (Python):
N = int(input())
D = [int(d) for d in input().split()]

D.append(0)
D.sort()
ans = 0
time1 = []
time2 = []
for i in range(N+1):
    if D.count(D[i]) == 2:
        if D[i] == 0:
            ans = -1
            break
        time2.append(D[i])
        time2.append(24-D[i])
    if D.count(D[i]) + D.count(24-D[i]) > 2:
        ans = -1
        break
    if D.count(D[i]) == 1:
        time1.append(D[i])

time2 = set(time2)    
if ans == 0:
    for i in range(2**len(time1)):
        temp = []
        for j in range(len(time1)):
            if i & 1<<j:
                temp.append(24-time1[j])
            else:
                temp.append(time1[j])
        temp += list(time2)
        temp.sort()
        cnt = 24-temp[-1]
        if temp[-1] == 0:
            cnt = 0
        for k in range(len(temp)-1):
            cnt = min(cnt, temp[k+1]-temp[k])
        ans = max(ans, cnt)
else:
    ans = 0

print(ans)

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

from collections import Counter
C = Counter(D)

ans = 0
import heapq
for pattern in range(1<<11):
    if C[0] >= 1: continue
    if C[12] >= 2: continue
    if C.most_common(1)[0][1] >= 3: continue
    q = [24]
    for d in range(1,12):
        if C[d] == 1:
            if pattern & 1: heapq.heappush(q, d)
            else: heapq.heappush(q, 24-d)
        elif C[d] == 2:
            heapq.heappush(q, d)
            heapq.heappush(q, 24-d)
        pattern>>=1
    if C[12] == 1: heapq.heappush(q, 12)
    pre = 0
    tmp = 24
    for _ in range(N+1):
        d = heapq.heappop(q)
        tmp = min(tmp, d-pre)
        pre = d
    ans = max(ans, tmp)

print(ans)

Solution (Python):
from collections import Counter
N = int(input())
D = list(map(int,input().split()))
dc = Counter(D)
flag = True
count = 0
cand = [0]
one = []
if 0 in dc:
    flag = False
if 12 in dc:
    if dc[12] >= 2:
        flag = False
    else:
        cand.append(12)
for i in range(1,12):
    if i not in dc:
        continue
    if dc[i] == 1:
        count += 1
        one.append(i)
    elif dc[i] >= 3:
        flag = False
        break
    else:
        cand += [24-i,i]
if flag:
    if count:
        ans = 0
        for i in range(2**count):
            candone = []
            for j in range(count):
                if (i >> j) & 1 == 1:
                    candone.append(one[j])
                else:
                    candone.append(24-one[j])
            cand2 = cand+candone
            cand2.sort()
            ans1 = min(cand2[-1]-cand2[0],24-cand2[-1]+cand2[0])
            for i in range(1,len(cand2)):
                ans1 = min(ans1,cand2[i]-cand2[i-1])
            ans = max(ans,ans1)
        print(ans)
    else:
        cand.sort()
        if len(cand) == 1:
            print(0)
        else:
            ans = 24
            for i in range(1,len(cand)):
                ans = min(ans,cand[i]-cand[i-1])
            print(ans)
else:
    print(0)

Solution (Python):

import sys
from collections import Counter
import itertools
import copy

def solve(N: int, D: "List[int]"):
    counter = dict(Counter(D+[0]))
    time = [False]*25
    once = []
    
    for i in range(13): 
        if counter.get(i) == None:
            continue
        if counter[i] == 2:
            if i == 0:
                print(0)
                return
            time[12+i] = True
            time[12-i] = True
        elif counter[i] >= 3: 
            print(0)
            return
        else:
            
            once.append(i)

    LEN = len(once)
    bit_list = list(itertools.product([0, 1], repeat=LEN))

    if LEN == 0:
        min_dif = 12        
        time = [i for i,t in enumerate(time) if t]
        for first,second in list(itertools.combinations(time,2)):
            min_dif = min(min_dif,min(abs(first-second),24-abs(first-second)))
        print(min_dif)
        return

    answer = 0

    
    for bit in bit_list:
        tmp = copy.deepcopy(time)
        
        for i in range(LEN): 
            if bit[i] == 1:
                tmp[12+once[i]] = True
            else:
                tmp[12-once[i]] = True

        
        tmp = [i for i,t in enumerate(tmp) if t]
        min_dif = 12
        for first,second in list(itertools.combinations(tmp,2)):
            min_dif = min(min_dif,min(abs(first-second),24-abs(first-second)))
        answer = max(min_dif,answer)

    print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    D = [int(next(tokens)) for _ in range(N)]  
    solve(N, D)

if __name__ == '__main__':
    main()

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


for i in range(n):
    for j in range(n):
        if i != j:
            ans = min(ans, abs(d[i]-d[j]))

print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from collections import deque

def main():
    N = int(input())
    d = list(map(int,input().split()))
    
    d.sort()
    now = 1
    time = [0]
    for num in d:
        if now == 1:
            time.append(num)
        else:
            time.append(24-num)
        now *= -1
    
    time.sort()
    ans = 24-time[-1]
    for i in range(N):
        ans = min(ans,time[i+1]-time[i])
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
cnt=[0]*24
cnt[0]=1
for i in range(n):
  cnt[d[i]]+=1
  cnt[(24-d[i])%24]+=1
  if cnt[d[i]]>=3 or cnt[24-d[i]]>=3:
    print(0)
    exit()
ans=[0]*24
ans[0]=1
right=True
for i in range(1,13):
  if cnt[i]>=2:
    ans[i]=1
    ans[24-i]=1
  elif cnt[i]==0:
    continue
  else:
    if right:
      right=False
      ans[i]=1
    else:
      right=True
      ans[24-i]=1
L=24
num=0
for i in range(1,24):
  if ans[i]==0:
    num+=1
  else:
    L=min(L,num)
    num=0
L=min(L,num)
print(L+1)

Solution (Python):
from collections import Counter
from itertools import product
import numpy as np


def timeGap(D):
    day = []
    candidate = []
    c = Counter(D)

    for k, v in c.items():
        
        if k == 0 or k == 12:
            if v >= 2:
                return 0
            else:
                day.append(k)
            continue

        
        if v >= 3:
            return 0
        
        elif v == 2:
            day.append(k)
            day.append(24-k)
        else:
            candidate.append(k)

    
    res = 0
    n = len(candidate)
    for pair in product(range(2), repeat=n):
        tmp = day[::]
        for i in range(n):
            if pair[i]:
                tmp.append(candidate[i])
            else:
                tmp.append(24 - candidate[i])
        tmp.sort()
        tmp.append(tmp[0]+24)
        res = max(res, np.diff(tmp).min())
    return res


if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    ans = timeGap(D)
    print(ans)

Solution (Python):
n=int(input())
D=[int(i) for i in input().split()]
C=[1]+[0]*12

for d in D:
  C[min(d,24-d)]+=1
if max(C)>2 or C[0]>1 or C[12]>1:
  print(0)
else:
  ans=0
  
  for i in range(2**(11)):
    CC=[1]+[0]*23+[1]
    for j in range(1,12):
      if C[j]==2:
        CC[j]=1
        CC[24-j]=1
      elif C[j]==1:
        if i&(1<<(j-1)):
          CC[j]=1
        else:
          CC[24-j]=1
    if C[12]==1:
      CC[12]=1
    cur=24
    t=0
    for j in range(1,25):
      if CC[j]==1:
        cur=min(cur,t+1)
        t=0
      else:
        t+=1
    ans=max(ans,cur)
  print(ans)

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


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def calc(F):    
    res = 0
    for f in F:
        S = format(f, "024b")[22::-1]
        count = 12
        tmp = 1
        for s in S:
            if s == "1":
                count = min(count, tmp)
                tmp = 1
            else:
                tmp += 1
        count = min(count, tmp)
        res = max(res, count)
    
    return res


def g(D, F):
    if len(D) == 0:
        return calc(F)

    d = D[0]
    s = set()
    for f in F:
        if (f >> d) & 1 == 0:
            s.add(f + 2 ** d)
        
        d = 24 - d
        if (f >> d) & 1 == 0:
            s.add(f + 2 ** d)
    
    return g(D[1:], s)
    

def main():
    N = int(input())

    D = read_list()
    if D.count(0) > 0:
        print(0)
        return
    F = {1,}    
    print(g(D, F)) 
    

if __name__ == "__main__":
    main()

Solution (Python):
N = input()
D_list = list(map(int, input().split()))
lis = sorted(D_list)
lis_plus = lis[::2]
lis_minus = lis[1::2]
lis_plus_all = [0] + lis_plus
lis_minus_all = [0] + lis_minus

if lis_plus:
    lis_minus_all += [24 - lis_plus[-1]]
if lis_minus:
    lis_plus_all += [24 - lis_minus[-1]]

gaps_plus = [lis_plus_all[n + 1] - lis_plus_all[n] for n in range(len(lis_plus_all) - 1)] + [24]
gaps_minus = [lis_minus_all[n + 1] - lis_minus_all[n] for n in range(len(lis_minus_all) - 1)] + [24]

print(min(min(gaps_plus), min(gaps_minus)))

Solution (Python):
import sys
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False

def ddprint(x):
  if DBG:
    print(x)

n = inn()
d = inl()
a = [0]*13
for i in range(n):
    a[d[i]] += 1
    if d[i]==0 or d[i]==12 and a[12]>=2 or a[d[i]]>=3:
        print(0)
        exit()
if n>=13:
    print(1)
    exit()
ddprint('a')
ddprint(a)
mx = -1
for i in range(2**n):
    s = [(i>>j)%2 for j in range(n)]
    e = [0]*25
    e[0] = 1
    for j in range(n):
        suf = (d[j] if s[j]==0 else 24-d[j])
        e[suf] += 1
    ddprint('s,e')
    ddprint(s)
    ddprint(e)
    mn = 25
    strk = 0
    for j in range(1,24):
        if e[j]>=2:
            mn = 0
            break
        elif e[j]==1:
            mn = min(mn, strk+1)
            strk = 0
        else:
            strk += 1
    ddprint(mn)
    mn = min(mn, strk+1)
    mx = max(mx,mn)
print(mx)

Solution (Python):
n = int(input())
D = [0] + sorted(list(map(int, input().split())))
E = [D[i] if i%2 else 24-D[i] for i in range(n+1)]
ans = float("inf")
for i in range(n):
  for j in range(i+1, n+1):
    k = (E[i]-E[j])%24
    ans = min(ans, min(k, 24-k))
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from itertools import combinations

def distance(i, j):
	return min(abs(j - i), abs(24-j+i))

def main():
	n = int(input().strip())
	v = sorted(list(map(int, input().strip().split())))
	t = [0] * n

	for i in range(n):
		d = v[i]
		if i % 2 == 0:
			t[i] = d
		else:
			t[i] = 24 - d
	cmin = min(v) 
	for z in combinations(range(n), 2):
		i, j = z[0], z[1]
		cmin = min(cmin, distance(t[i], t[j]))
	print(cmin)

if __name__ == '__main__':
	main()

Solution (Python):
def dif(x,y):
    if x<y:
        x,y = y,x
    return min(x-y,24-(x-y))
from itertools import combinations
N = int(input())
D = sorted(list(map(int,input().split())))
T = [0 for _ in range(N)]
for i in range(N):
    d = D[i]
    if i%2==0:
        T[i] = d
    else:
        T[i] = 24-d
cmin = min(D)
for z in combinations(range(N),2):
    i,j = z[0],z[1]
    cmin = min(cmin,dif(T[i],T[j]))
print(cmin)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
d.sort()
time=[0]*24
time[0]=1
for i in range(n):
  if i%2:
    time[d[i]]+=1
  else:
    time[-d[i]]+=1
if max(time)>1:
  print(0)
  exit()
ans=[]
cnt=1
for i in range(1,25):
  i=i%24
  if time[i]==0:
    cnt+=1
  else:
    ans.append(cnt)
    cnt=1
print(min(ans))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
exist=[0 for i in range(13)]
for i in l:
  exist[i]+=1
if exist[0]>=1 or exist[-1]>=2 or max(exist)>=3:print(0)
else:
  single=[]
  stand=[]
  if exist[-1]==1:single.append(12)
  for i in range(1,12):
    if exist[i]==1:
      single.append(i)
    if exist[i]==2:
      stand.append(i)
      stand.append(24-i)
  m=len(single)
  def getstatus(num):
    L=[]
    for i in range(m)[::-1]:
      if num//(2**i)==1:
        L.append(single[i])
      else:
        L.append(24-single[i])
      num%=2**i
    return L
  def dif(LIST):
    ret=100
    for i in range(len(LIST)-1):
      if LIST[i+1]-LIST[i]<ret:ret=LIST[i+1]-LIST[i]
    return ret
  ans=0
  for i in range(2**m):
    if dif(sorted([0]+getstatus(i)+stand+[24]))>ans:ans=dif(sorted([0]+getstatus(i)+stand+[24]))
  print(ans)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
D=list(map(int,input().split()))
D.sort();
for i in range(n-1,-1,-1):
    D.append(24-D[i])




def solve_binary(mid):
    now=0; count=0
    used=[0]*(2*n)
    for i,di in enumerate(D):
        if not used[i] and now+mid<=di:
            now=di; count+=1
            used[i]=used[2*n-1-i]=1
    return count==n and now+mid<=24

def binary_search(n):
    ok=0
    ng=n
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve_binary(mid):
            ok=mid
        else:
            ng=mid
    return ok


print(binary_search(24))

Solution (Python):




n = int(input())
if n>=25:
    print(0)
    exit()

d = list(map(int, input().split( )))
if 0 in d:
    print(0)
    exit()
d.append(0)
dic = {}

for di in d:
    if di in dic:
        dic[di] += 1
    else:
        dic[di]= 1
solo = []
times = []
for di in dic:
    if dic[di]==1:
        solo.append(di)
    elif dic[di]==2:
        times.append(di)
        if di!=12:
            times.append(24-di)
        else:
            print(0)
            exit()
    else:
        print(0)
        exit()
        
ans = 0
m = len(solo)
bit = 1<<m

for i in range(bit):
    times2 = []
    for t in times:
        times2.append(t)
    for j in range(m):
        if i>>j&1:
            times2.append(solo[j])
        else:
            times2.append(24-solo[j])
    times2.sort()
    dif = 24
    if len(times2)==1:
        print(0)
        exit()
    

    for k in range(len(times2)-1):
        dif = min(dif,times2[k+1]-times2[k])
    dif = min(dif,times2[0] + 24 - times2[-1])
    ans = max(ans,dif)
print(ans)

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

D_list = [0] * (12 + 1)
for d in D:
  D_list[d] += 1

  
for d in D_list:
  if d >= 3:
    print(0)
    exit()    
    
answer = []
for n in range(2 ** 13):
  tmp = [0]
  for j in range(13):
    if D_list[j] == 0:
      continue
    if D_list[j] == 1:
      if (n >> j) & 1:
        tmp.append(j)
      else:
        tmp.append(24 - j)
    if D_list[j] == 2:
      tmp.append(j)
      tmp.append(24 - j)
  tmp.sort()
  tmp.append(tmp[0] + 24)
  
  res = []
  for i in range(1, len(tmp)):
    res.append(tmp[i] - tmp[i - 1])
  answer.append(min(res))
print(max(answer))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


n = onem()

d = l()

d.sort()

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

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

co = 12

for i in range(24):
    if a[i] >= 2:
        co = 0
        break
    for j in range(i+1,24):
        if a[i] >= 1 and a[j] >= 1:
            co = min(co,min(24-(j-i),j-i))
print(co)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

ans = 0
def gap(a,b):
    c = abs(a-b)
    return min(c,24-c)
def solve():
    global ans
    def dfs(f,i,k):
        global ans
        if i == n:
            ans = k
        else:
            nd = d[i]
            nk = min([gap(nd,j) for j in f])
            if nk > ans:
                nf = [i for i in f]+[nd]
                dfs(nf,i+1,min(k,nk))
            if d[i] != 12:
                nd = 24-d[i]
                nk = min([gap(nd,j) for j in f])
                if nk > ans:
                    nf = [i for i in f]+[nd]
                    dfs(nf,i+1,min(k,nk))

    n = I()
    d = LI()
    f = [0]
    dfs(f,0,float("inf"))
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import bisect
n = int(input())
d = list(map(int,input().split()))
d.sort()
l = [0,24]
for i in range(n):
    if i % 2 == 0:
        l.append(24-d[i])
    else:
        l.append(d[i])
l.sort()
ans = l[1]-l[0]
for i in range(n+1):
    if l[i+1] - l[i] < ans:
        ans = l[i+1] - l[i]
print(ans)

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

res = 0
for k in range(2):
    b = [0, 24]
    tmp = 10 ** 10
    for i, j in enumerate(a):
        if i % 2 == k:
            b.append(24 - j)
        else:
            b.append(j)
    b = sorted(b)
    for i, j in zip(b, b[1:]):
        tmp = min(tmp, j - i)
    res = max(res, tmp)
print(res)

Solution (Python):
N = int(input())
D = [int(a) for a in input().split()]
X = [0] * 13

for d in D:
    X[d] += 1
if X[0] or max(X) >= 3 or X[12] >= 2:
    print(0)
else:
    ma = 0
    for i in range(1<<13):
        Y = [0]
        for j in range(13):
            if X[j] == 2:
                Y.append(j)
                Y.append(24-j)
            elif X[j] == 1:
                if i >> j & 1:
                    Y.append(j)
                else:
                    Y.append(24-j)
        Y = sorted(Y)
        M = len(Y)
        ma = max(ma, min([abs((Y[i] - Y[i-1]) % 24) for i in range(M)]))
    print(ma)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])


def main():
    n=inp()
    a=inpl()
    a.sort()
    dic = defaultdict(int)
    dic[0] += 1
    for i in range(n):
        dic[a[i]] += 1
    for e in dic:
        if dic[e] > 2:
            print(0)
            return
        if e == 0 or e == 12:
            if dic[e]>1:
                print(0)
                return
    x = [0 for _ in range(24)]
    cnt = 0
    for i in range(13):
        if dic[i]==0:
            continue
        elif dic[i] == 1:
            if cnt % 2 == 0:
                x[i] += 1
            else:
                x[24-i] += 1
        else:
            x[i] += 1
            x[24-i] += 1
        cnt += 1
    ans = 24
    for i in range(24):
        for j in range(24):
            if i==j or x[i]==0 or x[j]==0:
                continue
            ans = min(ans,abs(i-j),24-abs(i-j))
    print(ans)

if __name__ == "__main__":
    main()

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

s = 24
D.sort()
D2 = [_ for _ in D]
for i in range(N):
  if D2[i] == D2[i + 1]:
    D2[i] = 24 - D2[i]

D2.sort()
for i in range(N):
  s = min(D2[i + 1] - D2[i], s)
s = min(24 - D2[N] + D2[0], s)

if s != 0 and N <= 12:
  for b in range(1 << N):
    D2 = [_ for _ in D]
    for i in range(N):
      if b & (1 << i):
        D2[i + 1] = 24 - D2[i + 1]
    D2.sort()
    s2 = 24
    for i in range(N):
      s2 = min(D2[i + 1] - D2[i], s2)
    s2 = min(24 - D2[N] + D[0], s2)
    s = max(s2, s)

print(s)

Solution (Python):
from collections import Counter

N = int(input())
D = [int(i) for i in input().split()]

def solve() :
  cnt = Counter(D)
  one = []
  L1 = []
  for k, v in cnt.items() :
    if v >= 3 :
      return 0
    if v == 2 :
      L1.append(k)
      L1.append(24 - k)
    if v == 1 :
      one.append(k)

  ret = 0
  for i in range(1 << len(one)) :
    L2 = L1[::]
    for j in range(len(one)) :
      if i & 1 :
        L2.append(one[j])
      else :
        L2.append(24 - one[j])

      i >>= 1

    L2.append(0)
    L2.append(24)
    L2.sort()
    tmp = float('inf')
    for i in range(1, len(L2)) :
      tmp = min(tmp, L2[i] -  L2[i-1])
    if tmp != float('inf') :
      ret = max(ret, tmp)

  return ret

print(solve())

Solution (Python):
from collections import Counter
import copy


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

if N >= 24:
    print (0)
    exit()

lst_time = [0]
lst_candidate = []

C = Counter(D)
for i in range(13):
    if i == 0:
        if C[0] > 0:
            print (0)
            exit()
    elif i == 12:
        if C[12] >= 2:
            print (0)
            exit()
        if C[12] == 1:
            lst_time.append(12)
    else:
        if C[i] >= 3:
            print (0)
            exit()
        elif C[i] == 2:
            lst_time.append(i)
            lst_time.append(24 - i)
        elif C[i] == 1:
            lst_candidate.append(i)




def calc(lst):
    tmp = 10 ** 9
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            tmp = min(tmp, min(abs(lst[i] - lst[j]), 24 - abs(lst[i] - lst[j])))
    return tmp

s = 0 
n = len(lst_candidate)
for i in range(2 ** n):
    tmp_lst = copy.deepcopy(lst_time)
    for j in range(n):
        if (i >> j) & 1:
            tmp_lst.append(lst_candidate[j])
        else:
            tmp_lst.append(24 - lst_candidate[j])
    
    
    
    s = max(s, calc(tmp_lst))

print (s)

Solution (Python):
from copy import deepcopy

N = int(input())
D = list(map(int,input().split()))
T = [0 for _ in range(13)]
ans = 0

for i in range(N):
    if D[i]==0:
        print(0)
        break
    T[D[i]] += 1
    if T[D[i]] >=3:
        print(0)
        break
        
else:
    C = [0]
    S = []
    for j in range(13):
        if T[j]==2:
            C.append(j)
            C.append(-j)
        elif T[j]==1:
            S.append(j)

    for j in range(2**len(S)):
        CC = deepcopy(C)
        for k in range(len(S)):
            if ((j >> k) & 1):
                CC.append(S[k])
            else:
                CC.append(-S[k])
                
        mind = 25
        for p in range(N):
            for q in range(p+1,N+1):
                mind = min(mind,min(abs(CC[p]-CC[q]),24-abs(CC[p]-CC[q])))
        ans = max(ans,mind)
        
    print(ans)