{"id":34280,"date":"2024-11-01T09:26:20","date_gmt":"2024-11-01T09:26:20","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34280"},"modified":"2024-11-01T10:57:52","modified_gmt":"2024-11-01T10:57:52","slug":"c-coding-test-course-binary-search-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34280\/","title":{"rendered":"C++ Coding Test Course, Binary Search"},"content":{"rendered":"<p><body><\/p>\n<p>Binary Search is an efficient method for finding a specific value in a sorted array. This algorithm has a time complexity of O(log n) because it repeatedly divides the array in half to find the target value. In this lecture, we will cover the basic concepts of binary search, implementation methods, and practical exercises using this technique.<\/p>\n<h2>Basic Principle of Binary Search<\/h2>\n<p>Binary search operates according to the following procedure:<\/p>\n<ol>\n<li>Check the central element of the array.<\/li>\n<li>Compare the central element with the target value:<\/li>\n<ul>\n<li>If the target value is less than the central element, search in the left half of the array.<\/li>\n<li>If the target value is greater than the central element, search in the right half of the array.<\/li>\n<li>If the target value is equal to the central element, the search is complete.<\/li>\n<\/ul>\n<\/ol>\n<p>This process is repeated until the target value is found.<\/p>\n<h2>Problem: Finding a Specific Value in a Sorted Array<\/h2>\n<p>Given an integer array <code>arr<\/code> and an integer <code>target<\/code>, write a program that returns the index of <code>target<\/code>. If <code>target<\/code> is not found, it should return <code>-1<\/code>.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains the size of the array <code>n<\/code>. (1 \u2264 n \u2264 10<sup>5<\/sup>)<\/li>\n<li>The second line contains the sorted array <code>arr<\/code> in ascending order.<\/li>\n<li>The third line contains the number <code>target<\/code> to be found.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Print the index of the target. (An integer between 0 and n-1)<\/p>\n<h4>Example<\/h4>\n<pre><code>Input:\n    5\n    1 3 5 7 9\n    5\n\n    Output:\n    2<\/code><\/pre>\n<h2>Solution Process<\/h2>\n<h3>Step 1: Understanding the Problem<\/h3>\n<p>First, it is important to accurately understand the problem. Since we need to quickly find a specific element in a sorted array, we should use binary search.<\/p>\n<h3>Step 2: Implementing the Binary Search Algorithm<\/h3>\n<p>Let&#8217;s implement the binary search algorithm in C++.<\/p>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n\nint binarySearch(const vector&lt;int&gt; &amp;arr, int target) {\n    int left = 0;\n    int right = arr.size() - 1;\n\n    while (left &lt;= right) {\n        int mid = left + (right - left) \/ 2;\n\n        if (arr[mid] == target) {\n            return mid;  \/\/ Found the target.\n        } else if (arr[mid] &lt; target) {\n            left = mid + 1;  \/\/ Move to the right half.\n        } else {\n            right = mid - 1;  \/\/ Move to the left half.\n        }\n    }\n    return -1;  \/\/ Target not found.\n}\n\nint main() {\n    int n;\n    cout &lt;&lt; \"Enter the size of the array: \";\n    cin &gt;&gt; n;\n    \n    vector&lt;int&gt; arr(n);\n    cout &lt;&lt; \"Enter the elements of the array: \";\n    for (int i = 0; i &lt; n; i++) {\n        cin &gt;&gt; arr[i];\n    }\n    \n    int target;\n    cout &lt;&lt; \"Enter the number you want to find: \";\n    cin &gt;&gt; target;\n\n    int result = binarySearch(arr, target);\n    if (result != -1) {\n        cout &lt;&lt; \"Index of the target: \" &lt;&lt; result &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"Target not found.\" &lt;&lt; endl;\n    }\n\n    return 0;\n}<\/code><\/pre>\n<h3>Step 3: Code Explanation<\/h3>\n<p>\n    &#8211; The <code>binarySearch<\/code> function takes an integer vector and a target value as input and returns the index of the target value.<br \/>\n    &#8211; <code>left<\/code> and <code>right<\/code> represent the start and end indices of the search interval.<br \/>\n    &#8211; The <code>while<\/code> loop runs while <code>left<\/code> is less than or equal to <code>right<\/code>.<br \/>\n    &#8211; In each iteration, the central index <code>mid<\/code> is computed, and the central element is compared with the target value to determine the next search interval.\n    <\/p>\n<h3>Step 4: Testing and Verification<\/h3>\n<p>After writing the code, it is essential to run it against various test cases to ensure it returns the correct results. Consider the following tests:<\/p>\n<ul>\n<li>When the target value exists in the array<\/li>\n<li>When the target value does not exist in the array<\/li>\n<li>When the array size is 1<\/li>\n<li>When there are duplicate elements<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Binary search is a very useful algorithm, especially as it exhibits excellent performance in terms of speed when working with sorted arrays. In this lecture, we understood the basic principles of binary search and implemented actual code. Mastering binary search will greatly help in solving many other algorithm problems.<\/p>\n<p>In the next lecture, we will cover variations of binary search and various applications. I encourage you to enhance your algorithm skills through continuous learning!<\/p>\n<footer>\n<p>\u00a9 2023 Algorithm Course | C++ Binary Search<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Binary Search is an efficient method for finding a specific value in a sorted array. This algorithm has a time complexity of O(log n) because it repeatedly divides the array in half to find the target value. In this lecture, we will cover the basic concepts of binary search, implementation methods, and practical exercises using &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34280\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Binary Search&#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-34280","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, Binary Search - \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\/34280\/\" \/>\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, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Binary Search is an efficient method for finding a specific value in a sorted array. This algorithm has a time complexity of O(log n) because it repeatedly divides the array in half to find the target value. In this lecture, we will cover the basic concepts of binary search, implementation methods, and practical exercises using &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Binary Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34280\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:52+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\/34280\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34280\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Binary Search\",\"datePublished\":\"2024-11-01T09:26:20+00:00\",\"dateModified\":\"2024-11-01T10:57:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34280\/\"},\"wordCount\":485,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34280\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34280\/\",\"name\":\"C++ Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:20+00:00\",\"dateModified\":\"2024-11-01T10:57:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34280\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34280\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34280\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Binary Search\"}]},{\"@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, Binary Search - \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\/34280\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Binary Search is an efficient method for finding a specific value in a sorted array. This algorithm has a time complexity of O(log n) because it repeatedly divides the array in half to find the target value. In this lecture, we will cover the basic concepts of binary search, implementation methods, and practical exercises using &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Binary Search\"","og_url":"https:\/\/atmokpo.com\/w\/34280\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:20+00:00","article_modified_time":"2024-11-01T10:57:52+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\/34280\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34280\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Binary Search","datePublished":"2024-11-01T09:26:20+00:00","dateModified":"2024-11-01T10:57:52+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34280\/"},"wordCount":485,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34280\/","url":"https:\/\/atmokpo.com\/w\/34280\/","name":"C++ Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:20+00:00","dateModified":"2024-11-01T10:57:52+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34280\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34280\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34280\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Binary Search"}]},{"@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\/34280","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=34280"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34280\/revisions"}],"predecessor-version":[{"id":34281,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34280\/revisions\/34281"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}