{"id":34284,"date":"2024-11-01T09:26:22","date_gmt":"2024-11-01T09:26:22","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34284"},"modified":"2024-11-01T10:57:51","modified_gmt":"2024-11-01T10:57:51","slug":"c-coding-test-course-finding-incomplete-numbers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34284\/","title":{"rendered":"C++ Coding Test Course, Finding Incomplete Numbers"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>This article will explain how to find binary friends (binary numbers without consecutive 1s) using C++. This problem will serve as a good practice to enhance algorithm problem-solving skills.<\/p>\n<\/header>\n<section>\n<h2>Problem Description<\/h2>\n<p>A binary friend refers to a binary number composed of 0s and 1s that does not have consecutive 1s. For example, 0, 1, 10, 100, 101 are binary friends. However, 11, 111, and 1100 are not. The problem is to find how many binary friends of n digits exist for a given natural number n.<\/p>\n<h3>Input<\/h3>\n<p>The first line contains a natural number n (1 \u2264 n \u2264 45).<\/p>\n<h3>Output<\/h3>\n<p>Output the count of binary friends with n digits.<\/p>\n<\/section>\n<section>\n<h2>Approach and Solution Method<\/h2>\n<p>To solve this problem, we will utilize the properties of the Fibonacci sequence. The ways to create a binary friend can be divided as follows:<\/p>\n<ul>\n<li>If the leftmost bit of the n-digit binary friend is 0, the remaining bits become (n-1)-digit binary friends.<\/li>\n<li>If the leftmost bit of the n-digit binary friend is 1, the next bit must be 0, thus the remaining bits will become (n-2)-digit binary friends.<\/li>\n<\/ul>\n<p>Therefore, the count of binary friends can be defined as follows:<\/p>\n<pre><code>f(n) = f(n-1) + f(n-2)<\/code><\/pre>\n<p>Here, f(n) represents the count of n-digit binary friends. The initial values can be set as follows:<\/p>\n<ul>\n<li>f(1) = 2 (0, 1)<\/li>\n<li>f(2) = 3 (00, 01, 10)<\/li>\n<\/ul>\n<p>Thus, to find n binary friends, we can utilize the method of calculating the Fibonacci sequence. Now, let&#8217;s implement the actual code in C++.<\/p>\n<\/section>\n<section>\n<h2>Code Implementation<\/h2>\n<pre><code>\n#include &lt;iostream&gt;\n\nusing namespace std;\n\nlong long findBinaryFriend(int n) {\n    long long *dp = new long long[n + 1];\n    dp[1] = 2; \/\/ 1-digit binary friend\n    dp[2] = 3; \/\/ 2-digit binary friend\n\n    for (int i = 3; i &lt;= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n\n    return dp[n];\n}\n\nint main() {\n    int n;\n    cout &lt;&lt; \"Enter a natural number n (1\u2264n\u226445): \";\n    cin &gt;&gt; n;\n\n    if (n &gt;= 1 &amp;&amp; n &lt;= 45) {\n        cout &lt;&lt; n &lt;&lt; \" digit binary friend's count is: \" &lt;&lt; findBinaryFriend(n) &lt;&lt; \".\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"Invalid input.\" &lt;&lt; endl;\n    }\n\n    return 0;\n}\n            <\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>The above code is a program that calculates the count of n-digit binary friends for a given n. First, it creates a dynamic array and allocates memory setting the initial values. Then, it proceeds from 3 to n, finding the binary friends for each digit by summing the previous two terms.<\/p>\n<p>Finally, in the main function, it takes input from the user for n and, under the appropriate conditions, outputs the count of binary friends. Do not forget to free the allocated array for memory management in any case.<\/p>\n<\/section>\n<section>\n<h2>Testing and Validation<\/h2>\n<p>It is important to test the code after writing it. Check the results for the following input values:<\/p>\n<ul>\n<li>n = 1: Result is 2 (0, 1)<\/li>\n<li>n = 2: Result is 3 (00, 01, 10)<\/li>\n<li>n = 3: Result is 5 (000, 001, 010, 100, 101)<\/li>\n<li>n = 4: Result is 8<\/li>\n<li>n = 5: Result is 13<\/li>\n<\/ul>\n<p>You should confirm that the correct results are output for such inputs. It is advisable to verify by manually counting binary friends for each n value.<\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>In this class, we learned how to solve the binary friend problem using C++. This problem is a good example of the Fibonacci sequence and dynamic programming. You can develop your algorithm problem-solving skills through this problem, so practice with various input values.<\/p>\n<\/section>\n<footer>\n<p>I hope this article helps you. If you want to learn more algorithms, check out other courses as well.<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will explain how to find binary friends (binary numbers without consecutive 1s) using C++. This problem will serve as a good practice to enhance algorithm problem-solving skills. Problem Description A binary friend refers to a binary number composed of 0s and 1s that does not have consecutive 1s. For example, 0, 1, 10, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34284\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding Incomplete 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":[111],"tags":[],"class_list":["post-34284","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Finding Incomplete 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\/34284\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Finding Incomplete Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This article will explain how to find binary friends (binary numbers without consecutive 1s) using C++. This problem will serve as a good practice to enhance algorithm problem-solving skills. Problem Description A binary friend refers to a binary number composed of 0s and 1s that does not have consecutive 1s. For example, 0, 1, 10, &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding Incomplete Numbers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34284\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:51+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\/34284\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34284\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding Incomplete Numbers\",\"datePublished\":\"2024-11-01T09:26:22+00:00\",\"dateModified\":\"2024-11-01T10:57:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34284\/\"},\"wordCount\":450,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34284\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34284\/\",\"name\":\"C++ Coding Test Course, Finding Incomplete Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:22+00:00\",\"dateModified\":\"2024-11-01T10:57:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34284\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34284\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34284\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding Incomplete 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":"C++ Coding Test Course, Finding Incomplete 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\/34284\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding Incomplete Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This article will explain how to find binary friends (binary numbers without consecutive 1s) using C++. This problem will serve as a good practice to enhance algorithm problem-solving skills. Problem Description A binary friend refers to a binary number composed of 0s and 1s that does not have consecutive 1s. For example, 0, 1, 10, &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding Incomplete Numbers\"","og_url":"https:\/\/atmokpo.com\/w\/34284\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:22+00:00","article_modified_time":"2024-11-01T10:57:51+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\/34284\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34284\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding Incomplete Numbers","datePublished":"2024-11-01T09:26:22+00:00","dateModified":"2024-11-01T10:57:51+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34284\/"},"wordCount":450,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34284\/","url":"https:\/\/atmokpo.com\/w\/34284\/","name":"C++ Coding Test Course, Finding Incomplete Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:22+00:00","dateModified":"2024-11-01T10:57:51+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34284\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34284\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34284\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding Incomplete 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\/34284","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=34284"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34284\/revisions"}],"predecessor-version":[{"id":34285,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34284\/revisions\/34285"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}