Summary of C++ solutions
1. Make an extra copy and then rotate. \nTime complexity: O(n). Space complexity: O(n).\n    \n        class Solution \n        {\n        public:\n            
#1. Make an extra copy and then rotate. \nTime complexity: O(n). Space complexity: O(n).\n    \n        class Solution \n        {\n        public:\n            void rotate(int nums[], int n, int k) \n            {\n                if ((n == 0) || (k <= 0))\n                {\n                    return;\n                }\n                \n                // Make a copy of nums\n                vector<int> numsCopy(n);\n                for (int i = 0; i < n; i++)\n                {\n                    numsCopy[i] = nums[i];\n                }\n                \n                // Rotate the elements.\n                for (int i = 0; i < n; i++)\n                {\n                    nums[(i + k)%n] = numsCopy[i];\n                }\n            }\n        };\n\n#2. Start from one element and keep rotating until we have rotated n different elements.\nTime complexity: O(n). Space complexity: O(1).\n    \n        class Solution \n        {\n        public:\n            void rotate(int nums[], int n, int k) \n            {\n                if ((n == 0) || (k <= 0))\n                {\n                    return;\n                }\n                \n                int cntRotated = 0;\n                int start = 0;\n                int curr = 0;\n                int numToBeRotated = nums[0];\n                int tmp = 0;\n                // Keep rotating the elements until we have rotated n \n                // different elements.\n                while (cntRotated < n)\n                {\n                    do\n                    {\n                        tmp = nums[(curr + k)%n];\n                        nums[(curr+k)%n] = numToBeRotated;\n                        numToBeRotated = tmp;\n                        curr = (curr + k)%n;\n                        cntRotated++;\n                    } while (curr != start);\n                    // Stop rotating the elements when we finish one cycle, \n                    // i.e., we return to start.\n                    \n                    // Move to next element to start a new cycle.\n                    start++;\n                    curr = start;\n                    numToBeRotated = nums[curr];\n                }\n            }\n        };\n    \n#3. Reverse the first n - k elements, the last k elements, and then all the n elements.\nTime complexity: O(n). Space complexity: O(1).\n        \n        class Solution \n        {\n        public:\n            void rotate(int nums[], int n, int k) \n            {\n                k = k%n;\n        \n                // Reverse the first n - k numbers.\n                // Index i (0 <= i < n - k) becomes n - k - i.\n                reverse(nums, nums + n - k);\n                \n                // Reverse tha last k numbers.\n                // Index n - k + i (0 <= i < k) becomes n - i.\n                reverse(nums + n - k, nums + n);\n                \n                // Reverse all the numbers.\n                // Index i (0 <= i < n - k) becomes n - (n - k - i) = i + k.\n                // Index n - k + i (0 <= i < k) becomes n - (n - i) = i.\n                reverse(nums, nums + n);\n            }\n        };\n\n#4. Swap the last k elements with the first k elements.\nTime complexity: O(n). Space complexity: O(1).\n\n    class Solution \n    {\n    public:\n        void rotate(int nums[], int n, int k) \n        {\n            for (; k = k%n; n -= k, nums += k)\n            {\n                // Swap the last k elements with the first k elements. \n                // The last k elements will be in the correct positions\n                // but we need to rotate the remaining (n - k) elements \n                // to the right by k steps.\n                for (int i = 0; i < k; i++)\n                {\n                    swap(nums[i], nums[n - k + i]);\n                }\n            }\n        }\n    };\n\n#5. Keep swapping two subarrays.\nTime complexity: O(n). Space complexity: O(1).\n    \n    class Solution \n    {\n    public:\n        void rotate(int nums[], int n, int k) \n        {\n            if ((n == 0) || (k <= 0) || (k%n == 0))\n            {\n                return;\n            }\n            \n            k = k%n;\n            // Rotation to the right by k steps is equivalent to swapping \n            // the two subarrays nums[0,...,n - k - 1] and nums[n - k,...,n - 1].\n            int start = 0;\n            int tmp = 0;\n            while (k > 0)\n            {\n                if (n - k >= k)\n                {\n                    // The left subarray with size n - k is longer than \n                    // the right subarray with size k. Exchange \n                    // nums[n - 2*k,...,n - k - 1] with nums[n - k,...,n - 1].\n                    for (int i = 0; i < k; i++)\n                    {\n                        tmp = nums[start + n - 2*k + i];\n                        nums[start + n - 2*k + i] = nums[start + n - k + i];\n                        nums[start + n - k + i] = tmp;\n                    }\n                    \n                    // nums[n - 2*k,...,n - k - 1] are in their correct positions now.\n                    // Need to rotate the elements of nums[0,...,n - k - 1] to the right \n                    // by k%n steps.\n                    n = n - k;\n                    k = k%n;\n                }\n                else\n                {\n                    // The left subarray with size n - k is shorter than \n                    // the right subarray with size k. Exchange \n                    // nums[0,...,n - k - 1] with nums[n - k,...,2*(n - k) - 1].\n                    for (int i = 0; i < n - k; i++)\n                    {\n                        tmp = nums[start + i];\n                        nums[start + i] = nums[start + n - k + i];\n                        nums[start + n - k + i] = tmp;\n                    }\n                    \n                    // nums[n - k,...,2*(n - k) - 1] are in their correct positions now.\n                    // Need to rotate the elements of nums[n - k,...,n - 1] to the right \n                    // by k - (n - k) steps.\n                    tmp = n - k;\n                    n = k;\n                    k -= tmp;\n                    start += tmp;\n                }\n            }\n        }\n    };
[]