0

I am looking for a code in C++ to produce such sequence :
INPUT :
n = 4(max element in the sequence), k = 3 (length of sequence)

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 4
1 4 2
2 1 4
2 4 1
4 1 2
4 2 1

I gone through Internet but could only get increasing sequence of given input length. I am figuring out how do I produce such sequence !

  • When will this sequence end? this looks like an infinity sequence, you need to be more specific. – Pham Trung Jun 08 '18 at 10:48
  • edited the text! – Raghav James Jun 08 '18 at 10:51
  • 2
    I think this should help https://stackoverflow.com/questions/29928236/print-all-permutation-in-lexicographic-order, you just need a little tweak to figure out the rest – Pham Trung Jun 08 '18 at 10:59
  • Does your sequence need to be in increasing order or are you just looking at all the permutations? If so take a look here: https://www.geeksforgeeks.org/print-all-combinations-of-given-length/ – hbejgel Jun 08 '18 at 16:19

1 Answers1

0

I think this is something like what you're looking for

#include <iostream>
#include <algorithm>
using namespace std;

int n, k;
int main(){
    cin>>n;
    cin>>k;
    int a[n];
    for(int i = 0; i < n; i++)
            a[i] = i + 1;
    do{
        for(int i = 0; i < k; i++)
                cout<<a[i]<<" ";
        cout<<endl;
    }while(next_permutation(a, a + n));
    return 0;
}