How to get the next lexicographically greater permutation in C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>  

int main() 
{ 
    int arr[] = { 1, 2, 3 }; 
  
  
    std::cout << "The next lexicographical permutation:\n"; 
    
    std::next_permutation(arr, arr + 3);                    
  
    for (int i = 0; i < 3; i++) {
        std::cout << arr[i];
    }
}



/*
run:

The next lexicographical permutation:
132

*/

 



answered Oct 24, 2025 by avibootz
...