Permutation Mod K
Source: codechef
Difficulty: 1760

Permutation Mod K
A permutation$P_1, P_2, .... P_N$is called good if$P_i \mod K \ne i \mod K$for all$1 \le i \le N$.
Given the values of$N$and$K$, find a good permutation, or claim it does not exist.
A permutation of length$N$contains each of the values$1, 2, ...N$exactly once.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first and only line of input contains$2$integers$N$and$K$.
Output Format
For each test case, output on a new line$N$integers$P_1, P_2, ...., P_N$: a good permutation if it exists$-1$if no good permutation exists
Constraints$1 \le T \le 100$$1 \le K \le N \le 100$Sample 1:
Explanation:
Test Case 1
: We can see that the permutation$P = [3, 4, 1, 2]$is valid because :$P_1 \mod K = 0$, while$1 \mod K = 1$$P_2 \mod K = 1$, while$2 \mod K = 2$$P_3 \mod K = 1$, while$3 \mod K = 0$$P_4 \mod K = 2$, while$4 \mod K = 1$.
Other permutations such as$[3, 1, 4, 2]$will also be valid.
Test Case 2
: There exists no valid permutation.

Sample Input:
3
4 3
1 1
2 2

Sample Output:
3 4 1 2
-1
2 1

C++ solution:
#include <iostream>
#include <vector>
#include <numeric> // For std::iota if needed, not here

void solve() {
    int n, k;
    std::cin >> n >> k;

    if (k == 1) {
        std::cout << -1 << std::endl;
        return;
    }

    // K >= 2 from here
    if (k == 2 && n % 2 == 1) { // N is odd
        std::cout << -1 << std::endl;
        return;
    }

    // General case: K >= 2.
    // And if K=2, then N must be even (otherwise caught above).
    
    std::vector<int> p(n);
    if (n % k == 1) {
        // This case implies K >= 3.
        // If K=2, then N must be even for this path. N%K = N%2 = 0.
        // So if N%K == 1, K cannot be 2. Thus K >= 3.
        // Use P_cyclic_3 (values start from 3, then wrap around to 1, 2).
        // P_idx = (idx + 2) % n + 1 (for 0-indexed idx)
        for (int i = 0; i < n; ++i) {
            p[i] = (i + 2) % n + 1;
        }
    } else {
        // N % K != 1. This includes N%K=0 and N%K=r where 1 < r < K.
        // (If K=2, N is even, so N%K = N%2 = 0, which is != 1).
        // Use P_cyclic_2 (values start from 2, then wrap around to 1).
        // P_idx = (idx + 1) % n + 1 (for 0-indexed idx)
        for (int i = 0; i < n; ++i) {
            p[i] = (i + 1) % n + 1;
        }
    }

    for (int i = 0; i < n; ++i) {
        std::cout << p[i] << (i == n - 1 ? "" : " ");
    }
    std::cout << std::endl;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Go solution:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    writer := bufio.NewWriter(os.Stdout)
    defer writer.Flush()

    tcStr, _ := reader.ReadString('\n')
    tc, _ := strconv.Atoi(strings.TrimSpace(tcStr))

    for tc > 0 {
        tc--

        line, _ := reader.ReadString('\n')
        parts := strings.Fields(line)
        n, _ := strconv.Atoi(parts[0])
        k, _ := strconv.Atoi(parts[1])

        if n == 1 || k == 1 || (k == 2 && n%2 != 0) {
            fmt.Fprintln(writer, -1)
        } else {
            if n%k != 1 {
                fmt.Fprintf(writer, "%d ", n)
                for i := 1; i < n; i++ {
                    fmt.Fprintf(writer, "%d ", i)
                }
            } else {
                fmt.Fprint(writer, "2 1 ")
                t := 3
                if t < n {
                    fmt.Fprintf(writer, "%d ", n)
                }
                for t < n {
                    fmt.Fprintf(writer, "%d ", t)
                    t++
                }
            }
            fmt.Fprintln(writer)
        }
    }
}

Java solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Codechef {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;

    public static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static long gcd(long a, long b) {
        if (a % b == 0)
            return b;
        return gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return (a / gcd(a, b)) * b;
    }

    public static void input(long[] arr) throws IOException {
        for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
    }

    public static void input(int[] arr) throws IOException {
        for (int i = 0; i < arr.length; i++) arr[i] = nextInt();
    }
    static int mod = 1000000000 + 7;

    public static void main(String[] args) throws IOException {
        int t = nextInt();
        while (t-- > 0) {
            run();
        }
    }

    /*****************************************CODE START HERE*********************************************************/
    public static void run() throws IOException
    {
        int n = nextInt();
        int k = nextInt();
        if (k==1 || ((n%2==1) && k==2))
        {
            System.out.println(-1);
            return;
        }
        else
        {
            if (n%2==0)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i <=n; i+=2)
                {
                    sb.append(i+1).append(" ").append(i).append(" ");
                }
                System.out.println(sb);
            }
            else if(n==1) System.out.println(1);
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.append(2).append(" ").append(3).append(" ").append(1).append(" ");
                for (int i = 4; i <=n; i+=2)
                {
                    sb.append(i+1).append(" ").append(i).append(" ");
                }
                System.out.println(sb);
            }
        }
    }
}

JavaScript solution:
// your code goes here
// Function to check if a given permutation is good
function isGoodPermutation(perm, K) {
    for (let i = 0; i < perm.length; i++) {
        if (perm[i] % K === (i + 1) % K) {
            return false;
        }
    }
    return true;
}

// Function to solve a single test case
function solve(n, K) {
    if (K === 1) {
        console.log(-1);
        return;
    }

    const array = Array.from({ length: n }, (_, i) => i + 1);

    // Try cyclic shifts for 2 to K
    for (let k = 2; k <= K; k++) {
        // Perform a right cyclic shift by (k - 1)
        const shifted = array.slice(-k + 1).concat(array.slice(0, -k + 1));
        if (isGoodPermutation(shifted, K)) {
            console.log(shifted.join(' '));
            return;
        }
    }

    console.log(-1);
}

// Main function to handle multiple test cases
function main() {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    let input = [];

    rl.on('line', (line) => {
        input.push(line.trim());
    });

    rl.on('close', () => {
        const t = parseInt(input[0], 10); // Number of test cases
        let index = 1;
        for (let i = 0; i < t; i++) {
            const [n, K] = input[index].split(' ').map(Number);
            index++;
            solve(n, K);
        }
    });
}

// Run the main function if this script is executed directly
if (require.main === module) {
    main();
}

Python solution:
# cook your dish here
for i in range(int(input())):
    N,K=map(int,input().split())
    if N==1 or K==1:
        print(-1)
    else:
        if K>N:
            A=[N]
            for i in range(N-1):
                A.append(i+1)
            print(*A)
        else:
            if 1!=N%K:
                A=[N]
                for i in range(N-1):
                    A.append(i+1)
                print(*A)
            else:
                if K==2:
                    print(-1)
                else:
                    A=[N-1,N]
                    for i in range(N-2):
                        A.append(i+1)
                    print(*A)

Ruby solution:
def solve
  test_cases = gets.to_i
  test_cases.times do
    n, k = gets.split.map(&:to_i)
    
    if n == 1 || k == 1
      puts -1
    elsif k > n
      # When k > n, we can rotate the array
      permutation = (2..n).to_a + [1]
      puts permutation.join(' ')
    else
      if n % k != 1
        # Standard rotation works when n mod k != 1
        permutation = (2..n).to_a + [1]
        puts permutation.join(' ')
      else
        if k == 2
          # No solution exists when k=2 and n mod k == 1
          puts -1
        else
          # Special case handling when n mod k == 1 and k > 2
          permutation = [n-1, n] + (1..n-2).to_a
          puts permutation.join(' ')
        end
      end
    end
  end
end

solve