For which positive integers $n$ is there a permutation $(x_1,x_2,\ldots,x_n)$ of $1,2,\ldots,n$ such that all the differences $|x_k-k|$, $k = 1,2,\ldots,n$, are distinct?
1. **Introduction:**
   We claim that the statement only works for \( n \equiv 0,1 \pmod{4} \).

2. **Proof that \( n \equiv 2,3 \pmod{4} \) do not work:**
   - Consider the sum of the differences \( \sum_{i=1}^n |x_i - i| \). Each value of \( x_i \) and \( i \) is counted once, whether in the positive or in the negative. Thus, each number from \( \{1, 2, \ldots, n\} \) is counted \( j \) times where \( j \in \{2, 0, -2\} \). Therefore, the left-hand side (LHS) is even.
   - However, note that non-negative integers only can appear on the right-hand side (RHS) and no number \( \geq n \) can appear. So the set of numbers on the RHS is \( \{0, 1, 2, \ldots, n-1\} \). Therefore, the sum of these numbers is:
     \[
     \sum_{i=0}^{n-1} i = \frac{n(n-1)}{2}
     \]
   - This sum is odd when \( n \equiv 2,3 \pmod{4} \). Hence, for \( n \equiv 2,3 \pmod{4} \), it is impossible to have all differences \( |x_k - k| \) distinct.

3. **Construction for \( n \equiv 0 \pmod{4} \):**
   - We need to construct a permutation \( (x_1, x_2, \ldots, x_n) \) such that all differences \( |x_k - k| \) are distinct.
   - Consider the following construction:
     \[
     \begin{aligned}
     &4n-1: (1, 4n) \\
     &4n-2: (4n, 2) \\
     &4n-3: (2, 4n-1) \\
     &4n-4: (4n-1, 3) \\
     &\vdots \\
     &2n+1: (n, 3n+1) \\
     &2n: (3n+1, n+1) \\
     &2n-1: (2n, 1) \\
     &2n-2: (n+1, 3n-1) \\
     &2n-3: (3n-1, n+2) \\
     &\vdots \\
     &2: (2n-1, 2n+1) \\
     &1: (2n+1, 2n) \\
     &0: (3n, 3n)
     \end{aligned}
     \]
   - This construction ensures that all differences \( |x_k - k| \) are distinct.

4. **Construction for \( n \equiv 1 \pmod{4} \):**
   - Similarly, we need to construct a permutation \( (x_1, x_2, \ldots, x_n) \) such that all differences \( |x_k - k| \) are distinct.
   - Consider the following construction:
     \[
     \begin{aligned}
     &4n: (1, 4n+1) \\
     &4n-1: (4n+1, 2) \\
     &4n-2: (2, 4n) \\
     &4n-3: (4n, 3) \\
     &\vdots \\
     &2n+2: (n, 3n+2) \\
     &2n+1: (3n+2, n+1) \\
     &2n: (2n+1, 1) \\
     &2n-1: (n+1, 3n) \\
     &2n-2: (3n, n+2) \\
     &\vdots \\
     &2: (2n+2, 2n) \\
     &1: (2n, 2n+1) \\
     &0: (3n+1, 3n+1)
     \end{aligned}
     \]
   - This construction ensures that all differences \( |x_k - k| \) are distinct.

The final answer is \( \boxed{ n \equiv 0,1 \pmod{4} } \).