You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) )
namespace Homework{ template<typename _Iterator_t> size_t countSwaps( _Iterator_t && _numSorted, std::vector<int> const & _nums ){ size_t swaps = 0; auto nums = _nums; for(auto num = std::begin(nums); num != std::end(nums); ++num, ++_numSorted ){ if(*num == *_numSorted){ continue; } std::swap(*num, *std::find(num + 1, std::end(nums), *_numSorted)); ++swaps; } return swaps; } } // Homework int lilysHomework( std::vector<int> const & _nums ){ auto numsSorted = _nums; std::sort(std::begin(numsSorted), std::end(numsSorted)); return static_cast<int>( std::min(Homework::countSwaps(std::cbegin(numsSorted), _nums), Homework::countSwaps(std::crbegin(numsSorted), _nums))); }
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) )