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):
import sys
input = sys.stdin.readline
n = int(input())
D = list(map(int,input().split()))





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

if C[0] >= 2 or C[12] >=2:
    print(0)
    sys.exit()

for i in range(13):
    if C[i] >= 3:
        print(0)
        sys.exit()









ans0 = 0
for i in range(2**13):
    zero = 0
    ib = format(i, "b").zfill(13)
    A = []
    for j in range(13):
        if C[j] == 1:
            A.append(j * (-1) ** int(ib[j]))
        if C[j] == 2:
            if int(ib[j]) == 0:
                A.append(j)
                A.append(-j)
            else:
                zero = 1
    if zero == 1:
        continue
    
    ans = 30
    
        
    
    for j in range(len(A)):
        for k in range(j+1, len(A)):
            di = abs(A[k] - A[j])
            
            ans = min(ans, min(di, 24-di))
            if ans <= ans0:
                break
        if ans <= ans0:
            break
    
    ans0 = max(ans0, ans)
print(ans0)

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    d = list(map(int, input().split()))
    if n == 1:
        print(d[0])
        exit()
    dc = defaultdict(int)
    dc[0] = 1
    for i in range(n):
        v = d[i]
        if dc[v] >= 2 or (dc[v] == 1 and (v == 12 or v == 0)):
            print(0)
            exit()
        dc[v] += 1
    
    dd = []
    ddt = []
    for i in range(0, 13):
        if (i == 0 or i == 12) and dc[i] == 1:
            ddt.append(i)
        elif dc[i] == 1:
            dd.append(i)
        elif dc[i] == 2:
            ddt.append(i)
            ddt.append(24 - i)


    
    ans = 0
    for i in range(1 << len(dd)):
        d3 = []
        for j in range(len(ddt)):
            d3.append(ddt[j])

        for j in range(len(dd)):
            if (i & (1 << j)):
                d3.append(dd[j])
            else:
                d3.append(24 - dd[j])
        
        d3 = sorted(d3)
        t = float("inf")
        for i in range(len(d3) - 1):
            t = min(t, d3[i + 1] - d3[i])
        t = min(t , 24 - d3[-1])
        ans = max(t, ans)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

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


def main():
	N = int(input())
	D = getlist()
	if N >= 24:
		print(0)
	elif N >= 12:
		d = defaultdict(int)
		judge = "Yes"
		for i in D:
			d[i] += 1
		if d[0] >= 1:
			judge = "No"
		if d[12] >= 2:
			judge = "No"
		for i in range(1, 12):
			if d[i] >= 3:
				judge = "No"
		if judge == "No":
			print(0)
		else:
			print(1)
	else:
		ans = 0
		for i in range(1 << N):
			L = [0, 24]
			for j in range(N):
				if ((i >> j) & 1) == 1:
					L.append(D[j])
				else:
					L.append(24 - D[j])
			L = sorted(L)
			val = 24
			for i in range(N + 1):
				val = min(val, L[i + 1] - L[i])
			ans = max(ans, val)
		print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def main():
    num = int(input())
    data = list(map(int, input().split()))
    count = Counter(data)

    ans = 0
    for i in range(2 ** 11):
        bin_list = bin(i)[2:]
        bin_list = bin_list.zfill(11)
        bin_list = list(map(int, list(bin_list)))
        now_data = [0, 24]

        if count[12] >= 2:
            print(0)
            sys.exit()
        elif count[12] == 1:
            now_data.append(12)

        if count[0] >= 1:
            print(0)
            sys.exit()


        for j in range(1, 12):
            if count[j] == 0:
                continue
            elif count[j] >= 3:
                print(0)
                sys.exit()
            elif count[j] == 2:
                now_data.append(j)
                now_data.append(24 - j)
            elif bin_list[j - 1]:
                now_data.append(j)
            else:
                now_data.append(24 - j)
        now_data.sort()

        kari = 12
        
        for j in range(len(now_data) - 1):
            kari = min(kari, now_data[j + 1] - now_data[j])
        ans = max(ans, kari)

    print(ans)




if __name__ == '__main__':
    main()

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

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

p = m = 0
a = 99

for d in D:
    
    e = min(a, d - p, 24 - d - m)
    
    f = min(a, d - m, 24 - d - p)
    if e < f:
        a = f
        m = d
    else:
        a = e
        p = d
print(a)

Solution (Python):
from copy import copy
import sys
from collections import Counter
n = int(input())
d = sorted(list(map(int, input().split())))
if d[0] == 0:
	print(0)
	sys.exit()
c = Counter(d)
if max(c.values()) >= 3:
	print(0)
	sys.exit()
if c[12] >= 2:
	print(0)
	sys.exit()

reserved = []
alternatives = []
for k, v in c.items():
	if v == 2:
		reserved.append(k)
		reserved.append(24-k)
	elif k == 12:
		reserved.append(12)
	else:
		alternatives.append(k)

m = len(alternatives)

ans = 0
for i in range(2**m):
	zones = copy(reserved)
	for j, a in enumerate(alternatives):
		if (i>>j)&1:
			zones.append(24-a)
		else:
			zones.append(a)
	zones.sort()
	zones = [0] + zones + [24]
	
	res = 24
	for j in range(len(zones)-1):
		res = min(res, zones[j+1]-zones[j])
	ans = max(ans, res)

print(ans)

Solution (Python):
import itertools


def calc(s):
    ret = 24
    for i in range(1, len(s)):
        ret = min(ret, s[i] - s[i-1])
    return ret


def main():
    N = int(input())
    D = list(map(int, input().split()))
    if D.count(0) >= 1 or D.count(12) >= 2:
        print(0)
        return
    cnt = [0] * 13
    for d in D:
        cnt[d] += 1
    fixed = [0, 24]
    variable = []
    for i in range(1, 12):
        if cnt[i] >= 3:
            print(0)
            return
        elif cnt[i] == 2:
            fixed.append(i)
            fixed.append(24-i)
        elif cnt[i] == 1:
            variable.append(i)
    if cnt[12] == 1:
        fixed.append(12)
    ans = 0
    for p in itertools.product((False, True), repeat=len(variable)):
        s = list(fixed)
        for i in range(len(variable)):
            if p[i]:
                s.append(variable[i])
            else:
                s.append(24-variable[i])
        s.sort()
        ans = max(ans, calc(s))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import itertools

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

counter = [1] + [0] * 12
for x in D:
    counter[x] += 1

bl = counter[0] > 1 or counter[12] > 1 or any(x >= 3 for x in counter)
if bl:
    print(0)
    exit()

itrs = [[[0]]]
for x in range(1,12):
    if counter[x] == 0:
        itr = [[]]
    elif counter[x] == 1:
        itr = [[x],[-x]]
    else:
        itr = [[x,-x]]
    itrs.append(itr)
itrs.append([[]] if counter[12] == 0 else [[12]])

def F(arr):
    f = lambda x,y: min((x-y)%24, (y-x)%24)
    return min(f(x,y) for x,y in itertools.combinations(arr,2))

answer = 0
for p in itertools.product(*itrs):
    arr = []
    for x in p:
        arr += x
    answer = max(answer, F(arr))

print(answer)

Solution (Python):
import copy

N=int(input())
D=list(map(int,input().split()))
D.sort()
time=[]
kakutei=[]
tmp=0
for d in D: 
  if d==0:
    print(0)
    exit()
  elif tmp==d:
    count+=1
    if count>=2 or (d==12 and count>=1):
      print(0)
      exit()
  else:
    count=0
  if d not in time:
    if d==12:
      kakutei.append(d)
    else:
      time.append(d)
  elif 24-d not in time:
    kakutei.append(d)
    kakutei.append(24-d)
    time.remove(d)
  tmp=d
kakutei.append(0)
kakutei.append(24)
ans=0
if len(time)==0:
  kakutei.sort()
  minclock=12
  for j in range(len(kakutei)-1):
    minclock=min(minclock,kakutei[j+1]-kakutei[j])
  ans=max(ans,minclock)
  print(ans)
  exit()
for i in range(2**len(time)):
  clock=kakutei.copy()
  bit = format(i, "b").zfill(len(time))
  for i in range(len(bit)):
    if bit[i]=='0':
      clock.append(time[i])
    else:
      clock.append(24-time[i])
  clock.sort()
  minclock=12
  for j in range(len(clock)-1):
    minclock=min(minclock,clock[j+1]-clock[j])
  ans=max(ans,minclock)
print(ans)

Solution (Python):
N=int(input())

D=list(map(int,input().split()))
flg =False
A = [0 for i in range(13)]
A[0]=1
for i in range(N):
    j = min(D[i],24-D[i])
    if j==0:
        flg=True
        break
    elif j==12:
        if A[j]==1:
            flg=True
            break
        else:
            A[j]+=1
    else:
        if A[j]==2:
            flg=True
            break
        else:
            A[j]+=1

import copy

if flg:
    print(0)
else:
    count=0
    one_times=[]
    times=[0]
    for i in range(1,12): 
        if A[i]==1:
            count+=1
            one_times.append(i)
        if A[i]==2:
            times.append(i)
            times.append(24-i)
    if A[12]==1:
        times.append(12)
    score_max=0
    for i in range(2**count):
        t=copy.copy(times)
        for j in range(count):
            if (i//(2**j))%2==0:
                t.append(one_times[j])
            else:
                t.append(24-one_times[j])
        t.sort()
        
        score_min=24
        for j in range(len(t)):
            if j==len(t)-1:
                score_min=min(t[0]+24-t[len(t)-1],score_min)
            else:
                score_min=min(t[j+1]-t[j],score_min)
        score_max=max(score_min,score_max)
    print(score_max)

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

for i in range(13):
    Dcount[i] = D.count(i)
if Dcount[0] > 0 or Dcount[12] > 1: print(0)
else:
    for i in Dcount[1:12]:
        if i >= 3:
            print(0)
            break
    else:
        base = [0, 24]
        if Dcount[12] == 1: base.append(12)
        Single = []
        for i in range(1, 12):
            if Dcount[i] == 2:
                base.append(i)
                base.append(24-i)
            elif Dcount[i] == 1:
                Single.append(i)
        lensingle = len(Single)
        minmax = 0
        for i in range(2**lensingle):
            Clock = base.copy()
            bit = format(i, "b").zfill(lensingle)
            for j in range(lensingle):
                if bit[j] == "0": Clock.append(Single[j])
                else: Clock.append(24 - Single[j])
            Clock.sort()
            minsub = 24
            for j in range(1, len(Clock)):
                minsub = min(minsub, Clock[j] - Clock[j-1])
            minmax = max(minmax, minsub)
        print(minmax)

Solution (Python):
N=int(input())
from collections import defaultdict
data=defaultdict(int)
data[0]+=1
for d in map(int,input().split()):
    data[d]+=1
check=[[]]
ans=1
for d in data.keys():
    if data[d]>2:
        ans=0
        break
    checkc=check.copy()
    if data[d]==2:
        if d==0 or d==12:
            ans=0
            break
        for c in check:
            c+=[d,-1*d]
    elif data[d]==1:
        if d==0:
            for c in check:
                c.append(d)
            continue
        check=[]
        for c in checkc:
            check.append(c+[d])
            check.append(c+[-1*d])
if ans!=0:
    ans=0
    for c in check:
        c.sort()
        now=24
        for i in range(len(c)):
            now=min(now,(c[i]-c[i-1])%24)
        ans=max(now,ans)
print(ans)

Solution (Python):
n = int(input())
t = [0]*24
t[0] = 1
a = 1
for i in sorted(list(map(int, input().split()))):
    t[a*i] += 1
    a *= -1
for i in t:
  if i > 1:
    print(0)
    break
else:
  m = 24
  c = 1
  t += [1]
  for i in t[1:]:
    if i == 1:
      if m > c:
        m = c
      c = 1
    else:
      c += 1       
  print(m)

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

D1 = [0] + [(D[i] if i % 2 == 0 else 24 - D[i]) for i in range(N)]
D2 = [0] + [(D[i] if i % 2 == 0 else 24 - D[i]) for i in range(N)]

ans1 = float('inf')
ans2 = float('inf')

for i in range(N + 1):
    for j in range(i + 1, N + 1):
        ans1 = min(ans1, abs(D1[i] - D1[j]))
        ans2 = min(ans2, abs(D2[i] - D2[j]))

print(max(ans1, ans2))

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

if rec[0] >= 1:
    print(0)
    exit()

ans = 0
for i in range(1 << 12):
    tmp = [0]
    for j in range(13):
        if rec[j] >= 3:
            print(0)
            exit()
        elif rec[j] == 2:
            tmp.append(j)
            tmp.append(24 - j)
        elif rec[j] == 1:
            if i & 1 << j:
                tmp.append(j)
            else:
                tmp.append(24 - j)
    tmp = sorted(tmp)
    pre = tmp[0]
    num = float("inf")
    for i in range(1, len(tmp)):
        num = min(num, tmp[i] - pre)
        pre = tmp[i]

    ans = max(min(num, 24 - tmp[-1]), ans)

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from heapq import heappush, heappop

n = ni()
d = list(li())

def isok(dur: int, d: list):
    pque = [24]
    for di in d:
        heappush(pque, di)

    cur = 0
    while pque:
        tmp = heappop(pque)
        if tmp - cur < dur and tmp >= 12:
            return False
        
        elif tmp - cur < dur:
            heappush(pque, 24-tmp)
            
        else:
            cur = tmp
        
    return True

ans = 13
for cand in range(12, -1, -1):
    if isok(cand, d):
        ans = cand
        break
        
print(ans)

Solution (Python):
import collections


def main(N, D):
    Dcounter = collections.Counter(D)
    a = [0, 24]
    b = []
    for i, v in Dcounter.items():
        if v > 2 or (i == 0 and v > 0) or (i == 12 and v > 1):
            print(0)
            return

        if v == 2:
            a.append(i)
            a.append(24-i)
        if v == 1:
            b.append(i)
    ans = 0
    for i in range(1 << len(b)):
        use = a.copy()
        for j in range(len(b)):
            if ((i >> j) & 1) == 1:
                use.append(b[j])
            else:
                use.append(24-b[j])
        use = sorted(use)
        distance = [use[i+1] - use[i] for i in range(len(use)-1)]
        ans = max(ans, min(distance))
    print(ans)

if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split(" ")))
    main(N, D)

Solution (Python):
import sys
N=int(input())
D=[int(i) for i in input().split()]
L=[0]*13
L[0]=1
for d in D:
    L[d]+=1
for s in L:
    if s>=3:
        print(0)
        sys.exit()
if L[0]>=2 or L[12]>=2:
    print(0)
    sys.exit()

def check(K):
    sta=0
    
    num=10**9
    ans=10**9
    while sta<24:
        if K[sta]==1:
            ans=min(ans,num)
            num=0
        sta+=1
        num+=1
        
    ans=min(num,ans)
    return ans


ans=-1
for s in range(1<<11):
    K=[0]*24
    K[0]=1
    if L[12]==1:
        K[12]=1
    for j in range(11):
        if L[j+1]==0:
            continue
        if L[j+1]==2:
            K[j+1]=1
            K[24-(j+1)]=1
            continue
        if (s&(1<<j)):
            K[j+1]=1
        else:
            K[24-(j+1)]=1
    ans=max(ans,check(K))
print(ans)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
import sys
N = int(input())

D = inpl()
tt = [0 for _ in range(24)]
tt[0] = 2
for d in D:
    tt[d] += 1
    tt[-d] += 1    

for t in tt:
    if t > 2:
        print(0)
        sys.exit()
if N > 11:
    prev = tt[-1]
    for t in tt:
        if t == 0:
            prev = 0
            continue
        if t == 1:
            if prev == 2:
                print(1)
                break
            prev = 1
        if t == 2:
            if prev == 1 or prev == 2:
                print(1)
                break
            prev = 2
    else:
        print(2)
    sys.exit()
ans = 0
for i in range(2**N):
    stri = format(i, 'b')
    stri = '0'*(N-len(stri)) +stri
    tt = [0 for _ in range(24)]
    tt[0] = 1
    for j in range(N):
        c = 1
        if stri[j] == '0':
             c = -1
        tt[c * D[j]] += 1
    for t in tt:
        if t > 1:
            break
    else:
        ctr = 1
        H = []
        for i in range(48):
            if tt[i%24] == 0:
                ctr += 1
            else:
                H.append(ctr)
                ctr = 1
        H.pop(0)
        ans = max(ans, min(H))
print(ans)

Solution (Python):
from numpy import *
from scipy import *

n = array(input(), dtype=int)
d = array(input().split(), dtype=int)
h = zeros(24, dtype=int)
h[0] = 1

for x in d:
    h[x] += 1
ans = array(0, dtype=int)
if h[0] >= 2 or h[12] >= 2:
    print(ans)
    exit()
for x in h:
    if x >= 3:
        print(ans)
        exit()

for i in range(12):
    if h[i] >= 2:
        h[24-i] = 2

def dfs(depth, h):
    global ans
    r = 0
    res = inf
    for i in range(1,24):
        r += 1
        if h[i] != 0:
            res = min(res,r)
            r = 0
    res = min(res,r+1)
    ans = max(ans,res)
    if depth == 12:
        return
    if h[depth] == 1:
        h[depth], h[24-depth] = h[24-depth], h[depth]
        dfs(depth+1, h)
        h[depth], h[24-depth] = h[24-depth], h[depth]
    dfs(depth+1, h)

dfs(1, h)
print(ans)

Solution (Python):
def solve(N, D):
    fill = [0] * 24
    fill[0] = 1

    left = False
    for i in range(N):
        if D[i] == 0:
            print(0)
            return 0
        elif D[i] == 12:
            if fill[12] == 1:
                print(0)
                return 0
            else:
                fill[12] = 1
        else:
            if not left:
                if fill[D[i]] == 0:
                    fill[D[i]] = 1
                    left = True
                elif fill[24-D[i]] == 0:
                    fill[24-D[i]] = 1
                    left = False
                else:
                    print(0)
                    return 0
            else:
                if fill[24-D[i]] == 0:
                    fill[24-D[i]] = 1
                    left = False
                elif fill[D[i]] == 0:
                    fill[D[i]] = 1
                    left = True
                else:
                    print(0)
                    return 0

    s = 24
    cnt = 0
    for i in range(1, 24):
        if i == 23:
            if fill[i] == 0:
                cnt += 1
                s = min(s, cnt + 1)
            else:
                s = min(s, cnt + 1)
        else:
            if fill[i] == 0:
                cnt += 1
            else:
                s = min(s, cnt + 1)
                cnt = 0
    print(s)

if __name__ == '__main__':
    N = int(input())
    D = list(map(int, input().split()))
    solve(N, sorted(D))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = sorted(LI())
    i = 0
    b = [0]
    for c,i in zip(a,range(n)):
        if i % 2 == 0:
            b.append(c)
        else:
            b.append(24-c)
    b.sort()
    r = a[0]
    for i in range(n):
        if b[i+1] - b[i] < r:
            r = b[i+1] - b[i]

    return r



print(main())

Solution (Python):
n=input()
a=sorted(map(int,raw_input().split()))
r=0
b=[0]+[a[j]*(-1)**j%24 for j in range(n)]
s=24
for j in range(n):
 for k in range(j+1,n+1):s=min(s,(b[j]-b[k])%24,(b[k]-b[j])%24)
r=max(r,s)
print r

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

Solution (Python):
from collections import Counter


def solve(n, ds):
    if n >= 24:
        return 0
    freq = Counter(ds)
    if (0 in freq) or (freq[12] >= 2) or max(freq.values()) >= 3:
        return 0
    up_low = [0, 0]
    switch = 0
    ans = 25
    for d, s in sorted(freq.items()):
        if s == 2:
            ans = min(ans, d - up_low[0], d - up_low[1])
            up_low[0] = up_low[1] = d
        else:
            ans = min(ans, d - up_low[switch])
            up_low[switch] = d
            switch ^= 1
    ans = min(ans, 24 - sum(up_low))
    return ans


print(solve(int(input()), list(map(int, input().split()))))

Solution (Python):
from collections import Counter
import sys

N = int(input())
ds = list(map(int,input().split()))
ctr = Counter(ds)
if 0 in ctr:
    print(0)
    sys.exit()

hs = (1 << 24) + 1
undef = []
for d,c in ctr.items():
    if c > 2:
        print(0)
        sys.exit()
    elif c == 2:
        hs |= (1 << d)
        hs |= (1 << (24-d))
    else: 
        undef.append(d)

ans = 0
for i in range(2**len(undef)):
    _hs = hs
    for j,u in enumerate(undef):
        if (i >> j) & 1:
            _hs |= (1 << u)
        else:
            _hs |= (1 << (24-u))
    min_interval = 24
    prev = 0
    for k in range(1,25):
        if (_hs >> k) & 1 == 0: continue
        min_interval = min(min_interval, k - prev)
        prev = k
    ans = max(ans, min_interval)
print(ans)

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


counts = [0] * 13

if 0 in D or D.count(12) > 1:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] > 12:
        D[i] = 24 - D[i]
D.append(0)

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

E = []
F = []
for idx, count in enumerate(counts):
    if count > 2:
        print(0)
        sys.exit()
    elif count == 2:
        E.append(idx)
        E.append(24 - idx)
    elif idx == 0 or (idx == 12 and count > 0):
        E.append(idx)
    elif count == 1:
        F.append(idx)

max_val = 0


def update(arr):
    global max_val
    min_val = 24
    arr.sort()
    for i in range(1, len(arr)):
        if arr[i] - arr[i - 1] < min_val:
            min_val = arr[i] - arr[i - 1]
    if 24 - arr[-1] < min_val:
        min_val = 24 - arr[-1]
    if min_val > max_val:
        max_val = min_val

for i in range(2 ** len(F)):
    G = [0] * len(F)
    for j in range(len(F)):
        if i & (2 ** j):
            G[j] = 24 - F[j]
        else:
            G[j] = F[j]
    update(E + G)

print(max_val)

Solution (Python):
import sys

N = int(sys.stdin.readline().strip())
D = [0] + [int(x) for x in sys.stdin.readline().strip().split()]

def kei(xs, n):
    xs = xs + [x + 24 for x in xs]
    m1 = min(abs(n - x) for x in xs)
    m2 = min(abs((24 - n) - x) for x in xs)
    if m1 >= m2:
        return n, m1
    else:
        return 24 - n, m2

def calc(D):
    D.sort()
    
    n = len(D)
    if N > 24:
        return 0
    if D[1] == 0:
        return 0
    xs = D[:2]
    D = D[2:]
    r = xs[1] - xs[0]
    for d in D:
        
        c, m = kei(xs, d)
        
        if m < r:
            r = m
        xs += [c]
    return r



print(calc(D))

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

ans = D[:]
D.sort()
D_1 = []
D_2 = []

for i in range(N):
	if i % 2 == 0:
		D_1.append(24-D[i])
		D_2.append(D[i])
	else:
		D_2.append(24-D[i])
		D_1.append(D[i])


for i in range(N):
	for j in range(i+1,N):
		df1 = abs(D_1[j] - D_1[i])
		df2 = abs(D_2[j] - D_2[i])
		
		df1 = min([df1,24-df1])
		df2 = min([df2,24-df2])
			
		ans.append(df1)
		ans.append(df2)

print(min(ans))

Solution (Python):
from collections import Counter
import itertools

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

cnt = Counter(Ds)

if max(cnt.values()) >= 3:
    print(0)
    exit()

citys = []
rest = []
for k, v in cnt.items():
    if v == 2:
        if k == 0 or k == 12:
            print(0)
            exit()
        citys.append(k)
        citys.append(24 -k)
    else:
        rest.append(k)


def check(citys):
    if len(citys) <= 1:
        return 24

    citys.sort()

    ans = 24
    prev = citys[-1] - 24
    for c in citys:
        ans = min(ans, c - prev)
        prev = c

    return ans


if check(citys) == 1:
    print(1)
    exit()

ans = 0
for ops in itertools.product('+-', repeat=len(rest)):
    citys2 = citys.copy()
    for i in range(len(rest)):
        if ops[i] == '+':
            citys2.append(rest[i])
        else:
            citys2.append(24 - rest[i])

    ans = max(ans, check(citys2))

print(ans)

Solution (Python):
def timdiff(dif):
    l=[]
    for i in range(24):
        if dif[i]==1:
            l+=[i]
        if dif[i]>=2:
            return 0
    m=24
    for i in range(len(l)):
        if i < len(l)-1:
            m=min(m,l[i+1]-l[i])
    if len(l)>=2:
        m=min(m,24-l[len(l)-1])
    return m

n=int(input())
d=list(map(int,input().split(' ')))
diff=[0]*24
if n>=24:
    print(0)
elif n<12: 
    m=0
    for i in range(2**n):
        for j in range(1,24):
            diff[j]=0
        diff[0]=1
        for j in range(n):
            if (i >> j)%2==1:
                diff[d[j]]+=1
            elif d[j]==0:
                diff[0]+=1
            else:
                diff[24-d[j]]+=1
        m=max(m,timdiff(diff))
    print(m)
else: 
    bl=False
    for i in range(13):
        if bl:
            break
        m=0
        for j in range(n):
            if d[j] == i:
                m+=1
            if m>2 or (i==0 and d[j]==0) or (i==12 and m>=2):
                bl=True
                break
        
    if bl:
        print(0)
    else:
        print(1)

Solution (Python):


import itertools
import collections


def main():
    n = int(input())
    c = collections.Counter(map(int, input().split()))
    if c[0] > 0:
        return 0
    if c.most_common(1)[0][1] >= 3:
        return 0
    l1 = []
    l2 = []
    for i, cnt in c.most_common():
        if cnt == 2:
            l2.append(i)
        elif cnt == 1:
            l1.append(i)
    s = 0
    for i in range(len(l1) + 1):
        for invs in itertools.combinations(l1, i):
            l = [0]
            for k in l1:
                l.append(-k if k in invs else k)
            for k in l2:
                l.append(k)
                l.append(-k)
            l.sort()
            ss = 24
            for j in range(len(l)-1):
                ss = min(l[j + 1] - l[j], ss)
            d = l[-1] - l[0]
            ss = min(min(d, 24-d), ss)
            s = max(s, ss)

    return s

print(main())

Solution (Python):
from collections import Counter
N = int(input())
ds = list(map(int, input().split()))
c = Counter(ds)
cs = c.most_common()

if N >= 24 or cs[0][1] >= 3 or 0 in c:
  print("0")
  exit()

l = len(cs)
k = 0
s = (1 << 0) + (1 << 24)
while k < l and cs[k][1] == 2:
  s += (1 << cs[k][0]) + (1 << (24 - cs[k][0]))
  k += 1

def process(s, i):
  if i == l:
    return min(map(len, bin(s)[2:].split("1")[1:-1])) + 1
  return max(
    process(s + (1 << cs[i][0]), i+1),
    process(s + (1 << (24 - cs[i][0])), i+1),
  )

print(process(s, k))