#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<vector<int>> ans;
void permute(vector<int> a, int ind)
{
if (ind == a.size())
{
ans.push_back(a);
return;
}
for (int i = ind; i < a.size(); i++)
{
if (i != ind && a[ind] == a[i])
{
// continue;
// i was using it but i have added below ans.push_back to see what it is printing
ans.push_back({a[ind], a[i], i, ind});
}
swap(a[i], a[ind]);
permute(a, ind + 1);
}
}
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (auto &it : a)
{
cin >> it;
}
sort(a.begin(), a.end());
permute(a, 0);
for (auto it : ans)
{
for (auto v : it)
{
cout << v << " ";
}
cout << endl;
}
}
I tried to print the permutation of the same elements by swapping only once and in for loop when i ==2 it should suppose to print the output 122 but it is not showing any output
so i tried to check what is happening by adding the ans.push_back({a[ind],a[i],i,ind}) and it was found that it is giving arr[i] =2 where index of the i=2 and arr[2] is 1 at that instance.
// input n = 3
// input vector = 1 2 2
// output :
// 122
// 2221
// 122
// 212
// 221
// 2220 why arr[i] = 2 where index of i = 2 and arr[2]=1??
// 212
// 221