How to get the previous lexicographically smaller permutation in C++

1 Answer

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

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



/*
run:

The previous lexicographical permutation:
312

*/

 



answered Oct 24, 2025 by avibootz
...