C++ || Easy to understand || Multiple Approaches
Runtime: 16 ms, faster than 21.15% of C++ online submissions for Rotate Array.\nMemory Usage: 10 MB, less than 54.57% of C++ online submissions for Rotate Array
***Runtime: 16 ms, faster than 21.15% of C++ online submissions for Rotate Array.\nMemory Usage: 10 MB, less than 54.57% of C++ online submissions for Rotate Array.***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        k %= nums.size();\n        vector<int> result(nums.end()-k,nums.end());\n        result.insert(result.end(),nums.begin(),nums.end()-k);\n        nums = result;\n    }\n};\n```\n***Time Limit Exceeded***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        k %= nums.size();\n        while(k--){\n            int temp = nums[nums.size()-1];\n            for(int i = nums.size()-2; i>= 0 ;i--){\n                nums[i+1] = nums[i];\n            }\n            nums[0] = temp;\n        }   \n    }\n};\n```\n***Runtime: 28 ms, faster than 11.54% of C++ online submissions for Rotate Array.\nMemory Usage: 10 MB, less than 50.27% of C++ online submissions for Rotate Array.***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int orig_size = nums.size();\n        k %= orig_size;\n        nums.insert(nums.end(),nums.end()-k,nums.end());\n        int i=orig_size - k - 1, j = orig_size - 1;\n        while(j >= 0){\n            nums[j--] = nums[i--];\n            if(i == -1){\n                i = nums.size()-1;\n            }\n        }\n        nums.resize(orig_size, 0);\n    }\n};\n```\n***Runtime: 8 ms, faster than 83.90% of C++ online submissions for Rotate Array.\nMemory Usage: 10.1 MB, less than 21.86% of C++ online submissions for Rotate Array.***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int sz = nums.size();\n        vector<int> result(sz,0);\n        for(int i=0;i<sz;i++){\n            result[(i + k)%sz] = nums[i];\n        }\n        nums = result;\n    }\n};\n```\n***Runtime: 12 ms, faster than 37.86% of C++ online submissions for Rotate Array.\nMemory Usage: 10 MB, less than 47.04% of C++ online submissions for Rotate Array.***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        k %= nums.size();\n        reverse(nums.begin(),nums.end());\n        reverse(nums.begin(),nums.begin()+k);\n        reverse(nums.begin()+k,nums.end());\n    }\n};\n```\n***Runtime: 8 ms, faster than 84.24% of C++ online submissions for Rotate Array.\nMemory Usage: 10.1 MB, less than 32.86% of C++ online submissions for Rotate Array.***\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int n = nums.size();\n        k %= n;\n        for(int i=0;i<gcd(n,k);i++){\n            int prev = i, next = (prev+k)%n;\n            int temp = nums[i];\n            while(next != i){\n                int temp2 = nums[next];\n                nums[next] = temp;\n                temp = temp2;\n                prev = next;\n                next = (prev+k)%n;\n            }\n            nums[next] = temp;\n        }\n    }\n};\n```
['C', 'C++']