{"id":34186,"date":"2024-11-01T09:25:16","date_gmt":"2024-11-01T09:25:16","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34186"},"modified":"2024-11-01T10:58:16","modified_gmt":"2024-11-01T10:58:16","slug":"c-coding-test-course-arrays-and-lists-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34186\/","title":{"rendered":"C++ Coding Test Course, Arrays and Lists"},"content":{"rendered":"<div class=\"post\">\n<p>Coding tests are an important tool used by many companies today to evaluate job seekers. Today, we will discuss a problem centered around arrays and lists. Arrays and lists are the most basic forms of data structures and are fundamentally used in many algorithm problems. An array stores data in a fixed size of contiguous memory space, while a list can store elements in a dynamic size. Understanding the differences between these two and developing problem-solving skills using them is important.<\/p>\n<h2>Problem: Find the Sum of Two Numbers in an Array<\/h2>\n<p>Find the indices of two numbers in the given integer array that can make a specific sum. The elements of the array can be duplicated, and the two numbers must be selected from different indices.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains the size of the array <code>n<\/code> (1 \u2264 <code>n<\/code> \u2264 10^4).<\/li>\n<li>The second line contains <code>n<\/code> integers of the array <code>a[0], a[1], ..., a[n-1]<\/code> (1 \u2264 <code>a[i]<\/code> \u2264 10^9).<\/li>\n<li>The third line contains the integer <code>target<\/code> that needs to be found.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Output the indices of the two found numbers, separated by a space. If they do not exist, output <code>-1<\/code>.<\/p>\n<h4>Example<\/h4>\n<pre>\n    Input:\n    5\n    2 7 11 15\n    9\n\n    Output:\n    0 1\n    <\/pre>\n<h3>Problem-Solving Strategy<\/h3>\n<p>This problem is a classic example of finding the sum of two numbers in an array and can be solved through the following process:<\/p>\n<ol>\n<li>Using a HashMap:<\/li>\n<p>Use a HashMap to store the array elements and their indices. For each element, calculate <code>target - current_number<\/code> and check if that value is present in the HashMap. If it exists, return that index and the current index. This approach has an average time complexity of O(n).<\/p>\n<\/ol>\n<h3>C++ Code Implementation<\/h3>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;unordered_map&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n\nvector<int> twoSum(vector<int>&amp; nums, int target) {\n    unordered_map<int, int=\"\"> hashMap; \/\/ Declaring HashMap\n    for (int i = 0; i &lt; nums.size(); ++i) {\n        int complement = target - nums[i]; \/\/ Finding needed number\n        if (hashMap.find(complement) != hashMap.end()) {\n            return {hashMap[complement], i}; \/\/ Returning indices\n        }\n        hashMap[nums[i]] = i; \/\/ Storing number and its index\n    }\n    return {-1}; \/\/ If not found\n}\n\nint main() {\n    int n, target;\n    cin &gt;&gt; n;\n    vector<int> nums(n);\n    for (int i = 0; i &lt; n; ++i) {\n        cin &gt;&gt; nums[i]; \/\/ Input array\n    }\n    cin &gt;&gt; target; \/\/ Input target\n\n    vector<int> result = twoSum(nums, target);\n    for (int idx : result) {\n        cout &lt;&lt; idx &lt;&lt; \" \"; \/\/ Output result\n    }\n    return 0;\n}\n<\/int><\/int><\/int,><\/int><\/int><\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>The above code works by taking the given array and target value as input and finding the indices of two numbers. By using <code>unordered_map<\/code> to store each number and its index, it can be solved while traversing the array only once. This ensures a time complexity of O(n) even in the worst case.<\/p>\n<h3>Result Verification<\/h3>\n<p>To verify that the written code works correctly, we will prepare various test cases. For example:<\/p>\n<ul>\n<li>n=5, input array: [2, 7, 11, 15], target: 9 \u2192 result: 0 1<\/li>\n<li>n=4, input array: [3, 2, 4], target: 6 \u2192 result: 1 2<\/li>\n<li>n=3, input array: [3, 3], target: 6 \u2192 result: 0 1<\/li>\n<li>n=2, input array: [1, 2], target: 3 \u2192 result: 0 1<\/li>\n<li>n=1, input array: [2], target: 2 \u2192 result: -1 (no element exists)<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Today, we covered a coding test problem using arrays and lists. The problem of finding the sum of two numbers is not a difficult one, but it is worth practicing given the various approaches available. Using HashMaps is a good example of efficiently reducing time complexity. In the next lesson, we will cover other data structures or algorithm approaches. Keep practicing coding!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Coding tests are an important tool used by many companies today to evaluate job seekers. Today, we will discuss a problem centered around arrays and lists. Arrays and lists are the most basic forms of data structures and are fundamentally used in many algorithm problems. An array stores data in a fixed size of contiguous &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34186\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Arrays and Lists&#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-34186","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, Arrays and Lists - \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\/34186\/\" \/>\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, Arrays and Lists - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Coding tests are an important tool used by many companies today to evaluate job seekers. Today, we will discuss a problem centered around arrays and lists. Arrays and lists are the most basic forms of data structures and are fundamentally used in many algorithm problems. An array stores data in a fixed size of contiguous &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Arrays and Lists&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34186\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:16+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\/34186\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34186\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Arrays and Lists\",\"datePublished\":\"2024-11-01T09:25:16+00:00\",\"dateModified\":\"2024-11-01T10:58:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34186\/\"},\"wordCount\":426,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34186\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34186\/\",\"name\":\"C++ Coding Test Course, Arrays and Lists - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:16+00:00\",\"dateModified\":\"2024-11-01T10:58:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34186\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34186\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34186\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Arrays and Lists\"}]},{\"@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, Arrays and Lists - \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\/34186\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Arrays and Lists - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Coding tests are an important tool used by many companies today to evaluate job seekers. Today, we will discuss a problem centered around arrays and lists. Arrays and lists are the most basic forms of data structures and are fundamentally used in many algorithm problems. An array stores data in a fixed size of contiguous &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Arrays and Lists\"","og_url":"https:\/\/atmokpo.com\/w\/34186\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:16+00:00","article_modified_time":"2024-11-01T10:58:16+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\/34186\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34186\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Arrays and Lists","datePublished":"2024-11-01T09:25:16+00:00","dateModified":"2024-11-01T10:58:16+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34186\/"},"wordCount":426,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34186\/","url":"https:\/\/atmokpo.com\/w\/34186\/","name":"C++ Coding Test Course, Arrays and Lists - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:16+00:00","dateModified":"2024-11-01T10:58:16+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34186\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34186\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34186\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Arrays and Lists"}]},{"@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\/34186","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=34186"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34186\/revisions"}],"predecessor-version":[{"id":34187,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34186\/revisions\/34187"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}