You are viewing a single comment's thread. Return to all comments →
My in-place O(n) solution. No allocation required
template <typename Iterator> static void rotate_left(Iterator begin, Iterator mid, Iterator end) { while (begin != mid && mid != end) { auto it = mid; while (begin != mid && it != end) { std::swap(*begin++, *it++); } if (begin == mid) { mid = it; } } } int main() { int n{}; int k{}; std::cin >> n >> k; std::vector<int> values(n); std::copy_n(std::istream_iterator<int>(std::cin), n, values.begin()); auto mid = values.begin(); std::advance(mid, k); ::rotate_left(values.begin(), mid, values.end()); std::copy(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " ")); return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Left Rotation
You are viewing a single comment's thread. Return to all comments →
My in-place O(n) solution. No allocation required