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

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


def solve(LIST):
    stmp = [i for i, li in enumerate(LIST * 2) if li == 1]
    S = min(stmp[i] - stmp[i - 1] for i in range(1, len(stmp)))
    return S


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

if any(d[num] > (2 - (num in {0, 12})) for num in d):
    print(0)
    exit()

ans, cnt = [0] * 24, []
for num in d:
    if (num in {0, 12}) and d[num] == 1:
        ans[num] = 1 
    elif d[num] == 2:
        ans[num], ans[24 - num] = 1, 1
    elif d[num] == 1:
        cnt.append(num)

s = 0
if len(cnt) == 0:
    s = max(s, solve(ans))
for bit in product([1, -1], repeat=len(cnt)):
    tmp = ans[:]
    for bi, ci in zip(bit, cnt):
        tmp[(ci - 12) * bi + 12] = 1
    s = max(s, solve(tmp))
print(s)

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


def solve(LIST):
    S = 24
    slvtmp = [i for i, li in enumerate(LIST * 2) if li == 1]
    for i in range(1, len(slvtmp)):
        S = min(S, slvtmp[i] - slvtmp[i - 1])
    return S


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

if any(d[num] > (2 - (num in {0, 12})) for num in d):
    print(0)
    exit()

ans, cnt = [0] * 24, []
for num in d:
    if (num in {0, 12}) and d[num] == 1:
        ans[num] = 1 
    elif d[num] == 2:
        ans[num], ans[24 - num] = 1, 1
    elif d[num] == 1:
        cnt.append(num)

s = 0
if len(cnt) == 0:
    s = max(s, solve(ans))
for bit in product([1, -1], repeat=len(cnt)):
    tmp = ans[:]
    for bi, ci in zip(bit, cnt):
        tmp[(ci - 12) * bi + 12] = 1
    s = max(s, solve(tmp))
print(s)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    d = list(map(int, readline().split()))
    d.sort()
    t = [d[0]]
    s = min(24 - d[0], d[0])

    for i in range(1, n):
        cur = d[i]
        am = cur
        pm = 24 - cur
        s_am = s
        s_pm = s
        for j in range(i):
            prev = t[j]
            s_am = min(s_am, min(24 - abs(am - prev), abs(am - prev)))
            s_pm = min(s_pm, min(24 - abs(pm - prev), abs(pm - prev)))

        if s_am > s_pm:
            t.append(am)
            s = s_am
        else:
            t.append(pm)
            s = s_pm

    print(s)


if __name__ == '__main__':
    main()

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
	n=II()
	D=LI()+[0]

	D.sort()
	

	DD=[]

	for i,v in enumerate(D):
		if i%2==0:
			DD.append(v)
		else:
			DD.append(24-v)
	DD.sort()
	DD.append(24)

	

	ans=25

	for i,j in zip(DD[:-1],DD[1:]):
		ans=min(ans,j-i)
	print(ans)











if __name__ == "__main__":
	main()

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
	n=II()
	D=LI()+[0]

	D.sort()
	

	DD=[]

	for i,v in enumerate(D):
		if i%2==0:
			DD.append(v)
		else:
			DD.append(24-v)
	DD.sort()
	DD.append(24)

	

	ans=25

	for i,j in zip(DD[:-1],DD[1:]):
		ans=min(ans,j-i)
	print(ans)











if __name__ == "__main__":
	main()

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

if 0 in dl:
    print(0)
    exit()

dl.sort()
dl1 = [0,24]
dl2 = [0,24]
for i, d in enumerate(dl):
    if i%2 == 0:
        dl1.append(d)
        dl2.append(24-d)
    else:
        dl1.append(24-d)
        dl2.append(d)

dl1.sort()
dl2.sort()

min_1 = 24
for a,b in zip(dl1[1:],dl1[:-1]):
    diff = a-b
    min_1 = min(min_1,diff)

min_2 = 24
for a,b in zip(dl2[1:],dl2[:-1]):
    diff = a-b
    min_2 = min(min_2,diff)

ans = max(min_1,min_2)
print(ans)

Solution (Python):
from itertools import product
import random

def solve(n,dl):
    
    

    if 0 in dl:
        return 0

    doubles = []
    dl1 = []
    dl2 = []
    for i in range(1,13):
        i_cnt = dl.count(i)
        if i_cnt > 2:
            return 0
        elif i_cnt == 2:
            dl2.append(24-i)
            dl2.append(i)
        elif i_cnt == 1:
            dl1.append(i)        

    ans = 0
    ite = list(product(range(2),repeat=len(dl1)))
    for pattern in ite:
        dl24 = [0,24]
        for i, v in enumerate(pattern):
            if v == 1:
                dl24.append(24-dl1[i])
            else:
                dl24.append(dl1[i])
        dl24 = dl24 + dl2
        dl24.sort()
        diff_min = 24
        
        if len(dl24) == 1:
            ans = max(ans, min(dl24[0], 24-dl24[0]))
        else:
            for a,b in zip(dl24[:-1],dl24[1:]):
                diff = b-a
                if diff != 24:
                    diff_min = min(diff_min,diff)
            ans = max(diff_min,ans)
    return ans


def solve2(n,dl):
    ite = list(product(range(2),repeat=len(dl)))
    ans = 0
    for pattern in ite:
        dl_new = [0]
        for i, v in enumerate(pattern):
            if v == 1:
                dl_new.append(24-dl[i])
            else:
                dl_new.append(dl[i])
        dl_new.sort()
        diff_min = 24
        for a,b in zip(dl_new[:-1], dl_new[1:]):
            diff = b-a
            diff_min = min(diff_min, diff)
        ans = max(ans,diff_min)
    return ans


if __name__ == "__main__":
    n = int(input())
    dl = list(map(int, input().split()))
    ans = solve(n,dl)
    print(ans)

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


N = int(input())
D = list(map(int, input().split()))
D_cnt = Counter(D)
if N >= 24 or D_cnt[0] > 0 or D_cnt[12] > 1 or any(D_cnt[i] > 2 for i in range(1, 12)):
    print(0)
    exit()
clock = [0, 24]
undet = []
if 12 in D_cnt:
    clock.append(12)
for i in range(1, 12):
    if D_cnt[i] == 2:
        clock.append(i)
        clock.append(24 - i)
    elif D_cnt[i] == 1:
        undet.append(i)

ans = 0
for rp in product([0, 1], repeat=len(undet)):
    candidate = clock[:]
    for j, is_flipped in enumerate(rp):
        candidate.append(undet[j]) if is_flipped else candidate.append(24 - undet[j])
    candidate.sort()
    ans = max(ans, min(ca - cb for cb, ca in zip(candidate, candidate[1:])))
print(ans)

Solution (Python):
N = int(input())
D = list(map(int,input().split()))
s = min(D)
if 12 in D:
    for i in range(N):
        if D[i]!=12:
            s = min(s,12-D[i])
    D.remove(12)
D = sorted(D)
E = []
for i in range(len(D)):
    if i%2==0:
        E.append(D[i])
    else:
        E.append(24-D[i])
for i in range(len(E)-1):
    for j in range(i+1,len(E)):
        a = E[i]
        b = E[j]
        d = abs(a-b)
        if d>12:
            d = 24-d
        s = min(s,d)
print(s)

Solution (Python):
n, *d = map(int, open(0).read().split())
d.sort()

a = [24 - b if i & 1 else b for i, b in enumerate(d)]
a.append(0)


ans = 24
print(min([abs(a[i] - a[j]) for i in range(n + 1)
           for j in range(i + 1, n + 1)]))

Solution (Python):
import sys,math,collections,itertools,copy
input = sys.stdin.readline

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

for d in D:
    time_list[d] += 1
    if time_list[d]>=3:
        print(0)
        exit()


time24list=[]
singlelist=[]
for jisa,cnt in enumerate(time_list):
    if cnt == 0:
        continue
    elif cnt==2:
        time24list.append(jisa)
        time24list.append(24-jisa)
    elif jisa ==0 or jisa == 12:
        time24list.append(jisa)
    else:
        singlelist.append(jisa)


lst = len(singlelist)
bit = 2**lst
ans = 0
for bi in range(bit):
    tmp = copy.deepcopy(time24list)
    min_jisa = 12 
    for j in range(lst):
        if bi>>j & 1:
            tmp.append(singlelist[j])
        else:
            tmp.append(24-singlelist[j])
    tmp.sort()
    tmp.append(24)
    for i in range(len(tmp)-1):
        dtime = tmp[i+1]-tmp[i]
        min_jisa = min(dtime,min_jisa)
    ans = max(ans,min_jisa)
print(ans)

Solution (Python):
import sys
import math
import collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    n = ni()
    a = na()
    a.sort()

    tmp = []
    for i, ai in enumerate(a):
        if i % 2:
            tmp.append(ai)
        else:
            tmp.append(24 - ai)
    tmp.append(0)
    tmp.append(24)
    tmp.sort()
    ans = INF
    for i in range(len(tmp) - 1):
        ans = min(ans, tmp[i + 1] - tmp[i])

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations, product
from collections import Counter
def calc(a):
    ret = 10 ** 20
    for comb in combinations(a, 2):
        tmp = abs(comb[0] - comb[1])
        ret = min(ret, min(tmp, 24-tmp))
    return ret

def main():
    N = int(input())
    D = list(map(int, input().split()))
    
    if D.count(0):
        return 0
    cnt = Counter(D)
    
    if max(cnt.values()) >= 3: return 0
    
    if cnt[12] >= 2: return 0
    d = []
    d2 = [0]
    single_cnt = 0
    for i, c in cnt.items():
        if i == 12:
            d2.append(12)
        elif c == 1:
            d.append((i, 24-i))
            single_cnt += 1
        elif c == 2:
            d2.append(i)
            d2.append(24-i)
    ans = 0
    for prod in product(range(2), repeat=single_cnt):
        tmp = []
        for i, p in enumerate(prod):
            tmp.append(d[i][p])
        ans = max(ans, calc(tmp + d2))
    return ans
            
print(main())

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

if N >= 24:
    print(0)

elif N == 23:
    d = [0 for _ in range(13)]
    d[0] = 1
    for i in range(N):
        d[D[i]] += 1
    if d[0] > 1 or d[12] > 1:
        print(0)
    else:
        for i in range(1, 12):
            if d[i] > 2:
                print(0)
                break
        else:
            print(1)

else:
    res = 0
    for bit in range(2**N):
        d = [0 for _ in range(24)]
        d[0] = 1
        for i in range(N):
            if (bit >> i) & 1:
                d[D[i]] += 1
            else:
                d[(24 - D[i]) % 24] += 1
        s = 25
        tmp = 0
        if d[0] > 1:
            s = 0
        else:
            for i in range(1, 24):
                if d[i] > 1:
                    s = 0
                    break
                elif d[i] == 1:
                    s = min(s, i - tmp)
                    tmp = i
            s = min(s, (24 - tmp) % 24)
        res = max(res, s)
    print(res)

Solution (Python):
n = int(input())
D = map(int, input().split())
cnt = [0]*13
for d in D:
    cnt[d] += 1

if any(2 < x for x in cnt[1:12]) or cnt[0] != 0 or cnt[12] == 2:
    print(0)
else:
    world = [0]*24
    world[0] = 1
    flag = 1
    for i, c in enumerate(cnt[1:], 1):
        if c == 0:
            continue
        if c == 2:
            world[i] = 1
            world[-i] = 1
        elif c == 1:
            if flag:
                world[i] = 1
            else:
                world[-i] = 1
            flag ^= 1
    print(
        min(map(lambda x: len(x)+1, (''.join(map(str, world[1:])).split('1')))))

Solution (Python):
from collections import Counter

def main():
    N = int(input())
    D = list(map(int, input().split()))
    d = Counter(D)
    
    d[0] += 1
    for i in range(13):
        if (i == 0 or i == 12) and d[i] == 2:
            print(0)
            exit()
        elif d[i] >= 3:
            print(0)
            exit()
    ans = 0
    for i in range(2**11):
        time = [0, 24]
        if d[12] == 1:
            time.append(12)
        for j in range(1,12):
            if d[j] == 1:
                if i >> j & 1:
                    time.append(24-j)
                else:
                    time.append(j)
            elif d[j] == 2:
                time.append(24-j)
                time.append(j)
        time.sort()
        ret = 25
        for i in range(1,len(time)):
            ret = min(ret, time[i]-time[i-1])
        ans = max(ans, ret)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
dcnt=[0 for i in range(13)]
for i in d:
  dcnt[i]+=1

zf=False
for i in range(1,12):
  if dcnt[i]>=3:
    zf=True
if dcnt[0]:
  zf=True
if dcnt[12]>=2:
  zf=True

if zf:
  print(0)
else:
  ison=[0 for i in range(24)]
  oned=[]
  ison[12]=1
  ison[0]=dcnt[12]
  for i in range(1,12):
    if dcnt[i]==1:
      oned.append(i)
    elif dcnt[i]==2:
      ison[12-i]=1
      ison[12+i]=1
  maxd=0
  for i in range(2**len(oned)):
    for j in range(len(oned)):
      if i&(2**j):
        ison[12+oned[j]]=1
        ison[12-oned[j]]=0
      else:
        ison[12-oned[j]]=1
        ison[12+oned[j]]=0
        
    mind=24
    p=100
    s=23
    while True:
      if ison[s]:
        mind=min(mind,p-s)
        p=s
        if s<0:
          break
      s-=1
    maxd=max(maxd,mind)
  print(maxd)

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    D = in_nl() + [0]

    cnt = Counter(D)

    det = []
    non_det = []

    
    for i in (0, 12):
        if cnt[i] >= 2:
            print(0)
            exit()
        elif cnt[i] == 1:
            det.append(i)

    
    for i in range(1, 12):
        if cnt[i] >= 3:
            print(0)
            exit()
        elif cnt[i] >= 2:
            det.append(i)
            det.append(24 - i)
        elif cnt[i] == 1:
            non_det.append(i)

    
    

    non_det_len = len(non_det)
    comb = itertools.product([0, 1], repeat=non_det_len)

    ans = 0

    def solve(city):

        comb = itertools.combinations(city, 2)

        ret = 12
        for c in comb:
            t1, t2 = c[0], c[1]
            diff = abs(t2 - t1)
            ret = min(ret, min(diff, 24 - diff))

        return ret

    for com in comb:

        city = copy(det)
        for i in range(non_det_len):
            if com[i] == 0:
                city.append(non_det[i])
            else:
                city.append(24 - non_det[i])

        ans = max(ans, solve(city))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

times = [0] * 24
times[0] = 1
for d in D:
    times[d] += 1

if max(times) > 2 or times[0] == 2 or times[12] == 2:
    print(0)
else:
    ans = 0
    
    for i in range(2**11):
        positions = [0]
        for j in range(1, 12):
            if times[j] == 2:
                positions.append(j)
                positions.append(24 - j)
            elif times[j] == 1:
                if (i >> j) & 1:
                    positions.append(24 - j)
                else:
                    positions.append(j)
        if times[12] == 1:
            positions.append(12)
        positions.sort()
        
        lp = len(positions)
        d = float("inf")
        for p in range(lp):
            
            if p == lp -1:
                diff = 24 - positions[p] + positions[0]
            else:
                diff = positions[p+1] - positions[p]
            d = min(d, diff if diff != 24 else 0)
        ans = max(ans, d)

    print(ans)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





N = getN()
D = getList()
D = Counter(D)

l_1 = []
l_2 = []
for i in D.items():
    if i[0] == 0:
        print(0)
        exit()
    if i[1] >= 3:
        print(0)
        exit()
    if i[1] == 2:
        l_2.append(i[0])
        l_2.append(24 - i[0])
    elif i[1] == 1:
        l_1.append(i[0])

def judge(array):
    res = min(24 - array[-1], array[-1])
    for i in range(len(array) - 1):
        res = min(res, array[i + 1] - array[i])
    return res

ans = 0

N_1 = len(l_1)
for bit in range(1 << N_1):
    opt = copy.deepcopy(l_2)
    opt.append(0)
    for i in range(N_1):
        if bit & (1 << i):
            opt.append(l_1[i])
        else:
            opt.append(24 - l_1[i])
    opt.sort()
    ans = max(ans, judge(opt))
print(ans)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





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






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


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


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = IL()
dic = DD(int)
dic[0] += 1
for a in A:
  dic[a] += 1
  
if dic[0] >= 2:
  print(0)
  exit()
if dic[12] >= 2:
  print(0)
  exit()
  
kaku = [0,24]
if dic[12]:
  kaku.append(12)
pending = []
for i in range(1,12):
  if dic[i] >= 3:
    print(0)
    exit()
  elif dic[i] == 2:
    kaku.append(i)
    kaku.append(24-i)
  elif dic[i] == 1:
    pending.append(i)
data = [0]
n = len(pending)
for k in range(1<<n):
  temp = set(kaku)
  for i in range(n):
    if (k>>i)&1:
      temp.add(pending[i])
    else:
      temp.add(24-pending[i])
  temp = list(temp)
  temp.sort()
  nn = len(temp)
  data.append(min(temp[i+1]-temp[i] for i in range(nn-1)))

print(max(data)%24)

Solution (Python):



from collections import Counter

def main():
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    D = Counter(D)
    for i in range(13):
        if (i==0 or i==12) and D[i]==2:
            print(0)
            exit()
        elif D[i]>=3:
            print(0)
            exit()
    ret = 0
    for i in range(2**11):
        l = [0,24]
        if D[12]==1:
            l.append(12)
        for j in range(1,12):
            if D[j]==1:
                if i>>j & 1:
                    l.append(24 - j)
                else:
                    l.append(j)
            elif D[j]==2:
                l.append(24 - j)
                l.append(j)
        l.sort()
        mi = 24
        for i in range(1,len(l)):
            mi = min(mi, l[i]-l[i-1])

        ret = max(ret, mi)
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    dn = [0]*13
    dn[0] = 1
    for i in ipt().split():
        dn[int(i)] += 1
    st = set()
    if dn[12] > 1:
        print(0)
        exit()
    elif dn[12] == 1:
        st.add(12)
    if dn[0] > 1:
        print(0)
        exit()
    elif dn[0] == 1:
        st.add(0)
    p = 1

    for i,di in enumerate(dn[1:12:]):
        if di > 2:
            print(0)
            exit()
        elif di == 2:
            st.add(i+1)
            st.add(24-i-1)
        elif di == 1:
            if p:
                st.add(i+1)
                p = 0
            else:
                st.add(24-i-1)
                p = 1

    ma = 25
    st = sorted(st)

    for i,si in enumerate(st):
        if ma > abs(si-st[i-1]):
            ma = abs(si-st[i-1])
    print(ma)

    return None

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

def dist(x,y):
    return min(abs(x-y), abs(24-abs(x-y)))

n = readint()
d = readints()

d.sort()

a = [0]*24
a[0]=1
for i in range(n):
    if d[i]==0:
        a[0]+=1
        continue
    if d[i]==12:
        a[12]+=1
        continue
    x = d[i]
    y = 24-d[i]
    ansx = 100
    ansy = 100
    for j in range(24):
        if a[j]:
            ansx = min(ansx, dist(x,j))
            ansy = min(ansy,dist(y,j))
    if ansx >= ansy:
        a[x]+=1
    else:
        a[y]+=1

ans = 100
for x in a:
    if x>1:
        ans = 0

if ans:
    b = []
    for i,x in enumerate(a):
        if x:
            b.append(i)
    for i in range(len(b)):
        ans = min(ans,dist(b[i],b[i-1]))
print(ans)

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter



import bisect
n=int(input())
d=list(map(int,input().split()))
d.sort()
res=24
if n==1 or 0 in d:
  print(d[0])
  exit()
for i in range(0,n-2):
    res=min(res,d[i+2]-d[i])
for i in range(n-1):
    res=min(res,d[i]+d[i+1],24-d[i]-d[i+1])
ans=min(d[0],res)
print(ans)

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

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

time = [0]
for i in range(N):
    max_24 = 24
    max_0 = 24
    for j in range(len(time)):
        max_0 = min(abs(time[j] - D[i]), max_0)
        max_24 = min(abs(time[j] - (24-D[i])), max_24)
    if max_0 >= max_24:
        time.append(D[i])
    else:
        time.append(24 - D[i])

time.append(24)
ans = 12
for i in range(len(time)):
    for j in range(i+1, len(time)):
        ans = min(ans, abs(time[i] - time[j]))

print(ans)

Solution (Python):
n = int(input())
d = list(map(int, input().split()))
li = [0]*13
for i in d:
    li[i] += 1
if li[0]>0 or li[12]>1:
    print(0)
    exit(0)
l1 = []
l2 = []
for i in range(1, 12):
    if li[i]>2:
        print(0)
        exit(0)
    if li[i]==1:
        l1.append(i)
    elif li[i]==2:
        l2.extend([i, 24-i])
if li[12]:
    l2.append(12)
ans = 0
for i in range(2**len(l1)):
    tl = l2[:]
    for j in range(len(l1)):
        if i>>j&1:
            tl.append(l1[j])
        else:
            tl.append(24-l1[j])
    tl.sort()
    tmp = 24-tl[-1]
    pre = 0
    for j in tl:
        tmp = min(tmp, j-pre)
        pre = j
    ans = max(ans, tmp)
print(ans)

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

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

N = int(readline())

D = list(map(int,readline().split()))
cnt = [0]*13
for d in D:
    cnt[min(24-d,d)] += 1
cnt[0] += 1
for i in range(1,12):
    if cnt[i] >= 3:
        print(0)
        exit()
if cnt[0] >= 2 or cnt[12] >= 2:
    print(0)
    exit()

where = [0]*24
where[0] = 1
b = 0
if cnt[12]:
    where[12] = 1
for i in range(1,12):
    c = cnt[i]
    if c == 0:
        continue
    if c == 2:
        where[i] = 1
        where[24-i] = 1
        b = 0
    else:
        if b:
            where[i] = 1
            b = 0
        else:
            where[24-i] = 1
            b = 1

ans = INF
a = 1
for i in range(1,24):
    if where[i]:
        ans = min(ans,a)
        a = 1
    else:
        a += 1
ans = min(ans,a)
print(ans)

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

ans = 12
while ans > 1:
  Fix = [0]
  for i in range(N):
    MIN = []
    for dotti in range(2):
      if dotti == 0:
        nxt = D[i]
      else:
        nxt = 24-D[i]
      now = 100
      for t in Fix:
        now = min(now,abs(nxt-t),24-abs(nxt-t))
      MIN.append(now)
    if MIN[0] <= MIN[1]:
      Fix.append(24-D[i])
    else:
      Fix.append(D[i])
  
  break
ans = 100
for i in range(N+1):
  for j in range(i+1,N+1):
    
    ans = min(ans,abs(Fix[i]-Fix[j]),24-abs(Fix[i]-Fix[j]))
    
print(ans)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    D=LI()
    
    if 0 in D:
        print(0)
        exit()
    if D.count(12)>=2:
        print(0)
        exit()
    
    temp=[0]*24
    temp[0]=1
    if 12 in D:
        temp[12]=1
    
    d=[]
    cnt=0
    for i in range(1,12):
        if D.count(i)==1:
            d.append(i)
            cnt+=1
        elif D.count(i)==2:
            temp[i]=1
            temp[24-i]=1
        elif D.count(i)>=3:
            print(0)
            exit()
            
            
    ans=[]
    
    for i in range(1<<cnt):
        temp2=[0]*24
        
        
        for k in range(24):
            temp2[k]=temp[k]
        
        for j in range(cnt):
            if i>>j & 1:
                temp2[d[j]]=1
            else:
                temp2[24-d[j]]=1
                
        aaa=24
        for ii in range(24):
            for jj in range(ii+1,24):
                if temp2[ii]==1 and temp2[jj]==1:
                    diff=min(jj-ii,24-(jj-ii))
                    if diff<aaa:
                        aaa=diff
        ans.append(aaa)
    print(max(ans))
            
    
        

main()