I have to do a permution generation of all elements from an array, but without touching elements 2 and 23 Example: array is: 3 23 2 1 4 Output:
3 23 2 1 4
3 23 2 4 1
1 23 2 3 4
1 23 2 4 3
4 23 2 3 1
4 23 2 1 3
I do not know how to realise this task without printing the same answers multiple times
#include <iostream>
using namespace std;
void printingArr(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void perm(int arr[], int n, int size)
{
if (n == 1)
printingArr(arr, size);
else
for (int j = n - 1; j >= 0; j--)
{
if (arr[j] != arr[n - 1] && arr[j] != 23 && arr[j] != 2 && arr[n - 1] != 23 && arr[n - 1] != 2)
swap(arr[j], arr[n - 1]);
perm(arr, n - 1, size);
if (arr[j] != arr[n - 1] && arr[j] != 23 && arr[j] != 2 && arr[n - 1] != 23 && arr[n - 1] != 2)
swap(arr[n - 1], arr[j]);
}
}
int main()
{
int arr[] = {3, 23, 2, 1, 4};
int size = sizeof(arr) / sizeof(arr[0]);
perm(arr, size, size);
cout << endl;
system("PAUSE");
return 0;
}