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

    You are viewing a single comment's thread. Return to all comments →

  • tanhtanh1505
    5 months 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy