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

Absolute Permutation

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 503 Discussions, By:

recency

Please Login in order to post a comment

  • agentkaumal88
    4 days ago+ 0 comments

    Same code logic using python and C# but this may not be the optimal way. If you use a list instead of hashset 4 test cases will be failed due to time limit exceed.

    Python

    def absolutePermutation(n, k):
        permutation = []
        #if you use a list instead of a hashset 4 tests are failed.
        rn = set(range(1, n + 1))
        
        if k == 0:
            return list(rn)
        
        for i in range(1, n + 1):
            low = i - k
            up = i + k
            if low in rn:
                rn.discard(low)
                permutation.append(low)
            elif up in rn:
                rn.discard(up)
                permutation.append(up)
            else:
                return [-1]
                
        return permutation
    

    C#

    public static List<int> absolutePermutation(int n, int k)
    {
    	var permutation = new List<int>();
    	var rn = Enumerable.Range(1, n);
    	/*
    	if you use a list
    	instead of a hashset
    	4 tests are failed.
    	*/
    	var hs = new HashSet<int>(rn);
    	
    	if (k == 0)
    		return rn.ToList();
    	
    	for (int i = 1; i <= n; ++i)
    	{
    		var low = i - k;
    		var up = i + k;
    		
    		if (hs.Contains(low))
    		{
    			hs.Remove(low);
    			permutation.Add(low);
    		}
    		else if (hs.Contains(up))
    		{
    			hs.Remove(up);
    			permutation.Add(up);
    		}
    		else
    		{
    			return new List<int>(){ - 1};
    		}
    	}
    	
    	return permutation;
    }
    
    0|
    Permalink
  • renushankarjha
    3 weeks ago+ 0 comments
    def absolutePermutation(n, k):
        # Write your code here
        
        if ((n % 2 != 0) & (k != 0)) | (k > n // 2):
            return [-1]
        arr = [i+1 for i in range(n)]
        if k == 0:
            return arr
        changed = [0 for _ in range(n)]
        for i in range(n - k):
            if (not changed[i]) & (not changed[i+k]):
                arr[i], arr[i+k] = arr[i+k], arr[i]
                changed[i], changed[i+k] = 1, 1
        if 0 in changed[n-k:]:
            return [-1]
        return arr
    
    0|
    Permalink
  • G_Ruiz
    2 months ago+ 1 comment

    Python 3 Solution

        def absolutePermutation(n, k):
    
                if k == 0:
                        return [x for x in range(1, n+1)]
    
                elif n % (2*k) != 0:
                        return [-1]
    
                else:
                        abs_permutation = list()
    
                        for i in range(0, int(n/(2*k))):
    
                                for j in range(1, (2*k)+1):
    
                                        if j <= k:
                                                abs_permutation.append(((i*(k*2))+j)+k)
                                        else:
                                                abs_permutation.append(((i*(k*2))+j)-k)
    
                        return abs_permutation
    
    0|
    Permalink
  • claravivian75
    2 months ago+ 1 comment

    Can someone help to find why my code in Python 3 would have some cases to have exceeded the time limit?

    def absolutePermutation(n, k):

    pos = [i for i in range(1, n+1)]

    length = len(pos)

    item = pos

    new = []

    if k != 0:

    for i in item:

        for j in range(length):
    
                if abs(pos[j] - i) == k and pos[j] not in new:
                        new.append(pos[j])
                        break
                continue
    

    else: new = pos if len(new) != n: new = [-1] return new

    0|
    Permalink
  • mohamedcherrou21
    2 months ago+ 0 comments

    java 8 :

    public static List<Integer> absolutePermutation(int n, int k) {
        // Write your code here
            List<Integer> permutation = new ArrayList<>();
            
            if (k == 0) {
                for (int i = 1; i <= n; i++) {
                    permutation.add(i);
                }
                return permutation;
            }
    
            if (n % (2 * k) != 0) {
                return Arrays.asList(-1); // Not possible to form absolute permutation
            }
            
            int sign = 1;
            for (int i = 1; i <= n; i++) {
                permutation.add(i + sign * k);
                if(i%k == 0){
                    sign = -sign;
                }
            }
    
            return permutation;
    
        }
    
    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