Largest Permutation Discussions | Algorithms | HackerRank

Largest Permutation

  • + 0 comments

    C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) )

    std::vector<int> largestPermutation(
            int _swaps
        ,   std::vector<int> _arr
    ){
        using namespace std;
        size_t dst = 0;
        for(auto it = begin(_arr);
            it != end(_arr) && _swaps != 0; ++it){
            dst = distance(it, end(_arr));
            if(dst == *it){
                continue;
            }
            swap(*it, *(find( it + 1, end(_arr), dst)));
            --_swaps;
        }
        return _arr;
    }