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

Project Euler #24: Lexicographic permutations

Contest ends in
Problem
Submissions
Leaderboard
Discussions

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

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