heap algo

how do i generate all permutations of an array when a no can repeat in that array with the help of heap algorithm?
Hello ak281378,

To some of us your question is a bit vague. Others may understand it.

If you could give an example of the array and the desired output I would understand better. Also include any code you have written so far. It goes a long way to understand what you know and are trying to do.

Andy
actually the heap algo works for suppose an array {1,2,3} and prints permutations as
1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
but when I print the permutations for {1,2,2} i.e. a no is repeating then it prints
1 2 2
2 1 2
2 1 2
1 2 2
2 2 1
2 2 1
i want unique permutations of this array.i.e. the same permutation does not repeat again.
please help me how to do it
Why don't you use std::next_permutation ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

ostream& operator<<(ostream& oss, vector<int> &v)
{
  for (const int n: v)
  {
    oss << n << ' ';
  }
  return oss;
}

int main()
{
  vector<int> numbers = {1, 2, 2};
  cout << "Original numbers: \n";
  cout << numbers << '\n';
  cout << "Permutations: \n";
  while (next_permutation(numbers.begin(), numbers.end()))
  {
    cout << numbers << '\n'; 
  }
}



Original numbers:
1 2 2
Permutations:
2 1 2
2 2 1
Topic archived. No new replies allowed.