{"id":3514,"date":"2017-12-31T23:34:45","date_gmt":"2017-12-31T18:04:45","guid":{"rendered":"http:\/\/theoryofprogramming.azurewebsites.net\/?p=3514"},"modified":"2023-12-09T13:00:04","modified_gmt":"2023-12-09T13:00:04","slug":"rotate-matrix-clockwise","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2017\/12\/31\/rotate-matrix-clockwise\/","title":{"rendered":"Rotate matrix clockwise"},"content":{"rendered":"\n<p><strong>Problem statement<\/strong> &#8211; Given an array of N rows and N columns (square matrix), rotate the matrix by 90\u00b0 in clockwise direction.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"443\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=846%2C443&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3522\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?w=846&amp;ssl=1 846w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=768%2C402&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=230%2C120&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=350%2C183&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-1.jpg?resize=480%2C251&amp;ssl=1 480w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Solution &#8211; This is an implementation based problem, which means that when asked in an interview, the interviewer is mainly testing your skill to write a program which follows some set of rules. There are no tricky cases here but you might struggle in the interview if you don&#8217;t have the right approach.<\/p>\n\n\n\n<p>For this problem, let us define a cycle like this &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"699\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?resize=548%2C699&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3515\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?w=548&amp;ssl=1 548w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?resize=235%2C300&amp;ssl=1 235w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?resize=230%2C293&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?resize=350%2C446&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-2.jpg?resize=480%2C612&amp;ssl=1 480w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>So the cycle is a ring of elements which consists of mirroring row and column. We will solve this problem cycle-by-cycle, which means, we will rotate the 0<sup>th<\/sup> cycle, then the 1<sup>st<\/sup> cycle and so on.<\/p>\n\n\n\n<p>Now our rotation will start from the upper left corner element. This element&#8217;s vertices (i, j) can be easily evaluated from the cycle number it is in.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"699\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?resize=541%2C699&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3516\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?w=541&amp;ssl=1 541w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?resize=232%2C300&amp;ssl=1 232w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?resize=230%2C297&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?resize=350%2C452&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-3.jpg?resize=480%2C620&amp;ssl=1 480w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>So, if the upper left corner element of a cycle is in the cycle number <em>c<\/em>, then its position in the matrix will be <em>(c, c)<\/em>. Now that we have defined one corner of our cycle, let us find the others. If <em>n<\/em> is the size of the matrix, can you find the indexes of other corners?<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"493\" height=\"522\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?resize=493%2C522&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3517\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?w=493&amp;ssl=1 493w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?resize=283%2C300&amp;ssl=1 283w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?resize=230%2C244&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?resize=350%2C371&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-4.jpg?resize=480%2C508&amp;ssl=1 480w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Okay so <em>n &#8211; 1 &#8211; c<\/em> seems to be an important term, let us call it <em>l<\/em> (like last index).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"512\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?resize=435%2C512&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3518\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?w=435&amp;ssl=1 435w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?resize=255%2C300&amp;ssl=1 255w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?resize=230%2C271&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-5.jpg?resize=350%2C412&amp;ssl=1 350w\" sizes=\"auto, (max-width: 435px) 100vw, 435px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Now to rotate these values, we need to do &#8211;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/44554764da427545bb8299883afe84d4.js\"><\/script>\n\n\n\n<p>Now, if we go to the next element of the ring &#8211;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"530\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?resize=633%2C530&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3519\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?w=633&amp;ssl=1 633w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?resize=300%2C251&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?resize=230%2C193&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?resize=350%2C293&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-6.jpg?resize=480%2C402&amp;ssl=1 480w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>To rotate these values, we need to do &#8211; <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/f8ae12d99c13584a5b1dbbbfb2821147.js\"><\/script>\n\n\n\n<p>Similarly, for the next item in the ring.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"532\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?resize=630%2C532&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3520\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?resize=300%2C253&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?resize=230%2C194&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?resize=350%2C296&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-7.jpg?resize=480%2C405&amp;ssl=1 480w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>We see that the indices vary by 0, 1, 2, etc. This can be generalized into a loop variable, say <em>i<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"556\" height=\"621\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?resize=556%2C621&#038;ssl=1\" alt=\"Rotate matrix Theory of Programming\" class=\"wp-image-3521\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?w=556&amp;ssl=1 556w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?resize=269%2C300&amp;ssl=1 269w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?resize=230%2C257&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?resize=350%2C391&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2017\/12\/rotate-8.jpg?resize=480%2C536&amp;ssl=1 480w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>So, for any matrix, number of cycles <em>c<\/em> will be from [0, &#8230; n \/ 2]. If you think about it even number sized matrices have n \/ 2 cycles. Odd number sized matrices have n \/ 2 + 1 cycles, but the inner-most cycle would be a single integer which doesn&#8217;t need to be touched.<\/p>\n\n\n\n<p>Value of <em>i<\/em> will be from [0 &#8230; , <em>l &#8211; c<\/em>). Why? Because we need to increment <em>i<\/em>, until <em>c + i &lt; l<\/em>. So <em>i &lt; l &#8211; c<\/em>.<\/p>\n\n\n\n<p>So, you have two loops and inside them, we need to write those 5 statements which make the rotation. Simple, isn&#8217;t it? Try to code it, you can refer to my code below if you get stuck.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/6038205af56c6301a9ae6da842d0f8c1.js\"><\/script>\n\n\n\n<p>Keep practicing! Happy Coding! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem statement &#8211; Given an array of N rows and N columns (square matrix), rotate the matrix by 90\u00b0 in clockwise direction. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[16],"tags":[22,61],"class_list":["post-3514","post","type-post","status-publish","format-standard","hentry","category-array-interview-questions","tag-arrays-interview-questions","tag-interview-questions"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/3514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=3514"}],"version-history":[{"count":3,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/3514\/revisions"}],"predecessor-version":[{"id":3910,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/3514\/revisions\/3910"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=3514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=3514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=3514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}