Problem: Given any integer x, Aoki can do the operation below.  Operation: Replace x with the absolute difference of x and K.  You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Submission attempts (in order):
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0 or K%N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1 or K%N == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 5:
            if res[0] == res[2] or res[1] == res[3]:
                print(min(res))
                exit()
    print(min(res))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 2:
            if res[i] == res[i-2]:
                print(min(res))
                exit()
    print(min(res))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0 or K%N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1 or K%N == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        l.append(ans)
        if i >= 5:
            if ans in res:
                print(min(res))
                exit()
    print(min(res))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                print(l)
                exit()
    print(min(l))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 2:
            if res[i] == res[i-2]:
                print(min(res))
                exit()
    print(min(res))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K or N%K == 0 or K%N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1 or N%K == 1 or K%N == 1:
    print(1)
    exit()
else:
    res = []
    ans = N
    for i in range(max(N,K)):
        ans = ans - K
        res.append(ans)
        if i >= 5:
            if ans in res:
                print(min(res))
                exit()
    print(min(res))

N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1 or N == K-1 or K == N-1:
    print(1)
    exit()
else:
    ans = (N//K) + 1
    res = abs(N - (2**ans))
    print(res)

N, K = map(int, input().split()); print(0 if N%K==0 else min(N%K, abs(K-(N%K))))

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted