Tuesday 31 December 2019

std::next_permutation(arr.begin(),arr.end());

What is next_permutation(a.begin(),a.end()) ?

Next permutation is like what its name says, finds the next permutation of a and assigns this permutation to a.

next_permutation is often used in brute force solutions.

So here's how to use it :

do{
   // do something
} while(next_permutation(a.begin(),a.end()));

The complexity of this code is O(number of elements in a !).

(Note : ! = factorial)

Extra : prev_permutation

prev_permutation(a.begin(),a.end()) has very similar functionalities. In fact, the only difference is that prev_permutation takes the previous permutation of a and assignes this permutation to a.

Thanks for reading and Happy New Year !

No comments:

Post a Comment

std::next_permutation(arr.begin(),arr.end());

What is next_permutation(a.begin(),a.end()) ? Next permutation is like what its name says, finds the next permutation of a and assigns ...