Problem:
Distance II Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$. The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance. \[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\] It can be the Manhattan distance \[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\] where $p = 1 $.  It can be the Euclidean distance \[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\] where $p = 2 $.  Also, it can be the Chebyshev distance  \[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]  where $p = \infty$ Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively. Input In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers. Output Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10 -5 . Constraints  $1 \leq n \leq 100$  $0 \leq x_i, y_i \leq 1000$  Sample Input 3
1 2 3
2 0 4 Sample Output 4.000000
2.449490
2.154435
2.000000

Solution (Python):
p1 = 0
p2Tmp = 0
p3Tmp = 0
pInfElement = []
dataNum = int(input())
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

for index in range(dataNum):
    p1 += abs(x[index] - y[index])
    p2Tmp += ((x[index] - y[index])**2)
    p3Tmp += (abs(x[index] - y[index])**3)
    pInfElement.append(abs(x[index] - y[index]))

p2 = p2Tmp **(1/2)
p3 = p3Tmp **(1/3)
pInf = max(pInfElement)

print(p1)
print(p2)
print(p3)
print(pInf)

Solution (Python):
n = int(input())
x = input().split()
y = input().split()

D1 = 0
D2 = 0
D3 = 0
D = []

for i in range(n):
    D1 += abs(float(x[i])-float(y[i]))
    D2 += (abs(float(x[i])-float(y[i])))**2
    D3 += (abs(float(x[i])-float(y[i])))**3
    D.append(abs(float(x[i])-float(y[i])))

print(D1)
print(D2**(1/2))
print(D3**(1/3))
print(max(D))

Solution (Python):

import math

def Manhattan(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]
    return sum

def Euclid(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]**2
    return math.sqrt(sum)

def Minkowski3(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]**3
    return math.pow(sum, 1/3)

def Chebichev(n, *d):
    return max(d)

n = int(input().rstrip())
x = list(map(float, input().rstrip().split()))
y = list(map(float, input().rstrip().split()))
d = []
for i in range(n):
    d.append(abs(x[i] - y[i]))
print("{0:.5f}".format(Manhattan(n,*d)))
print("{0:.5f}".format(Euclid(n,*d)))
print("{0:.5f}".format(Minkowski3(n,*d)))
print("{0:.5f}".format(Chebichev(n,*d)))

Solution (Python):
num = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

mh = sum(abs(x-y) for x, y in zip(xs, ys))
ec = pow(sum(abs(x-y)**2 for x, y in zip(xs, ys)), 1/2)
p3 = pow(sum(abs(x-y)**3 for x, y in zip(xs, ys)), 1/3)
cv = max(abs(x-y) for x, y in zip(xs, ys))

print(round(mh, 8))
print(round(ec, 8))
print(round(p3, 8))
print(round(cv, 8))

Solution (Python):
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

sum1 = 0
sum2 = 0
sum3 = 0
inf = 0
for i in range(n):
    tmp = abs(x[i] - y[i])
    sum1 += tmp
    sum2 += tmp ** 2
    sum3 += tmp ** 3
    inf = max(inf, tmp)
print("{:.6f}".format(sum1))
print("{:.6f}".format(sum2 ** (1 / 2)))
print("{:.6f}".format(sum3 ** (1 / 3)))
print("{:.6f}".format(inf))

Solution (Python):
from math import inf
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
def dist(x,y,k):
    if k == inf:
        d = 0
        for i in range(n):
            tmp = abs(x[i] - y[i])
            if tmp > d:
                d = tmp
        return d
    d = 0
    for i in range(n):
        d += (abs(x[i] - y[i]))**k
    return d**(1/k)
print(dist(x,y,1))
print(dist(x,y,2))
print(dist(x,y,3))
print(dist(x,y,inf))

Solution (Python):
num = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0

for i in range(num):
        sum1 += abs(x[i] - y[i])
        sum2 += abs(x[i] - y[i]) ** 2
        sum3 += abs(x[i] - y[i]) ** 3
        if sum4 < abs(x[i] - y[i]):
                sum4 = abs(x[i] - y[i])

print("{:.5f}".format(sum1))
print("{:.5f}".format(pow(sum2, 1 / 2)))
print("{:.5f}".format(pow(sum3, 1 / 3)))
print("{:.5f}".format(sum4))

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

for i in [1, 2, 3]:
    g = [abs(x[m] - y[m]) ** i for m in range(n)]
    print(sum(g) ** (1 / i))
print(max([abs(x[m] - y[m]) for m in range(n)]))

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d1, d2, d3 = 0, 0, 0
dm = []
for i in range(n):
    d1 += abs(x[i] - y[i])
    d2 += (abs(x[i] - y[i])) ** 2
    d3 += (abs(x[i] - y[i])) ** 3
    dm.append(abs(x[i] - y[i]))
print(d1)
print(d2 ** 0.5)
print(d3 ** (1/3))
print(max(dm))

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
s = [0] * 4
for i in range(n):
	s[0] += abs(x[i] - y[i])
	s[1] += abs(x[i] - y[i])**2
	s[2] += abs(x[i] - y[i])**3
	s[3] = max(s[3], abs(x[i] - y[i]))
s[1] = s[1]**(1/2)
s[2] = s[2]**(1/3)
for i in range(4):
	print('%f' % s[i])

Solution (Python):
input()
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]
print("{:.6f}".format(sum([abs(i - j) for i, j in zip(x, y)])))
print("{:.6f}".format(sum([(i - j) ** 2 for i, j in zip(x, y)]) ** 0.5))
print("{:.6f}".format(sum([abs(i - j) ** 3 for i, j in zip(x, y)]) ** (1/3)))
print("{:.6f}".format(max([abs(i - j) for i, j in zip(x, y)])))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
def euc(p):
    return sum([abs(i-j)**p for i,j in zip(a,b)])**(1/p)
for i in range(1,4):
    print("{:.8f}".format(euc(i)))
print("{:.8f}".format(max([abs(i-j) for i,j in zip(a,b)])))

Solution (Python):
import math


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

d = []
for i in range(n):
    dis = math.fabs(a[i]-b[i])
    d.append(dis)

print(sum(d))

dd_sum = 0
for i in range(n):
    dd_sum += d[i]**2
print(math.sqrt(dd_sum))

ddd_sum = 0
for i in range(n):
    ddd_sum += d[i]**3
print(math.pow(ddd_sum,(1.0/3.0)))

print(max(d))

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
for i in range(1,4):
    print(sum([abs(a[j]-b[j])**i for j in range(n)])**(1/i))
print(max([abs(a[i]-b[i]) for i in range(n)]))

Solution (Python):
dim = int(input())
x = [int(s) for s in input().split()]
y = [int(s) for s in input().split()]
one = 0
two = 0
three = 0
infinity = 0
for i in range(dim):
    term = abs(x[i] - y[i])
    one += term
    two += term ** 2
    three += term ** 3
    infinity = max(infinity, term)
print(round(one, 6))
print(round(two ** (1 / 2), 6))
print(round(three ** (1/3), 6))
print(infinity)

Solution (Python):
import sys
import math

n = int(sys.stdin.readline())
x, y = (list(map(int, line.strip().split())) for line in sys.stdin.readlines()[0:2])


def distance(p):
    return math.pow(sum(abs(x[i] - y[i]) ** p for i in range(n)), 1/p)


def distance_inf():
    return max(abs(x[i] - y[i]) for i in range(n))


for i in range(1, 4):
    print('{0:.8f}'.format(distance(i)))

print('{0:.8f}'.format(distance_inf()))

Solution (Python):
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
p = [0 for i in range(4)]
buf = 0
for i in range(n):
    buf = abs(x[i]-y[i])
    p[0] += buf
    p[1] += buf ** 2
    p[2] += buf ** 3
    if p[3] <= buf:
        p[3] = buf
p[1] = p[1] ** (1/2)
p[2] = p[2] ** (1/3)
for i in range(4):
    print('{:.8f}'.format(p[i]))

Solution (Python):
n = input()
xs = map(float, input().split())
ys = map(float, input().split())
abs_d = [abs(x - y) for (x, y) in zip(xs, ys)]
d = [sum([i ** p for i in abs_d]) ** (1 / p) for p in range(1, 4)]
d.append(max(abs_d))

print(*d, sep='\n')

Solution (Python):
def minkowski_distance(arr1, arr2, p):
    if p == 'inf':
        cmp_arr = [abs(x-y) for x, y in zip(arr1, arr2)]
        return max(cmp_arr)
    sum_cmp_arr = sum([pow(abs(x-y), p) for x, y in zip(arr1, arr2)])
    return pow(sum_cmp_arr, 1/p)


n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
for p in [1, 2, 3, 'inf']:
    print(f'{minkowski_distance(arr1,arr2,p):.8f}')

Solution (Python):
n = input()
xs = map(float, input().split())
ys = map(float, input().split())
abs_d = [abs(x - y) for (x, y) in zip(xs, ys)]
d = [sum([i ** p for i in abs_d]) ** (1 / p) for p in range(1, 4)]
d.append(max(abs_d))

print(*d, sep='\n')

Solution (Python):
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

d1=[]
p1=0
p2=0
p3=0
pi=0

for i in range(n):
    di=abs(x[i]-y[i])
    p1=p1+di
    p2=p2+pow(di,2)
    p3=p3+pow(di,3)
    if di>pi:
        pi=di

p2=math.sqrt(p2)
p3=pow(p3,(1.0/3.0))

print(p1)
print(p2)
print(p3)
print(pi)

Solution (Python):

from functools import reduce
import math


n = int(input())
l1  =  list(map(int,input().split()))
l2  =  list(map(int,input().split()))

sum1 = sum2 = sum3 = 0
max = 0
for i in range(0,n):
    sum1 += abs(l1[i] - l2[i])
    sum2 += pow(abs(l1[i] - l2[i]),2)
    sum3 += pow(abs(l1[i] - l2[i]),3)
    if ( max < abs(l1[i] - l2[i])):
        max = abs(l1[i] - l2[i])


print (sum1)
print (math.sqrt(sum2))
print (math.pow(sum3,1.0/3.0))
print (max*1.0)

Solution (Python):
n = int(input())
x, y = [list(map(int, input().split())) for _ in range(2)]
print(sum([abs(x[i] - y[i]) for i in range(n)]))
print(pow(sum([(x[i] - y[i]) ** 2 for i in range(n)]), 1/2))
print(pow(sum([abs(x[i] - y[i]) ** 3 for i in range(n)]), 1/3))
print(max([abs(x[i] - y[i]) for i in range(n)]))

Solution (Python):
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d1,d2,d3,d_inf=0,0,0,[]
for i in range(n):
    d1 += abs(x[i]-y[i])
    d2 += abs(x[i]-y[i])**2
    d3 += abs(x[i]-y[i])**3
    d_inf.append(abs(x[i]-y[i]))
print(d1)
print(d2**(1/2))
print(d3**(1/3))
print(max(d_inf))

Solution (Python):
import math
import statistics

n = int(input())

xlist = list(map(int, input().split()))
ylist = list(map(int, input().split()))

p1 = 0
p2 = 0
p3 = 0
plist = []

for x, y in zip(xlist, ylist):
    p1 += abs(x - y)
    p2 += (abs(x - y)**2)
    p3 += (abs(x - y)**3)
    plist.append(abs(x - y))

p2 = math.sqrt(p2)
p3 = math.pow(p3, 1/3)
p4 = max(plist)

print(p1)
print(p2)
print(p3)
print(p4)

Solution (Python):
n = int(input())
x_list = list(map(float,input().split()))
y_list = list(map(float,input().split()))

print(sum([abs(x_list[i]-y_list[i]) for i in range(n)]))
print(pow(sum([abs(x_list[i]-y_list[i])**2 for i in range(n)]),1/2))
print(pow(sum([abs(x_list[i]-y_list[i])**3 for i in range(n)]),1/3))
print(max([abs(x_list[i]-y_list[i]) for i in range(n)]))

Solution (Python):
import math 
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
for j in range(3):
	w=0
	dist=0
	dist_2=[]
	for i in range(n):
		w=abs(x[i]-y[i])
		dist+=w**(j+1)
		dist_2.append(w)
	print(pow(dist,1/(j+1)))
print(float(max(dist_2)))

Solution (Python):
import math

def dist(z,p):
    sigma=0
    for i in range(0,len(z)):
        sigma += z[i]**p
    print(sigma**(1/p))


n=int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))
z=[abs(x[i]-y[i]) for i in range(0,n)]
dist(z,1)
dist(z,2)
dist(z,3)
print(max(z))

Solution (Python):
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
print(sum(abs(x-y) for x, y in zip(X, Y)))
print((sum((x-y)**2 for x, y in zip(X, Y)))**(1/2))
print((sum(abs(x-y)**3 for x, y in zip(X, Y)))**(1/3))
print(max([abs(x-y) for x, y in zip(X, Y)]))

Solution (Python):
def non_minus(n):
    if n < 0:
        return (-1) * n
    else:
        return n

def kyori(n1, n2):
    n1 = non_minus(n1)
    n2 = non_minus(n2)
    n3 = non_minus(n1 - n2)
    return n3

def minkov(x: list, y: list, p: int):
    len_x = len(x)
    dm = 0
    for i in range(len_x):
        dm += kyori(x[i], y[i])**p
    dxy = dm**(1/p)
    return dxy

def chebyshev(x: list, y: list):
    d_ary = []
    len_x = len(x)
    for i in range(len_x):
        d_ary.append(kyori(x[i], y[i]))
    
    return max(d_ary)

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

print(minkov(x,y,1))
print(minkov(x,y,2))
print(minkov(x,y,3))
print(float(chebyshev(x,y)))

Solution (Python):
input()
x=list(map(int,input().split()))
z=[abs(x[i]-y) for i,y in enumerate(map(int, input().split()))]
print(sum(z),sum([u**2 for u in z])**(1/2),sum([u**3 for u in z])**(1/3),max(z),sep='\n')

Solution (Python):
from math import sqrt
n = int(input())
x = input().split()
y = input().split()
max_num = -1
D_p1 = 0
D_p2 = 0
D_p3 = 0

for i in range(0, n):
    x[i] = abs(float(x[i]) - float(y[i]))
    y[i] = x[i] ** 2
    D_p1 += x[i]
    D_p2 += y[i]
    D_p3 += x[i] ** 3
    if (x[i] > max_num):
        max_num = x[i]

print(D_p1)
print(sqrt(D_p2))
print(pow(D_p3, 1.0 / 3))
print(max_num)

Solution (Python):
import sys

n = int(input())
Vx = list(map(int, input().split()))
Vy = list(map(int, input().split()))
maxList = []

plist = [1, 2, 3]
for p in plist:
    Sum = 0
    for i in range(n):
        Sum += abs(Vx[i] - Vy[i])**p
        D = Sum**(1/p)
    print(D)

D = 0
for i in range(n):
    maxList.append(abs(Vx[i] - Vy[i]))
    
print(max(maxList))

Solution (Python):
import math

n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

for i in range(1,4):
	sum=0
	for j in range(n):
		sum+=pow(math.fabs(x[j]-y[j]),i)
	print(str(pow(sum,1/i)))

infD=-1
for i in range(n):
	infD=max(math.fabs(x[i]-y[i]),infD)
print(float(infD))

Solution (Python):
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

tmp = 0
for i in range(n):
    tmp+=abs(x[i]-y[i])
print(tmp)
tmp = 0
for i in range(n):
    tmp+=(x[i]-y[i])**2
print(math.sqrt(tmp))
tmp = 0
for i in range(n):
    tmp+=abs(x[i]-y[i])**3
print(tmp**(1/3))
tmp = 0
for i in range(n):
    tmp = max(tmp, abs(x[i]-y[i]))
print(tmp)

Solution (Python):
import math

n = int(input())
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]

for p in range(1, 4):
    D = sum([abs(x[i]-y[i]) ** p for i in range(n)]) ** (1/p)
    print(D)

print(max([abs(x[i]-y[i]) for i in range(n)]))

Solution (Python):
import math

n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))

p1 = sum([math.fabs(x[i]-y[i]) for i in range(n)])
p2 = math.sqrt(sum([math.fabs(x[i]-y[i])**2 for i in range(n)]))
p3 = math.pow(sum([math.fabs(x[i]-y[i])**3 for i in range(n)]), 1/3)
pp = max([math.fabs(x[i]-y[i]) for i in range(n)])

print("{0:.6f}".format(p1))
print("{0:.6f}".format(p2))
print("{0:.6f}".format(p3))
print("{0:.6f}".format(pp))

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
dxy = [abs(a - b) for a, b in zip(x, y)]
D = [sum([n ** p for n in dxy]) ** (1 / p) for p in range(1, 4)]
D.append(max(dxy))
for d in D:
    print("{:.5f}".format(d))

Solution (Python):
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))

for k in range(1,4):
    print('{0:.6f}'.format(pow(sum(abs((x[i] - y[i])**k)for i in range(n)),1/k)))

print('{0:.6f}'.format(max(abs((x[i] - y[i]))for i in range(n))))

Solution (Python):
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

print("{0:.6f}".format(sum([math.fabs(x[i]-y[i]) for i in range(n)])))
print("{0:.6f}".format(math.sqrt(sum([math.fabs(x[i]-y[i])**2 for i in range(n)]))))
print("{0:.6f}".format(math.pow(sum([math.fabs(x[i]-y[i])**3 for i in range(n)]), 1/3)))
print("{0:.6f}".format(max([math.fabs(x[i]-y[i]) for i in range(n)])))

Solution (Python):
import math


loops = int(input())
x_vec = list(map(int, input().split()))
y_vec = list(map(int, input().split()))

manhattan = sum([abs(x_vec[i] - y_vec[i]) for i in range(len(x_vec))])
euclid = math.sqrt(sum([(abs(x_vec[i] - y_vec[i])) ** 2 for i in range(len(x_vec))]))
pequalthree = math.pow(sum([(abs(x_vec[i] - y_vec[i])) ** 3 for i in range(len(x_vec))]), 1.0/3.0)
chebyshev = max([abs(x_vec[i] - y_vec[i]) for i in range(len(x_vec))])

print(manhattan)
print(euclid)
print(pequalthree)
print(chebyshev)

Solution (Python):
def pN(n, x, y, N):
	result = list()
	for i in range(n):
		result.append(abs(x[i] - y[i]) ** N)
	return (sum(result) ** (1 / N))

def pMax(n, x, y):
	result = list()
	for i in range(n):
		result.append(abs(x[i] - y[i]))
	return max(result)

n = int(input())
x = [int(x) for x in input().split()]
y = [int(y) for y in input().split()]
print('%.8f' % pN(n, x, y, 1))
print('%.8f' % pN(n, x, y, 2))
print('%.8f' % pN(n, x, y, 3))
print('%.8f' % pMax(n, x, y))

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d1, d2, d3, di = 0, 0, 0, 0
for i in range(n):
  d1 += abs(x[i]-y[i])
  d2 += (x[i]-y[i])**2
  d3 += abs((x[i]-y[i]))**3
  di = max(di, abs(x[i]-y[i]))
print(d1)
print(d2**0.5)
print(d3**(1/3))
print(di)

Solution (Python):
n = int(input())
X = [float(i) for i in input().split(' ')]
Y = [float(i) for i in input().split(' ')]
d1, d2, d3, di = 0,0,0,0
for x,y in zip(X,Y):
	d1 += abs(x-y)
	d2 += pow(abs(x-y), 2)
	d3 += pow(abs(x-y), 3)
	if di < abs(x-y): di = abs(x-y)
print(d1)
print(pow(d2, 1/2))
print(pow(d3, 1/3))
print(di)

Solution (Python):
import math
num = int(input())
data1 = []
data2 = []
tmp = []
tmp = input().split()
data1 = [float(tmp[i]) for i in range(num)]
tmp = input().split()
data2 = [float(tmp[i]) for i in range(num)]
print(sum( abs(data1[i] - data2[i]) for i in range(num) ))
print(math.sqrt( sum(pow( data1[i] -data2[i] ,2 ) for i in range(num))))
print(pow( sum(pow( abs(data1[i] -data2[i]) ,3 ) for i in range(num)) ,1/3 ))
print(max( abs(data1[i] - data2[i]) for i in range(num) ))