You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )
std::vector<int> absolutePermutation( int const & _numsLimit, int const & _diff ){ using namespace std; using num_t = remove_const<remove_reference<decltype(_numsLimit)> ::type>::type; if(_diff == 0){ auto result = std::vector<num_t>(_numsLimit); iota(begin(result), end(result), 1); return result; } if(_numsLimit % (_diff * 2) != 0){ return {-1}; } auto result = std::vector<num_t>(_numsLimit); iota(begin(result), end(result), 1); auto window = _diff * 2; for(auto idx = 0; idx != result.size(); idx += window){ for(auto idx1 = idx, idx2 = idx1 + _diff; idx2 != idx + window; ++idx1, ++idx2 ){ swap(result.at(idx1), result.at(idx2)); } } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master , feel free to give a star :) )