{"id":4349,"date":"2022-04-17T16:15:45","date_gmt":"2022-04-17T16:15:45","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4349"},"modified":"2022-04-17T16:15:46","modified_gmt":"2022-04-17T16:15:46","slug":"rotate-an-array","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/04\/17\/rotate-an-array\/","title":{"rendered":"Rotate an Array"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>We see how to rotate elements in an array both right and left a certain number of times. We use the mod operator to cut down the number of iterations specified. For eg.if there are 5 elements in an array and we are required to rotate it  5 times then it is the same as not rotating it at all. Likewise if we need to rotate it 6 times then it is the same as rotating it just once.<\/p>\n\n\n\n<p><strong>SAMPLE CODE TO ROTATE RIGHT<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace Algorithms2\n{\n    internal class Program\n    {\n        static void Main(string&#x5B;] args)\n        {\n            int&#x5B;] nums = { 5, 6, 7, 8, 9, 10, 11, 12, 13 };\n            int rotate = 2;\n            Console.WriteLine(&quot;&quot;);\n            foreach(int num in nums)\n            {\n                Console.Write(num + &quot;,&quot;);\n            }\n            Console.WriteLine(&quot; rotate &quot; + rotate + &quot; times.&quot;);\n\n            int&#x5B;] nums2 = rotateRight(nums, rotate);\n            Console.WriteLine(&quot;&quot;);\n            foreach (int num in nums2)\n            {\n                Console.Write(num + &quot;,&quot;);\n            }\n            Console.WriteLine(&quot;&quot;);\n        }\n\n        static int&#x5B;] rotateRight(int&#x5B;] nums, int k)\n        {\n\n            int maxElems = nums.Length;\n            if (maxElems &lt; 2)\n                return nums;\n            k = k % maxElems;\n            for (int steps = 0; steps &lt; k; steps++)\n            {\n                int temp = nums&#x5B;maxElems - 1];\n\n                for (int i = maxElems - 2; i &gt;= 0; i--)\n                {\n                    nums&#x5B;i + 1] = nums&#x5B;i];\n                }\n                nums&#x5B;0] = temp;\n            }\n\n            return nums;\n        }\n\n  \n    }\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5,6,7,8,9,10,11,12,13, rotate 2 times.\n 12,13,5,6,7,8,9,10,11,\n Press any key to continue . . .<\/pre>\n\n\n\n<p> <strong>SAMPLE CODE TO ROTATE LEFT<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace Algorithms2\n{\n    internal class Program\n    {\n        static void Main(string&#x5B;] args)\n        {\n            int&#x5B;] nums = { 5, 6, 7, 8, 9, 10, 11, 12, 13 };\n            int rotate = 2;\n            Console.WriteLine(&quot;&quot;);\n            foreach(int num in nums)\n            {\n                Console.Write(num + &quot;,&quot;);\n            }\n            Console.WriteLine(&quot; rotate &quot; + rotate + &quot; times.&quot;);\n\n            int&#x5B;] nums2 = rotateLeft(nums, rotate);\n            Console.WriteLine(&quot;&quot;);\n            foreach (int num in nums2)\n            {\n                Console.Write(num + &quot;,&quot;);\n            }\n            Console.WriteLine(&quot;&quot;);\n        }\n\n      \n        static int&#x5B;] rotateLeft(int&#x5B;] nums, int k)\n        {\n\n            int maxElems = nums.Length;\n            if (maxElems &lt; 2)\n                return nums;\n            k = k % maxElems;\n            for (int steps = 0; steps &lt; k; steps++)\n            {\n                int temp = nums&#x5B;0];\n\n                for (int i = 1; i &lt;= maxElems-1; i++)\n                {\n                    nums&#x5B;i-1] = nums&#x5B;i];\n                }\n                nums&#x5B;maxElems-1] = temp;\n            }\n\n            return nums;\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5,6,7,8,9,10,11,12,13, rotate 2 times.\n 7,8,9,10,11,12,13,5,6,\n Press any key to continue . . .<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We see how to rotate elements in an array both right and left a certain number of times. We use the mod operator to <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/04\/17\/rotate-an-array\/\" title=\"Rotate an Array\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[362,366],"tags":[],"class_list":["post-4349","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-computer-science"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4349"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4349\/revisions"}],"predecessor-version":[{"id":4351,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4349\/revisions\/4351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4308"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}