How many palindromes can you create from array - c++

I'm trying to build a basic code that is taking an int array and outputing all of its optional palindromes.
Example: {1, 2, 2, 2, 3}
Output: {2 2}, {2 1 2}, {2 3 2}, {2 2 2}
I was thinking of reorganizing the array in every way possible and checking if it's an array by each time. But, I think I'm heading to it wrongly.
The code I made for now is:
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 2, 1 };// 7 int //56
bool check = true;
for(int i=0; i< sizeof(arr)/sizeof(int)/2;i++)
{
if(arr[i] != arr[sizeof(arr)/sizeof(int)-1-i])
check = false;
}
if(check)
{
for(int i=0; i< sizeof(arr)/sizeof(int);i++)
{
cout<<arr[i]<< " ";
}
cout<<endl;
}
return 0;
}
I will appreciate help if possible. Thank you all and have an amazing day!

We have to implement 2 major mathematical concepts here
Create a power set
Get all permutations from powerset.
Then, of course check for the palindrome property.
Let's start with the power set.
We will first make a simplification for an easier understanding of the task. We will use lower case letters in this example. Then we will take 4 elements {A,B,C,D} for demo purposes.
What we need to generate is a power set from this. There are 2 main standard approaches.
Counting with checking of set bits via it mask
Setting more and more bits in a field and do permutations.
Let us look at solution 1. Counting. Based on "abcd" we will get
Count Binary Selection (sub set)
DCBA Cardinality
0 0000 {----} 0 Empty set
1 0001 {---A} 1
2 0010 {--B-} 1
3 0011 {--BA} 2
4 0100 {-C--} 1
5 0101 {-C-A} 2
6 0110 {-CB-} 2
7 0111 {-CBA} 3
8 1000 {D---} 4
9 1001 {D--A} 2
10 1010 {D-B-} 2
11 1011 {D-BA} 3
12 1100 {DC--} 3
13 1101 {DC-A} 3
14 1110 {DCB-} 3
15 1111 {DCBA} 4
As a result we will get the powerset:
{{A},{B},{AB},{C},{AC},{BC},{ABC},{D},{AD},{BD},{ABD},{CD},{ACD},{BCD},{ABCD}}
If you look at the output, we see that the elements are not sorted according to their cardinality. Therefore we will choose a different approach. We will set more and more bits in a field an do permutations on them. Also with a selector. Then, we will get something like this:
Binary Selection
0000 {----} Empty set. Ignored
0001 {---A} Set bit 0
0010 {--B-} Permutation
0101 {-C--} Permutation
1000 {D---} Permutation
0011 {--BA} Set next bit number 1
0101 {-C-A} Permutation
1001 {D--A} Permutation
0110 {-CB-} Permutation
1010 {D-B-} Permutation
1100 {D--A} Permutation
0111 {-CBA} Set next bit number 2
1011 {D-BA} Permutation
1101 {DC-A} Permutation
1110 {DCB-} Permutation
1111 {DCBA} Set next bit number 3
As a result we will get the "more reasonable/expected" powerset:
{{A},{B},{C},{D},{AB},{AC},{AD},{BC},{BD},{AD},{ABC},{ABD},{ACD},{BCD},{ABCD}}
By the way. The overall number of sub sets is of course 2^n and the size of the cardinality group is equal to the corresponding binomial coeeficient.
Cardinality
0 4 choose 0 --> 1
1 4 choose 1 --> 4
2 4 choose 1 --> 6
3 4 choose 1 --> 4
4 4 choose 1 --> 1
Sum: 16
The corresponding algorithm is easy to implement, which can also be seen later in the code example.
Next, after having calculated one subset, we will create all permutations of data in this subset with the existing C++ function std::next_permutation.
After a successful check for a Palindrome, we will add this subset to a resulting std::set . We will use the std::set to avoid duplicates.
At the end, we can show the result.
The below example is brute force and hence very very slow for big input arrays.
Anyway, please see the example code below:
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
int main() {
std::vector testData{ 1,2,2,2,3 };
// Here we will store the result
std::set<std::vector<int>> result{};
// Selectors for creating the subsets of the power set
std::vector<bool> selector(testData.size());
// For all number of elements in the original test data
for (size_t k{0}; k < testData.size(); ++k) {
// Set selector bits. In each loop one more bit. So, 1 then 11, then 111 and so on
selector[k] = true;
// Do all permutations of the above set selector bits
do {
// Build the subset
std::vector<int> subset{};
for (std::size_t n{}; n < testData.size(); ++n)
if (selector[n]) subset.push_back(testData[n]);
do {
// Check, if palindrome
if (subset == std::vector<int>(subset.rbegin(), subset.rend()))
result.insert(subset);
} while (std::next_permutation(subset.begin(), subset.end()));
} while (std::prev_permutation(selector.begin(), selector.end()));
}
// Show all resulting palindromes
std::cout << "\n\nNumber of palindromes: " << result.size() << "\n\n";
int counter{ 1 };
for (const std::vector<int>& v : result) {
std::cout << "Palindrome " << counter++ << ":\t";
for (const int i : v) std::cout << ' ' << i;
std::cout << '\n';
}
}

Related

generating all permuated products from a map is not working

My code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> generate_all(map<ll, int> mp, int keys_done, ll prod) {
if (keys_done == mp.size()) {
vector<ll> v;
v.push_back(prod);
return v;
}
vector<ll> vr;
int ctr = 0;
for (auto it = mp.begin(); it != mp.end(); ++it, ctr++) {
if (ctr < keys_done or ctr > keys_done)
continue;
ll next_prod = 1;
for (int j = 1; j <= it->second; j++) {
next_prod *= it->first;
vector<ll> v1 = generate_all(mp, 1 + keys_done, prod * next_prod);
for (int k = 0; k < v1.size(); k++) {
vr.push_back(v1[k]);
}
}
}
return vr;
}
int main() {
map<ll, int> mp = {{2,4},{3,1}, {5,3}};
vector<ll> v_final=generate_all(mp,0,1);
for (const auto & val:v_final) {
cout << val << endl;
}
}
current output:
2
Expected output
2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 20, 24, 25, 30, 40, 48, 50, 60, 75, 80, 100, 120, 125, 150, 200, 240, 250, 300, 375, 400, 500, 600, 750, 1000, 1200, 1500, 2000, 3000, 6000
To illustrate the output it is the following:
2^1,2^2, 2^3, 2^4, 3^1, 5^1, 5^2, 5^3,
2^1 * 3, 2^2 * 3, 2^3 * 3, 2^4 * 3, 5^1 * 3, 5^2 * 3, 5^3 * 3,
2^1 * 5, 2^2 * 5, 2^3 * 5, 2^4 * 5,
2^1 * 5^2, 2^2 * 5^2, 2^3 * 5^2, 2^4 * 5^2, 3^1 * 5^2,
2^1 * 5^3, 2^2 * 5^3, 2^3 * 5^3, 2^4 * 5^3, 3^1 * 5^3,
2^1 * 3^1 * 5^1, 2^1 * 3^1 * 5^2, 2^1 * 3^1 * 5^3,
2^2 * 3^1 * 5^1, 2^2 * 3^1 * 5^2, 2^2 * 3^1 * 5^3,
2^3 * 3^1 * 5^1, 2^3 * 3^1 * 5^2, 2^3 * 3^1 * 5^3,
2^4 * 3^1 * 5^1, 2^4 * 3^1 * 5^2, 2^4 * 3^1 * 5^3,
Here 2 can have 0 to 4 th power multiplied by 3 from 0 to 4 th power and so on.
If the choices were limited to 3 like this example. this is how my code would have looked:
vector<ll>v;
for(int i=0;i<=4;i++)
for(int j=0;j<=1;j++)
for(int k=0;k<=3;k++)
v.insert(pow(2,i)*pow(3,j)*pow(5,k));
return v;
But I need to solve for k such keys not just 3 keys.
If possible do also suggest a non-recusrvice method.
We have to implement 2 major mathematical concepts here
Create a power set
Building the cartesian product over several sets (or the comnination of all elements over several sets)
The task contains some additional indirections to add a little bit complexity.
Let's start with the power set.
We will first make a simplification for an easier understanding of the task. We will name the input pairs, consisting of a base value and an exponent, as set elements a,b,c and so on. If we assume an input of { {2,4},{3,1}, {5,3}, {4,2} }, then we will name this now as a set with 4 elements {a,b,c,d}.
What we need to generate is a power set from this. There are 2 main standard approaches.
Counting with checking of set bits via it mask
Setting more and more bits in a field and do permutations.
Let us look at solution 1. Counting. Based on "abcd" we will get
Count Binary Selection (sub set)
DCBA Cardinality
0 0000 {----} 0 Empty set
1 0001 {---A} 1
2 0010 {--B-} 1
3 0011 {--BA} 2
4 0100 {-C--} 1
5 0101 {-C-A} 2
6 0110 {-CB-} 2
7 0111 {-CBA} 3
8 1000 {D---} 4
9 1001 {D--A} 2
10 1010 {D-B-} 2
11 1011 {D-BA} 3
12 1100 {DC--} 3
13 1101 {DC-A} 3
14 1110 {DCB-} 3
15 1111 {DCBA} 4
As a result we will get the powerset:
{{A},{B},{AB},{C},{AC},{BC},{ABC},{D},{AD},{BD},{ABD},{CD},{ACD},{BCD},{ABCD}}
If you look at the output, we see that the elements are not sorted according to their cardinality. Therefore we will choose a different approach. We will set more and more bits in a field an do permutations on them. Also with a selector. Then, we will get something like this:
Binary Selection
0000 {----} Empty set. Ignored
0001 {---A} Set bit 0
0010 {--B-} Permutation
0101 {-C--} Permutation
1000 {D---} Permutation
0011 {--BA} Set next bit number 1
0101 {-C-A} Permutation
1001 {D--A} Permutation
0110 {-CB-} Permutation
1010 {D-B-} Permutation
1100 {D--A} Permutation
0111 {-CBA} Set next bit number 2
1011 {D-BA} Permutation
1101 {DC-A} Permutation
1110 {DCB-} Permutation
1111 {DCBA} Set next bit number 3
As a result we will get the "more reasonable/expected" powerset:
{{A},{B},{C},{D},{AB},{AC},{AD},{BC},{BD},{AD},{ABC},{ABD},{ACD},{BCD},{ABCD}}
By the way. The overall number of sub sets is of course 2^n and the size of the cardinality group is equal to the corresponding binomial coeeficient.
Cardinality
0 4 choose 0 --> 1
1 4 choose 1 --> 4
2 4 choose 1 --> 6
3 4 choose 1 --> 4
4 4 choose 1 --> 1
Sum: 16
The corresponding algorithm is easy to implement, which can also be seen later in the code example.
Next. If we have the powerset, then we need to combine all elements. Let us take as an example the sub set {ABC}. We can translate this back to the original input data:
{2,4},{3,1},{5,3}
which would expand with the given rule to
{2,4,8,16},{3},{5,25,125}
And now we need to build all combinations of that. The number of combinations is of cause the product of the cardinality of the sub sets. Here 4*1*3 = 12. To illustrate that and get a first idea of the solution approach, we list up all combinations.
2 3 5
4 3 5
8 3 5
16 3 5
2 3 25
4 3 25
8 3 25
16 3 25
2 3 125
4 3 125
8 3 125
16 3 125
We could imagine that we have an odometer with 3 disks. Diks 1 contains 2,4,8,13, disk 2 contins 3 and disk 3 contains 5,25,125.
Everytime, when a disks rolls over than the next disk will be triggered as well. So, if the first diks is at 16 and rolls over to 2 then diks 3 is triggered. Because disk 3 contains only 1 number (the 3), it will also immediately roll over and trigger disk 3. This will then show the next value. And so on and so on.
So, we also do some sort of counting, but the counters have a different base number. Anyway. All this is easy to implement.
As an additional implementation detail, I will not use the "pow" function. I will just multiple the counter, the value on the disk, with its base value. This will save space and time. And all of this put together could be implemented like the following:
(Please note. I use "easy code". We could use many more advanced algorithms, but for the sake of readability, I go this way. As said. Many other solutions possible)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using MyType = unsigned long long;
// The base-number and the max exponent are the elements of a set
struct Element {
MyType base{};
MyType maxExponent{};
// Actually, this elements does not store one value only, but "maxExponent" elements
// Do get all of those, we use the well known odometer approach
// We can always calculate the next value in the list and inform, if we have a rollover
// In case of a roll over, we can inform other sets about this and the other set can also count up
MyType currentResult{ base };
MyType currentExponent{};
// Calculate next value. So, will for {2,4} get 2, 4, 8, 16
bool next(bool overflowFromPrevious) {
// Indicates that we will wrap around from previous
bool overflow{};
// If a previous odometer roll has overflowed
if (overflowFromPrevious) {
// Check, if we are NOT at the end. This will happen, if ther is only 1 as the max exponent.
if (currentExponent < maxExponent) {
// Get next exponent
++currentExponent;
// And calculate current data. We could also store those values,
// but we do not want to waste space
currentResult *= base;
// Now check again for overflow (after we incremented the exponent)
if (currentExponent >= maxExponent) {
// If overlow, then reset exponent counter and base
currentExponent = 0;
currentResult = base;
overflow = true;
}
}
else overflow = true;
}
return overflow;
}
// Simple inserter operator override, to be able to do an easier output
friend std::ostream& operator << (std::ostream& os, const Element& e) {
return os << '{' << e.base << ',' << e.maxExponent << '}';
}
};
// We will use a vetcor and not a set, because we need random access via the index operator
using MSet = std::vector<Element>;
void getProducts(MSet& mset) {
// Selectors for creating the subsets of the power set
std::vector<bool> selector(mset.size());
// For all number of elements in the original set
for (size_t k{}; k < mset.size(); ++k) {
// Set selecot bits. In each loop one more bit. So, 1 then 11, then 111 and so on
selector[k] = true;
// Do all permutations of the above set bits
do {
// For debug output
for (bool b : selector) std::cout << b * 1; std::cout << " ";
std::ostringstream oss{};
// Here we will store all elements of a resulting subset
MSet subSet{};
// Check if the selector bit is set, and if so, then add to subset
for (size_t n{}; n < mset.size(); ++n) {
if (selector[n]) {
subSet.push_back(mset[n]);
oss << mset[n]; // For debug output
}
}
// Debug output of powerset with number of subsets
std::cout << "Powerset("<< subSet.size()<< "): " << std::left << std::setw(22) << oss.str() << ' ';
// Now, we want to calculate all combinations of subsets, using the odometer approach
// Here we will store the overall number of combinations. It is the product of all max exponents
MyType combinations{ 1 };
for (const Element& element : subSet)
combinations *= element.maxExponent;
// Now get the product for all combinations over all subsets
for (MyType i{}; i < combinations; ++i) {
// Get the product for one combination
MyType product{ 1 };
for (Element& element : subSet)
product *= element.currentResult;
std::cout << product << ' '; // For debug output
// And, using the odometer approach, create the next combination
bool overflow{true};
for (Element& element : subSet)
overflow = element.next(overflow);
}
std::cout << '\n'; // For debug output
} while (std::prev_permutation(selector.begin(), selector.end()));
}
}
// Test / driver code
int main() {
MSet mset{ {2,4},{3,1}, {5,3}, {4,2} };
getProducts (mset);
}
I added a lot of debug output. You may delete that.
With that the solution the result will be:
By the way. I think that your expected output is wrong . . .

What is the logic to use bitwise operation in generating subsequences? [duplicate]

This question already has answers here:
What does the statement if (counter & (1<<j)) mean and how does it work?
(2 answers)
Closed last year.
I have an array, A=[1,2,3,4] (where n=4). I want to generate sub-sequences from this array.
Number of subsequences is (2^n -1)
Run from counter 0001 to 1111
for (int counter = 1; counter < 2^n; counter++)
{
for (int j = 0; j < n; j++)
{
Check if the jth bit in the counter is set. If set then print jth element from arr[]
if (counter & (1<<j))
cout << arr[j] << " ";
}
cout << endl;
}
}
can anyone explain me? How it works " counter & (1<
The logic goes like this. Say, like you put in the example, you have n = 4, and, to avoid confusion, let's say the array is A = [5, 7, 8, 9], for example. Then you want all the possible sequences containing some elements (at least one) from the original array. So you want a sequence containing only the first one, a sequence containing the first and the second, the first and the third, etc. Each printed sequence may or may not contain each of the elements in the array. So you could see it as something like this:
| 5 | 7 | 8 | 9 | Sequence
----------------- --------
| 1 | 0 | 0 | 0 | -> 5
| 1 | 1 | 0 | 0 | -> 5 7
| 1 | 0 | 1 | 0 | -> 5 8
...
That is, each sequence can be expressed as a list of bits, each indicating whether each member of the array is included.
In this loop:
for (int counter = 1; counter < 2^n; counter++)
The program iterates from 1 to 2^n - 1, that is 15 in this case. So the values that you get for counter are:
Dec Binary
--- ------
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
If you look closely, you will see that we have all the possible sequences of four elements composed of 0 and 1 in binary (except 0000, which would be the empty sequence and does not interest us). In this loop:
for (int j = 0; j < n; j++)
The program just goes through each bit of counter, from 0 (the rightmost) to n - 1 and whenever it finds a 1 it outputs the corresponding array element. The condition:
if (counter & (1<<j))
Simply computes the number 1<<j which is 1 plus j number of zeros at its right (so, for example, for j = 0 it would be 1 and for j = 2 it would be 100) and then a bitwise and operation, so it basically "filters" the j-th bit only (e.g. 1101 & 100 = 0100), and if the result is not zero then it means there was a one, and so arr[j] must be printed.
Obviously, the problem with the function is that it is limited to the number of bits that the variable counter can hold. That depends on its declared type and your architecture/compiler, but typically it will be either 32 or 64.

What does this vector array code do? (C++)

Having difficulty finding an explanation to this.
What does this code do? I understand it creates an array of vector but that's about it.
How can I print the vector array and access elements to experiment with it?
#define MAXN 300009
vector<int>dv[MAXN];
int main()
{
for(int i=1;i<MAXN;i++)
for(int j=i;j<MAXN;j+=i)
dv[j].push_back(i);
}
The code is easy enough to instrument. The reality of what it ends up producing is a very simple (and very inefficient) Sieve of Eratosthenes. Understanding that algorithm, you'll see what this code does to produce that ilk.
Edit: It is also a factor-table generator. See Edit below.
Instrumenting the code and dumping output afterward, and reducing the number of loops for simplification we have something like the following code. We use range-based-for loops for enumerating over each vector in the array of vectors:
#include <iostream>
#include <vector>
#define MAXN 20
std::vector<int>dv[MAXN];
int main()
{
for(int i=1;i<MAXN;i++)
{
for(int j=i;j<MAXN;j+=i)
dv[j].push_back(i);
}
for (auto const& v : dv)
{
for (auto x : v)
std::cout << x << ' ';
std::cout << '\n';
}
}
The resulting output is:
1
1 2
1 3
1 2 4
1 5
1 2 3 6
1 7
1 2 4 8
1 3 9
1 2 5 10
1 11
1 2 3 4 6 12
1 13
1 2 7 14
1 3 5 15
1 2 4 8 16
1 17
1 2 3 6 9 18
1 19
Now, note each vector that only has two elements (1 and an additional number). That second number is prime. In our test case those two-element vectors are:
1 2
1 3
1 5
1 7
1 11
1 13
1 17
1 19
In short, this is a very simple, and incredibly inefficient way of finding prime numbers. A slight change to the output loops to only output the second element of all vectors of length-two-only will therefore generate all the primes lower than MAXN. Therefore, using:
for (auto const& v : dv)
{
if (v.size() == 2)
std::cout << v[1] << '\n';
}
We will get all primes from [2...MAXN)
Edit: Factor Table Generation
If it wasn't obvious, each vector has an ending element (that not-coincidentally also lines up with the subscripts of the outer array). All preceding elements make up the positive factors of that number. For example:
1 2 5 10
is the dv[10] vector, and tells you 10 has factors 1,2,5,10. Likewise,
1 2 3 6 9 18
is the dv[18] vector, and tells you 18 has factors 1,2,3,6,9,18.
In short, if someone wanted to know all the factors of some number N that is < MAXN, this would be a way of putting all that info into tabular form.

How does this implementation of bitset::count() work?

Here's the implementation of std::bitset::count with MSVC 2010:
size_t count() const
{ // count number of set bits
static char _Bitsperhex[] = "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4";
size_t _Val = 0;
for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
for (_Ty _Wordval = _Array[_Wpos]; _Wordval != 0; _Wordval >>= 4)
_Val += _Bitsperhex[_Wordval & 0xF];
return (_Val);
}
Can someone explain to me how this is working? what's the trick with _Bitsperhex?
_Bitsperhex contains the number of set bits in a hexadecimal digit, indexed by the digit.
digit: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
value: 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
index: 0 1 2 3 4 5 6 7 8 9 A B C D E F
The function retrieves one digit at a time from the value it's working with by ANDing with 0xF (binary 1111), looks up the number of set bits in that digit, and sums them.
_Bitsperhex is a 16 element integer array that maps a number in [0..15] range to the number of 1 bits in the binary representation of that number. For example, _Bitsperhex[3] is equal to 2, which is the number of 1 bits in the binary representation of 3.
The rest is easy: each multi-bit word in internal array _Array is interpreted as a sequence of 4-bit values. Each 4-bit value is fed through the above _Bitsperhex table to count the bits.
It is a slightly different implementation of the lookup table-based method described here: http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable. At the link they use a table of 256 elements and split 32-bit words into four 8-bit values.

Permutations with some fixed numbers

How to effectively generate permutations of a number (or chars in word), if i need some char/digit on specified place?
e.g. Generate all numbers with digit 3 at second place from the beginning and digit 1 at second place from the end of the number. Each digit in number has to be unique and you can choose only from digits 1-5.
4 3 2 1 5
4 3 5 1 2
2 3 4 1 5
2 3 5 1 4
5 3 2 1 4
5 3 4 1 2
I know there's a next_permutation function, so i can prepare an array with numbers {4, 2, 5} and post this in cycle to this function, but how to handle the fixed positions?
Generate all permutations of 2 4 5 and insert 3 and 1 in your output routine. Just remember the positions were they have to be:
int perm[3] = {2, 4, 5};
const int N = sizeof(perm) / sizeof(int);
std::map<int,int> fixed; // note: zero-indexed
fixed[1] = 3;
fixed[3] = 1;
do {
for (int i=0, j=0; i<5; i++)
if (fixed.find(i) != fixed.end())
std::cout << " " << fixed[i];
else
std::cout << " " << perm[j++];
std::cout << std::endl;
} while (std::next_permutation(perm, perm + N));
outputs
2 3 4 1 5
2 3 5 1 4
4 3 2 1 5
4 3 5 1 2
5 3 2 1 4
5 3 4 1 2
I've read the other answers and I believe they are better than mine for your specific problem. However I'm answering in case someone needs a generalized solution to your problem.
I recently needed to generate all permutations of the 3 separate continuous ranges [first1, last1) + [first2, last2) + [first3, last3). This corresponds to your case with all three ranges being of length 1 and separated by only 1 element. In my case the only restriction is that distance(first3, last3) >= distance(first1, last1) + distance(first2, last2) (which I'm sure could be relaxed with more computational expense).
My application was to generate each unique permutation but not its reverse. The code is here:
http://howardhinnant.github.io/combinations.html
And the specific applicable function is combine_discontinuous3 (which creates combinations), and its use in reversible_permutation::operator() which creates the permutations.
This isn't a ready-made packaged solution to your problem. But it is a tool set that could be used to solve generalizations of your problem. Again, for your exact simple problem, I recommend the simpler solutions others have already offered.
Remember at which places you want your fixed numbers. Remove them from the array.
Generate permutations as usual. After every permutation, insert your fixed numbers to the spots where they should appear, and output.
If you have a set of digits {4,3,2,1,5} and you know that 3 and 1 will not be permutated, then you can take them out of the set and just generate a powerset for {4, 2, 5}. All you have to do after that is just insert 1 and 3 in their respective positions for each set in the power set.
I posted a similar question and in there you can see the code for a powerset.