{"id":35102,"date":"2024-11-01T09:35:32","date_gmt":"2024-11-01T09:35:32","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35102"},"modified":"2024-11-01T11:44:59","modified_gmt":"2024-11-01T11:44:59","slug":"course-on-kotlin-coding-test-finding-cichlid-numbers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35102\/","title":{"rendered":"course on Kotlin Coding Test, Finding Cichlid Numbers"},"content":{"rendered":"<p><body><\/p>\n<p>Hello, everyone! In this lecture, we will solve the problem of finding &#8220;binary friends&#8221; using Kotlin. A binary friend is a sequence made up of 0s and 1s that satisfies the condition of alternating &#8216;0&#8217;s and &#8216;1&#8217;s. This problem can be efficiently solved using dynamic programming techniques.<\/p>\n<h2>1. Problem Definition<\/h2>\n<p>The problem of finding binary friends can be defined as follows:<\/p>\n<p>Given an integer <strong>N<\/strong>, find the number of binary friends with length <strong>N<\/strong>.<\/p>\n<h3>Examples<\/h3>\n<ul>\n<li>N = 1: The binary friends are &#8220;0&#8221;, &#8220;1&#8221; \u2192 Count: 2<\/li>\n<li>N = 2: The binary friends are &#8220;00&#8221;, &#8220;01&#8221;, &#8220;10&#8221;, &#8220;11&#8221; (but &#8217;00&#8217; is not a binary friend, so it is excluded) \u2192 Count: 1 (only 01 and 10 are possible)<\/li>\n<li>N = 3: The binary friends are &#8220;001&#8221;, &#8220;010&#8221;, &#8220;100&#8221;, &#8220;101&#8221;, &#8220;110&#8221; \u2192 Count: 5<\/li>\n<\/ul>\n<h2>2. Understanding the Problem<\/h2>\n<p>To solve this problem, it is essential to understand how binary friends are structured. Binary friends have the following conditions:<\/p>\n<ol>\n<li>It cannot start with 0.<\/li>\n<li>0 cannot appear consecutively more than 2 times.<\/li>\n<li>1 cannot appear consecutively more than 2 times.<\/li>\n<\/ol>\n<p>Thus, when the length N of the binary friends is given, if the last digit is 0, the preceding digit must be 1. Conversely, if the last digit is 1, the preceding digit can be either 0 or 1, but the conditions for it to be a binary friend must still be maintained.<\/p>\n<h2>3. Dynamic Programming Approach<\/h2>\n<p>This problem can be efficiently solved through dynamic programming. We will construct an array divided into two states. Here:<\/p>\n<ul>\n<li><strong>dp[i]<\/strong> represents the number of binary friends of length i.<\/li>\n<li><strong>dp0[i]<\/strong> represents the number of binary friends of length i that end with 0.<\/li>\n<li><strong>dp1[i]<\/strong> represents the number of binary friends of length i that end with 1.<\/li>\n<\/ul>\n<h3>Recurrence Relation<\/h3>\n<p>Therefore, we can establish the following recurrence relations:<\/p>\n<ul>\n<li>dp0[i] = dp1[i-1]<\/li>\n<li>dp1[i] = dp0[i-1] + dp1[i-1]<\/li>\n<li>dp[i] = dp0[i] + dp1[i]<\/li>\n<\/ul>\n<h3>Initial Values<\/h3>\n<p>The initial values are as follows:<\/p>\n<ul>\n<li>dp[1] = 2 (binary friends are 0 and 1)<\/li>\n<li>dp0[1] = 1 (case that ends with 1)<\/li>\n<li>dp1[1] = 1 (case that ends with 0)<\/li>\n<\/ul>\n<h2>4. Code Implementation<\/h2>\n<p>Now, let&#8217;s implement the code to find binary friends using Kotlin.<\/p>\n<pre><code>\nfun countBinaryFriends(n: Int): Int {\n    if (n == 1) {\n        return 2\n    }\n    \n    val dp0 = IntArray(n + 1)\n    val dp1 = IntArray(n + 1)\n    \n    dp0[1] = 1\n    dp1[1] = 1\n    \n    for (i in 2..n) {\n        dp0[i] = dp1[i - 1]\n        dp1[i] = dp0[i - 1] + dp1[i - 1]\n    }\n    \n    return dp0[n] + dp1[n]\n}\n\nfun main() {\n    val n = readLine()!!.toInt()\n    println(countBinaryFriends(n))\n}\n    <\/code><\/pre>\n<h2>5. Code Explanation<\/h2>\n<p>The above code defines a function <code>countBinaryFriends<\/code> to find binary friends of length N. It takes an integer N as input and returns the count of binary friends related to it. It first handles the case for length 1, then updates the arrays <code>dp0<\/code> and <code>dp1<\/code> using dynamic programming. Finally, it sums <code>dp0[n]<\/code> and <code>dp1[n]<\/code> to return the result.<\/p>\n<h2>6. Time Complexity<\/h2>\n<p>The time complexity of this algorithm is O(N). It calculates values by traversing the array once, making it efficient. The space complexity is also O(N), allowing dynamic memory management for finding binary friends.<\/p>\n<h2>7. Conclusion<\/h2>\n<p>Through this lecture, we solved the problem of finding binary friends using Kotlin. This problem is a good example for understanding dynamic programming and is one of the frequently asked problems in practical coding tests. If you know the basic principles of dynamic programming and can apply them, it will enhance your ability to approach and solve various problems.<\/p>\n<p>I hope to continue solving various algorithm problems together and improve coding skills. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! In this lecture, we will solve the problem of finding &#8220;binary friends&#8221; using Kotlin. A binary friend is a sequence made up of 0s and 1s that satisfies the condition of alternating &#8216;0&#8217;s and &#8216;1&#8217;s. This problem can be efficiently solved using dynamic programming techniques. 1. Problem Definition The problem of finding binary &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35102\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;course on Kotlin Coding Test, Finding Cichlid Numbers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35102","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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:\/\/atmokpo.com\/w\/35102\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone! In this lecture, we will solve the problem of finding &#8220;binary friends&#8221; using Kotlin. A binary friend is a sequence made up of 0s and 1s that satisfies the condition of alternating &#8216;0&#8217;s and &#8216;1&#8217;s. This problem can be efficiently solved using dynamic programming techniques. 1. Problem Definition The problem of finding binary &hellip; \ub354 \ubcf4\uae30 &quot;course on Kotlin Coding Test, Finding Cichlid Numbers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35102\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:35:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:59+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"course on Kotlin Coding Test, Finding Cichlid Numbers\",\"datePublished\":\"2024-11-01T09:35:32+00:00\",\"dateModified\":\"2024-11-01T11:44:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/\"},\"wordCount\":492,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35102\/\",\"name\":\"course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:35:32+00:00\",\"dateModified\":\"2024-11-01T11:44:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35102\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35102\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"course on Kotlin Coding Test, Finding Cichlid Numbers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/35102\/","og_locale":"ko_KR","og_type":"article","og_title":"course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, everyone! In this lecture, we will solve the problem of finding &#8220;binary friends&#8221; using Kotlin. A binary friend is a sequence made up of 0s and 1s that satisfies the condition of alternating &#8216;0&#8217;s and &#8216;1&#8217;s. This problem can be efficiently solved using dynamic programming techniques. 1. Problem Definition The problem of finding binary &hellip; \ub354 \ubcf4\uae30 \"course on Kotlin Coding Test, Finding Cichlid Numbers\"","og_url":"https:\/\/atmokpo.com\/w\/35102\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:35:32+00:00","article_modified_time":"2024-11-01T11:44:59+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35102\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35102\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"course on Kotlin Coding Test, Finding Cichlid Numbers","datePublished":"2024-11-01T09:35:32+00:00","dateModified":"2024-11-01T11:44:59+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35102\/"},"wordCount":492,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35102\/","url":"https:\/\/atmokpo.com\/w\/35102\/","name":"course on Kotlin Coding Test, Finding Cichlid Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:35:32+00:00","dateModified":"2024-11-01T11:44:59+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35102\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35102\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35102\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"course on Kotlin Coding Test, Finding Cichlid Numbers"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35102"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35102\/revisions"}],"predecessor-version":[{"id":35103,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35102\/revisions\/35103"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}