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):
import math
deg = int(input())
li_x = list(map(int,input().split()))
li_y = list(map(int, input().split()))
p1_dis = 0.0
p2_dis = 0.0
p2_temp = 0.0
p3_dis = 0.0
p3_temp = 0.0
p4_dis = 0.0
p4_temp = [ ]

for i in range(deg):
	p1_dis += math.fabs(li_x[i] - li_y[i])
	p2_temp += (li_x[i] - li_y[i]) **2.0
	p3_temp += math.fabs(li_x[i] - li_y[i]) **3.0
	p4_temp.append( math.fabs(li_x[i] - li_y[i]) )

p2_dis = math.sqrt( p2_temp )
p3_dis = math.pow(p3_temp, 1.0/3.0)
p4_dis = max(p4_temp)

print('{0:.6f}'.format(p1_dis))
print('{0:.6f}'.format(p2_dis))
print('{0:.6f}'.format(p3_dis))
print('{0:.6f}'.format(p4_dis))

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

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [abs(x[i] - y[i]) for i in range(n)]
for i in range(1,4):
    print('{0:f}'.format(sum(j ** i for j in d) ** (1 / i)))
print('{0:f}'.format(max(d)))

Solution (Python):
import math

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

for p in range(1, 4):
    if p == 1:
        print('{0:f}'.format(sum([abs(a - b) for a, b in zip(x, y)])))
    else:
        print('{0:f}'.format(math.pow(sum([math.pow(abs(a - b), p) for a, b in zip(x, y)]), 1 / p)))

print('{0:f}'.format(max(abs(a - b) for a, b in zip(x, y))))

Solution (Python):

import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
t1 = t2 = t3 = ti = 0.0
for a, b in zip(x, y):
	d1 = abs(a - b)
	ti = max(ti, d1)
	t1 += d1
	t2 += d1**2
	t3 += d1**3
print(t1)
print(math.sqrt(t2))
print(t3 ** (1.0/3))
print(ti)

Solution (Python):
import math
n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
p1, p2, p3, p4 = 0, 0, 0, 0
hyp = 0
for i in range(n):
    p1 += math.fabs(a1[i] -a2[i])
    p2 += (a1[i]-a2[i])**2
    p3 += math.fabs((a1[i]-a2[i])**3)
    hyp = math.fabs(a1[i] -a2[i]) 
    if hyp > p4:
        p4 = hyp

p2 = math.sqrt(p2)
if p3 != 0:
    p3 = math.exp(math.log(p3)/3)
print(p1)
print(p2)
print(p3)
print(p4)

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

sum1 = 0.00
for i in range(n):
	sum1 += abs(x[i]-y[i])
print '%.6f'%sum1

sum2 = 0.00
for i in range(n):
	sum2 += pow(abs(x[i]-y[i]), 2)
print '%.6f'%pow(sum2, 0.5)

sum3 = 0.00
for i in range(n):
	sum3 += pow(abs(x[i]-y[i]), 3)
print '%.6f'%pow(sum3, 1.00/3)

sum4 = 0.00
for i in range(n):
	sum4 = max(sum4, abs(x[i]-y[i]))
print '%.6f'%(sum4)

Solution (Python):
import math

n = int(raw_input())
X = map(float, raw_input().split(" "))
Y = map(float, raw_input().split(" "))

print(sum([math.fabs(X[i] - Y[i]) for i in range(n)]))
print(math.sqrt(sum([math.fabs(X[i] - Y[i])**2 for i in range(n)])))
print(math.pow(sum([math.fabs(X[i] - Y[i])**3 for i in range(n)]), (1.0/3.0)))
print(max([math.fabs(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())))
l = [0.0]*n

for i in range(n):
    l[i] = abs(x[i]-y[i])
print(sum(l))
che = max(l)

for i in range(n):
    l[i] = abs(x[i]-y[i])**2
print(math.sqrt(sum(l)))

for i in range(n):
    l[i] = abs(x[i]-y[i])**3
print(math.pow(sum(l), 1.0/3.0))
print(che)

Solution (Python):
import math
n = int(input())
x, y = [list(map(int, input().split())) for _ in range(2)]
for p in (1,2,3,0):
    if p == 0:
        print(max(abs(x-y) for x, y in zip(x,y)))
    else:
        print(math.pow(sum(abs(x-y) ** p for x, y in zip(x,y)), 1.0 / p))

Solution (Python):
n = input()
x = list(map(int,raw_input().split()))
y = list(map(int,raw_input().split()))
gap = []
gap1 = 0.0
gap2 = 0.0
gap3 = 0.0
a = 0.0

for i in range(n):
    gap.append(abs(x[i]-y[i]))

for i in range(n):
    gap1 += gap[i]
print gap1

for i in range(n):
    gap2 += gap[i] ** 2  

print pow(gap2,0.5)

for i in range(n):
    gap3 += gap[i] ** 3
print pow(gap3,0.3333333333)

print max(gap)

Solution (Python):

import math

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

manh_dist=0.0
for i in range(0,N):
    manh_dist=manh_dist+math.fabs(x[i]-y[i])

print('%.6f'%manh_dist)

eucl_dist=0.0
for i in range(0,N):
    eucl_dist=eucl_dist+(x[i]-y[i])**2
eucl_dist=math.sqrt(eucl_dist)

print('%.6f'%eucl_dist)

mink_dist=0.0
for i in range(0,N):
    mink_dist=mink_dist+math.fabs(x[i]-y[i])**3
mink_dist=math.pow(mink_dist,1.0/3)

print('%.6f'%mink_dist)

cheb_dist=0.0
for i in range(0,N):
    if cheb_dist<math.fabs(x[i]-y[i]):
        cheb_dist=math.fabs(x[i]-y[i])

print('%.6f'%cheb_dist)

Solution (Python):
import math

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

for p in range(4):
    p = float(p)
    D = 0
    for j in range(n):
        if p == 3.0:
            D = max(D, abs(x[j] - y[j]))
        else:
            D += math.pow(abs(x[j] - y[j]), p+1)
    if p == 3.0:
        print D
    else:
        print math.pow(D, 1/(p+1))

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

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

a = []
for i in range(n):
 a.append(math.fabs(x[i]-y[i]))
a.sort()
print(a[n-1])

Solution (Python):
def calc_minkowski_distance(x1, x2, p):
    """calc L_p norm(finite dimension)

    Args:
        x1: vector1
        x2: vector2
        p: p in L_p

    Returns:
        L_p distance between x1 and x2 (float)
    """
    if p == "inf":
        return max([abs(x[0] - x[1]) for x in zip(x1, x2)])
    else:
        return sum([abs(x[0] - x[1]) ** (float(p)) for x in zip(x1, x2)]) ** float(1.0 / p)


target = [1, 2, 3, "inf"]
dimension = int(raw_input())
x1 = [float(x) for x in raw_input().split()]
x2 = [float(x) for x in raw_input().split()]
for p in target:
    print(calc_minkowski_distance(x1, x2, p))

Solution (Python):
import math


def calc_minkowski_distance(x1, x2, p):
    """calc L_p norm(finite dimension)

    Args:
        x1: vector1
        x2: vector2
        p: p in L_p

    Returns:
        L_p distance between x1 and x2 (float)
    """
    if p == "inf":
        return max([math.fabs(x[0] - x[1]) for x in zip(x1, x2)])
    else:
        return math.pow(sum([math.pow(math.fabs(x[0] - x[1]), p) for x in zip(x1, x2)]), 1.0 / p)


target = [1, 2, 3, "inf"]
dimension = int(raw_input())
x1 = [float(x) for x in raw_input().split()]
x2 = [float(x) for x in raw_input().split()]
for p in target:
    print(calc_minkowski_distance(x1, x2, p))

Solution (Python):
import math


def calc_minkowski_distance(x1, x2, p):
    """calc L_p norm(finite dimension)

    Args:
        x1: vector1
        x2: vector2
        p: p in L_p

    Returns:
        L_p distance between x1 and x2 (float)
    """
    if p == "inf":
        return max([math.fabs(x[0] - x[1]) for x in zip(x1, x2)])
    else:
        return math.pow(sum([math.pow(math.fabs(x[0] - x[1]), p) for x in zip(x1, x2)]), 1.0 / p)


target = [1, 2, 3, "inf"]
dimension = int(raw_input())
x1 = [float(x) for x in raw_input().split()]
x2 = [float(x) for x in raw_input().split()]
for p in target:
    print(calc_minkowski_distance(x1, x2, p))

Solution (Python):
import math


def calc_minkowski_distance(data_x, data_y, types):
    
    distance = list(map(lambda x, y:abs(x - y), data_x, data_y))

    results = []
    for t in types:
        if t == 'Inf':
            results.append(max(distance))
        else:
            temp = map(lambda x:math.pow(x, t), distance)
            total = sum(temp)
            results.append(math.pow(total, 1.0/t))
    return results


if __name__ == '__main__':
    
    
    
    num = int(input())
    data_x = [int(x) for x in input().split(' ')]
    data_y = [int(x) for x in input().split(' ')]

    
    types = [1, 2, 3, 'Inf']  
    results = calc_minkowski_distance(data_x, data_y, types)

    
    for r in results:
        print('{0:.8f}'.format(r))

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(float,input().split()))
y=list(map(float,input().split()))

d1=0
d2=0
d3=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

from math import pow
d2=pow(d2,(1.0/2.0))
d3=pow(d3,(1.0/3.0))
di=max([abs(x[i]-y[i]) for i in range(n)])

print(d1)
print(d2)
print(d3)
print(di)

Solution (Python):

n = int(input())

x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

dl = [abs(i - j) for (i,j) in zip(x,y)]

for p in range(1,4):
    d = sum([i ** p for i in dl]) ** (1/p)
    print("{:.8f}".format(d))

print("{:.8f}".format(max(dl)))

Solution (Python):
import math
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

c = list(map(lambda x: abs(x[0]-x[1]), list(zip(a, b))))
print("{:.5f}".format(sum(c))) 

d = list(map(lambda x: x**2, c))
print("{:.5f}".format(math.sqrt(sum(d)))) 

e = list(map(lambda x: x**3, c))
print("{:.5f}".format(math.pow(sum(e), 1.0/3.0))) 

print("{:.5f}".format(max(c)))

Solution (Python):
import math
n = int(input())
xs = [int(_) for _ in input().split()]
ys = [int(_) for _ in input().split()]

print(sum([abs(x-y) for x, y in zip(xs, ys)]))
print(math.sqrt(sum([pow(abs(x-y),2) for x, y in zip(xs, ys)])))
print(math.pow(sum([pow(abs(x-y),3) for x, y in zip(xs, ys)]), 1/3))
print(max([abs(x-y) for x, y in zip(xs, ys)]))

Solution (Python):
n=eval(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
def minkowski(p,x_1,x_2,n):
    if p==0:
        return float(max([abs(x_1[i]-x_2[i]) for i in range(n)]))
    else:
        return sum([(abs(x_1[i]-x_2[i]))**p for i in range(n)])**(1/p)
for i in range(4):
    print(minkowski((i+1)%4,x,y,n))

Solution (Python):
n = int(input())
a, b = [[int(el) for el in input().split(' ')] for _ in range(2)]
print(sum(abs(a[i] - b[i]) for i in range(n)))
print(sum((a[i] - b[i]) ** 2 for i in range(n)) ** 0.5)
print(sum(abs(a[i] - b[i]) ** 3 for i in range(n)) ** (1/3))
print(max(abs(a[i] - b[i]) for i in range(n)))

Solution (Python):
input()
X_vec = map(float, input().split())
Y_vec = map(float, input().split())
pair = list(zip(X_vec, Y_vec))
D1 = sum(abs(x-y) for x,y in pair)
D2 = (sum((x-y)**2 for x,y in pair)) ** .5
D3 = (sum(abs(x-y)**3 for x,y in pair)) ** (1/3)
Dinf = max(abs(x-y) for x,y in pair)
print("{0:.6f}\n{1:.6f}\n{2:.6f}\n{3:.6f}\n".format(D1,D2,D3,Dinf))

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

Dp1, Dp2, Dp3, Dpinf = [0.0]*4

for (x, y) in zip(xn, yn):
    a = abs(x-y)
    Dp1 += a
    Dp2 += a**2
    Dp3 += a**3
    Dpinf = a if a > Dpinf else Dpinf

Dp2 = Dp2**(1/2)
Dp3 = Dp3**(1/3)

print(Dp1, Dp2, Dp3, Dpinf, sep='\n')

Solution (Python):
n = int(input())
x = map(float, input().strip().split())
y = map(float, input().strip().split())
d = tuple(map(lambda x,y:abs(x-y), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Solution (Python):
n = int(input())
x = map(float, input().strip().split())
y = map(float, input().strip().split())
d = tuple(map(lambda i,j: abs(i-j), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Solution (Python):
import math
n = int(raw_input())
x = map(lambda l: int(l)*1.0, (raw_input()).split(" "))
y = map(lambda l: int(l)*1.0, (raw_input()).split(" "))

for p in range(1,4):
    d = [math.pow(math.fabs(x[i] - y[i]), p) for i in range(n)]
    print math.pow(sum(d), 1.0/p)

d = [math.fabs(x[i]-y[i]) for i in range(n)]
print max(d)

Solution (Python):
def minkowski(n, x, y, p):
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i])**p

    return(D**(1/p))

def chebyshev(n, x, y):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))

    return(max(D))

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

print(minkowski(n, x, y, 1))
print(minkowski(n, x, y, 2))
print(minkowski(n, x, y, 3))

print(chebyshev(n, x, y))

Solution (Python):
import math
def nor(x,y,p):
    sum = 0
    for i in range(len(x)):
        sum += (abs(x[i]-y[i]))**p
    return sum**(1/p)
def nor_inf(x,y):
    tmp = []
    for i in range(len(x)):
        tmp.append(abs(x[i]-y[i]))
    return max(tmp)
                 
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

print(nor(x,y,1))
print(nor(x,y,2))
print(nor(x,y,3))
print(nor_inf(x,y))

Solution (Python):
from math import sqrt

n = int(input())
vector1 = map(int,input().split())
vector2 = map(int,input().split())
diffs = [abs(x-y) for x,y in zip(vector1,vector2)]
print (sum(diffs))
print (sqrt(sum(map(lambda n:pow(n,2),diffs))))
print ((sum(map(lambda n:pow(n,3),diffs)))**(1/3))
print (max(diffs))

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

D = [0,0,0,0]
dis = []
for i in range(n):
    D[0] += abs(x[i]-y[i])
    D[1] += (x[i]-y[i])**2
    D[2] += abs((x[i]-y[i]))**3
    dis.append(abs(x[i]-y[i]))

D[1] = math.sqrt(D[1])
D[2] = math.pow(D[2],1/3)
D[3] = max(dis)

for i in range(4):
    print(D[i])

Solution (Python):
import math

N = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = [abs(X-Y) for (X,Y) in zip(x, y)]

print(sum(z))

z2 = [Z ** 2 for Z in z]
z3 = [Z ** 3 for Z in z]

print(math.sqrt(sum(z2)))
print(math.pow(sum(z3),1/3))
print(max(z))

Solution (Python):
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
for p in range(1,4):
    s = [math.fabs(x[i]-y[i])**p for i in range(n)]
    z = math.pow(sum(s), 1/p)
    print("{:.6f}".format(z))
t = [math.fabs(x[i]-y[i]) for i in range(n)]
print("{:.6f}".format(max(t)))

Solution (Python):
from math import *


def distance (p,n,x,y):
    dis = 0.0
    if p == "inf":
        for i in range(0,n) :
            if dis < abs(x[i]-y[i]):
                dis = abs(x[i]-y[i])
    else :
        for i in range(0,n) :
            dis += abs(x[i]-y[i])**p
        dis = dis ** (1.0/p)
    return dis


if  __name__ == '__main__' :
     n = int(input())
     x = map(int,raw_input().split())
     y = map(int,raw_input().split())
     print distance(1,n,x,y)
     print distance(2,n,x,y)
     print distance(3,n,x,y)
     v = distance('inf',n,x,y)
     print "%.6f" % v

Solution (Python):
import math

dim = int(input().strip())
vec_x = list(map(int,input().strip().split(" ")))
vec_y = list(map(int,input().strip().split(" ")))

dist_1 = 0
dist_2 = 0
tmp_dist_2 = 0
dist_3 = 0
tmp_dist_3 = 0
dist_infinite = 0
abs_xy = [0 for i in range(dim)]
for i in range(dim):
    abs_xy[i]= abs(vec_x[i]-vec_y[i])
    
    dist_1 = dist_1 + abs_xy[i]
    tmp_dist_2 = tmp_dist_2 + abs_xy[i] ** 2
    tmp_dist_3 = tmp_dist_3 + abs_xy[i] ** 3
    
dist_2 = math.sqrt(tmp_dist_2)
dist_3 = tmp_dist_3 ** (1/3)
dist_infinite = max(abs_xy)

print("{:.5f}\n{:.5f}\n{:.5f}\n{:.5f}".format(dist_1,dist_2,dist_3,dist_infinite))

Solution (Python):
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))
for p in [1.0,2.0,3.0]:
    s = 0
    for i in range(n):
        s += abs(x[i]-y[i])**p
    print(pow(s,1.0/p))
print(max([abs(x[i]-y[i]) for i in range(n)]))

Solution (Python):
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(j) for j in input().split()]
p_1 = p_2 = p_3 = p_m = 0

for i in range(n):
	diff = math.fabs(x[i]-y[i])
	p_1 += diff
	p_2 += diff*diff
	p_3 += diff*diff*diff
	if p_m < diff:
		p_m = diff
	
p_2 = math.sqrt(p_2) 
p_3 = math.pow(p_3, 1/3)

print('%.5f\n%.5f\n%.5f\n%.5f'% (p_1, p_2, p_3, p_m))

Solution (Python):
import math

n = int(input())
vector_x = [float(x) for x in input().split(" ")]
vector_y = [float(y) for y in input().split(" ")]

p1 = sum([abs(x - y) for x, y in zip(vector_x, vector_y)])
p2 = sum([abs(x - y) ** 2 for x, y in zip(vector_x, vector_y)]) ** (1/2)
p3 = sum([abs(x - y) ** 3 for x, y in zip(vector_x, vector_y)]) ** (1/3)
t = max([abs(x - y) for x, y in zip(vector_x, vector_y)])

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

Solution (Python):
import math
n = int(input())
array = [[int(i) for i in input().split()] for i in range(2)] 
p1 = 0
p2 = 0
p3 = 0
pm = []
for i in range(n):
    p1 +=math.fabs(array[0][i]-array[1][i])
    p2 +=math.fabs(array[0][i]-array[1][i])**2
    p3 +=math.fabs(array[0][i]-array[1][i])**3
    pm.append(math.fabs(array[0][i]-array[1][i]))
print(p1)
print(math.sqrt(p2))
print(p3**(1/3))
print(max(pm))

Solution (Python):
n, x, y = int(input()), list(map(int, input().split())), list(map(int, input().split()))
d = tuple(map(lambda x, y:abs(x - y), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Solution (Python):
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
p_1 = sum([abs(x[i]-y[i]) for i in range(n)])
p_2 = (sum([(x[i]-y[i])**2 for i in range(n)]))**0.5
p_3 = (sum([(abs(x[i]-y[i]))**3 for i in range(n)]))**(1/3)
p_inf = max([abs(x[i]-y[i]) for i in range(n)])

print("{:.5f}".format(p_1))
print("{:.5f}".format(p_2))
print("{:.5f}".format(p_3))
print("{:.5f}".format(p_inf))

Solution (Python):
elements = input()
vector_1 = list(map(float,input().split()))
vector_2 = list(map(float,input().split()))

for i in range(1,4):
    minkowski = sum([abs(a-b)**i for (a,b) in zip(vector_1,vector_2)])**(1/i)
    print("{0:.7f}".format(minkowski))

minkowski_inf=max([abs(a-b) for (a,b) in zip(vector_1,vector_2)])
print("{0:.7f}".format(minkowski_inf))

Solution (Python):


import sys
import os
import math

def distance(v0, v1, p):
    n = len(v0)
    s = 0
    for i in range(n):
        diff = abs(v0[i] - v1[i])
        s += diff ** p
    return s ** (1/p)

def chebyshev(v0, v1):
    n = len(v0)
    ret = 0
    for i in range(n):
        diff = abs(v0[i] - v1[i])
        if diff > ret:
            ret = diff
    return ret

n = int(input())
v0 = list(map(float, input().split()))
v1 = list(map(float, input().split()))

print(distance(v0, v1, 1))
print(distance(v0, v1, 2))
print(distance(v0, v1, 3))
print(chebyshev(v0, v1))

Solution (Python):
import math

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

d = [0 for _ in range(n)]
for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 1)
D_1 = pow(sum(d), 1/1)

for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 2)
D_2 = pow(sum(d), 1/2)

for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 3)
D_3 = pow(sum(d), 1/3)

for i in range(n):
    d[i] = math.fabs(x[i]-y[i])
D_inf = max(d)

print(D_1)
print(D_2)
print(D_3)
print(D_inf)

Solution (Python):



def calculate_minkowski_distances():
    _ = input()
    data = []
    for i in range(2):
        data.append([float(value) for value in input().rstrip().split()])

    for exponent in (1, 2, 3, float("inf")):
        print(minkowski_distance(data[0],data[1], exponent))


def minkowski_distance(vector_x, vector_y, exponent):

    abs_xy = [abs(vector_x[i] - vector_y[i]) for i in range(len(vector_x))]
    
    m_dis = 0
    if exponent == float("inf") :
        m_dis = max(abs_xy)
    elif exponent == 0 :
        m_dis = nan
    else :
        m_dis = sum([ elem**exponent for elem in abs_xy ])**(1/exponent)
    
    return m_dis

if __name__ == "__main__" :
    calculate_minkowski_distances()

Solution (Python):


def dis_func(x, y):
    dis = [round(0, 6) for i in range(4)]
    for x, y in zip(x, y):
        tmp = abs(x - y)
        dis[0] += tmp
        dis[1] += (tmp ** 2)
        dis[2] += (tmp ** 3)
        dis[3] = max(dis[3], tmp)

    dis[1] = dis[1] ** (1/2)
    dis[2] = dis[2] ** (1/3)
    print('\n'.join(list(map(str, dis))))


if __name__ == '__main__':
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))

    dis_func(x, y)

Solution (Python):
n = int(input())
X = [int(i) for i in input().split(" ")]
Y = [int(i) for i in input().split(" ")]

for p in range(1, 4):
    print("{:.6f}".format(sum( [abs(a - b) ** p for a, b in zip(X,Y) ] ) ** (1 / p) ) )

print("{:.6f}".format(max( [ abs(a - b) for a, b in zip(X, Y) ] ) ) )

Solution (Python):

import math

chebyshev = []

n = int(input())

x = input().split()
y = input().split()

for i in range(n):
    x[i] = int(x[i])
for i in range(n):
    y[i] = int(y[i])

for p in range(3):
    total = 0
    for i in range(n):
        total += (math.fabs(x[i]-y[i]))**(p+1)
    distance = total**(1/(p+1))
    print("%.5f" % distance)

for i in range(n):
    chebyshev.append(math.fabs(x[i]-y[i]))
print("%.5f" % max(chebyshev))

Solution (Python):

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

d1=0.0
for i in range(n):
    d1 += abs(x[i]-y[i])
print(d1)

d2=0.0
for i in range(n):
    d2 += (x[i]-y[i])**2
print(d2**(1/2))

d3=0.0
for i in range(n):
    d3 += abs((x[i]-y[i])**3)
print(d3**(1/3))

d_inf=0.0
for i in range(n):
    if abs(x[i]-y[i])>d_inf:
        d_inf = abs(x[i]-y[i])
print(d_inf)

Solution (Python):
import math

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

Dp1 = 0
Dp2 = 0
Dp3 = 0
Dp4 = []
for i in range(n):
 xyi = abs(x[i] - y[i])
 Dp1 += xyi
 Dp2 += xyi ** 2
 Dp3 += xyi ** 3
 Dp4.append(xyi)
print("{0:05f}".format(Dp1))
print("{0:05f}".format(math.sqrt(Dp2)))
print("{0:05f}".format(math.pow(Dp3, (1 / 3))))
print("{0:05f}".format(max(Dp4)))

Solution (Python):
from math import pow

N = int(input())
X = list(map(int, input().split(' ')))
Y = list(map(int, input().split(' ')))

for i in range(1, 4):
    print('{:.5f}'.format(
        pow(sum([abs(X[j] - Y[j])**i for j in range(N)]), 1/i)
    ))

print('{:.5f}'.format(
    max([abs(X[j] - Y[j]) for j in range(N)])
))

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(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = absDiff ** 3
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(math.sqrt(tmp[n-1][4]))
print(tmp[n-1][5] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = absDiff ** 3
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(tmp[n-1][4] ** (1/2))
print(tmp[n-1][5] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = tmp[i][1] * absDiff
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(tmp[n-1][4] ** (1/2))
print(tmp[n-1][5] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = tmp[i][1] * absDiff
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n][3])
print(tmp[n][4] ** (1/2))
print(tmp[n][5] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(3)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] += tmp[i-1][0] + absDiff
    tmp[i][1] += tmp[i-1][1] + absDiff ** 2
    tmp[i][2] += tmp[i-1][2] + absDiff ** 3
    if absDiff > max:
        max = absDiff
print(tmp[n][0])
print(tmp[n][1] ** (1/2))
print(tmp[n][2] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(3)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    absDiff2 = absDiff * absDiff
    absDiff3 = absDiff2 * absDiff
    tmp[i][0] += tmp[i-1][0] + absDiff
    tmp[i][1] += tmp[i-1][1] + absDiff2
    tmp[i][2] += tmp[i-1][2] + absDiff3
    if absDiff > max:
        max = absDiff
print(tmp[n][0])
print(tmp[n][1] ** (1/2))
print(tmp[n][2] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(3)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    absDiff2 = absDiff * absDiff
    absDiff3 = absDiff2 * absDiff
    tmp[i][0] = tmp[i-1][0] + absDiff
    tmp[i][1] = tmp[i-1][1] + absDiff2
    tmp[i][2] = tmp[i-1][2] + absDiff3
    if absDiff > max:
        max = absDiff
print(tmp[n][0])
print(tmp[n][1] ** (1/2))
print(tmp[n][2] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(3)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    absDiff2 = absDiff * absDiff
    absDiff3 = absDiff2 * absDiff
    tmp[i][0] = tmp[i-1][0] + absDiff
    tmp[i][1] = tmp[i-1][1] + absDiff2
    tmp[i][2] = tmp[i-1][2] + absDiff3
    if absDiff > max:
        max = absDiff
print(tmp[n][0])
print(tmp[n][1] ** (1/2))
print(tmp[n][2] ** (1/3))
print(max)

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
p = 0
q = 0
p1 = 0
p2 = 0
p3 = 0
max = 0
import math
for i, j in zip(x, y):
    p1 += abs(i - j)
    p += abs((i - j) ** 2)
    p2 = math.sqrt(p)
    q += abs((i - j) ** 3)
    p3 = math.pow(q, 1.0/3.0)
    if abs(i - j) > max:
        max = abs(i - j)

print(p1)
print(p2)
print(p3)
print(max)

Solution (Python):
import math
n=int(input())
x=list(map(int, input().split()))
y=list(map(int, input().split()))
print(sum([abs(a-b) for (a,b) in zip(x,y)]))
print(math.sqrt(sum([abs(a-b)**2 for (a,b) in zip(x,y)])))
print(math.pow(sum([abs(a-b)**3 for (a,b) in zip(x,y)]), 1/3))
print((max([abs(a-b) for (a,b) in zip(x,y)])))

Solution (Python):
import math
n = int(input())
s = input().split(" ")
x = list(map(int,s))
s = input().split(" ")
y = list(map(int,s))
sa = []
for i in range(n):
    sa.append(math.fabs(x[i]-y[i]))
goukei = 0
for i in range(n):
    goukei += sa[i]
p1 = goukei
print(p1)
goukei = 0
for i in range(n):
    goukei += sa[i]**2
p2 = math.sqrt(goukei)
print(p2)
goukei = 0
for i in range(n):
    goukei += sa[i]**3
p3 = math.pow(goukei,1/3)
print(p3)
saidaisa = 0
for i in range(n):
    if sa[i] > saidaisa:
        saidaisa = sa[i]
print(saidaisa)

Solution (Python):
import math

n = int(input())
x = [float(x) for x in input().split()]
y = [float(y) for y in input().split()]
d = 0
for i in range(n):
    d += abs(x[i] - y[i])

print(d)

d = 0
for i in range(n):
    d += abs((x[i] - y[i])**2)

d = math.sqrt(d)
print(d)

d = 0
for i in range(n):
    d += abs((x[i] - y[i])**3)

d = math.pow(d, 1.0/3.0)
print(d)

d = 0
for i in range(n):
    if d <= abs(x[i] - y[i]):
        d = abs(x[i] - y[i])

print(d)

Solution (Python):
import math

n = int(input())
xs = list(map(float, input().split()))
ys = list(map(float, input().split()))


def mink(p):
    return math.pow(sum([
        math.pow(math.fabs(xs[i] - ys[i]), p)
        for i in range(n)
    ]), 1 / p)


def mink_inf():
    return max([math.fabs(xs[i] - ys[i]) for i in range(n)])


def float_print(v):
    print('{:.5f}'.format(v))


float_print(mink(1))
float_print(mink(2))
float_print(mink(3))
float_print(mink_inf())

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
D1=0
for i in range(n):
    D1+=abs(x[i]-y[i])
D2=0
for i in range(n):
    D2+=(x[i]-y[i])**2
D2=D2**0.5
D3=0
for i in range(n):
    D3+=abs(x[i]-y[i])**3
D3=D3**(1/3)
D4=0
for i in range(n):
    if D4<=abs(x[i]-y[i]):
        D4=abs(x[i]-y[i])
print("%.6f\n%.6f\n%.6f\n%.6f\n" %(D1,D2,D3,D4))