3 lines of C++ in one pass using swap
Every swap will put one number into its correct position, so the running time is O(n)\n\nFor example,\n\nat first, nums[] is [1,2,3,4,5,6,7], n is 7, k is 3\n\n
Every swap will put one number into its correct position, so the running time is O(n)\n\nFor example,\n\nat first, `nums[]` is `[1,2,3,4,5,6,7]`, n is 7, k is 3\n\nafter first outer loop, `nums[]` is `[4,1,2,3]`, n is 4, k is 3\n\nafter second outer loop, `nums[]` is `[4]`, n is 1, k is 0\n\nloop ends.\n\n    void rotate(int nums[], int n, int k) {\n        for (; k %= n; n -= k)\n            for (int i = 0; i < k; i++)\n                swap(*nums++, nums[n - k]);\n    }
[]