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):
def examC():
    N = I(); D = LI()
    ans = -1
    choice = [0]*13; choice[0] +=1
    for i in D:
        choice[i] +=1
        if choice[i]>2:
            ans = 0
            break
    if choice[0]>=2 or choice[12]>=2:
        ans = 0

    curT = [0]; cur = []
    for i in range(1,12):
        if choice[i]==1:
            cur.append(i)
        elif choice[i]==2:
            curT.append(i)
            curT.append(24-i)
    ne = Counter(choice)
    if choice[12]==1:
        ne[1] -=1
        curT.append(12)
    loop = 2**(ne[1]-1)

    if ans==0:
        print(ans)
        exit()
    ans = 0
    for i in range(loop):
        now = copy.deepcopy(curT); ansC = 24
        for j in range(ne[1]-1):
            if i&(1<<j)==(1<<j):
                now.append(cur[j])
            else:
                now.append(24-cur[j])
        now.sort()
        for k in range(len(now)):
            if k != len(now) - 1:
                ansC = min(ansC, now[k+1]-now[k], 24-(now[k+1]-now[k]))
            else:
                ansC = min(ansC, now[k], 24-now[k])
        ans = max(ans, ansC)
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Solution (Python):
import sys
read = sys.stdin.read

N, *D = map(int, read().split())

D_1 = [D.count(i) for i in range(13)]

if D_1[0] or D_1[-1] > 1:
    print(0)
    exit()

for i in D_1:
    if i > 2:
        print(0)
        exit()


d = [False] * (12 + 1 + 12)


d[12] = True

rl = True
for i in range(13):
    for _ in range(D_1[i]):
        if rl:
            d[12 + i] = True
            rl = False
        else:
            d[12 - i] = True
            rl = True

a = [i for i, j in enumerate(d) if j]
a.append(24 + a[0])
s = 24
for i, j in zip(a, a[1:]):
    if j - i < s:
        s = j - i

print(s)

Solution (Python):
import collections
import copy
N = int(input())
D = [int(i) for i in input().split()]

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

D1 = [0, 24]
m = []
for k, v in C.items():
    if v == 2:
        D1.append(k)
        D1.append(24-k)
    elif v == 1:
        m.append(k)

ans = 0
for i in range(2 ** len(m)):
    D0 = copy.deepcopy(D1)
    for j in range(len(m)):  
        if ((i >> j) & 1):  
            D0.append(24-m[j])
        else:
            D0.append(m[j])

    

    D0 = sorted(D0)
    c = 24
    for i in range(N+1):
        c = min(c, D0[i+1]-D0[i])
    ans = max(ans, c)

print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



from itertools import product

N = I()
D = III()

if 0 in D:
    print(0)
elif N>=24:
    print(0)
elif N==23:
    D.sort()
    for i in range(N):
        if D[i]!=i//2+1:
            print(0)
            exit()
    print(1)
else:
    a = list(product([0,1],repeat=N))
    ans = 0
    for a0 in a:
        l = [False]*24
        flag = False
        for i in range(N):
            if a0[i]==0:
                if l[D[i]]:
                    flag = True
                    break
                else:
                    l[D[i]] = True
            else:
                if l[24-D[i]]:
                    flag = True
                    break
                else:
                    l[24-D[i]] = True
        if flag:
            continue
        before = 0
        val = 13
        for i in range(24):
            if l[i]:
                temp = min(i-before, 24-i+before)
                before = i
                if temp<val:
                    val = temp
        temp = min(before,24-before)
        if temp<val:
            val = temp
        if val>ans:
            ans = val
    print(ans)

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

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


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

D.sort()


def dist(h, counts):
    i = 0
    while counts[(h - i) % 24] == counts[(h + i) % 24] == 0:
        i += 1
    return i


counts = [0] * 25
counts[0] = 1
counts[24] = 1
for i in range(N):
    if dist(D[i], counts) > dist(24 - D[i], counts):
        counts[D[i]] += 1
    else:
        counts[24 - D[i]] += 1

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

cc = []
for i in range(len(counts)):
    if counts[i]:
        cc.append(i)

ans = 24
for a, b in zip(cc, cc[1:]):
    ans = min(ans, abs(a - b))
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l1=sorted(l)
import copy
if l1[0]==0:
  print(0)
else:
  d={}
  for i in range(1,13):
    d[i]=0
  for i in range(n):
    d[l1[i]]+=1

  d1=[]
  d2=[]
  d1.append(12)
  for i in range(1,13):
    if d[i]>=3:
      print(0)
      exit()
    elif d[i]==2:
      d1.append(12+i)
      d1.append(12-i)
      d[i]=0
    elif d[i]==1:
      d2.append(i)
  
  num=len(d2)
  num1=2**num-1
  bitlist=[]
  b=[]
  while num1!=0:
    b.append(num1%2)
    num1//=2
  bitlist.append(b)
  for i in range(2,2**num+1):
    num1=2**num-i
    b=[]
    while num1!=0 or len(b)<len(bitlist[0]):
      b.append(num1%2)
      num1//=2
    bitlist.append(b)
  
  m=0
  for i in range(2**num):
    d3=copy.deepcopy(d1)
    b1=bitlist[i]
    m1=24
    for j in range(num):
      if b1[j]==1:
        d3.append(12+d2[j])
      else:
        d3.append(12-d2[j])
    d3=sorted(d3)
    for i in range(1,len(d3)):
      if m1>d3[i]-d3[i-1]:
        m1=d3[i]-d3[i-1]
    if m1>(24-d3[-1])+d3[0]:
      m1=(24-d3[-1])+d3[0]
    if m<m1:
      m=m1
  print(m)

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

DC = {}
for d in D:
  if d == 0:
    print(0)
    exit()
  if d in DC:
    DC[d] += 1
  else:
    DC[d] = 1

def Gap(T):
  res = 10**18
  mn = 0
  for i in range(1,len(T)):
    if T[i]:
      res = min(res,i-mn)
      mn = i
  return res

T = [True] + [False for _ in range(23)] + [True]
DD = []
for k,v in DC.items():
  if v == 1:
    DD.append(k)
  elif v == 2:
    T[k] = True
    T[24-k] = True
  else:
    print(0)
    exit()

if len(DD) == 0:
  print(Gap(T))
else:
  ans = 0
  for i in range(1<<len(DD)):
    TT = T[:]
    for j in range(len(DD)):
      if (i>>j)&1 == 1:
        TT[DD[j]] = True
      else:
        TT[24-DD[j]] = True
    ans = max(ans,Gap(TT))
  print(ans)

Solution (Python):
n=int(input())
d=[int(j) for j in input().split()]
d.sort()
d=[0]+[j if i&1 else 24-j for i,j in enumerate(d,1)]
d.sort()
ans=min(j-i for i,j in zip(d,d[1:]))
print(ans)

Solution (Python):
from collections import Counter
N=int(input())
D=list(map(int,input().split()))
c=Counter(D)
  
S=[]
D=[]
for i in c:
  if c[i]==1:
    S.append(i)
  elif c[i]==2:
    D.append(i)
    D.append(24-i)
  else:
    print(0)
    exit()

n=len(S)
ans=-1
for i in range(2**n):
  L=[d for d in D]
  for j in range(n):
    if (i>>j)&1==0:
      L.append(S[j])
    else:
      L.append(24-S[j])
    
  L.append(0)
  L.sort()
  m=25
  for i in range(N+1):
    d=abs(L[i]-L[i-1])
    m=min(d,24-d,m)
  ans=max(ans,m)
  
print(ans)

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, D):
    D.append(0)
    C = Counter(D)
    if C[0] > 1 or C[12] > 1:
        return 0
    

    K = []
    F = []
    for k in C:
        if C[k] > 2:
            return 0
        elif C[k] == 2:
            F.append(24-k)
            F.append(k)
        else:
            K.append(k)
    M = len(K)
    ans = 0
    for S in product([1, -1], repeat=M):
        tmp = []
        for s, v in zip(S, K):
            tmp.append(s*v)
        tmp.extend(F)
        tmp.sort()
        cand = INF
        for i in range(len(tmp)):
            d = abs(tmp[i] - tmp[i-1])
            cand = min(cand, min(24-d, d))

        ans = max(ans, cand)

    return ans

def main():
    N = read_int()
    D = read_int_n()
    print(slv(N, D))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque

N = int(input())

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

ans = 0

dic = {}
dic[0] = 1

for i in D:

    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

q = deque([[]])

for i in dic:
    nq = deque([])

    if dic[i] >= 3 or (i in [0,12] and dic[i] >= 2):
         print (0)
         sys.exit()

    elif dic[i] == 2:

        for j in q:
            nj = j.copy()
            nj.append(i)
            nj.append(24-i)
            nq.append(nj)

    else:

        for j in q:

            nj = j.copy()
            nj.append(i)
            nq.append(nj)

            if i not in [0,12]:
                nj = j.copy()
                nj.append(24-i)
                nq.append(nj)

    q = nq



while len(q) > 0:

    nlis = q.pop()
    nlis.sort()

    nlis.append(nlis[0])
    nmin = float("inf")

    for i in range(len(nlis)-1):
        
        nmin = min(nmin, abs(nlis[i] - nlis[i+1]) , 24 - abs(nlis[i] - nlis[i+1]))

    ans = max(ans,nmin)

print (ans)

Solution (Python):
def main():
    from collections import Counter
    def check(l):
        ll=[]
        for i in range(24):
            if l[i]:
                ll.append(i)
                ll.append(i+24)
        ll.sort()
        return min([ll[i+1]-ll[i] for i in range(len(ll)-1)])

    def dfs(d_dfs,l_dfs):
        if len(d_dfs)==0:
            return check(l_dfs)
        l_dfs1=[i for i in l_dfs]
        l_dfs2=[i for i in l_dfs]
        d_dfs1=[i for i in d_dfs]
        i=d_dfs1.pop()
        l_dfs1[i]=True
        l_dfs2[24-i]=True
        return max(dfs(d_dfs1,l_dfs1),dfs(d_dfs1,l_dfs2))

    n=int(input())
    a=list(map(int,input().split()))
    a=Counter(a)
    d=[]
    now=[False]*24
    now[0]=True
    if a[0]>0 or a[12]>1:
        print(0)
        return 0
    if a[12]==1:
        now[12]=True
    for i in range(1,12):
        if a[i]>2:
            print(0)
            return 0
        elif a[i]==2:
            now[i]=True
            now[24-i]=True
        elif a[i]==1:
            d.append(i)
    print(dfs(d,now))
main()

Solution (Python):

n=int(input())
lists=list(map(int,input().split()))
if n>24:
    print(0)
else:
    timelist=[0 for i in range(13)]
    
    for i in lists:
        timelist[i]+=1
    flag=True
    for j in range(13):
        if j==0:
            if timelist[j]>0:
                flag=False
        else:
            if timelist[j]>2:
                flag=False
    if flag:
        uselist1=[]
        uselist2_=[]
        for i in range(13):
            if timelist[i]==1:
                uselist1.append((i))
            if timelist[i]==2:
                uselist2_.append((i))
            else:
                continue
        uselist2=[]
        for j in uselist2_:
            uselist2.append(j)
            uselist2.append(24-j)
        uselist2=list(set(uselist2))
        lens=len(uselist1)
        minilist=[]
        for i in range(2**lens):
            sublist=[0]
            mini=100
            sublist=sublist+uselist2
            j=format(i,"0%ib"%lens)
            for k in range(lens):
                if j[k]=="0":
                    sublist.append(uselist1[k])
                else:
                    sublist.append(24-uselist1[k])
            sublists=sorted(sublist)
            lenss=len(sublists)
            for k in range(1,lenss):
                mini=min(mini,sublists[k]-sublists[k-1])
            mini=min(mini,24-sublists[-1],sublists[-1])
            minilist.append(mini)  
        else:
            print(max(minilist))
    if not flag:
        print(0)

Solution (Python):

n=int(input())
lists=list(map(int,input().split()))
if n>24:
    print(0)
else:
    timelist=[0 for i in range(13)]
    
    for i in lists:
        timelist[i]+=1
    flag=True
    for j in range(13):
        if j==0:
            if timelist[j]>0:
                flag=False
        else:
            if timelist[j]>2:
                flag=False
    if flag:
        uselist1=[]
        uselist2_=[]
        for i in range(13):
            if timelist[i]==1:
                uselist1.append((i))
            if timelist[i]==2:
                uselist2_.append((i))
            else:
                continue
        uselist2=[]
        for j in uselist2_:
            uselist2.append(j)
            uselist2.append(24-j)
        uselist2=list(set(uselist2))
        lens=len(uselist1)
        minilist=[]
        for i in range(2**lens):
            sublist=[0]
            mini=100
            sublist=sublist+uselist2
            j=format(i,"0%ib"%lens)
            for k in range(lens):
                if j[k]=="0":
                    sublist.append(uselist1[k])
                else:
                    sublist.append(24-uselist1[k])
            sublists=sorted(sublist)
            lenss=len(sublists)
            for k in range(1,lenss):
                mini=min(mini,sublists[k]-sublists[k-1])
            mini=min(mini,24-sublists[-1],sublists[-1])
            minilist.append(mini)  
        else:
            print(max(minilist))
    if not flag:
        print(0)

Solution (Python):
import sys
import heapq
input = sys.stdin.readline
N = int(input())
d = list(map(int, input().split()))
rres = 0
if N == 1:
  print(d[0])
  exit(0)
for t in range(13):
  res = True
  q = sorted(d)
  heapq.heapify(q)
  l = [0, 24]
  i = 0
  c = 0
  while len(q) and c < 2 * N ** 2:
    p = heapq.heappop(q)
    if abs(p - l[i]) < t:
      heapq.heappush(q, 24 - p)
    else:
      i += 1
      l.append(p)
      l.sort()
    c += 1
  if c >= 2 * N ** 2:
    res = False
  l.sort()
  for i in range(1, len(l)):
    if abs(l[i - 1] - l[i]) < t:
      res = False
      break
  
  if res:
    rres = t
  else:
    break
 
print(rres)

Solution (Python):
from collections import Counter
inf = float('inf')

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

def cal(a):
    n = len(a)
    res = inf
    for i in range(n):
        for j in range(i+1, n):
            d = abs(a[i] - a[j])
            d = min(d, 24 - d)
            res = min(res, d)
    return res

c = Counter(D)
ans = -inf
for S in range(1 << 13):
    D = []
    if c[0] > 1 or c[12] > 1:
        print(0)
        exit()
    if c[0] == 1:
        D.append(0)
    if c[12] == 1:
        D.append(12)
    for j in range(1, 12):
        if c[j] == 0:
            continue
        elif c[j] == 1:
            if (S >> j) & 1:
                D.append(j)
            else:
                D.append(24-j)
        elif c[j] == 2:
            D.append(j)
            D.append(24-j)
        else:
            print(0)
            exit()
    ans = max(ans, cal(D))

print(ans)

Solution (Python):
inf = float('inf')

N = int(input())
D = sorted(map(int,input().split()), reverse=True) + [0]
N += 1

for i in range(N):
    if i % 2 == 0:
        D[i] = 24 - D[i]

ans = inf
for i in range(N):
    for j in range(i+1, N):
        d = abs(D[i] - D[j])
        d = min(d, 24 - d)
        ans = min(ans, d)

print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
if n>=22:
    t=[]
    d.append(0)
    d.sort()
    f=1
    for i in range(n):
        if d[i]==d[i+1]:
            f+=1
        else:
            if f>2:
                print(0)
                exit()
            f=1
    print(1)
    exit()

op=2**n-1
ans=0
for i in range(op):
    t=[0]
    for j in range(n):
        if i&(2**j)>0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1,1+n):
        for q in range(p):
            b=abs(t[p]-t[q])
            a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

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

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

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

Solution (Python):
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()
if D.count(0) >= 2:
    print(0)
    exit()
for i in range(1, 13):
    if D.count(i) >= 3:
        print(0)
        exit()
for i in range(13):
    if (D.count(i) + D.count(i+1)) == 4:
        print(1)
        exit()


def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Solution (Python):
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()
for i in range(13):
    if D.count(i) >= 3:
        print(0)
        exit()
    elif D.count(i) == 2 and D.count(i+1) == 2:
        print(1)
        exit()


def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Solution (Python):
import sys
import collections

N = int(input())
D = list(map(int, input().split()))
D.append(0)
D.sort()
count = collections.Counter(D)

E = []
F = []
for k, v in count.items():
  if v >= 3:
    print(0)
    sys.exit(0)
  if v == 2:
    F.append(24 - k)
    F.append(k)
  if v == 1:
    E.append(k)
E.sort()
for i in range(len(E)):
  if i % 2 == 0:
    F.append(E[i])
  else:
    F.append(24 - E[i])
F.append(24)
F.sort()
ans = 24
for i in range(len(F) - 1):
    ans = min(ans, F[i+1] - F[i])
print(ans)

Solution (Python):
N = int(input())
D = [0]+[int(x) for x in input().split()]
D.sort()
li = []
for ind, d in enumerate(D):
    if(ind%2==0):
        li.append(d)
    else:
        li.append(24-d)
li.sort()
li.append(24)
ans = 24
for ind, d in enumerate(li[:-1]):
    ans = min(li[ind+1]-d, ans)
print(ans if ans!=24 else 0)

Solution (Python):
import sys
import collections
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")

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

c=collections.Counter(d)
if c.most_common()[0][1]>2:
    print(0)
    quit()
if 0 in c:
    if c[0]==1:
        print(0)
        quit()

li=list(map(int,c.keys()))
li.append(0)
ans=0


for i in range(2**len(li)):
    hour=[]
    for j in range(len(li)):
        if c[li[j]]==2:
            hour.append(li[j])
            hour.append(24-li[j])
            continue
        if i&(1<<j):
            hour.append(li[j])
        else:
            hour.append(24-li[j])
    hour.sort()
    
    tmp=24+hour[0]-hour[-1]
    for j in range(len(hour)-1):
        tmp=min(tmp,hour[j+1]-hour[j])
    
    ans=max(ans,tmp)
print(ans)

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

Solution (Python):
from collections import Counter 
import itertools

N = int(input())
D = list(map(int, input().split()))
D.append(12)
M = Counter(D)
if max(M.values()) > 2:
    print(0)
elif 0 in D:
    print(0)
elif N > 11:
    print(1)
else:
    D.remove(12)
    D = [[x, 24-x] for x in D]
    generator = list(itertools.product([0, 1], repeat=N))
    D_gen = [[D[i][g[i]] for i in range(N)] for g in generator]
    ans = 0
    for d in D_gen:
        if len(set(d)) < N:
            pass
        else:
            tmp = [0] + sorted(d) + [24]
            s = 13
            for i in range(N+1):
                s = min(s, tmp[i+1]-tmp[i])
            ans = max(ans, s)
    print(ans)

Solution (Python):

def main():
    N, *D = map(int, open(0).read().split())
    D.sort()
    D = [0] + [d if i & 1 else 24 - d for i, d in enumerate(D, 1)]
    D.sort()
    ans = min(j - i for i, j in zip(D, D[1:]))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, *D = map(int, open(0).read().split())
    D.append(0)
    D.sort()
    for i in range(1, N + 1):
        if i & 1 == 0:
            D[i] = 24 - D[i]
    D.sort()
    ans = min(j - i for i, j in zip(D, D[1:]))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
import copy

N = int(input())
D = list(map(int, input().split()))
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

D.append(0)
D.append(24) 

c = Counter(D)

times = []
p = []
for num, cnt in c.items():
    if (num == 0 or num == 12) and cnt >= 2:
        
        print(0)
        exit()
    elif cnt >= 3:
        
        print(0)
        exit()
    elif cnt == 2:
        
        times.append(num)
        times.append(24-num)
    elif cnt == 1:
        
        p.append((num, 24-num))

num_b = len(p)
ans = 0

for b in range(2**num_b):
    b = bin(b)[2:].zfill(num_b)
    time = copy.deepcopy(times)
    for i in range(num_b):
        if b[i] == "0":
            time.append(p[i][0])
        else:
            time.append(p[i][1])
    tmp = 24
    for i in range(len(time)):
        for j in range(i+1, len(time)):
            tmp = min(tmp, abs(time[i] - time[j]))

    ans = max(ans, tmp)

print(ans)

Solution (Python):
from collections import Counter
import copy

N = int(input())
D = list(map(int, input().split()))
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

D.append(0)
D.append(24)

c = Counter(D)

times = []
p = []
for num, cnt in c.items():
    if (num == 0 or num == 12) and cnt >= 2:
        
        print(0)
        exit()
    elif cnt >= 3:
        
        print(0)
        exit()
    elif cnt == 2:
        
        times.append(num)
        times.append(24-num)
    elif cnt == 1:
        
        p.append((num, 24-num))

num_b = len(p)
ans = 0

for b in range(2**num_b):
    b = bin(b)[2:].zfill(num_b)
    time = copy.deepcopy(times)
    for i in range(num_b):
        if b[i] == "0":
            time.append(p[i][0])
        else:
            time.append(p[i][1])
    tmp = 24
    for i in range(len(time)):
        for j in range(i+1, len(time)):
            tmp = min(tmp, abs(time[i] - time[j]))

    ans = max(ans, tmp)

print(ans)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
D.append(0)
D.sort()
min_v = D[1]
for n in range(1,N+1,2):
  D[n] = 24 - D[n]
D.sort()
for i in range(1, N+1):
  min_v = min(min_v, D[i] - D[i - 1])
print(min_v)

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

Solution (Python):
from collections import Counter
N = int(input())
now_time = [0, 24]
D = list(map(int, input().split()))
c = Counter(D)
candidate = []
if N == 1:
    print(min(D[0], 24-D[0]))
    exit()

for num, amount in c.items():
    if num == 0:
        print(0)
        exit()
    if amount > 2:
        print(0)
        exit()
    if amount == 2:
        
        now_time.append(num)
        now_time.append(24-num)
    if amount == 1:
        candidate.append((num, 24-num))  
ans = 0

for mask in range(1 << len(candidate)):
    list_0_1 = []
    time_list = now_time.copy()  
    for i in range(len(candidate)):
        list_0_1.append(int((mask >> i) & 1))
    
    for i, num in enumerate(list_0_1):
        time_list.append(candidate[i][num])
    
    tmp = 100
    for i in range(len(time_list)):
        for j in range(i+1, len(time_list)):
            tmp = min(tmp, abs(time_list[i] - time_list[j]))
    ans = max(tmp, ans)


print(ans)

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

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

    de = []
    count = [0] * 12
    for d in D:
        if d == 0 or d == 12:
            de.append(d)
        else:
            count[d] += 1
    
    ind = []
    for i in range(12):
        if count[i] == 1:
            ind.append(i)
        elif count[i] == 2:
            de.append(i)
            de.append(24-i)
        elif count[i] >= 3:
            print(0)
            return
    
    for i in range(len(ind)):
        if i&1:
            de.append(ind[i])
        else:
            de.append(24-ind[i])

    de = sorted(de)

    
    
    

    ans = de[0] - 0
    for i in range(1, N):
        ans = min(ans, de[i] - de[i-1])
    ans = min(ans, 24-de[-1])
    print(ans)
    



if __name__ == "__main__":
    main()

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

cnt_memo = {}
for num in s:
    if num in cnt_memo:
        cnt_memo[num] += 1
    else:
        cnt_memo[num] = 1

kakutei = [0]
mikakutei = []
for i in cnt_memo:
    if i == 0:
        print(0)
        exit()
    elif i == 12:
        if cnt_memo[i] == 1:
            kakutei.append(12)
        else:
            print(0)
            exit()
    else:
        if cnt_memo[i] == 1:
            mikakutei.append(i)
        elif cnt_memo[i] == 2:
            kakutei.append(i)
            kakutei.append(24 - i)
        else:
            print(0)
            exit()
tmp_ans1 = 25
for num1 in kakutei:
    for num2 in kakutei:
        if num1 != num2:
            tmp_ans1 =  min(tmp_ans1, min(abs(num1 - num2), 24 -abs(num1 - num2)))

len_mikakutei = len(mikakutei)
mikakutei_pattern = 2**len_mikakutei
ans = 0
for i in range(mikakutei_pattern):
    tmp = []
    num = i
    cnt = 0
    for _ in range(len_mikakutei):
        if num % 2 == 1:
            tmp.append(mikakutei[cnt])
        else:
            tmp.append(24 - mikakutei[cnt])
        num //= 2
        cnt += 1
    tmp_ans = 25
    for num1 in tmp:
        for num2 in kakutei:
            tmp_ans = min(tmp_ans, min(abs(num1 - num2), 24 -abs(num1 - num2)))
    for num1 in tmp:
        for num2 in tmp:
            if num1 != num2:
                tmp_ans = min(tmp_ans, min(abs(num1 - num2), 24 -abs(num1 - num2)))
    tmp_ans = min(tmp_ans, tmp_ans1)
    ans = max(ans, tmp_ans)

print(ans)

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


D.sort()
ans = [0]
ans = [D[i]
       for i in range(N) if i % 2 == 0] + [24 - D[i] for i in reversed(range(N)) if i % 2 == 1]

ans = [0] + ans + [0]

ans = [abs(ans[i+1] - ans[i]) for i in range(N+1)]

print(min(ans))

Solution (Python):
import collections
N = int(input())
D = list(map(int, input().split()))
D.append(0)
ans = 0  
    
x = collections.Counter(D)
y = x.most_common(1)


if y[0][1] >= 3:
  print(ans)
  quit()
         
if y[0][1] == 2:
  if (y[0][0] == 0) or (y[0][0] == 12):
    print(ans)
    quit()
 
day = [0] * 24 
ilist = [0] * 13
for i in range(13):
  if (i == 0) or (i == 12):
    if i in D:
      day[i] = 1
      ilist[i] = 1
  else:
    if D.count(i) == 2:
      day[i] = 1
      day[24 - i] = 1
      ilist[i] = 2
    elif D.count(i) == 1:
      
      p = 0
      l = 1
      while p == 0:
        p = ilist[i - l]
        if p == 0:
          l += 1
        if (p == 0) and (l == i):
          p = 3
      
      if p == 3:
        day[i] = 1
      if p == 2:
        day[i] = 1
      if p == 1:
        
        if day[i - l] == 1:
          day[24-i] = 1
        else:
          day[i] = 1
      ilist[i] = 1
      

day2 = day * 2
ans = 25
ans2 = 1
xp = 1

for i in range(len(day2)):
  if day2[i] == 0:
    ans2 += 1
    xp = 0
  elif (day2[i] == 1) and (xp == 0):
    
    ans = min(ans, ans2)
    ans2 = 1

if ans > 24:
  ans = ans - 24
print(ans)