Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:35:11+00:00 2026-05-27T07:35:11+00:00

Suppose I have an array of integers int a[] = {0, 1, … N-1}

  • 0

Suppose I have an array of integers int a[] = {0, 1, ... N-1}, where N is the size of a. Now I need to generate all permutations of a s that a[i] != i for all 0 <= i < N. How would you do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:35:12+00:00Added an answer on May 27, 2026 at 7:35 am

    Here’s some C++ implementing an algorithm based on a bijective proof of the recurrence

    !n = (n-1) * (!(n-1) + !(n-2)),
    

    where !n is the number of derangements of n items.

    #include <algorithm>
    #include <ctime>
    #include <iostream>
    #include <vector>
    
    static const int N = 12;
    static int count;
    
    template<class RAI>
    void derange(RAI p, RAI a, RAI b, int n) {
        if (n < 2) {
            if (n == 0) {
                for (int i = 0; i < N; ++i) p[b[i]] = a[i];
                if (false) {
                    for (int i = 0; i < N; ++i) std::cout << ' ' << p[i];
                    std::cout << '\n';
                } else {
                    ++count;
                }
            }
            return;
        }
        for (int i = 0; i < n - 1; ++i) {
            std::swap(a[i], a[n - 1]);
            derange(p, a, b, n - 1);
            std::swap(a[i], a[n - 1]);
            int j = b[i];
            b[i] = b[n - 2];
            b[n - 2] = b[n - 1];
            b[n - 1] = j;
            std::swap(a[i], a[n - 2]);
            derange(p, a, b, n - 2);
            std::swap(a[i], a[n - 2]);
            j = b[n - 1];
            b[n - 1] = b[n - 2];
            b[n - 2] = b[i];
            b[i] = j;
        }
    }
    
    int main() {
        std::vector<int> p(N);
        clock_t begin = clock();
        std::vector<int> a(N);
        std::vector<int> b(N);
        for (int i = 0; i < N; ++i) a[i] = b[i] = i;
        derange(p.begin(), a.begin(), b.begin(), N);
        std::cout << count << " permutations in " << clock() - begin << " clocks for derange()\n";
        count = 0;
        begin = clock();
        for (int i = 0; i < N; ++i) p[i] = i;
        while (std::next_permutation(p.begin(), p.end())) {
            for (int i = 0; i < N; ++i) {
                if (p[i] == i) goto bad;
            }
            ++count;
        bad:
            ;
        }
        std::cout << count << " permutations in " << clock() - begin << " clocks for next_permutation()\n";
    }
    

    On my machine, I get

    176214841 permutations in 13741305 clocks for derange()
    176214841 permutations in 14106430 clocks for next_permutation()
    

    which IMHO is a wash. Probably there are improvements to be made on both sides (e.g., reimplement next_permutation with the derangement test that scans only the elements that changed); that’s left as an exercise to the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a sorted array of integers int[] , and I want to
Suppose I have an array that mimics a database table. Each array element represents
Suppose I have an array of nodes (objects). I need to create a duplicate
I have a question about Quantifiers. Suppose that I have an array and I
I can't figure this interview question. You have an array of integers. You need
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
I have a dword array of 15 random integers that is stored in esi.
I have a 2D array. Lets suppose that it has some connected region which
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
Suppose I have an array like this: $array = array(a,b,c,d,a,a); and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.