We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Absolute Permutation
  5. Discussions

Absolute Permutation

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 486 Discussions, By:

recency

Please Login in order to post a comment

  • thanhtai1703
    2 weeks ago+ 0 comments

    C++ code solution

    vector<int> Permutation(vector<int> arr, int k){
        vector<int>result;
        int size = arr.size();
        int start = 0;
        int end = k;
        int count = 0;
        while(start < size){
            swap(arr[start], arr[end]);
            count++;
            if (k != 1){
                start = start + 1;
                end = end + 1;
                if (count == k){
                    start = start + k;
                    end = end + k;
                    count = 0;
                }
            } else {
                if (count == 1){
                    start = start + 2*k;
                    end = end + 2*k;
                    count = 0;
                }
            }
        }
        result = arr;
        return result;
    }
    vector<int> absolutePermutation(int n, int k) {
        vector<int> newarr;
        vector<int> result;
        int m = n % k;
        if (k == 0){
            for(int i = 0; i < n; i++){newarr.push_back(i+1);}
            result = newarr;
        }
        else if (k == 1)
        {
            if (n % 2 != 0){newarr.push_back(-1);result = newarr;}
            else {
                for (int i = 0; i < n; i++) newarr.push_back(i+1);
                result = Permutation(newarr, k);
            }
        }
        else {
            if (m != 0){newarr.push_back(-1);result = newarr;}
            else 
            {
                if ((n/k) % 2 != 0){newarr.push_back(-1);result = newarr;}
                else {
                    for (int i = 0; i < n; i++) newarr.push_back(i+1);
                    result = Permutation(newarr, k);}
            }
        }
        return result;
    }
    
    0|
    Permalink
  • tiagoiesbick
    2 weeks ago+ 0 comments

    Python 3

    def absolutePermutation(n, k):
        # Write your code here    
        if k == 0:
            return [i for i in range(1,n+1,1)]
        elif n % (2*k) != 0:
            return [-1]
        else:
            if k == 1:
                return [i+1 if i%2 != 0 else i-1 for i in range(1,n+1,1)]
            elif k <= n/2 and n % k == 0:
                a = 1
                flag = True
                ans = []
                while a <= n:
                    if flag:
                        ans.append(a+k)
                    else:
                        ans.append(a-k)
                    a += 1
                    if a % k == 1:
                        flag = not flag
                return ans
            else:
                return [-1]
    
    0|
    Permalink
  • tanhtanh1505
    3 weeks ago+ 0 comments

    js

    function absolutePermutation(n, k) {
      let a = [];
      for (let i = 1; i <= n; i++) {
        a.push(i);
      }
      k = Math.abs(k);
      if (k == 0) return a;
      else if (k > n / 2 + 1 || n % k != 0 || (n/k % 2 != 0)) return [-1];
      else {
        a = [];
        for (let i = 1; i <= n; i++) {
          a.push(i + Math.pow(-1, Math.floor((i - 0.1) / k)) * k);
        }
        return a;
      }
    }
    
    0|
    Permalink
  • mohammadhammads1
    4 weeks ago+ 0 comments

    Cpp solution O(n) time complexity

    vector<int> absolutePermutation(int n, int k) {
            vector<int> ans(n,0) ;
    
            for(int i = 1 ; i <= n ; i++){
                    int pos = i + k ; 
                    if(i-k > 0 && ans[i-k-1] == 0 ){
                            ans[i-k-1] = i ;
                    }else if(i+k <= n && ans[i+k-1] == 0){
                            ans[i+k-1] = i ;
                    }else{
                            vector<int> n ;
                            n.push_back(-1) ; 
                            return n ;
                    }
            }
            cout << endl ;
            for(int i = 0 ; i < n ; i++){
                 if(abs(ans[i] - (i+1)) != k || ans[i] == 0 ){
                         vector<int> n ;
                         n.push_back(-1) ;
                         return n ;
                 }
            }
    
            return ans ;
    }
    
    0|
    Permalink
  • yashparihar729
    2 months ago+ 0 comments

    here is my solution in java, javascript, python, C, C++, Csharp HackerRank Absolute Permutation Problem Solution

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy