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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Absolute Permutation
  5. Discussions

Absolute Permutation

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 477 Discussions, By:

recency

Please Login in order to post a comment

  • mrtakeeex
    2 weeks ago+ 0 comments

    C# without 'else' keyword ;)

    List<int> absolutePermutation(int n, int k)
    {
        if (k == 0) return Enumerable.Range(1, n).ToList();
        
        HashSet<int> result = new();
    
        for (int i = 1; i <= n; i++)
        {
            var permutationElement = i > k ? i - k : i + k;
            if (permutationElement > n || result.Contains(permutationElement))
            {
                permutationElement = i < k ? i - k : i + k;
                if (permutationElement > n || result.Contains(permutationElement))
                {
                    return new List<int> { -1 };
                }
            }
            result.Add(permutationElement);
        }
        return result.ToList();
    }
    
    0|
    Permalink
  • ujjwalgupta0512
    2 weeks ago+ 0 comments
    vector<int> absolutePermutation(int n, int k) {
    vector<int>arr,brr;
    for(int i=1;i<=n;i++) arr.push_back(i);
    if(k==0) return arr;
    if(n%(2*k)==0){
        for(int i=0;i<n/(2*k);i++){
            for(int j=1;j<=k;j++) brr.push_back((2*k*i)+k+j);
            for(int j=1;j<=k;j++){
                brr.push_back(2*k*i+j);
            }       
        }
    }
    if(brr.size()==0) return {-1};
        return brr;
    }
    
    0|
    Permalink
  • javi_tbs
    2 weeks ago+ 0 comments

    python

    # P 1 based indexing
        # hashMap for 1 to N values (P)
        # |pos[i]-i|=k
        # pos[i]-i=-k    pos[i]-i=k
        # pos[i]=-k+i    pos[i]=k+i
        #   -1  3
        #   0   4
        #   1   5
        #   2   6
        # k=4
        iValues={}
        posValues={}
        for i in range(1,n+1):
            iValues[i]=(-k+i,k+i)
            posValues[i]=False
        result = []
        #iValues
        for i in range(1,n+1):
            if(iValues[i][0]>0 and posValues[iValues[i][0]]==False):
                posValues[iValues[i][0]]=True
                result.append(iValues[i][0])
            elif(iValues[i][1]<=n and posValues[iValues[i][1]]==False):
                posValues[iValues[i][1]]=True
                result.append(iValues[i][1])
            else:
                return [-1]
                
        return result
                    
        #spaceComplexity 0(n)
        #runtimeComplexity 0(n)
    
    0|
    Permalink
  • dream101221
    3 weeks ago+ 0 comments

    Absolute Permutation problem Solution In Hacker Rank Here is the solutionClick Here

    0|
    Permalink
  • sphericalconic
    3 weeks ago+ 1 comment

    JAVA

    public static List<Integer> absolutePermutation(int n, int k) {
            Integer[] result = new Integer[n];
            int idx;
            for (int value = 1; value <= n; value++) {
                idx = value - k;
                if (idx <= n && idx > 0 && result[idx - 1] == null) {
                    result[idx - 1] = value;
                } else {
                    idx = value + k;
                    if (idx <= n && idx > 0 && result[idx - 1] == null) {
                        result[idx - 1] = value;
                    } else {
                        result = new Integer[1];
                        result[0] = -1;
                        return Arrays.asList(result);
                    }
                }
            }
            return Arrays.asList(result);
        }
    
    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