Problem:
 Score : 300 points    Problem Statement There is a circular pond with a perimeter of K meters, and N houses around them.  The i -th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.  When traveling between these houses, you can only go around the pond.  Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.      Constraints  2 \leq K \leq 10^6  2 \leq N \leq 2 \times 10^5  0 \leq A_1 < ... < A_N < K  All values in input are integers.         Input Input is given from Standard Input in the following format:  K  N  A_1  A_2  ...  A_N       Output Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.        Sample Input 1 20 3
5 10 15      Sample Output 1 10  If you start at the 1 -st house and go to the 2 -nd and 3 -rd houses in this order, the total distance traveled will be 10 .       Sample Input 2 20 3
0 5 15      Sample Output 2 10  If you start at the 2 -nd house and go to the 1 -st and 3 -rd houses in this order, the total distance traveled will be 10 .

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):
import numpy as np
 
k, n = map(int, input().split())
arr = list(map(int, input().split()))
diff = [0] * n
 
for i in range(1, n):
  diff[i] = arr[i] - arr[i-1]
diff[0] = k - arr[-1] + arr[0]
 
print(int(np.sum(diff) - max(diff)))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = min(A[-1]-A[0],K-(A[-1]-A[0]))
for i in range(N-1):
  ans = max(ans,A[i+1]-A[i])
print (K-ans)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
result = 0
mx = -float("inf")
for i in range(N-1):
    mx = max(mx, A[i+1] - A[i])
mx = max(mx, K - A[-1] + A[0])

result = K - mx
print(result)

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
if a[0] == 0:
	dis = [k - a[n-1]]
else:
	dis = [k - a[n-1] + a[0]]
for i in range(n-1):
	dis.append(a[i+1] - a[i])
print(k - max(dis))

Solution (Python):
K,N = input().split()
A = input().split()
 
a_list = list(A)

dist = 0
dist_list = []
max = 0
sum = 0
 
for i in range(len(a_list)):
  if i == len(a_list) - 1:
    dist = int(K) - abs(int(a_list[i]) - int(a_list[0]))
  else:
    dist = abs(int(a_list[i]) - int(a_list[i + 1]))
  if i != 0: 
    if max < dist:
        sum += max
        max = dist
    else:
        sum += dist
  else:
      max = dist
  
    
print(sum)

Solution (Python):
import numpy as np
kn = list(map(int,input().split()))
K = kn[0]
N = kn[1]
As = list(map(int,input().split()))

subtract_list_r = np.array([])
subtract_list_l = np.array([])
subtract_list_r = list(subtract_list_r)
subtract_list_l = list(subtract_list_l)

subtract_value_r = 0
subtract_value_l = 0
for i in range(1,N-1):
    subtract_value_r = K-(As[i]-As[i-1])
    subtract_value_l = K-(As[i+1]-As[i])
    subtract_list_r.append(subtract_value_r)
    subtract_list_l.append(subtract_value_l)


subtract_list_r.append(As[N-1]-As[0])
subtract_list_l.append(K-(As[1]-As[0]))
subtract_list_r.append(K-(As[N-1]-As[N-2]))
subtract_list_l.append(abs(As[0]-As[N-1]))
r_min = min(subtract_list_r)
l_min = min(subtract_list_l)

print("{}".format(int(min([r_min,l_min]))))

Solution (Python):

k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(len(a)):
  if i == len(a) - 1:
    distance.append(k+a[0] - a[i])
  else:
  	distance.append(a[i+1] - a[i])
distance.sort()
print(sum(distance[:-1]))

Solution (Python):


import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    minn = min(a[n - 1] - a[0], int(10e10))
    for i in range(n):
        ans = (a[n - 1] - a[i]) + (k + a[i - 1] - a[n - 1])
        minn = min(ans, minn)
    print(minn)


if __name__ == "__main__":
    
    main()

Solution (Python):
import sys
a,b=map(int,input().split())
c=list(map(int,input().split()))
c.sort()
d=[0]*1000000
for i in range(b-1):
  d[i]+=abs(c[i]-c[i+1])
  

d.append(a-(c[-1]-c[0]))

print(a-max(d))

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

B=[]

for i in range(N-1):
    B.append(A[i+1]-A[i])
B.append(K+A[0]-A[N-1])
print(K-max(B))

Solution (Python):
[K,N]= [int(x) for x in input().split()]

lists = [int(x) for x in input().split()]
lists.sort()
sor = []
for i in range(N-1):
    sor.append(lists[i+1]-lists[i])
sor.append(K-lists[N-1]+lists[0])
print(K-max(sor))

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):
def main():
    k, n = map(int, input().split())
    a = [int(i) for i in input().split()]
    r = [a[i+1] - a[i] for i in range(n-1)]
    if a[-1] - a[0] < k/2.:
        r.append(a[-1]-a[0])
    else:
        r.append(a[0] + k - a[-1])
    rmax = max(r)
    ans = k - rmax
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
K, N = map(int, input().split())
house = list(map(int,input().split()))
house = house + house
min = house[N - 1] - house[0]

for i in range(N - 1):
  if K - (house[N + 1 + i] - house[i]) < min and house[N + 1 + i] - house[i] <= K/2 :
    min = K - (house[N + 1 + i] - house[i])

print(int(min))

Solution (Python):
t = (int(x) for x in input().split(" "))
T = list(t)
K = T[0]
N = T[1]
x = (int(x) for x in input().split(" "))
X = list(x)
y = []
for i in range(0, N-1):
    y.append(X[i+1]-X[i])
y.append(X[0]+K-X[N-1])
z = max(y)
s =K - z
print(s)

Solution (Python):

from sys import stdin
from operator import itemgetter
import math


K, N = [int(x) for x in stdin.readline().rstrip().split()]
A = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
span_list = []

for i in range(N):
    
    if i < N-1:
        span = A[i+1] - A[i]
        span_list.append(span)
    else:
        span = K - A[i] + A[0]
        span_list.append(span)

span_list.sort()

del span_list[-1]

print(sum(span_list))

Solution (Python):
num_list = [int(i) for i in input().split()]
dist_list = [int(i) for i in input().split()]

temp = [(dist_list[-1]-num_list[0])] + dist_list[:-1]
sabun_list = [dist_list[i] - temp[i] for i in range(len(dist_list))]

print(sum(sabun_list) - max(sabun_list))

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):


K, N = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
m = 0
ind = 0
ans = 0

for i in range(N-1):
  _m = A[i+1] - A[i]
  if _m > m:
    m = _m
    ind = i+1
    last = i

_m = K + A[0] - A[N-1]
if _m > m:
  m = _m
  ind = 0
  last = N-1




i = ind
while i != last:
  
  if i < N-1:
    ans += A[i+1] - A[i]
    i += 1
  else:
    ans += K + A[0] - A[i]
    i = 0

print(ans)

Solution (Python):
k , n = (int(x) for x in input().split())
a = [int(i) for i in input().split()]
b = []
 
for i in range(n-1):
    b.append(a[i+1] - a[i])
    
b.append(a[0] + k -a[n-1])

ans=0 
b.sort()
 
for i in range(len(b)-1):
    ans = b[i] + ans

print(ans)

Solution (Python):
k, n = map(int,input(). split())
lstr_a = input().split(" ")
lint_a = [int(n) for n in lstr_a]
alist = sorted(lint_a)
t = alist.count(0)
x = 0
for i in range(n-1):
  x = max(alist[i+1]-alist[i], x)
b = k-alist[n-1]+alist[0]
c = max(x,b)
answer = k-c
print(answer)

Solution (Python):
import numpy as np
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=np.array(a)
if max(abs(a[1:]-a[:-1])) < k-abs(a[0]-a[-1]):b=k-abs(a[0]-a[-1])
else:b= max(abs(a[1:]-a[:-1]))
print(k-b)

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

A.append(K+A[0])

D = []

for i in range(N):
    D.append(abs(A[i+1]-A[i]))

print(K-max(D))

Solution (Python):
K,N=map(int,input().split())
dis_lst=list(map(int,input().split()))
sa_lst=[]
for i in range(N):
    if i==N-1 and 0 in dis_lst:
      sa_lst.append(K-dis_lst[i])
      break
    elif i==N-1:
      sa_lst.append(K-dis_lst[i]+dis_lst[0])
      
      break
    sa_lst.append(abs(dis_lst[i]-dis_lst[i+1]))
    
    
sa_lst.remove(max(sa_lst))
print(sum(sa_lst))

Solution (Python):


import sys
import math

K, N = map(int, input().split())
arr = list(map(int, input().split()))
List=[]
for i in range(N-1):
    sa =int(arr[i+1]-arr[i])
    List.append(sa)

max_num = max(List)
hasi = K-arr[N-1]+arr[0]
PPP = (max(max_num,hasi))
ans = K-PPP
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    
    mini=k
    for i in range(n):
        tmp=a[i-1]-a[i]
        if tmp<0:
            tmp+=k
        mini=min(mini,tmp)
    print(mini)

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

k,n = map(int,input().split())
A = list(map(int, input().split()))
A = np.array(A)
B = np.diff(A, n=1)

B_ = (A[0])+(k-A[n-1])
B_ans = np.append(B,B_)
ans = np.argmax(B_ans)

print(k - B_ans[ans])

Solution (Python):
k,n = map(int, input().split())
a = list(int(j) for j in input().split())

deff_list = []

for i in range(len(a)-1):
    deff_list.append(a[i+1]-a[i])

deff_list.append(min(a)+(k-max(a)))

deff_list[deff_list.index(max(deff_list))] = 0

print(sum(deff_list))

Solution (Python):
import numpy as np

k,n = map(int,input().split())
A = list(map(int, input().split()))
A = np.array(A)
B = np.diff(A, n=1)

B_ = (A[0])+(k-A[n-1])
B_ans = np.append(B,B_)
ans = np.argmax(B_ans)

print(k - B_ans[ans])

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
m=0
for i in range(N-1):
  if m<(A[i+1]-A[i]):
    m=(A[i+1]-A[i])
if m<(K-A[-1]+A[0]):
  print(A[-1]-A[0])
else:
  print(K-m)

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


sum=(K-A[N-1])+A[0]
for i in range(1,N):
    l=A[i]-A[i-1]
    if l>=sum:
        sum=l

print(int(K-sum))

Solution (Python):
from sys import stdin, stdout  

K, N = [int(c) for c in input().split()]
A = [int(c) for c in input().split()]
dist=[]
for i in range(0,N-1):
    dist.append(A[i+1]-A[i])
dist.append(abs(A[0]-A[-1]+K))

m=max(dist)
for idx,val in enumerate(dist):
    if(val==m):
        dist[idx]=0
        break
print(sum(dist))

Solution (Python):
K, N = list(map(int, input().split()))
A = [0] * N
A = list(map(int, input().split()))

sorted_A = sorted(A)
sorted_A = [i - sorted_A[0] for i in sorted_A]

dis = []
for i in range(len(sorted_A)-1):
    dis.append(abs(sorted_A[i] - sorted_A[i+1]))

last_dis = abs(sorted_A[-1] - sorted_A[0])

if(last_dis >= K/2):
    last_dis = K - last_dis

dis.append(last_dis)

print(K - max(dis))

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

dist=[]
dist.append(abs(Ai[N-1]-Ai[0]))
for i in range(N-1):
  dist.append(abs((K+Ai[i])-Ai[i+1]))

print(min(dist))

Solution (Python):

data = input().split()

round = int(data[0])
cnt_house = int(data[1])

distance = input().split()

min_dist = None

for start in range(cnt_house):
  if start == 0:
    move_dist = int(distance[cnt_house - 1]) - int(distance[start])
  else:
    move_dist = (round - int(distance[start])) + int(distance[start - 1])
  
  if min_dist is None or min_dist > move_dist:
    min_dist = move_dist

print(min_dist)

Solution (Python):
k, n = map(int,input().split())
list = [int(x) for x in input().split()]
root = []
for i in range(len(list)-1):
    root.append(list[i+1]-list[i])

root.append(k-list[-1]+list[0])

sum = sum(root)
ans = []

for j in range(len(root)):
    ans.append(sum-root[j])


ans.sort()
print(ans[0])

Solution (Python):
import math
a , b = map(int,input().split())
res = list(map(int,input().split()))
res2 = []
if len(res) == 1:
    print(0)
else:
    for i in range(len(res)-1):
        aa = res[i+1] - res[i]
        res2.append(aa)
    if res[0] == 0:
        res2.append(a-res[-1])
    else:
        res2.append(res[0] + (a - res[-1]))
print(sum(res2) - max(res2))

Solution (Python):
k, n = map(float, input().split())
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print(int(min))

Solution (Python):
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))
d = [A[i+1]-A[i] for i in range(N-1)]
de = K - A[N-1] + A[0]
d.append(de)
D = sorted(d)
dis = 0
for i in range(N-1):
  dis += D[i]
print(dis)

Solution (Python):
def main():
    input1 = input()
    input2 = input()
    k , n = map(int , input1.split())
    al = list(map(int , input2.split()))
    arg = [[k,n],al]
    ans = process(arg)
    print(ans)
    
def process(arg):
    ans = 0
    ensyu = arg[0][0]
    kensu = arg[0][1]
    aida = list()
    for x in range(arg[0][1]):
        mae = arg[1][x-1]
        b = arg[1][x]
        if x == 0:
            
            w = b + ensyu - arg[1][kensu-1]
        else:
            w = b - mae
        aida.append(w)

    m = max(aida)
    flg = False
    for y in aida:
        if flg == True or y != m :
            ans = ans + y
        else:
            flg = True
    return ans

def getInput(input):

    return 

if __name__ == '__main__':
    main()

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

a.append(a[0] + k)

distance = []

for i in range(len(a) - 1):
    distance.append(a[i+1] - a[i])

mindistance = 0
for j in distance:
    mindistance += j

print(mindistance - max(distance))

Solution (Python):

K,N = list(map(int, input().split()))
A = list(map(int, input().split()))

a = A[1] - A[0]
for i in range(1,N-1):
    j = i + 1
    a = max(a,A[j]-A[i])

b = K-(A[N-1]-A[0])
c = max(a,b)
ans = K -c
print(ans)

Solution (Python):
k, n = map(int, input().split())
*a, = map(int, input().split())
a += [i+k for i in a]
print(min(j-i for i, j in zip(a, a[n-1:])))

Solution (Python):
def solution():
    k, n = map(int, input().split())
    house = list(map(int, input().split()))
    next_dist = []

    for i in range(1, len(house)):
        next_dist.append(house[i]-house[i-1])
    next_dist.append(k-house[len(house)-1]+house[0])
    max_dis_index = next_dist.index(max(next_dist))
    del next_dist[max_dis_index]
    print(sum(next_dist))

solution()

Solution (Python):
k, n = map(float, input().split())
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print(int(min))

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

l = [0] * N
hoge = 0

for n in range(N):
  l[n] = A[n]-hoge
  hoge = hoge + l[n]
  
l = l + [K-A[N-1]]

if A[0]!=0 :
  l[N] = l[N]+l[0]
  l[0] = 0

print(sum(l)-max(l))

Solution (Python):
k, n, *a = map(int, open(0).read().split())
a.append(k + a[0]) 
ans = 0
for i in range(n):
    ans = max(ans, a[i + 1] - a[i]) 
print(k - ans)

Solution (Python):
i = list(map(int, input().split()))
K = i[0]
N = i[1]
max = 0
count = 0


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

for j in range(len(A)-1):
    count += 1
    if abs(A[j+1] - A[j]) > max:
        max = abs(A[j+1] - A[j])
    if count == (len(A)-1):
        if max < abs(A[0] + (K - A[j+1])):
            max = abs(A[0] + (K - A[j+1]))

print(K - max)

Solution (Python):
k, n = map(int , input().split())
lis = list(map(int,input().split()))
mi = [min(lis)] * n
li = [x - y for (x, y) in zip(lis, mi)]

li.append(k)
ans = list()
for i in range(n):
  ans.append(abs(li[(i + 1)] - li[i]))
print(k - max(ans))

Solution (Python):
k, n = map(float, input().split())
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i-1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print(int(min))

Solution (Python):
k, n = map(int, input().split())
a_array = [int(x) for x in input().split()]
diff = []
tmp = 0
for i in range(n - 1):
    diff.append(abs(a_array[i + 1] - a_array[i]))
diff.append(k - a_array[-1] + a_array[0])
print(sum(diff) - max(diff))

Solution (Python):
K, N = map(int,input().split())
A = list(map(int,input().split()))
ans = float("inf")
for i in range(N):
    if i == 0:
        ans = min(K+A[i]-A[1], -A[i]+A[N-1], ans)
    elif i == N-1:
        ans = min(A[i]-A[0], K-A[i]+A[i-1], ans)
    else:
        ans = min(K+A[i]-A[i+1], K-A[i]+A[i-1], ans)
print(ans)

Solution (Python):


def solve():
    K, N = map(int, input().split())
    As = list(map(int, input().split()))

    dis = 0
    point = 0
    for i in range(1, N):
        tmp = As[i]-As[i-1]
        if tmp > dis:
            dis = tmp
            point = i

    if As[N-1]-As[0] < K-As[point]+As[point-1]:
        print(As[N-1]-As[0])
    else:
        print(K-As[point]+As[point-1])


if __name__ == "__main__":
    solve()

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

max_distance = abs(k - a[-1] + a[0])
str_index = 0
gl_index = -1
for i in range(n):
    if i == 0:
        continue
    elif max_distance < abs(a[i-1] - a[i]):
        max_distance = abs(a[i-1] - a[i])
        str_index = i-1
        gl_index = i
if gl_index == -1:
    print(a[gl_index] - a[str_index])
else:
    print(a[str_index] + (k - a[gl_index]))

Solution (Python):
k,n = map(int,input().split())
a = [int(x) for x in input().split()]
if a[0] == 0:
    a.append(k)
else:
    a.append(k + a[0])

dis = 0
d_li = []
for i in range(1,len(a)):
    tmp = abs(a[i] - a[i-1])
    dis += tmp
    d_li.append(tmp)
    
print(dis - max(d_li))

Solution (Python):
import sys
from math import sqrt
from collections import Counter, defaultdict

input = sys.stdin.readline


def I():
    return int(input())


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    k, n = MI()
    a_list = LI()

    a_list.sort()
    max = 0
    for i, a in enumerate(a_list):
        if i != n - 1:
            d = a_list[i + 1] - a
        else:
            d = a_list[0] + k - a

        if d > max:
            max = d
            max_i = (i + 1) % n

    l = []
    for a in a_list:
        tmp = a - a_list[max_i]
        if tmp >= 0:
            l.append(tmp)
        else:
            l.append(k + tmp)

    l.sort()
    print(l[n - 1])

    pass


if __name__ == "__main__":
    main()

Solution (Python):
A=input()
LA=A.split()
K=int(LA[0])
N=int(LA[1])

B=input()
LB=B.split()

C=0
samax=0
start=0

for n in LB:
  sa=int(int(n)-int(C))
  C=n
  if samax < sa:
    samax=sa
    start=int(n)-int(samax)
    goal=int(n)
ans1=int(LB[N-1])-int(LB[0])
ans2=int((K-goal)+int(start))

if ans2 > ans1:
  ans=ans1
else :
  ans=ans2
print(ans)

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

def resolve():
    N, K = iim()
    A = list(iim())

    d1 = 0
    for i in range(K-1):
        d2 = A[i+1] - A[i]
        d1 = max(d1, d2)
    d1 = max(d1, N-A[-1]+A[0])
    print(N-d1)


if __name__ == "__main__":
    resolve()

Solution (Python):
i = list(map(int,input().split()))
house = list(map(int,input().split()))

ans = []
for h in range(len(house)-1):
  num = house[h+1]-house[h]
  ans.append(num)
l = i[0] - house[-1] + house[0]
ans.append(l)

ans.remove(max(ans))

print(sum(ans))

Solution (Python):


def main():
    k, n = map(int, input().split())
    A = list(map(int, input().split()))
    
    maxIndex = 0
    maxDist = 0
    
    for i in range(n):
        if i == n - 1:
            gap = A[0]+k - A[i]
        else:
            gap = A[i+1] - A[i]
        
        if gap >= maxDist:
            maxDist = gap
            maxIndex = i
    
    if maxIndex == n - 1:
        print(A[-1] - A[0])
    else:
        print((A[maxIndex]+k) - A[maxIndex+1])            

if __name__ == '__main__':
    main()

Solution (Python):
k,n,*a=map(int,open(0).read().split())
a+=[a[0]+k]
d=[abs(a[i]-a[i+1]) for i in range(n)]
print(k-max(d))

Solution (Python):
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
res = A[-1] - A[0]
for a1, a2 in zip(A, A[1:]):
    res = min(res, K - a2 + a1)
print(res)

Solution (Python):

k,n=map(int,input().split())
a=list(map(int,input().split()))

d=[]
for i in range(n-1):
    d.append(a[i+1]-a[i])




ans=10**10
d=[k-a[-1]+a[0]]+d
d+=[k-a[-1]+a[0]]

for i in range(1,n):
    t=min(k-d[i-1],k-d[i+1])
    ans=min(t,ans)
print(ans)

Solution (Python):
KN = list(map(int, input().split()))
A = list(map(int, input().split()))

answer = []

for i,home in enumerate(A):
    if home < A[i - 1]:
        answer.append(home - A[i - 1] + KN[0])
    else:
        answer.append(home - A[i - 1])

print(KN[0] - max(answer))

Solution (Python):
K,N=map(int,input().split())
a=list(map(int,input().split()))
route=[]
for i in range(N-1):
    route.append(int(a[i+1]-a[i]))
route.append(int(K-a[N-1])+a[0])
print(K-max(route))

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





minmin = k
for i in range(n):
    start = i
    end = -1 + i
    if i == 0:
        end = n - 1
    
    
    result = k
    if i == 0:
        result = a[end] - a[start]
        
    else:
        result = (a[end] + k) - a[start]
        
        
    if result < minmin:
        minmin = result
        
    
        
print(minmin)

Solution (Python):
K,N=input().split()
A=input().split()
n=0
m=1
S=list()
for i in range(int(N)-1):
  S.append(int(A[n+1])-int(A[n]))
  n=n+1
S.append(int(A[0])+(int(K)-int(A[-1])))

ans=int(K)-int(max(S))
print(ans)

Solution (Python):
K, N = (int(x) for x in input().split())
A = [int(i) for i in input().split()]
A.append(K + A[0])
D = [0] * N
for i in range(N):
    D[i] = A[i+1] - A[i]

print(K - max(D))

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
p=[]
for i in range(n-1):
    p.append(a[i+1]-a[i])
if a[0]==0:p.append(k-a[n-1])
else:p.append(k-a[n-1]+a[0])

print(k-max(p))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(A[0])
max=(K-A[-2])+A[0]
for i in range(N):
  if A[i]-A[i-1]>max:
    max=A[i]-A[i-1]
print(K-max)

Solution (Python):
a=input().split(' ')
b=input().split(' ')
c=0
for i in range(int(a[1])):
    if i != int(a[1])-1:
        if int(b[i+1]) - int(b[i]) > c:
            c = int(b[i+1]) - int(b[i])
    else:
        if int(a[0]) - int(b[-1])+ int(b[0]) > c:
            c = int(a[0]) - int(b[-1])+ int(b[0])
print(int(a[0])-c)

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

count = A[-1]-A[0]
def double(n):
     return n+K

B = map(double, A)
B = list(B)

i=0
while i<N-1:
    if B[i]-A[i+1] < count:
        count = B[i]-A[i+1]
    i+=1

print(count)

Solution (Python):
import numpy as np
 
tmp =list(map(int, input().split()))
num=list(map(int, input().split()))

num.append(num[0]+tmp[0])

print(np.diff(num).sum()-np.diff(num).max())

Solution (Python):
k,n = map(int,input().split())
A = list(map(int,input().split()))
result = 1000001
S = [0]
s = 0

for n,i in enumerate(A):
    if(n <= len(A)-2):
        s = A[n+1] - A[n]
        S.append(s)
S.append(k-A[len(A)-1]+A[0])

tmp = 0
for s in range(len(S)):
    tmp += S[s]
    S[s] = tmp



for i in range(len(S)):
    if(i == 0):
        ans = S[len(S)-2] - S[0]
        if(result>ans):
            
            result = ans
    else:
        ans = S[len(S)-1]- S[i] + S[i-1] - S[0]
        if(result>ans):
            
            result = ans
A.reverse()
S = [0]
for n,i in enumerate(A):
    if(n <= len(A)-2):
        s = k - A[n]
        S.append(s)
S.append(k)


for i in A:
    s += i
    S.append(s)
for i in range(len(S)):
    if(i == 0):
        S[len(S)-2] - S[0]
        if(result>ans):
            
            result = ans
    else:
        ans = S[len(S)-1]- S[i] + S[i-1] - S[0]
        if(result>ans):
            
            result = ans
print(result)

Solution (Python):
k,n = list(map(int,input().split()))
arr = list(map(int,input().split()))
x = []
val = arr[-1] - arr[0]
if val<= k/2:
	x.append(val)
else:
	x.append(k-val)
for i in range(1,n):
	val = arr[i] - arr[i-1]
	if val <= (k/2):
		x.append(val)
	else:
		x.append(k-val)
print(k-max(x))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=K
B=[0]*N
for i in range(N):
    B[i]=min(abs(A[i]-A[i-1]),K-abs(A[i]-A[i-1]))
s=sum(B)    
for i in range(N):
    ans=min(ans,s-B[i])
    
print(ans)

Solution (Python):
(n,m)=map(int,input().split())
a=input().split()
a=list(map(int,a))
l=[]
k=0
a=sorted(a)
for i in range(m-1):
    d=abs(a[i]-a[i+1])
    k=k+d
    l.append(d)

c=n-k
l.append(c)
l=sorted(l)
l1=sum(l)
print(l1-l[-1])

Solution (Python):

K, N = map(int, input().split())
A = list(map(int, input().split()))
lst = []
for i in range(1, N-1):
    diff_1 = abs(A[i + 1] - A[i])
    lst.append(diff_1)

lst.append(K + A[0] - A[N-1])
print(K - max(lst))

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

max_a = max(A)
min_a = min(A)
diff_min = max_a - min_a

if N == 2:
    if A[1] - A[0] < K - (A[1] - A[0]):
        diff_min = A[1] - A[0]
    else:
        diff_min = K - (A[1] - A[0])
else:
    for i in range(1,N-1):
        tmp = K - (A[i+1] - A[i])
        if tmp < diff_min:
            diff_min = tmp
        tmp = K - (A[i] - A[i-1])
        if tmp < diff_min:
            diff_min = tmp

print(diff_min)

Solution (Python):
K, N = map(int, input().split())
A = sorted(map(int, input().split()))

length = A[0] + K - A[-1]
for i in range(N-1):
    length = max(A[i+1] - A[i] , length)
    if length == A[i+1] - A[i]:
        ans = A[i] + K - A[i+1]
      
if length == A[0] + K - A[-1]:
    ans = A[-1] - A[0]
print(ans)

Solution (Python):
K, N = input().split()
K = int(K)
N = int(N)
houses = list(map(int, input().split()))
first = True
distance = 0
for i in range(N):
  firstHouse = houses[i]
  lastHouse = houses[i - 1]
  if lastHouse > firstHouse:
    distance = lastHouse - firstHouse
  else:
    distance = (K - firstHouse) + lastHouse
  if first or distance < minDistance:
    minDistance = distance
    first = False
print(minDistance)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(N-1):
    if A[i+1]-A[i]>ans:
        ans = A[i+1]-A[i]
if ans> K-A[N-1]+A[0]:
    print(K-ans)
else:
    print(A[N-1]-A[0])

Solution (Python):

K, N = map(int, input().split())
A = list(map(int, input().split()))
lst = []
for i in range(1, N-1):
    diff_1 = abs(A[i + 1] - A[i])
    lst.append(diff_1)
    diff_2 = abs(A[i] - A[i - 1])
    lst.append(diff_2)

lst.append
lst.append(K + A[0] - A[N-1])
print(K - max(lst))

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*(n-1)
b= [x - y for (x, y) in zip(a[1:],a[:-1])]
c=0
c=k+a[0]-a[n-1]
m=max(b)
M=max(m,c)
print(k-M)

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

dist = k
for i in range(len(a)-1):
    d = k - (a[i+1]-a[i])
    if d < dist:
        dist = d
if max(a)-min(a) < dist:
    print(max(a)-min(a))
else:
    print(dist)

Solution (Python):
K,N  =list(map(int,input().split()))
A = list(map(int,input().split()))
C = list()
ans = int()
B = list(sorted(A))
for i in range(len(B)-1):
    C.append(B[i+1]-B[i])
C.append((K-B[-1])+(B[0]))
C = sorted(C)
for i in range(N-1):
    ans += C[i]
print(ans)

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

ans = A[-1] - A[0]

for i in range(len(A) - 1):
    
    tmp = A[i] - (A[i + 1] - K)
    if ans > tmp:
        ans = tmp

print(ans)

Solution (Python):
kn = input().split()
a = input().split()

al = [int(i) for i in a]
k = int(kn[0])
n = kn[1]

ml = k - abs(al[len(al)-1] - al[0])


for i in range(len(al)-1):
	ml = max(ml,abs(al[i]-(al[i+1])))


print(k-ml)

Solution (Python):

temp=input()
temp2=temp.split(" ")
K=int(temp2[0])
N=int(temp2[1])

temp3=input()
A=temp3.split(" ")
A=[int(i) for i in A]

A.append(K+A[0])






D=list()

for i in range(N):
  D.append(A[i+1]-A[i])

maxind=D.index(max(D))


sumd=0


for i in range(N):
  if(i!=maxind):
    sumd+=D[i]


print(sumd)

Solution (Python):
K, N = map(int, input().strip().split())
As = list(map(int, input().strip().split()))

max_interval = 0
index = None
distance = 0
for i in range(N):
    if i == N-1:
        interval = K - As[i] + As[0]
    else:
        interval = abs(As[i+1] - As[i])
    if interval > max_interval:
        max_interval = interval
        index = i
        
if index == N-1:
    distance = As[index] - As[0]
else:
    distance = As[index] + K - As[index+1]
print(distance)

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
kyori=[]

for i in range(len(a)-1):
    l = abs(a[i] - a[i+1])
    kyori.append(l)
kyori.append(k - abs(a[0]-a[n-1]))

saitann = k - max(kyori)
print(saitann)

Solution (Python):
K,N = map(int,input().split())
A_str = input().split()
A = [int(h) for h in A_str]
D = []
for i in range(1,N):
  D.append(A[i]-A[i-1])
D.append(A[0]+(K-A[N-1]))
maximum = max(D)
print(K-maximum)

Solution (Python):
K,N=(int(x) for x in input().split())
A =[int(x) for x in input().split()]

A.append(K + A[0])

A_sub = []

for i in range(len(A)-1):
    A_sub.append(abs(A[i] - A[i + 1]))

print(sum(A_sub) - max(A_sub))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
k = 0
for i in A:
  k += 1
  if k != N:
    B[k-1]=A[k]-A[k-1]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))

Solution (Python):
import numpy as np
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
d = np.diff(a)
result = k - max(np.max(d), a[0] + k - a[-1])
print(result)

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

if __name__ == "__main__":
    K, N = MI()
    A = LI()
    a = []
    for i in range(N):
        if i != N-1:
            a.append(abs(A[i]-A[i+1]))
        else:
            a.append(abs(A[i]-K-A[0]))
    a.remove(max(a))
    print(sum(a))

Solution (Python):
K, N = map(int, input().split())
lis_A = list(map(int, input().split()))
dif = [0]*len(lis_A)
dif[0] = K-lis_A[-1] + lis_A[0]
for i in range(1,len(lis_A)):
    dif[i] = lis_A[i]-lis_A[i-1]
ans = sum(dif)-max(dif)
print(ans)