{"id":28186,"date":"2017-10-15T14:27:37","date_gmt":"2017-10-15T08:57:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28186"},"modified":"2017-10-15T14:27:37","modified_gmt":"2017-10-15T08:57:37","slug":"matrix-zigzag-or-diagonal-traversal-of-matrix","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/matrix-zigzag-or-diagonal-traversal-of-matrix\/","title":{"rendered":"Matrix-Zigzag (or diagonal) traversal of Matrix"},"content":{"rendered":"<p>Given a 2D matrix, print all elements of the given matrix in diagonal order. For example, consider the following 5 X 4 input matrix.<span id=\"more-115773\"><\/span><\/p>\n<pre>1     2     3     4\r\n    5     6     7     8\r\n    9    10    11    12\r\n   13    14    15    16\r\n   17    18    19    20<\/pre>\n<p>Diagonal printing of the above matrix is<\/p>\n<pre> 1\r\n    5     2\r\n    9     6     3\r\n   13    10     7     4\r\n   17    14    11     8\r\n   18    15    12\r\n   19    16\r\n   20<\/pre>\n<p>Following is C++ code for diagonal printing<\/p>\n<p>The diagonal printing of a given matrix \u2018matrix[ROW][COL]\u2019 always has \u2018ROW + COL \u2013 1\u2019 lines in output<\/p>\n[ad type=\u201dbanner\u201d]\n<div><strong>\u00a0C Programming<\/strong><\/div>\n<div>\n<div id=\"highlighter_464822\" class=\"syntaxhighlighter nogutter c\">\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%23define%20ROW%205%0A%23define%20COL%204%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20min%20of%20two%20integers%0Aint%20min(int%20a%2C%20int%20b)%0A%7B%20return%20(a%20%3C%20b)%3F%20a%3A%20b%3B%20%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20min%20of%20three%20integers%0Aint%20min(int%20a%2C%20int%20b%2C%20int%20c)%0A%7B%20return%20min(min(a%2C%20b)%2C%20c)%3B%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20max%20of%20two%20integers%0Aint%20max(int%20a%2C%20int%20b)%0A%7B%20return%20(a%20%3E%20b)%3F%20a%3A%20b%3B%20%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20prints%20given%20matrix%20in%20diagonal%20order%0Avoid%20diagonalOrder(int%20matrix%5B%5D%5BCOL%5D)%0A%7B%0A%20%20%20%20%2F%2F%20There%20will%20be%20ROW%2BCOL-1%20lines%20in%20the%20output%0A%20%20%20%20for%20(int%20line%3D1%3B%20line%3C%3D(ROW%20%2B%20COL%20-1)%3B%20line%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Get%20column%20index%20of%20the%20first%20element%20in%20this%20line%20of%20output.%0A%20%20%20%20%20%20%20%20%20%20%20The%20index%20is%200%20for%20first%20ROW%20lines%20and%20line%20-%20ROW%20for%20remaining%0A%20%20%20%20%20%20%20%20%20%20%20lines%20%20*%2F%0A%20%20%20%20%20%20%20%20int%20start_col%20%3D%20%20max(0%2C%20line-ROW)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Get%20count%20of%20elements%20in%20this%20line.%20The%20count%20of%20elements%20is%0A%20%20%20%20%20%20%20%20%20%20%20equal%20to%20minimum%20of%20line%20number%2C%20COL-start_col%20and%20ROW%20*%2F%0A%20%20%20%20%20%20%20%20%20int%20count%20%3D%20min(line%2C%20(COL-start_col)%2C%20ROW)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Print%20elements%20of%20this%20line%20*%2F%0A%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3Ccount%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%255d%20%22%2C%20matrix%5Bmin(ROW%2C%20line)-j-1%5D%5Bstart_col%2Bj%5D)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Ptint%20elements%20of%20next%20diagonal%20on%20next%20line%20*%2F%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20print%20a%20matrix%0Avoid%20printMatrix(int%20matrix%5BROW%5D%5BCOL%5D)%0A%7B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3C%20ROW%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3CCOL%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%255d%20%22%2C%20matrix%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20M%5BROW%5D%5BCOL%5D%20%3D%20%7B%7B1%2C%202%2C%203%2C%204%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B5%2C%206%2C%207%2C%208%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B9%2C%2010%2C%2011%2C%2012%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B13%2C%2014%2C%2015%2C%2016%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B17%2C%2018%2C%2019%2C%2020%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20printf%20(%22Given%20matrix%20is%20%5Cn%22)%3B%0A%20%20%20%20printMatrix(M)%3B%0A%20%0A%20%20%20%20printf%20(%22%5CnDiagonal%20printing%20of%20matrix%20is%20%5Cn%22)%3B%0A%20%20%20%20diagonalOrder(M)%3B%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Given matrix is\r\n    1     2     3     4\r\n    5     6     7     8\r\n    9    10    11    12\r\n   13    14    15    16\r\n   17    18    19    20\r\n\r\nDiagonal printing of matrix is\r\n    1\r\n    5     2\r\n    9     6     3\r\n   13    10     7     4\r\n   17    14    11     8\r\n   18    15    12\r\n   19    16\r\n   20<\/pre>\n<\/div>\n<\/div>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Matrix-Zigzag (or diagonal) traversal of Matrix &#8211; Matrix &#8211; Given a 2D matrix, print all elements of the given matrix in diagonal order. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,82291],"tags":[83493,83498,83499,83496,83494,83488,83487,83489,83492,83495,83497,83486,83491,81932,83500,83490],"class_list":["post-28186","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-matrix","tag-diagonal-traversal-of-matrix-in-java","tag-matrix-traversal-algorithm","tag-matrix-traversal-in-c","tag-matrix-traversal-problems","tag-matrix-zigzag-traversal-leetcode","tag-print-diagonal-elements-of-matrix","tag-print-matrix-diagonally-java","tag-print-matrix-diagonally-leetcode","tag-print-matrix-diagonally-python","tag-print-matrix-in-zigzag-order-in-java","tag-special-matrix-traversal","tag-traverse-matrix-diagonally-java","tag-zig-zag-matrix-code-in-c","tag-zigzag-array-in-java","tag-zigzag-matrix-in-c","tag-zigzag-matrix-traversal"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=28186"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28186\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}