Sort by

recency

|

544 Discussions

|

  • + 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.

  • + 0 comments

    Solution in Java using a HashSet

    public static List<Integer> absolutePermutation(int n, int k) {
        // Write your code here
        var set = new HashSet<Integer>();
        for (var i = 1; i <= n; i++) {
            set.add(i);
        }
        var result = new ArrayList<Integer>();
        for (var i = 1; i <= n; i++) {
            var lowerNumber = i - k;
            var higherNumber = i + k;
            if (set.remove(lowerNumber)) {
                result.add(lowerNumber);
            } else if (set.remove(higherNumber)) {
                result.add(higherNumber);
            } else {
                return List.of(-1);
            }
        }
        return result;
    }