{"id":56,"date":"2025-05-09T13:02:14","date_gmt":"2025-05-09T07:32:14","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=56"},"modified":"2025-05-09T13:10:20","modified_gmt":"2025-05-09T07:40:20","slug":"rotate-an-array-by-k-places","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/","title":{"rendered":"Right Rotate an Array by K places | Explained with Examples"},"content":{"rendered":"\n<p>In this article we&#8217;ll guide you through the Python program to Right Rotate an Array by K places <strong>(in-place)<\/strong> [<strong><a href=\"https:\/\/leetcode.com\/problems\/rotate-array\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>]. We will see <strong>brute force<\/strong>, <strong>better<\/strong> and <strong>optimal<\/strong> solution to Right Rotate an Array by K places and dry run with images to understand the solution properly. So let&#8217;s begin.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-26bbc3a1-df41-4445-9643-1bd2019a2505\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">hide<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#0-understand-the-problem\" style=\"\">Understand the problem<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#1-what-does-rotation-mean-\" style=\"\">What Does Rotation Mean?<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#2-examples-to-understand-rotation\" style=\"\">Examples to Understand Rotation<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#3-example-1-\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#4-example-2-\" style=\"\">Example 2<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#5-example-3-large-k-value-\" style=\"\">Example 3 (Large k value)<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#7-1-brute-force-solution-using-pop-and-insert\" style=\"\">1. Brute Force Solution (Using Pop and Insert)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#9-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#10-code-explanation-stey-by-step\" style=\"\">Code Explanation (Stey by Step)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#11-dry-run-with-images\" style=\"\">Dry Run (with images)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#12-edge-cases-to-consider\" style=\"\">Edge cases to consider<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#13-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#14-2-better-solution-using-slicing\" style=\"\">2. Better Solution (Using Slicing)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#15-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#16-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#17-code-explanation-step-by-step\" style=\"\">Code Explanation (Step by Step)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#18-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#19-edge-cases-to-consider\" style=\"\">Edge cases to consider<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#20-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#21-3-optimal-solution-using-while-loop-and-reversing-array\" style=\"\">3. Optimal Solution (Using While Loop and Reversing Array)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#22-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#23-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#24-code-explanation-step-by-step\" style=\"\">Code Explanation (Step by Step)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#25-dry-run-with-images\" style=\"\">Dry Run (with images)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#26-edge-cases-to-consider\" style=\"\">Edge cases to consider<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rotate-an-array-by-k-places\/#27-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-understand-the-problem\">Understand the problem<\/h2>\n\n\n\n<p>The problem is asking us to <strong>rotate an array to the right<\/strong> by <code>k<\/code> positions. This means that each element in the array will <strong>shift<\/strong> to the right, and the last <code>k<\/code> elements will move behind the beginning element.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-what-does-rotation-mean-\"><strong>What Does Rotation Mean?<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If <code>k = 1<\/code>, the last element moves to the first position.<\/li>\n\n\n\n<li>If <code>k = 2<\/code>, the last two elements move to the front, shifting everything else.<\/li>\n\n\n\n<li>If <code>k<\/code> is greater than the array length, we rotate it as if <code>k % n<\/code> times (where <code>n<\/code> is the length of the array).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-examples-to-understand-rotation\">Examples to Understand Rotation<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-example-1-\"><strong>Example 1<\/strong><\/h4>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [1, 2, 3, 4, 5, 6, 7]\nk = 3\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">k = <\/span><span style=\"color: #B5CEA8\">3<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Step-by-Step Rotation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Move last 3 elements <code>[5,6,7]<\/code> to the front.<\/li>\n\n\n\n<li>Shift the remaining <code>[1,2,3,4]<\/code> to the right.<\/li>\n<\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [5, 6, 7, 1, 2, 3, 4]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-example-2-\"><strong>Example 2<\/strong><\/h4>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [-1, -100, 3, 99]\nk = 2\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">99<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">k = <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Step-by-Step Rotation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Move last 2 elements <code>[3, 99]<\/code> to the front.<\/li>\n\n\n\n<li>Shift the remaining <code>[-1, -100]<\/code> to the right.<\/li>\n<\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [3, 99, -1, -100]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">99<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-example-3-large-k-value-\"><strong>Example 3 (Large <code>k<\/code> value)<\/strong><\/h4>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [10, 20, 30, 40, 50, 60, 70, 80]\nk = 10\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">70<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">k = <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Since <code>k &gt; n<\/code> (array length is 8), we take <code>k % n = 10 % 8 = 2<\/code>.<\/strong><br>So, we rotate by <strong>2 places<\/strong> instead of 10.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nums = [70, 80, 10, 20, 30, 40, 50, 60]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">nums = [<\/span><span style=\"color: #B5CEA8\">70<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-constraints\">Constraints:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>-2<sup>31<\/sup> &lt;= nums[i] &lt;= 2<sup>31<\/sup> - 1<\/code><\/li>\n\n\n\n<li><code>0 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read about the Python Program to <strong><a href=\"https:\/\/codeanddebugacademy.com\/move-zeros-to-end-of-array\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Move Zeros to End of the Array<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-1-brute-force-solution-using-pop-and-insert\">1. Brute Force Solution (Using Pop and Insert)<\/h2>\n\n\n\n<p>This solution will contain <strong>pop<\/strong> method which removes the last element from the array and then use <strong>insert<\/strong> method to put the element at first. Doing so, we rotated our array by <strong>1 time<\/strong>. Let&#8217;s see multiple rotations can lead to our answer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Given that rotating an array of length <strong>n<\/strong> by <strong>n<\/strong> times (or a <strong>multiple of n<\/strong>) results in the array looking the same as the original, the actual number of effective rotations needed is <strong>k % len(nums)<\/strong>. This approach ensures the minimal number of steps required to achieve our result, optimizing performance when <strong>k is larger than len(nums)<\/strong>.<\/p>\n\n\n\n<p>The logic we will apply is pretty simple:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Determine the number of necessary rotations using <strong>k % len(nums)<\/strong>.<\/li>\n\n\n\n<li>For each rotation, remove the last element of the array (<strong>nums.pop()<\/strong>) and insert it at the beginning (<strong>nums.insert(0, last)<\/strong>). This simulates a single right rotation.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def rotate(self, nums: List[int], k: int) -&gt; None:\n        &quot;&quot;&quot;\n        Do not return anything, modify nums in-place instead.\n        &quot;&quot;&quot;\n        # Becuase if k = 8 and length = 7, means\n        # we should only rotate 1 time instead of 8 times\n        rotations = k % len(nums)\n\n        for _ in range(rotations):\n            last = nums.pop()\n            nums.insert(0, last)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">rotate<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        Do not return anything, modify nums in-place instead.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Becuase if k = 8 and length = 7, means<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># we should only rotate 1 time instead of 8 times<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        rotations = k % <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(rotations):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            last = nums.pop()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            nums.insert(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, last)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation-stey-by-step\">Code Explanation (Stey by Step)<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Calculating Actual Rotations Needed:<\/strong>\n<ol class=\"wp-block-list\">\n<li>The <strong>rotations<\/strong> variable is assigned the value of<strong> k % len(nums)<\/strong> to ensure only the necessary number of rotations are performed.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Rotation Loop:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The loop runs <strong>rotations<\/strong> times. Each iteration performs one right rotation.<\/li>\n\n\n\n<li><strong>last = nums.pop()<\/strong>: Removes the last element of the array and stores it in <strong>last<\/strong>.<\/li>\n\n\n\n<li><strong>nums.insert(0, last)<\/strong>: Inserts the <strong>last <\/strong>element at the start of the array, thus rotating the array right by one position.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run-with-images\">Dry Run (with images)<\/h3>\n\n\n\n<p>Let&#8217;s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-1024x576.png\" alt=\"Dry of Right rotate an array by K places using a brute force solution\" class=\"wp-image-57\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-K-places-brute-force-solution.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-edge-cases-to-consider\">Edge cases to consider<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>k Equal to Zero:<\/strong> If <em>k is 0<\/em>, the array should remain unchanged. Our code handles this naturally since rotations would be 0, and no loop iterations would occur.<\/li>\n\n\n\n<li><strong>k Greater than Array Length:<\/strong> As shown in the example, our code recalculates <strong>k<\/strong> to ensure only necessary rotations are done.<\/li>\n\n\n\n<li><strong>Empty Array:<\/strong> If nums is empty, our code should handle it properly, performing no actions.<\/li>\n\n\n\n<li><strong>Array of Length One:<\/strong> An array with one element remains unchanged regardless of <strong>k<\/strong>, which our code will handle correctly since popping and inserting the same element does not alter the array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> The complexity is <strong>O(k\u00d7n)<\/strong>, where <strong>k<\/strong> is the number of rotations (after modulo operation) and <strong>n<\/strong> is the number of elements in the array. Each rotation involves a pop operation <strong>(O(1))<\/strong> and an insert operation at the beginning of the array <strong>(O(n))<\/strong>, making it inefficient for large arrays or high values of k.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> The space complexity is <strong>O(1)<\/strong> since no additional space is used and it does not depend on the size of the array.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Join our FREE dsa course<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-2-better-solution-using-slicing\">2. Better Solution (Using Slicing)<\/h2>\n\n\n\n<p>This solution will contain <strong>slice<\/strong> method which is available in python to achieve our rotation and get our answer. You need to have a basic understanding of how slicing works in Python to understand the solution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>The approach used here takes advantage of slicing in Python to rearrange segments of the array rather than manually moving elements. The core idea involves:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Modifying <strong>k <\/strong>to be within the range of the array\u2019s length using the modulus operation <strong>(k %= n)<\/strong>. This handles cases where <strong>k <\/strong>is greater than the length of the array, reducing unnecessary full rotations.<\/li>\n\n\n\n<li>Slicing the array into two segments:\n<ul class=\"wp-block-list\">\n<li>The last <strong>k<\/strong> elements: <strong>nums[n-k:]<\/strong><\/li>\n\n\n\n<li>The first <strong>n-k<\/strong> elements: <strong>nums[:n-k]<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Concatenating these two list\/array in reverse order to achieve the desired right rotation.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def rotate(self, nums: List[int], k: int) -&gt; None:\n        &quot;&quot;&quot;\n        Do not return anything, modify nums in-place instead.\n        &quot;&quot;&quot;\n        n = len(nums)\n        k %= n\n\n        # Rotate the list in-place\n        nums[:] = nums[n - k :] + nums[: n - k]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">rotate<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        Do not return anything, modify nums in-place instead.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k %= n<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Rotate the list in-place<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        nums[:] = nums[n - k :] + nums[: n - k]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-code-explanation-step-by-step\">Code Explanation (Step by Step)<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>n<\/strong> is set to the length of the array <strong>nums<\/strong>.<\/li>\n\n\n\n<li><strong>k<\/strong> is recalculated as <strong>k % n <\/strong>to ensure there isn&#8217;t any unnecessary rotations.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Rotation via Slicing:<\/strong>\n<ol class=\"wp-block-list\">\n<li>The array nums is updated by slicing and concatenating:\n<ul class=\"wp-block-list\">\n<li><strong>nums[n-k:]<\/strong> captures the <strong>last k<\/strong> elements which will move to the front of the array.<\/li>\n\n\n\n<li><strong>nums[:n-k]<\/strong> captures the <strong>remaining<\/strong> elements which will shift rightwards.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>These slices are concatenated to form the new order of elements in <strong>nums<\/strong>.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let\u2019s use the input <strong>nums = [1, 2, 3, 4, 5]<\/strong> and <strong>k = 3 <\/strong>as an example:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initial State: <strong>nums = [1, 2, 3, 4, 5]<\/strong>, <strong>n = 5<\/strong>, <strong>k = 3<\/strong><\/li>\n\n\n\n<li>Calculate Effective Rotations:\n<ul class=\"wp-block-list\">\n<li><strong>k = 3 % 5 = 3<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Rotate Operation:\n<ul class=\"wp-block-list\">\n<li>The last <strong>k<\/strong> elements are <strong>nums[5-3:5]<\/strong> = <strong>nums[2:5]<\/strong> = <strong>[3, 4, 5]<\/strong>.<\/li>\n\n\n\n<li>The first <strong>n-k <\/strong>elements are <strong>nums[0:5-3]<\/strong> = <strong>nums[0:2]<\/strong> = <strong>[1, 2]<\/strong>.<\/li>\n\n\n\n<li>Concatenation results in <strong>nums[:]<\/strong> = <strong>[3, 4, 5]<\/strong> + <strong>[1, 2]<\/strong> = <strong>[3, 4, 5, 1, 2]<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Final State: <strong><em>nums = [3, 4, 5, 1, 2]<\/em><\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-edge-cases-to-consider\">Edge cases to consider<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>k Equal to Zero:<\/strong> If <strong>k<\/strong> is <strong>0<\/strong>, the array remains unchanged. The method adjusts for this since <strong>nums[n-k:]<\/strong> and <strong>nums[:n-k]<\/strong> simply makes the original array.<\/li>\n\n\n\n<li><strong>k Greater than Array Length:<\/strong> The method correctly recalculates <strong>k<\/strong> to <strong>fit<\/strong> within the array bounds, preventing unnecessary rotations.<\/li>\n\n\n\n<li><strong>Empty Array:<\/strong> If <strong>nums <\/strong>is empty, our code handles this by not performing any actions since slicing an empty array remains empty.<\/li>\n\n\n\n<li><strong>Array of Length One:<\/strong> An array with one element remains unchanged regardless of <strong>k<\/strong>, which our code handles correctly as slicing and concatenating a single element array does not change it.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> The complexity is <strong>O(n)<\/strong> due to the slicing operations, where each slice operation handles all elements.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> The complexity is <strong>O(n)<\/strong> as the slicing operation creates new slices which are then used to overwrite the original array in-place.<\/li>\n<\/ul>\n\n\n\n<p><strong>Efficiency:<\/strong> This method is more efficient than manually rotating each element or using additional arrays. It takes advantage of Python\u2019s powerful slicing capabilities to perform the rotation cleanly and efficiently. However, space usage could be a concern due to the temporary slices, although the operation itself is performed in-place.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our advance python with dsa course<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-3-optimal-solution-using-while-loop-and-reversing-array\">3. Optimal Solution (Using While Loop and Reversing Array)<\/h2>\n\n\n\n<p>Before jumping on to this solution, please refer <a href=\"https:\/\/codeanddebugacademy.com\/reverse-array-using-recursion-and-while-loop\/\" target=\"_blank\" rel=\"noopener\"><\/a><em><strong><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">How to Reverse an Array using Loops<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>The approach we will apply uses reversals of array or a part of array to achieve the rotation without extra space for another array. The process is as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Normalize <strong>k<\/strong> to be less than the length of the array using <strong>k %= n<\/strong>. This handles cases where <strong>k<\/strong> is larger than the array size, reducing rotations.<\/li>\n\n\n\n<li>Reverse the <strong>last k <\/strong>elements of the array. This positions these elements as they should appear after a complete rotation.<\/li>\n\n\n\n<li>Reverse the <strong>first n-k<\/strong> elements. This prepares the front part of the array for a reversal.<\/li>\n\n\n\n<li>Reverse the <strong>entire array <\/strong>to finalize the positions of the elements for the right rotation.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def reverse(self, nums: List[int], l: int, r: int) -&gt; None:\n        &quot;&quot;&quot;\n        Reverse the portion of nums starting at index l up to index r in-place.\n        &quot;&quot;&quot;\n        while l &lt; r:\n            nums[l], nums[r] = nums[r], nums[l]\n            l += 1\n            r -= 1\n\n    def rotate(self, nums: List[int], k: int) -&gt; None:\n        &quot;&quot;&quot;\n        Do not return anything, modify nums in-place instead.\n        &quot;&quot;&quot;\n        n = len(nums)\n        k %= n  # Handle cases where k might be larger than n\n\n        # 1. Reverse the last k elements\n        self.reverse(nums, n - k, n - 1)\n\n        # 2. Reverse the first n-k elements\n        self.reverse(nums, 0, n - k - 1)\n\n        # 3. Reverse the entire array\n        self.reverse(nums, 0, n - 1)\n\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">reverse<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        Reverse the portion of nums starting at index l up to index r in-place.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> l &lt; r:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            nums[l], nums[r] = nums[r], nums[l]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            l += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            r -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">rotate<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        Do not return anything, modify nums in-place instead.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #CE9178\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        k %= n  <\/span><span style=\"color: #6A9955\"># Handle cases where k might be larger than n<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 1. Reverse the last k elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverse(nums, n - k, n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 2. Reverse the first n-k elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverse(nums, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n - k - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 3. Reverse the entire array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.reverse(nums, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"24-code-explanation-step-by-step\">Code Explanation (Step by Step)<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Helper Function &#8211; Reverse:<\/strong>\n<ol class=\"wp-block-list\">\n<li>A helper function <strong>reverse(l, r)<\/strong> is defined to reverse the elements of nums between indices <strong>l <\/strong>and <strong>r<\/strong>, inclusive. This function swaps elements from the outermost until it reaches the middle of the specified segment.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Initialization and Normalization:<\/strong>\n<ol class=\"wp-block-list\">\n<li><strong>n <\/strong>is set to the length of nums.<\/li>\n\n\n\n<li><strong>k<\/strong> is recalculated as <strong>k % n<\/strong> to ensure rotations are within bounds.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Rotation Steps:<\/strong>\n<ol class=\"wp-block-list\">\n<li><strong>reverse(n &#8211; k, n &#8211; 1)<\/strong>: Reverse the segment of the array that contains the last <strong>k<\/strong> elements.<\/li>\n\n\n\n<li><strong>reverse(0, n &#8211; k &#8211; 1)<\/strong>: Reverse the segment of the array before the last <strong>k<\/strong> elements.<\/li>\n\n\n\n<li><strong>reverse(0, n &#8211; 1)<\/strong>: Finally, reverse the entire array to align all elements correctly after the rotation.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"25-dry-run-with-images\">Dry Run (with images)<\/h3>\n\n\n\n<p>Let&#8217;s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-1024x576.png\" alt=\"A dry run of right rotate an array by K places optimally by reversing arrays\" class=\"wp-image-58\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-place-optimal-solution.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"26-edge-cases-to-consider\">Edge cases to consider<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>k Equal to Zero:<\/strong> If <strong>k is 0<\/strong>, the array remains unchanged. Our code handles this as the reversals will effectively leave the array as is.<\/li>\n\n\n\n<li><strong>k Greater than Array Length:<\/strong> Our code correctly recalculates <strong>k <\/strong>to fit within the array bounds, ensuring efficient rotations.<\/li>\n\n\n\n<li><strong>Empty Array:<\/strong> If nums is empty, our code avoids any operations, preserving the empty state.<\/li>\n\n\n\n<li><strong>Array of Length One:<\/strong> An array with one element remains unchanged regardless of <strong>k<\/strong>, which our code correctly handles since the operations do not change a single-element array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"27-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> The complexity is <strong>O(n)<\/strong> because each element is touched a constant number of times across the three reversal operations.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> The complexity is <strong>O(1)<\/strong> as the solution modifies the array in place and does not use additional space proportional to the input size.<\/li>\n<\/ul>\n\n\n\n<p><strong>Efficiency:<\/strong> This method is efficient in terms of both time and space. By using reversals, it avoids the need for extra storage and minimizes the number of operations, making it suitable for large datasets. The reversals ensure that each element is moved directly to its final position, optimizing the process compared to other methods that might involve multiple shifts or rotations.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our advance python with dsa course<\/a><\/div>\n<\/div>\n\n\n\n<p><em>For any changes to the document, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll guide you through the Python program to Right Rotate an Array by K places (in-place) [Problem Link]. We will see brute force, better and optimal solution to Right Rotate an Array by K places and dry run with images to understand the solution properly. So let&#8217;s begin. Understand the problem The<\/p>\n","protected":false},"author":1,"featured_media":60,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,8],"class_list":{"0":"post-56","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-easy"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/right-rotate-an-array-by-k-places-featured-image.jpg","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/96"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/60"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}