Sort by

recency

|

533 Discussions

|

  • + 0 comments

    Easy python

    def absolutePermutation(n, k):
        if k == 0: return list(range(1,n+1))
        if (n/k)%2 != 0: return [-1]
        res = []
        up = True
        for i in range(n//k):
            for j in range(k):
                res.append(i*k+1+j + (k if up else -k))
            up = not up
            
            
        return  res
    
  • + 0 comments

    Simple approach in python:

        P = [0]*(n+1)
        # Try to create P that fulfills |P[i] - i| = k
        for i in range(1,n+1):
            if P[i] == 0:
                # P[i] can be either i+k or i-k.
                # Greedy approach, try to fit the lexicographically smallest first
                if i-k > 0 and P[i-k] == 0:
                    P[i], P[i-k] = i-k, i
                elif i+k <= n and P[i+k] == 0:
                    P[i], P[i+k] = i+k, i
                else:
                    # Not possible
                    return [-1]
        # Skips the first element, added just for better code readability
        return P[1:]
    
  • + 0 comments
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<int> absolutePermutation(int n, int k) {
        vector<int> result;
    
        // If k is 0, the permutation is just the natural numbers from 1 to n.
        if (k == 0) {
            for (int i = 1; i <= n; ++i) {
                result.push_back(i);
            }
            return result;
        }
    
        // If n is not divisible by 2 * k, a valid permutation is not possible.
        if (n % (2 * k) != 0) {
            return {-1};
        }
    
        // Construct the permutation in groups of 2 * k.
        for (int i = 1; i <= n; i += 2 * k) {
            // Add the first k numbers in the group shifted by +k.
            for (int j = 0; j < k; ++j) {
                result.push_back(i + j + k);
            }
            // Add the next k numbers in the group shifted by -k.
            for (int j = 0; j < k; ++j) {
                result.push_back(i + j);
            }
        }
    
        return result;
    }
    
    int main() {
        int t;
        cin >> t; // Number of queries
    
        while (t--) {
            int n, k;
            cin >> n >> k;
    
            vector<int> result = absolutePermutation(n, k);
    
            if (result.size() == 1 && result[0] == -1) {
                cout << "-1\n";
            } else {
                for (int num : result) {
                    cout << num << " ";
                }
                cout << "\n";
            }
        }
    
        return 0;
    }
    
  • + 0 comments

    The task can only be solved under the condition that the array is divided into blocks of size multiples of 2*k, where in each block, result[i] = input[i] + k for "k" times and result[i] = input[i] - k for "k" times. For example, for k=2, (k+1, k+2, 3-k, 4-k) (k+5, k+6, 7-k, 8-k) ... result=[3,4,1,2, 7,8,5,6, ...]

    If you're looking for Can Am X3 accessories, they could be used to enhance the performance of the blocks in your task, providing better modularity and adjustability. Sorry if I didn't explain it very clearly.

  • + 0 comments

    This a is bit more intuitive approach.

    def condition(i: int, k: int, n: int, a: set) -> bool:
        return 0 < i + k <= n and (i + k) not in a
    
    
    def absolutePermutation(n: int, k: int) -> int | list[int]:
        ans = [-1 for _ in range(n + 1)]
        a = set()
        for i in range(1, n + 1):
            if condition(i, -k, n, a) is True:
                ans[i] = i - k
                a.add(i - k)
            elif condition(i, k, n, a) is True:
                ans[i] = i + k
                a.add(i + k)
            else:
                return [-1]
        return ans[1:]