{"id":26867,"date":"2022-12-20T12:03:41","date_gmt":"2022-12-20T06:33:41","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/"},"modified":"2022-12-20T12:03:41","modified_gmt":"2022-12-20T06:33:41","slug":"solved-how-to-find-contigious-elements-in-a-2d-array-using-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/","title":{"rendered":"[Solved] How to find contigious elements in a 2D array using C"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-23522299\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"23522299\" data-parentid=\"23521660\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You have a 2D array. The usual way to traverse it, is be double for loop.<br \/>\nThe usual order will be this:<br \/>\n1st row &#8211; 1st column<br \/>\n1st row &#8211; 2nd column<br \/>\n..<br \/>\n1st row &#8211; last column<br \/>\n2nd row &#8211; 1st column<br \/>\n&#8230;<br \/>\nlast row &#8211; last column<\/p>\n<p>Assume you are using the order described above, you may want to do something like this:<\/p>\n<p>Every time you find a land bit, explore the array at the right, upper, right-upper, right-bottom and at the bottom of this position (the other directions are visited before this step) and find the max length of contigius zeros.<\/p>\n<p>Here is very naive example:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n\nint main() {\n  const int N = 4;\n  const int M = 5;\n\n  int a[4][5] =\n  {\n    { 1, 0, 0, 0, 0},\n    { 0, 0, 1, 1, 0},\n    { 0, 1, 0, 0, 0},\n    { 0, 0, 0, 0, 0}\n  };\n\n  \/* A naive implementation, which can optimized.\n   * Some initializations are not needed.\n   *\/\n\n  int i, j, k, z, len;\n  int max_len = -1;\n\n  \/\/ traverse the array in the order described before\n  for(i = 0 ; i &lt; N ; ++i)\n  {\n    for(j = 0 ; j &lt; M ; ++j)\n    {\n      \/* Land bit found! Explore the array in the needed directions. *\/\n      if(a[i][j] == 0) {\n\n        \/\/ remember where we start from\n        k = i;\n        z = j;\n        len = 0;\n        \/\/ search right\n        \/\/ while we are inside the limits of the array\n        \/\/ (here we need to check only the columns, since\n        \/\/ we move to the right)\n        while(z &lt; M) {\n          \/\/ and we find land bits\n          if(a[k][z++] == 0)\n            len++; \/\/ increase the current length\n          else     \/\/ we want contiguous land bits\n            break; \/\/ we found a water bit, so break this loop\n        }\n\n        \/\/ is the current length better than the current found?\n        if(len &gt; max_len)\n          max_len = len; \/\/ yes, so update max_len\n\n\n        \/\/ return back to initial cell we started from\n        k = i;\n        z = j;\n        len = 0;\n        \/\/ search down\n        while(k &lt; N) {\n          if(a[k++][z] == 0)\n            len++;\n          else\n            break;\n        }\n        if(len &gt; max_len)\n          max_len = len;\n\n        k = i;\n        z = j;\n        len = 0;\n        \/\/ search right and down\n        while(k &lt; N &amp;&amp; z &lt; M) {\n          if(a[k++][z++] == 0)\n            len++;\n          else\n            break;\n        }\n        if(len &gt; max_len)\n          max_len = len;\n\n        k = i;\n        z = j;\n        len = 0;\n        \/\/ search upper\n        while(k &gt;= 0) {\n          if(a[k--][z] == 0)\n            len++;\n          else\n            break;\n        }\n        if(len &gt; max_len)\n          max_len = len;\n\n        k = i;\n        z = j;\n        len = 0;\n        \/\/ search upper and right\n        while(k &gt;= 0 &amp;&amp; z &lt; M) {\n          if(a[k++][z++] == 0)\n            len++;\n          else\n            break;\n        }\n        if(len &gt; max_len)\n          max_len = len;\n      }\n    }\n  }\n\n  printf(\"max_length = %d\\n\", max_len);\n\n  return 0;\n}\n<\/code><\/pre>\n<p>After understanding the naive approach, try to see where your approach misses. Use a paper and try with smaller examples. This site is not just for debugging. \ud83d\ude42<\/p>\n<p>[EDIT]<\/p>\n<p>As Floris mentioned, this example assumes that the land is a straight line, in any direction.<\/p>\n<p>Based on the answer of Floris, in order to get it work for the U shaped land, you need to modify your <code>countC<\/code> like this:<\/p>\n<pre><code>else\n{\n    input2[x][y]=2;\n    return 1+countC(input2,x-1,y,r1,c1)+\n        countC(input2,x+1,y,r1,c1)+\n        countC(input2,x,y-1,r1,c1)+\n        countC(input2,x,y+1,r1,c1)+\n        countC(input2,x+1,y+1,r1,c1)+\n        countC(input2,x+1,y-1,r1,c1)+\n        countC(input2,x-1,y+1,r1,c1)+\n        countC(input2,x-1,y-1,r1,c1);\n}\n<\/code><\/pre>\n<p>as you can see, you would need to go into diagonals too.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">13<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to find contigious elements in a 2D array using C <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You have a 2D array. The usual way to traverse it, is be double for loop. The usual order will be this: 1st row &#8211; 1st column 1st row &#8211; 2nd column .. 1st row &#8211; last column 2nd row &#8211; 1st column &#8230; last row &#8211; last column Assume you are using the &#8230; <a title=\"[Solved] How to find contigious elements in a 2D array using C\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/\" aria-label=\"More on [Solved] How to find contigious elements in a 2D array using C\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,324],"class_list":["post-26867","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How to find contigious elements in a 2D array using C - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to find contigious elements in a 2D array using C - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You have a 2D array. The usual way to traverse it, is be double for loop. The usual order will be this: 1st row &#8211; 1st column 1st row &#8211; 2nd column .. 1st row &#8211; last column 2nd row &#8211; 1st column &#8230; last row &#8211; last column Assume you are using the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-20T06:33:41+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to find contigious elements in a 2D array using C\",\"datePublished\":\"2022-12-20T06:33:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/\"},\"wordCount\":213,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"arrays\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/\",\"name\":\"[Solved] How to find contigious elements in a 2D array using C - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-12-20T06:33:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to find contigious elements in a 2D array using C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to find contigious elements in a 2D array using C - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to find contigious elements in a 2D array using C - JassWeb","og_description":"[ad_1] You have a 2D array. The usual way to traverse it, is be double for loop. The usual order will be this: 1st row &#8211; 1st column 1st row &#8211; 2nd column .. 1st row &#8211; last column 2nd row &#8211; 1st column &#8230; last row &#8211; last column Assume you are using the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/","og_site_name":"JassWeb","article_published_time":"2022-12-20T06:33:41+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to find contigious elements in a 2D array using C","datePublished":"2022-12-20T06:33:41+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/"},"wordCount":213,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/","name":"[Solved] How to find contigious elements in a 2D array using C - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-20T06:33:41+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-find-contigious-elements-in-a-2d-array-using-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to find contigious elements in a 2D array using C"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26867"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26867\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}