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()))

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


Times = [0]*24
ones =[]
for i,t in enumerate(T):
    if t>=3 or (t==2 and (i==0 or i==12)):
        print(0)
        exit()
    elif t==2:
        Times[i] = 1
        Times[24-i] = 1
    elif t==1:
        ones.append(i)

ans = 0
L = len(ones)
for i in range(2**L):
    stats = [False]*L
    temp = i
    for j in range(L):
        stats[j] = temp&1
        temp >>= 1
    
    newTime = [t for t in Times]
    res = 10**9
    for j,t in enumerate(ones):
        nt = t if stats[j] else (24-t)%24
        newTime[nt] = 1
    
    gaps = []
    for j,t in enumerate(newTime):
        if t!=0:
            gaps.append(j)
    for j in range(len(gaps)-1):
        res = min(res,min(gaps[j+1]-gaps[j],gaps[j]+24-gaps[j+1]))
    if len(gaps)>=2:
        res = min(res, gaps[0]+24-gaps[-1])
    ans = max(ans, res)
print(ans)

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

D.sort()
T = []
for i,d in enumerate(D):
    if i % 2 == 1:
        d = 24- d
    T.append(d)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *d = map(int, read().split())
d.sort()
p = [0, 24]
m = [0, 24]
for i, dd in enumerate(d):
    if i % 2 == 0:
        p.append(dd)
        m.append(24 - dd)
    else:
        p.append(24 - dd)
        m.append(dd)
pp, mm = float('inf'), float('inf')
p.sort()
m.sort()
for (bp, ap, bm, am) in zip(p, p[1:], m, m[1:]):
    pp = min(pp, ap - bp)
    mm = min(mm, am - bm)
print(max(pp, mm))

Solution (Python):
N=int(input())
D=list(map(int,input().split())) +[0]
from collections import Counter
cnt=Counter(D)

if cnt[0]>=2 or cnt[12]>=2:
    print(0)
    exit()

if any(v>=3 for v in cnt.values()):
    print(0)
    exit()

ans=0
for b in range(1<<11):
    ts=[0]*24
    ts[0]=1
    if cnt[12]:
        ts[12]=1
    for i in range(11):
        t=i+1
        if cnt[t]==0:
            continue
        if cnt[t]==2:
            ts[t]=ts[24-t]=1
            continue
        if b&(1<<i):
            ts[t]=1
        else:
            ts[24-t]=1
    tt=[i for i,b in enumerate(ts) if b]
    tmp=24-tt[-1]
    for t1,t2 in zip(tt,tt[1:]):
        tmp=min(tmp,t2-t1)
    ans=max(tmp,ans)
print(ans)

Solution (Python):
n = int(input())
d = [0]*13
for i in map(int,input().split()):
  if i == 0:
    print(0)
    exit()
  d[i] += 1
t = [0]*25
t[0] = t[-1] = 1
c = 0
for i,j in enumerate(d):
  if j >=3:
    print(0)
    exit()
  elif j == 0:
    continue
  elif j == 2:
    t[i] = t[24-i] = 1
  else:
    if c == 0:
      t[i] = 1
    else:
      t[24-i] = 1
    c = 1-c
m = 24
ans = 24
for i in t:
  m += 1
  if i:
    ans = min(ans,m)
    m = 0
print(ans)

Solution (Python):
n=int(input())
a=[0]+list(map(int,input().split()))
a.sort()
for i in range(n-1):
	if a[i]==a[i+1]==a[i+2]:
		print(0)
		exit()
double=[]
for i in range(n):
	if a[i]==a[i+1]:
		if a[i]==12 or a[i]==0:
			print(0)
			exit()
		else:
			double.append(a[i])
a=set(a);a=list(a)
b=[]
for x in a:
	if x not in double:
		b.append(x)
from copy import copy
a=copy(b)
n=len(a)
ans=0
def two(x):
	return (n-len(bin(x)[2:]))*"0"+bin(x)[2:]
for i in range(2**n):
	r=two(i)
	time=[0]*24
	time[0]=1
	for x in double:
		time[x]=1
		time[24-x]=1
	for j in range(n):
		if r[j]=="1":
			time[a[j]]=1
		else:
			if a[j]!=0:
				time[24-a[j]]=1
			else:
				time[0]=1
	pre_ans=24
	ren=0
	for j in range(1,24):
		if time[j]==0:
			ren+=1
		else:
			pre_ans=min(pre_ans,ren)
			ren=0
	pre_ans=min(pre_ans,ren)
	ans=max(ans,pre_ans+1)
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
D = [0] + list(map(int, input().split()))

C = Counter(D)

if C[0] >= 2 or C[12] >= 2 or any(v >= 3 for v in C.values()):
  print(0)
  exit()
  
ans = 0
for b in range(1<<11):
  T = []
  T.append(0)
  if C[12]:
    T.append(12)
  for i in range(11):
    if C[i + 1] == 2:
      T.append(i + 1)
      T.append(24 - (i + 1))
    else:
      if C[i + 1] == 1:
        if b & (1 << i):
          T.append(i + 1)
        else:
          T.append(24 - (i + 1))
  T.sort()
  T = T + [24]
  tmp = min(j - i for i, j in zip(T, T[1:]))
  ans = max(ans, tmp)
print(ans)

Solution (Python):


from collections import Counter
from collections import defaultdict

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




c = Counter(D)

c[0] += 1

if c[0] >= 2:
    print(0)
    exit(0)


for i in c.values():
    if i >= 3:
        print(0)
        exit()


times = defaultdict(int)


c[24] += 1
before_12 = True
D.sort()
for d in D:
    if before_12:
        times[d] += 1
        before_12 = False
    else:
        times[24-d] += 1
        before_12 = True



ans = min(D)

tlst = list(times.keys())
tlst.sort()

for a,b in zip(tlst, tlst[1:]):
    ans = min(ans, b - a)

print(ans)

Solution (Python):


from collections import Counter
from collections import defaultdict

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




c = Counter(D)



if c[0] >= 2:
    print(0)
    exit(0)


for i in c.values():
    if i >= 3:
        print(0)
        exit()


times = defaultdict(int)
c[0] += 1

c[24] += 1
before_12 = True
D.sort()
for d in D:
    if before_12:
        times[d] += 1
        before_12 = False
    else:
        times[24-d] += 1
        before_12 = True



ans = min(D)

tlst = list(times.keys())
tlst.sort()

for a,b in zip(tlst, tlst[1:]):
    ans = min(ans, b - a)

print(ans)

Solution (Python):
n = int(input())
d = sorted(list(map(int, input().split())))
ans_list = [0,24]
for k,v in enumerate(d):
  ans_list.append(24-v) if k % 2 else ans_list.append(v)
ans_list.sort()
ans = 12
for i in range(len(ans_list)-1):
  ans = min(ans, ans_list[i+1]-ans_list[i])
print(ans)

Solution (Python):
MOD = 10 **9 + 7
INF = 10 ** 10
from collections import Counter
from copy import deepcopy

def main():
    N = int(input())
    D = Counter(map(int,input().split()))
    l = 0
    time = [0]
    either = []
    for k,v in D.items():
        if v == 1:
            l += 1
            either.append(k)
        elif v == 2:
            if k == 12:
                print(0)
                return
            time.extend((k,24 - k))
        else:
            print(0)
            return
    
    if l == 0:
        gap = INF
        for t in time:
            for s in time:
                if s == t:
                    continue
                gap = min(gap,min(abs(s - t),24 - abs(s - t)))
        print(gap)
        return
    
    ans = 0
    for bit in range(1<<l):
        T = deepcopy(time)
        gap = INF
        for i in range(l):
            if (bit>>i)&1:
                t = either[i]
            else:
                t = 24 - either[i]
            for x in T:
                gap = min(gap,min(abs(t - x),24 - abs(t - x)))
            T.append(t)
        ans = max(ans,gap)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):












N = int(input())
D = list(map(int,input().split()))
D = sorted(D,key = lambda x:min(x,24-x)) 

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




ans = 0

for i in range(2 ** N):
  data = [0] * 25
  data[0] = 1 
  data[24] = 1 
  for j in range(N):
    if (i >> j)&1:
      if data[24-D[j]] == 1:
        break
      data[24-D[j]] += 1
    else:
      if data[D[j]] == 1: 
        break
      data[D[j]] += 1
  else:
    
    minval = 24
    lastexist = 0
    for j in range(1,len(data)):
      if data[j]:
        if j - lastexist < minval:
          minval = j - lastexist
        lastexist = j
    if minval > ans:
      ans = minval
print(ans)

Solution (Python):
import copy
N = int(input())
Dn = list(map(int,input().split(' ')))
d_map = {}
for d in Dn:
    if d not in d_map:
        d_map[d] = 1
    else:
        d_map[d] += 1
if 0 in d_map:
    print(0)
    exit(0)
if len(Dn) == 1:
    print(Dn[0])
    exit(0)
for v in d_map.values():
    if 3 <= v:
        print(0)
        exit(0)
all_combination = [ [0] ]
for k,v in d_map.items():
    if v == 1:
        len_all_combination = len(all_combination)
        for i in range(len_all_combination):
            tmp = copy.deepcopy(all_combination[i])
            all_combination[i].append(k)
            tmp.append(24-k)
            all_combination.append(tmp)
    else:
        for comb in all_combination:
            comb.append(k)
            comb.append(24-k)
ans = -1
for comb in all_combination:
    comb.sort()
    
    tmp = 100
    for i in range(1,len(comb)):
        if comb[i] > 12:
            tmp = min(tmp,min(comb[i]-comb[i-1],24-comb[i]))
        else:
            tmp = min(tmp,comb[i]-comb[i-1])
    ans = max(ans,tmp)
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
D = list(map(int, input().split()))
D.sort()
d = [0]*25
d[0] = d[-1] = 1
for i in range(N):
    if i & 1:
        d[D[i]] += 1
    else:
        d[24-D[i]] += 1
for i in range(25):
    if d[i] > 1:
        print(0)
        exit()
last, ans = 0, 24
for i in range(1, 25):
    if d[i]:
        ans = min(i-last, ans)
        last = i
print(ans)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
D = lr()

l = {i: 0 for i in range(13)}

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

possible_lists = [[]]

for k, v in l.items():
    if v > 2:
        possible_lists = [[0]]
        break
    elif v > 1:
        new = [[0]] + [pl + [k, 24-k] for pl in possible_lists]
        
        possible_lists = new
    elif v > 0:
        new = [pl + [k] for pl in possible_lists] + [pl + [24-k] for pl in possible_lists]
        possible_lists = new

Max = -1
for pl in possible_lists:
    Min = 13
    pl.sort()
    for i in range(len(pl)):
        if i == 0:
            cand = pl[i]-0
            Min = min(Min, min(cand, 24-cand))
        elif i == len(pl)-1:
            cand = pl[i]-pl[i-1]
            Min = min(Min, min(cand, 24-cand))
            cand = pl[i]-0
            Min = min(Min, min(cand, 24-cand))
        else:
            cand = pl[i]-pl[i-1]
            Min = min(Min, min(cand, 24-cand))
    Max = max(Min, Max)

print(Max)

Solution (Python):
from itertools import groupby

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

def dfs(list,num):
    global ans
    if num==N:
        m=100
        for i in range(0,N):
            for j in range(i+1,N+1):
                s=abs(list[i]-list[j])
                test=min(s,24-s)
                m=min(test,m)
        ans=max(ans,m)
    else:
        dfs(list+[D[num]],num+1)
        dfs(list+[24-D[num]],num+1)

if N>=24:
    print(0)
elif N>=12:
    D.sort()
    D=groupby(D)
    for key,group in D:
        g=len(list(group))
        if key==0:
            print(0)
            exit()
        elif key==12:
            if g>1:
                print(0)
                exit()
        else:
            if g>2:
                print(0)
                exit()
    print(1)
else:
    ans=0
    dfs([0],0)
    print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
A=sorted(A)
D=[0]*25
D[0]+=1
D[24]+=1
for i in range(N):
  if i%2==0:
    D[A[i]]+=1
  else:
    D[24-A[i]]+=1

for i in D:
  if i>1:
    print(0)
    exit()
p=0
ans=24
for i in range(1,25):
  if D[i]==1:
    d=(i-p)
    ans=min(d,ans)
    p=i
print(ans)

Solution (Python):


N = int(input())
D = list(map(int, input().split()))
num_time = [0] * 13
num_time[0] = 2
num_time[12] = 1

fixed = [0]
unfixed = []
for d in D:
  num_time[d] += 1
  if d == 12:
    fixed.append(12)
  if num_time[d] >= 3:
    print(0)
    quit()

for t in range(1, 12):
  if num_time[t] == 2:
    fixed.extend([t, 24 - t])
  elif num_time[t] == 1:
    unfixed.append(t)

ans = 0
n = len(unfixed)
for i in range(2**n):
  t = fixed.copy() + [24]
  
  for j in range(n):
    if i >> j & 1:
      t.append(unfixed[j])
    else:
      t.append(24 - unfixed[j])
  t.sort()
  s = 24
  m = len(t)
  for k in range(m-1):
    _s = t[k+1] - t[k]
    s = min(s, _s)
  ans = max(ans, s)

print(ans)

Solution (Python):
N = int(input())
D = [0] + list(map(int, input().split()))
Diffs = [[[0, 0] for i in range(N+1)] for j in range(N+1)]



candidates = []
for i in range(N+1):
  for j in range(i+1, N+1):
    di, dj = D[i], D[j]
    diff1, diff2 = abs(di-dj), min(24-di-dj, di+dj)
    candidates.extend([[i, j, diff1, 0], [i, j, diff2, 1]])
    Diffs[i][j] = [diff1, diff2]





ans = 0
for idx1, idx2, diff, c in candidates:
  conditions = [-1] * (N+1)
  if c == 0:
    conditions[idx1] = conditions[idx2] = 0
  else:
    conditions[idx1] = 0; conditions[idx2] = 1
  res = True
  for i in range(N+1):
    for j in range(i+1, N+1):
      if idx1 == i and idx2 == j:
        continue
      diff1, diff2 = Diffs[i][j]
      if diff1 < diff and diff2 < diff:
        res = False
        break
      elif diff1 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = 0; conditions[j] = 1
        elif c1 < 0:
          conditions[i] = 1 - c2
        elif c2 < 0:
          conditions[j] = 1 - c1
        else:
          if c1 == c2:
            res = False
            break
      elif diff2 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = conditions[j] = 0
        elif c1 < 0:
          conditions[i] = c2
        elif c2 < 0:
          conditions[j] = c1
        else:
          if c1 != c2:
            res = False
            break
    else:
      continue
    break
  if res:
    ans = max(ans, diff)

print(ans)

Solution (Python):
N = int(input())
D = [0] + list(map(int, input().split()))
Diffs = [[[0, 0] for i in range(N+1)] for j in range(N+1)]

candidates = []
for i in range(N+1):
  for j in range(i+1, N+1):
    di, dj = D[i], D[j]
    diff1, diff2 = abs(di-dj), min(24-di-dj, di+dj)
    candidates.extend([[i, j, diff1, 0], [i, j, diff2, 1]])
    Diffs[i][j] = [diff1, diff2]

ans = 0
for idx1, idx2, diff, c in candidates:
  conditions = [-1] * (N+1)
  if c == 0:
    conditions[idx1] = conditions[idx2] = 0
  else:
    conditions[idx1] = 0; conditions[idx2] = 1
  res = True
  for i in range(N+1):
    for j in range(i+1, N+1):
      if idx1 == i and idx2 == j:
        continue
      diff1, diff2 = Diffs[i][j]
      if diff1 < diff and diff2 < diff:
        res = False
        break
      elif diff1 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = 0; conditions[j] = 1
        elif c1 < 0:
          conditions[i] = 1 - c2
        elif c2 < 0:
          conditions[j] = 1 - c1
        else:
          if c1 == c2:
            res = False
            break
      elif diff2 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = conditions[j] = 0
        elif c1 < 0:
          conditions[i] = c2
        elif c2 < 0:
          conditions[j] = c1
        else:
          if c1 != c2:
            res = False
            break
    else:
      continue
    break
  if res:
    ans = max(ans, diff)

print(ans)

Solution (Python):
N = int(input())
D = [0] + list(map(int, input().split()))
Diffs = [[[0, 0] for i in range(N+1)] for j in range(N+1)]

candidates = []
for i in range(N+1):
  for j in range(i+1, N+1):
    di, dj = D[i], D[j]
    diff1, diff2 = abs(di-dj), min(24-di-dj, di+dj)
    candidates.extend([[i, j, diff1, 0], [i, j, diff2, 1]])
    Diffs[i][j] = [diff1, diff2]

ans = 0
for idx1, idx2, diff, c in candidates:
  conditions = [-1] * (N+1)
  if c == 0:
    conditions[idx1] = conditions[idx2] = 0
  else:
    conditions[idx1] = 0; conditions[idx2] = 1
  res = True
  for i in range(N+1):
    for j in range(i+1, N+1):
      if idx1 == i and idx2 == j:
        continue
      diff1, diff2 = Diffs[i][j]
      if diff1 < diff and diff2 < diff:
        res = False
        break
      elif diff1 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = 0; conditions[j] = 1
        elif c1 < 0:
          conditions[i] = 1 - c2
        elif c2 < 0:
          conditions[j] = 1 - c1
        else:
          if c1 == c2:
            res = False
            break
      elif diff2 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = conditions[j] = 0
        elif c1 < 0:
          conditions[i] = c2
        elif c2 < 0:
          conditions[j] = c1
        else:
          if c1 != c2:
            res = False
            break
    else:
      continue
    break
  if res:
    ans = max(ans, diff)

print(ans)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    D=LI()
    if( 0 in D):
        print(0)
    elif( N >= 24):
        print(0)
    elif( N>= 12):
        C=Counter(D)
        for c in C.values():
            if(c>=3):
                print(0)
                return
        print(1)
    else:
        ans = 0
        for mask in range(1<<N):
            times = [0,24]
            for i in range(N):
                if(mask >> i & 1):
                    times.append(24-D[i])
                else:
                    times.append(D[i])
            times.sort()
            sub = [times[i+1] - times[i] for i in range(len(times)-1)]
            ans = max(ans ,min(sub))
        print(ans)

if __name__ == '__main__':
    main()

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


def solve():
    N = int(input())
    *D, = map(int, input().split())

    l, m = [], []
    for i, j in Counter(D).items():
        if j>=3 or (i==12 and j>=2) or i==0:
            print(0)
            return
        elif j==2:
            l.append(i)
        elif j==1:
            m.append(i)

    size = len(m)
    ans = 0

    for i in product(range(2), repeat=size):
        c = []
        for j, k in enumerate(i):
            if k:
                c.append(m[j])
            else:
                c.append(24-m[j])
        for j in l:
            c.append(j)
            c.append(24-j)

        v = 24
        for j in c:
            d = min(j, 24-j)
            v = min(v, d)

        for j in range(N):
            for k in range(j+1, N):
                d = abs(c[j]-c[k])
                v = min(v, min(d, 24-d))
        ans = max(ans, v)

    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    d = LI()
    ans = 0
    q = [1]
    for i in range(n):
        q_ = []
        while q:
            p = q.pop()
            if not (1 << d[i]) & p:
                q_.append((1 << d[i]) | p)
            if not (1 << (24 - d[i])) & p:
                q_.append((1 << (24 - d[i])) | p)
        q = q_
    for qi in q:
        tmp = 25
        for i in range(25):
            for j in range(i + 1, 25):
                x = (1 << i) | (1 << j)
                if (qi & x) == x:
                    tmp = min(tmp, min(j - i, 24 - (j - i)))
        ans = max(ans, tmp)
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
n = int(input())
D = list(map(int,input().split()))
L = [1] + [0]*12
for d in D:
    L[min(d, 24-d)] += 1
if max(L)>2 or L[0]>1 or L[12]>1:
    print(0)
else:
    ans = 0
    for i in range(2**11):
        T = [1] + [0]*23 + [1]
        for j in range(1, 12):
            if L[j]==2:
                T[j] = 1
                T[24-j] = 1 
            elif L[j]==1:
                if i&(1<<(j-1)):
                    T[j] = 1
                else:
                    T[24-j] = 1
        if L[12] == 1:
            T[12] = 1
        cur = 24
        t = 0
        for j in range(1, 25):
            if T[j]==1:
                cur = min(cur, t+1)
                t = 0
            else:
                t += 1
        ans = max(ans, cur)
    print(ans)

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):

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

frqs = [0 for _ in range(24)]
frqs[0] = 1
for d in D:
    if not d:
        print(0)
        exit(0)

    i = min(d, 24 - d)
    frqs[i] += 1



indices = []
for i in range(13):
    if frqs[i] > 2:
        print(0)
        exit(0)

    elif frqs[i] == 2:
        frqs[i] -= 1
        frqs[24-i] += 1

    elif frqs[i] == 1:
        indices.append(i)


def get(frqs):
    ret = float('inf')
    for i in range(24):
        if frqs[i]:
            for j in range(i+1, 24):
                if frqs[j]:
                    d = abs(j - i)
                    ret = min(ret, min(d, 24 - d))
    return ret

buf = [[]]
for _ in indices:
    buf2 = []
    while buf:
        b = buf.pop()
        buf2.append(b + [True])
        buf2.append(b + [False])

    buf = buf2

ans = get(frqs)
for bools in buf:
    for i, b in zip(indices, bools):
        if i and b:
            frqs[i] -= 1
            frqs[24 - i] += 1

        ans = max(ans, get(frqs))

    for i, b in zip(indices, bools):
        if i and b:
            frqs[i] += 1
            frqs[24 - i] -= 1


print(ans)

Solution (Python):
N, *D = map(int, open(0).read().split())
D.append(0)
used = [0]*(N+1)
cnt = N
while True:
  ls = []
  for i in range(N+1):
    for j in range(N+1):
      if i==j:
        continue
      a,b = min(D[i],D[j]),max(D[i],D[j])
      d = b-a
      ls.append((min(d,24-d),i,j))
  ls.sort(key=lambda x:x[0])
  d,a,b = ls[0]
  if used[a]==0 and used[b]==0:
    da = (24-D[a])%24
    ma = 100
    for i in range(N+1):
      if i==a:
        continue
      x = abs(da-D[i])
      ma = min(ma,x,24-x)
    db = (24-D[b])%24
    mb = 100
    for i in range(N+1):
      if i==b:
        continue
      x = abs(db-D[i])
      mb = min(mb,x,24-x)
    if ma<mb:
      if d>=mb:
        print(d)
        break
      D[b] = db
      used[b] = 1
    else:
      if d>=ma:
        print(d)
        break
      D[a] = da
      used[a] = 1
  elif used[a]==0:
    used[a] = 1
    da = (24-D[a])%24
    ma = 100
    for i in range(N+1):
      if i==a:
        continue
      x = abs(da-D[i])
      ma = min(ma,x,24-x)
    if d>=ma:
      print(d)
      break
    D[a] = da
  elif used[b]==0:
    used[b] = 1
    db = (24-D[b])%24
    mb = 100
    for i in range(N+1):
      if i==b:
        continue
      x = abs(db-D[i])
      mb = min(mb,x,24-x)
    if d>=mb:
      print(d)
      break
    D[b] = mb
  else:
    print(d)
    break

Solution (Python):
n, *a = map(int, open(0).read().split())
a = sorted([0,24]+a)
for i in range(1, n+1, 2): a[i] = 24-a[i]
a.sort()
ans = 24
for i in range(n+1): ans = min(ans, abs(a[i]-a[i+1]))
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from datetime import date, timedelta

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
D = LIST()
D.sort()

cnt = Counter(D)
if cnt.most_common()[0][1] >= 3:
	print(0)
	exit()

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

Solution (Python):
def main():
    _ = int(input())
    D = [int(i) for i in input().split()]
    ans = 0
    cnt = [0]*24
    from collections import Counter
    c = Counter(D)
    cnt[0] = 1
    s = [0]*13
    for k, v in c.items():
        if v >= 3:
            ans = 0
            return print(ans)
        elif k == 0 or k == 12:
            cnt[k] += v
        elif v >= 2:
            cnt[k] += 1
            cnt[24 - k] += 1
        else:
            s[k] += 1
    if any(c >= 2 for c in cnt):
        return print(0)
    for bit in range(1 << 11):
        cur = cnt[:]
        for i in range(11):
            if s[i+1]:
                if bit & (1 << i):
                    cur[24 - (i+1)] += 1
                else:
                    cur[i+1] += 1
        pre = 0
        mi = 24
        for i, ne in enumerate(cur[1:] + [1], start=1):
            if ne:
                if i - pre < mi:
                    mi = i - pre
                pre = i
        ans = max(ans, mi)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    D = [int(i) for i in input().split()]
    ans = 0
    cnt = [0]*24
    from collections import Counter
    c = Counter(D)
    cnt[0] = 1
    s = [0]*13
    for k, v in c.items():
        if v >= 3 or k == 0:
            ans = 0
            return print(ans)
        elif v >= 2:
            cnt[k] += 1
            cnt[24 - k] += 1
        elif k == 12:
            cnt[12] += v
        else:
            s[k] += 1
    if any(c >= 2 for c in cnt):
        return print(0)
    for bit in range(1 << 11):
        cur = cnt[:]
        for i in range(11):
            if s[i+1]:
                if bit & (1 << i):
                    cur[24 - (i+1)] += 1
                else:
                    cur[i+1] += 1
        if any(c >= 2 for c in cnt):
            continue
        pre = 0
        mi = 24
        for i, ne in enumerate(cur[1:] + [1], start=1):
            if ne:
                if i - pre < mi:
                    mi = i - pre
                pre = i
        if mi != 24:
            ans = max(ans, mi)
    print(ans)


if __name__ == '__main__':
    main()

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

ctr = Counter(D)
if ctr[0] >= 2 or ctr[12] >= 2:
    print(0)
    exit()
if any(v >= 3 for v in ctr.values()):
    print(0)
    exit()

ans = 0
for b in range(1<<11):
    ts = [0] * 24
    ts[0] = 1
    if ctr[12]: ts[12] = 1
    for i in range(11):
        t = i+1
        if ctr[t] == 0: continue
        if ctr[t] == 2:
            ts[t] = ts[24-t] = 1
            continue
        if b&(1<<i):
            ts[t] = 1
        else:
            ts[24-t] = 1
    tt = [i for i,b in enumerate(ts) if b]
    tmp = 24 - tt[-1]
    for t1,t2 in zip(tt,tt[1:]):
        tmp = min(tmp, t2-t1)
    ans = max(tmp, ans)
print(ans)

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

memo = {0: 1}
for i in range(n):
    if d[i] not in memo:
        memo[d[i]] = 1
    else:
        memo[d[i]] += 1

used = [] * n
li = []
for i in memo:
    if memo[i] >= 3:
        print(0)
        exit()
    elif memo[i] == 2:
        used.append(i)
        used.append(24 - i)
    else:
        li.append(i)

ans = 0
for bit_state in range(1 << len(li)):
    tmp = used[0:]
    for i in range(len(li)):
        if bit_state & (1 << i):
            tmp.append(li[i])
        else:
            tmp.append(24 - li[i])
    tmp = sorted(tmp)
    diff = 10 ** 18
    for i in range(1, len(tmp)):
        diff = min(diff, tmp[i] - tmp[i - 1])
    diff = min(diff, 24 - (tmp[-1] - tmp[0]))
    ans = max(ans, diff)
print(ans)