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 :) )
void almostSorted(std::vector<int> & _nums){ auto const & sz = _nums.size(); auto indxs = std::vector<size_t>(); indxs.reserve(sz); auto numsSorted = _nums; sort(std::begin(numsSorted), std::end(numsSorted)); for(size_t a = 0; a < sz; ++a){ if(_nums.at(a) == numsSorted.at(a)){ continue; } indxs.emplace_back(a); } for(auto a1 = crbegin(indxs), a2 = --crend(indxs);a1 <= a2; ++a1, --a2 ){ if(_nums.at(*a1) == numsSorted.at(*a2)){ continue; } std::cout << "no\n"; return; } std::cout << "yes" << '\n'; if(indxs.empty()){ return; } std::cout << (indxs.size() <= 2 ? "swap " : "reverse ") << ++indxs.front() << " " << ++indxs.back() << '\n'; }
Seems like cookies are disabled on this browser, please enable them to open this website
Almost Sorted
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 :) )