{"id":34474,"date":"2024-11-01T09:28:25","date_gmt":"2024-11-01T09:28:25","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34474"},"modified":"2024-11-01T11:41:07","modified_gmt":"2024-11-01T11:41:07","slug":"javascript-coding-test-course-two-pointers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34474\/","title":{"rendered":"JavaScript Coding Test Course, Two Pointers"},"content":{"rendered":"<p><body><\/p>\n<p>Let&#8217;s take a closer look at the Two Pointer technique, which is a common algorithm that appears in coding tests. This course will explain the basic concept of the Two Pointer method and solve actual problems using it.<\/p>\n<h2>1. What is Two Pointer?<\/h2>\n<p>The Two Pointer technique is an algorithmic method that efficiently processes data in data structures like arrays or lists by using two pointers. Generally, it is effective in reducing unnecessary loops and improving time complexity as the size of the problem increases.<\/p>\n<ul>\n<li><strong>Efficiency:<\/strong> It often reduces the time complexity to O(N).<\/li>\n<li><strong>Simplicity:<\/strong> The code becomes simpler and more readable.<\/li>\n<li><strong>Application Scope:<\/strong> It can be used in various situations such as sorted arrays, strings, and subarray problems.<\/li>\n<\/ul>\n<h2>2. Basic Idea of Two Pointer<\/h2>\n<p>Two pointers are generally used in two ways:<\/p>\n<ul>\n<li><strong>Left and Right Pointers:<\/strong> Start from both ends of an array and move towards the center to find elements that satisfy the condition.<\/li>\n<li><strong>Moving in the Same Direction:<\/strong> Both pointers move in the same direction while examining surrounding elements until a specific condition is met.<\/li>\n<\/ul>\n<h2>3. Actual Problem: Two Sum<\/h2>\n<p>Here is an actual problem using the Two Pointer technique.<\/p>\n<h3>Problem Description<\/h3>\n<p>Given a sorted array <code>numbers<\/code> and a target sum <code>target<\/code>, write a function that returns the indices of the two numbers such that they add up to the target sum. Assume that each input has exactly one solution, and you cannot use the same element twice.<\/p>\n<h3>Input Format<\/h3>\n<pre><code>numbers: [2, 7, 11, 15]\ntarget: 9<\/code><\/pre>\n<h3>Output Format<\/h3>\n<pre><code>[0, 1]<\/code><\/pre>\n<h3>Example Explanation<\/h3>\n<p>In the above example, since <code>2 + 7 = 9<\/code>, the output is the indices <code>0<\/code> and <code>1<\/code>.<\/p>\n<h2>4. Problem Solving Process<\/h2>\n<p>Let&#8217;s solve this problem using the Two Pointer method. Proceed with the following steps:<\/p>\n<h3>Step 1: Initialize Pointers<\/h3>\n<p>Initialize pointers at the start and end of the array. Name the left pointer as <code>left<\/code> and the right pointer as <code>right<\/code>.<\/p>\n<h3>Step 2: Check Conditions<\/h3>\n<p>Use a while loop to repeat until the two pointers cross each other. In each iteration, calculate the sum of the two numbers pointed to by the current pointers and check if this sum equals <code>target<\/code>.<\/p>\n<h3>Step 3: Compare Sum<\/h3>\n<ul>\n<li>If the sum is less than <code>target<\/code>, move the <code>left<\/code> pointer one step to the right.<\/li>\n<li>If the sum is greater than <code>target<\/code>, move the <code>right<\/code> pointer one step to the left.<\/li>\n<li>If the sum is equal to <code>target<\/code>, return the two indices.<\/li>\n<\/ul>\n<h3>Step 4: Code Implementation<\/h3>\n<pre><code>function twoSum(numbers, target) {\n        let left = 0; \n        let right = numbers.length - 1;\n\n        while (left &lt; right) {\n            const sum = numbers[left] + numbers[right];\n\n            if (sum === target) {\n                return [left, right]; \n            } else if (sum &lt; target) {\n                left++; \n            } else {\n                right--; \n            }\n        }\n        return []; \/\/ In case there is no answer\n    }<\/code><\/pre>\n<h3>Step 5: Code Analysis<\/h3>\n<p>The time complexity of this code is O(N), and the space complexity is O(1). That means it can solve the problem without storing the array additionally.<\/p>\n<h2>5. Additional Examples and Variations<\/h2>\n<p>Now, let&#8217;s look at other variation problems. This can be applied to finding all combinations of three numbers that add up to <code>target<\/code> from the given array.<\/p>\n<h3>Problem Description<\/h3>\n<p>Given an integer array <code>numbers<\/code> and an integer <code>target<\/code>, return the indices of all unique combinations of three numbers that sum up to <code>target<\/code>.<\/p>\n<h3>Input Format<\/h3>\n<pre><code>numbers: [1, 2, 3, 4, 5]\ntarget: 9<\/code><\/pre>\n<h3>Output Format<\/h3>\n<pre><code>[[0, 3, 4], [1, 2, 4], [2, 3, 4]]<\/code><\/pre>\n<h3>Solution Approach<\/h3>\n<p>To solve this problem, we can add a second pointer to find combinations without duplicates. The modified algorithm is as follows:<\/p>\n<pre><code>function threeSum(numbers, target) {\n        let result = [];\n        numbers.sort((a, b) =&gt; a - b); \/\/ Sort the array\n\n        for (let i = 0; i &lt; numbers.length - 2; i++) {\n            let left = i + 1;\n            let right = numbers.length - 1;\n\n            while (left &lt; right) {\n                const sum = numbers[i] + numbers[left] + numbers[right];\n\n                if (sum === target) {\n                    result.push([i, left, right]);\n                    left++;\n                    right--;\n                    while (left &lt; right &amp;&amp; numbers[left] === numbers[left - 1]) left++; \/\/ Remove duplicates\n                    while (left &lt; right &amp;&amp; numbers[right] === numbers[right + 1]) right--; \/\/ Remove duplicates\n                } else if (sum &lt; target) {\n                    left++;\n                } else {\n                    right--;\n                }\n            }\n        }\n        return result;\n    }<\/code><\/pre>\n<h2>6. Conclusion<\/h2>\n<p>The Two Pointer technique is a very useful method for processing arrays or lists. It can significantly improve performance, especially when dealing with sorted data. Through the content covered in this course, I hope you gain an understanding of the basic concepts and applications of Two Pointers, and help you solve real-world problems.<\/p>\n<p>Continue practicing various situations where you can use Two Pointers in search and combination problems, and confidently apply them in actual coding interviews.<\/p>\n<h2>Practice Problems<\/h2>\n<p>Try the following problems:<\/p>\n<ul>\n<li>Given an integer array <code>numbers<\/code> and an integer <code>target<\/code>, return the combination of indices where the sum of the two closest numbers is equal to <code>target<\/code>.<\/li>\n<li>A problem that returns the length of a substring consisting of unique characters within a string.<\/li>\n<\/ul>\n<p>By solving problems like these, you can enhance your understanding of the Two Pointer technique and gain experience in solving various problems.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s take a closer look at the Two Pointer technique, which is a common algorithm that appears in coding tests. This course will explain the basic concept of the Two Pointer method and solve actual problems using it. 1. What is Two Pointer? The Two Pointer technique is an algorithmic method that efficiently processes data &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34474\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Two Pointers&#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":[141],"tags":[],"class_list":["post-34474","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Two Pointers - \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\/34474\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Two Pointers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Let&#8217;s take a closer look at the Two Pointer technique, which is a common algorithm that appears in coding tests. This course will explain the basic concept of the Two Pointer method and solve actual problems using it. 1. What is Two Pointer? The Two Pointer technique is an algorithmic method that efficiently processes data &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Two Pointers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34474\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:07+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Two Pointers\",\"datePublished\":\"2024-11-01T09:28:25+00:00\",\"dateModified\":\"2024-11-01T11:41:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/\"},\"wordCount\":639,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34474\/\",\"name\":\"JavaScript Coding Test Course, Two Pointers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:25+00:00\",\"dateModified\":\"2024-11-01T11:41:07+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34474\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34474\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Two Pointers\"}]},{\"@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":"JavaScript Coding Test Course, Two Pointers - \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\/34474\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Two Pointers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Let&#8217;s take a closer look at the Two Pointer technique, which is a common algorithm that appears in coding tests. This course will explain the basic concept of the Two Pointer method and solve actual problems using it. 1. What is Two Pointer? The Two Pointer technique is an algorithmic method that efficiently processes data &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Two Pointers\"","og_url":"https:\/\/atmokpo.com\/w\/34474\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:25+00:00","article_modified_time":"2024-11-01T11:41:07+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34474\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34474\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Two Pointers","datePublished":"2024-11-01T09:28:25+00:00","dateModified":"2024-11-01T11:41:07+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34474\/"},"wordCount":639,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34474\/","url":"https:\/\/atmokpo.com\/w\/34474\/","name":"JavaScript Coding Test Course, Two Pointers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:25+00:00","dateModified":"2024-11-01T11:41:07+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34474\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34474\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34474\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Two Pointers"}]},{"@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\/34474","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=34474"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34474\/revisions"}],"predecessor-version":[{"id":34475,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34474\/revisions\/34475"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}