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. All Contests
  2. ProjectEuler+
  3. Project Euler #24: Lexicographic permutations
  4. Discussions

Project Euler #24: Lexicographic permutations

Contest ends in
Problem
Submissions
Leaderboard
Discussions

Sort 39 Discussions, By:

recency

Please Login in order to post a comment

  • vidush_rk11
    4 months ago+ 0 comments

    Tried to reverse the process of finding the rank of a word in a dictionary
    100/-points

    from math import factorial
    def lint(n):
        if n==int(n):
            return int(n)
        else:
            return int(n)+1
    word='abcdefghijklm'
    li=[]
    for i in sorted(word):
        li.append(i)
    
    def answer_calc(pos,lis):
        answer=''
        for i in range(len(word)-1,-1,-1):
            index=lint(pos/factorial(i))-1
            answer+=lis.pop(index)
    
            pos+=-(index)*factorial(i)
    
        return answer
    for _ in range(int(input())):
        n=int(input())
        print(answer_calc(n,li[:]))
    
    0|
    Permalink
  • arninho
    1 year ago+ 0 comments

    My JavaScript solution:

    function processData(input) {
    
      const factoradic = (num) => {
        const factoradics = [];
        let i = 1;
    
        while (num !== 0) {
          factoradics.unshift(Math.floor(num % i));
          num = Math.floor(num / i);
          i++;
        }
        return factoradics;
      };
    
      const permutations = (str, n) => {
        let char;
        let result = [];
        let facNumber = factoradic(n - 1);
        let tempString = str.split('');
    
        while (facNumber.length < str.length) {
          facNumber.unshift(0);
        }
    
        for (let i = 0; i < str.length; i++) {
          char = tempString.splice(facNumber[i], 1);
          result.push(char);
        }
    
        return result.join('');
      };
    
      let str = 'abcdefghijklm';
      let inputString = input.split('\n');
      let currentLine = 0;
      const readLine = () => inputString[currentLine++];
    
      let t = Number(readLine().trim());
    
      for (let tItr = 0; tItr < t; tItr++) {
    
        let n = Number(readLine().trim());
    
        console.log(permutations(str, n));
      }
    }
    
    0|
    Permalink
  • sendagaijin
    1 year ago+ 0 comments

    Nice explanation of factorial number systems: https://stemhash.com/efficient-permutations-in-lexicographic-order/

    0|
    Permalink
  • shukla_manu09
    2 years ago+ 0 comments

    C++ Accepted Solution. Using Factorial Number System

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define lb endl
    #define mod 1000000007
    
    ll fact(ll n)
    {
        if (n <= 1)
            return 1;
    
        return fact(n - 1) * n;
    }
    
    void solve()
    {
        ll n;
        cin >> n;
        --n;
        vector<char> alpha = {'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};
        string ans = "";
      
    
    
        for (int i = 12; i >= 0; --i)
        {
    
            ll factorial = fact(i);
            
            ll x = (n) / factorial;
    
            ans += alpha[x];
            n -= factorial * x;
    
     
            alpha.erase(alpha.begin() + x);
    
        }
    
        cout << ans << endl;
    }
    
    int main()
    {
        OJ;
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        int t;
        cin >> t;
        while (t--)
        {
            solve();
        }
        return 0;
    }
    
    0|
    Permalink
  • yashdashadiya
    2 years ago+ 0 comments

    my python solution

    count=False
    for _ in range(int(input())):
        n=int(input())-1
        i=1
        array=[]
        while n!=0:
            a=n%i
            n=n//i
            # print(n,i,a)
            array.insert(0,a)
            i=i+1
        arraychar=['a','b','c','d','e','f','g','h','i','j','k','l','m']
        if len(array)<len(arraychar):
            for _ in range(len(arraychar)-len(array)):
                array.insert(0,0)
        newarray=[]
        for i in array:
            newarray.append(arraychar[i])
            arraychar.pop(i)
        if count:
            print("")
        for i in newarray:
            print(i,end="")
        count=True
    
    1|
    Permalink
Load more conversations

Need Help?


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