0
#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
S.Legends
  • 3
  • 2
  • 1
    Time to learn how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your program line by line while monitoring variables and their values. – Some programmer dude Mar 15 '23 at 11:44
  • what is the desired output? – 463035818_is_not_an_ai Mar 15 '23 at 12:47
  • 2
    It's not clear what the problem is. If I use the `continue` I get the three permuations "1 2 2", "2 1 2", and "2 2 1". Is that not what you were loooking for? – molbdnilo Mar 15 '23 at 12:59

0 Answers0