{"id":34306,"date":"2024-11-01T09:26:39","date_gmt":"2024-11-01T09:26:39","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34306"},"modified":"2024-11-01T10:57:46","modified_gmt":"2024-11-01T10:57:46","slug":"c-coding-test-course-representing-sets-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34306\/","title":{"rendered":"C++ Coding Test Course, Representing Sets"},"content":{"rendered":"<p><body><\/p>\n<p>We will explore algorithm problems related to set representation commonly used in coding tests. Understanding various data structures and algorithms is essential for effectively representing and manipulating sets. In this article, we will present an algorithm problem that involves representing sets and performing operations, and we will solve it step by step.<\/p>\n<h2>Problem: Intersection of Two Sets<\/h2>\n<p>This problem involves finding the set of common elements from the two given sets of numbers. This is one of the basic set operations and can be easily solved using C++ STL.<\/p>\n<h3>Problem Definition<\/h3>\n<pre><code>\nInput:\n - Two integers N, M (1 \u2264 N, M \u2264 100,000): the number of elements in the first set N, the number of elements in the second set M\n - Elements of the first set: N integers a1, a2, ..., aN\n - Elements of the second set: M integers b1, b2, ..., bM\n\nOutput:\n - Print the elements of the intersection of the two sets in ascending order.\n    <\/code><\/pre>\n<h3>Example<\/h3>\n<pre><code>\nInput:\n5 4\n1 2 3 4 5\n3 4 5 6\n\nOutput:\n3 4 5\n    <\/code><\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>Step 1: Problem Analysis<\/h3>\n<p>To find the intersection of the two given sets, we need to compare the elements of the two sets to find the common elements. For this, we can use a hash set data structure, and C++ STL provides <code>std::unordered_set<\/code> that we can utilize.<\/p>\n<h3>Step 2: Algorithm Design<\/h3>\n<p>The algorithm can be designed as follows.<\/p>\n<ol>\n<li>Take input for the two sets.<\/li>\n<li>Store the elements of the first set in a hash set.<\/li>\n<li>Iterate through each element of the second set and check if it exists in the hash set of the first set.<\/li>\n<li>If it exists, add it to the list of intersection elements.<\/li>\n<li>Sort the results in ascending order and print them.<\/li>\n<\/ol>\n<h3>Step 3: C++ Code Implementation<\/h3>\n<p>Let&#8217;s write the C++ code based on the above algorithm.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;unordered_set&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin &gt;&gt; N &gt;&gt; M;\n\n    unordered_set<int> setA;\n    vector<int> intersection;\n\n    \/\/ Input first set\n    for (int i = 0; i &lt; N; i++) {\n        int a;\n        cin &gt;&gt; a;\n        setA.insert(a);\n    }\n\n    \/\/ Input second set and find intersection\n    for (int i = 0; i &lt; M; i++) {\n        int b;\n        cin &gt;&gt; b;\n        \/\/ If b exists in setA, add to intersection\n        if (setA.find(b) != setA.end()) {\n            intersection.push_back(b);\n        }\n    }\n\n    \/\/ Sort intersection elements\n    sort(intersection.begin(), intersection.end());\n\n    \/\/ Print result\n    for (int num : intersection) {\n        cout &lt;&lt; num &lt;&lt; \" \";\n    }\n    cout &lt;&lt; endl;\n\n    return 0;\n}\n    <\/int><\/int><\/code><\/pre>\n<h3>Step 4: Code Explanation<\/h3>\n<p>This code performs the following tasks:<\/p>\n<ul>\n<li><code>unordered_set<int> setA;<\/int><\/code>: Declares an unordered set to store the elements of the first set.<\/li>\n<li><code>vector<int> intersection;<\/int><\/code>: Declares a vector to store the intersection elements.<\/li>\n<li>Receives input for the elements of the first set and adds them to the hash set using <code>setA.insert(a);<\/code>.<\/li>\n<li>Iterates through each element of the second set and checks if it exists in <code>setA.find(b)<\/code>, and if it does, adds it to the intersection array.<\/li>\n<li>Finally, sorts the results and prints them.<\/li>\n<\/ul>\n<h3>Step 5: Complexity Analysis<\/h3>\n<p>From a time complexity standpoint, the process of adding elements of the first set to the hash set takes O(N), and the process of searching the hash set for each element of the second set takes O(M). Therefore, the overall time complexity of the algorithm is O(N + M). The memory complexity is O(N + min(N, M)), considering the memory used by the hash set and the intersection storage.<\/p>\n<h3>Summary and Conclusion<\/h3>\n<p>In this post, we explored the problem of representing sets and calculating their intersection. We were able to implement it easily using C++ STL, and we confirmed that the algorithm&#8217;s time complexity is efficient. To effectively solve set-related problems this way, it is essential to choose the right data structure.<\/p>\n<p>For further learning, I recommend studying various practice problems and other operations on sets (such as union, difference, etc.). Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will explore algorithm problems related to set representation commonly used in coding tests. Understanding various data structures and algorithms is essential for effectively representing and manipulating sets. In this article, we will present an algorithm problem that involves representing sets and performing operations, and we will solve it step by step. Problem: Intersection of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34306\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Representing Sets&#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-34306","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, Representing Sets - \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\/34306\/\" \/>\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, Representing Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"We will explore algorithm problems related to set representation commonly used in coding tests. Understanding various data structures and algorithms is essential for effectively representing and manipulating sets. In this article, we will present an algorithm problem that involves representing sets and performing operations, and we will solve it step by step. Problem: Intersection of &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Representing Sets&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34306\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:46+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\/34306\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34306\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Representing Sets\",\"datePublished\":\"2024-11-01T09:26:39+00:00\",\"dateModified\":\"2024-11-01T10:57:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34306\/\"},\"wordCount\":456,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34306\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34306\/\",\"name\":\"C++ Coding Test Course, Representing Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:39+00:00\",\"dateModified\":\"2024-11-01T10:57:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34306\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34306\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34306\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Representing Sets\"}]},{\"@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, Representing Sets - \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\/34306\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Representing Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"We will explore algorithm problems related to set representation commonly used in coding tests. Understanding various data structures and algorithms is essential for effectively representing and manipulating sets. In this article, we will present an algorithm problem that involves representing sets and performing operations, and we will solve it step by step. Problem: Intersection of &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Representing Sets\"","og_url":"https:\/\/atmokpo.com\/w\/34306\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:39+00:00","article_modified_time":"2024-11-01T10:57:46+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\/34306\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34306\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Representing Sets","datePublished":"2024-11-01T09:26:39+00:00","dateModified":"2024-11-01T10:57:46+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34306\/"},"wordCount":456,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34306\/","url":"https:\/\/atmokpo.com\/w\/34306\/","name":"C++ Coding Test Course, Representing Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:39+00:00","dateModified":"2024-11-01T10:57:46+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34306\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34306\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34306\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Representing Sets"}]},{"@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\/34306","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=34306"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34306\/revisions"}],"predecessor-version":[{"id":34307,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34306\/revisions\/34307"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}