Sort by

recency

|

545 Discussions

|

  • + 0 comments

    Python3 Solution

    def absolutePermutation(n, k):
        if k == 0:
            return [i+1 for i in range(n)]
            
        lexMap = {}
        for i in range(n):
            lexMap[i+1] = True
        
        res = []
        for i in range(1, n+1):
            if i-k in lexMap:
                res.append(i-k)
                del lexMap[i-k]
            elif i+k in lexMap:
                res.append(i+k)
                del lexMap[i+k]
        
        return res if len(lexMap) == 0 else [-1]
    
  • + 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. 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, ...]

    Sorry if I didn't explain it very clearly. fore more visit https://www.firenzerentals.com/

  • + 0 comments

    c++ solution:

    vector<int> absolutePermutation(int n, int k) {
        if (k == 0) {
            vector<int> permutation(n, 0);
            for (int index = 0; index < n; index++) {
                permutation[index] = index + 1;
            }
            return permutation;
        }
        
        // we need an even count of k-chucks in order to find an absolute permutation
        if (k > n / 2 || n % k != 0 || (n / k) % 2 != 0) {
            return { -1 };
        }
        
        vector<int> permutation(n, 0);
        // process two chucks in a single iteration
        for (int indexChucks = 0; indexChucks < n; indexChucks += 2 * k) {
            for (int indexElement = 0; indexElement < k; indexElement++) {
                int index = indexChucks + indexElement;
                permutation[index] = k + index + 1;
                permutation[k + index] = index + 1;
            }
        }
        
        return permutation;
    }
    
  • + 0 comments

    c++

    vector<int> absolutePermutation(int n, int k) {    
        vector<int> permuted(n,0);
        vector<bool> ussage(n,false);
        
        for (int i = 1; i<=n; i++){
            
            if(i-k>0 && ussage[i-k-1]==false){
                permuted[i-k-1] = i;
                ussage[i-k-1] = true;
            }else if(i+k<=n && ussage[i+k-1]==false){
                permuted[i+k-1] = i;
                ussage[i+k-1] = true;
            }else{
                return {-1};
            }       
        }
        
        
        return permuted;
    }
    
  • + 0 comments

    Locksmith explains the concept of absolute permutation, a mathematical problem often explored in algorithm design and competitive programming. It involves arranging numbers in a sequence where the absolute difference between each element and its position matches a given value. Solving absolute permutation requires logical reasoning, efficient coding, and problem-solving skills. This challenge tests algorithmic thinking and is widely used for learning optimization techniques, making it both educational and practical for programmers and students.