Help me in solving MAX_DIFF problem

My issue

def max_absolute_difference(n, s):
hashmap = set()
a =

for j in range(n + 1):
    hashmap.add(j)
    diff = s - j
    if diff in hashmap:
        a.append([j, diff])

result = sorted([abs(x - y) for x, y in a])
return max(result)

Taking input outside the function

for _ in range(int(input())):
n, s = map(int, input().split())
print(max_absolute_difference(n, s))
this is my damn code and someone can fix this code without changing the logic just imporoving the time complexity of the code would be helpful

My code

def max_absolute_difference(n, s):
    hashmap = set()
    a = []

    for j in range(n + 1):
        hashmap.add(j)
        diff = s - j
        if diff in hashmap:
            a.append([j, diff])

    result = sorted([abs(x - y) for x, y in a])
    return max(result)

# Taking input outside the function
for _ in range(int(input())):
    n, s = map(int, input().split())
    print(max_absolute_difference(n, s))

Learning course: Level up from 1* to 2*
Problem Link: The Two Dishes Practice Problem in Level up from 1* to 2* - CodeChef