{"id":34440,"date":"2024-11-01T09:28:08","date_gmt":"2024-11-01T09:28:08","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34440"},"modified":"2024-11-01T11:41:15","modified_gmt":"2024-11-01T11:41:15","slug":"javascript-coding-test-course-interval-sum","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34440\/","title":{"rendered":"Javascript Coding Test Course, Interval Sum"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this tutorial, we will explore the &#8220;range sum&#8221; problem, which is frequently featured in JavaScript coding tests, in depth. The range sum problem involves efficiently calculating the sum of a specific range within a given sequence, and it can be solved using various optimization techniques. The range sum problems we will discuss can be particularly time-consuming when the size of the array is large, so it&#8217;s essential to design a more efficient algorithm.<\/p>\n<h2>Problem Introduction<\/h2>\n<p>Here is a problem related to range sums:<\/p>\n<h3>Problem Description<\/h3>\n<p>An array of integers <code>arr<\/code> and several pairs of integers <code>(l, r)<\/code> are given. Each pair represents the starting point <code>l<\/code> and endpoint <code>r<\/code> of a range. Write a program to calculate the sum of <code>arr[l] + arr[l + 1] + ... + arr[r]<\/code>. The length of the array and the number of pairs are as follows:<\/p>\n<ul>\n<li>1 \u2264 <code>arr.length<\/code> \u2264 10<sup>6<\/sup><\/li>\n<li>1 \u2264 <code>l<\/code>, <code>r<\/code> \u2264 <code>arr.length<\/code><\/li>\n<li>0 \u2264 <code>arr[i]<\/code> \u2264 10<sup>9<\/sup><\/li>\n<\/ul>\n<p>For example, if the array is <code>arr = [1, 2, 3, 4, 5]<\/code> and the pair <code>(1, 3)<\/code> is given, the range sum is <code>arr[1] + arr[2] + arr[3] = 2 + 3 + 4 = 9<\/code>.<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To solve this problem efficiently, simply using loops to calculate the sum for each range is not appropriate. The reason is that in the worst-case scenario, it would have a time complexity of O(N * M), which can lead to exponential time increases if the size of the data is large. Instead, we can use a more effective approach.<\/p>\n<h3>Preprocessing Technique: Prefix Sum Array<\/h3>\n<p>One way to solve the range sum problem is to create a Prefix Sum Array. Using a prefix sum array allows us to calculate the sum of a range in O(1) time. The definition of the prefix sum array is as follows:<\/p>\n<pre><code>prefix[i] = arr[0] + arr[1] + ... + arr[i-1]<\/code><\/pre>\n<p>Thus, the sum of the range <code>(l, r)<\/code> can be calculated as:<\/p>\n<pre><code>sum(l, r) = prefix[r + 1] - prefix[l]<\/code><\/pre>\n<p>This allows us to compute the range sum for each pair in O(1) time. The overall time complexity of the algorithm is O(N + M), where N is the size of the array and M is the number of pairs.<\/p>\n<h2>Implementation Steps<\/h2>\n<p>Now, let&#8217;s implement the JavaScript code that solves the problem using the prefix sum array.<\/p>\n<h3>Step 1: Create the Prefix Sum Array<\/h3>\n<pre><code>\nfunction createPrefixSum(arr) {\n    const prefix = new Array(arr.length + 1).fill(0);\n    for (let i = 0; i < arr.length; i++) {\n        prefix[i + 1] = prefix[i] + arr[i];\n    }\n    return prefix;\n}\n<\/code><\/pre>\n<h3>Step 2: Calculate the Range Sum<\/h3>\n<pre><code>\nfunction rangeSum(prefix, l, r) {\n    return prefix[r + 1] - prefix[l];\n}\n<\/code><\/pre>\n<h3>Step 3: Implement the Main Function<\/h3>\n<pre><code>\nfunction calculateRangeSums(arr, queries) {\n    const prefix = createPrefixSum(arr);\n    const results = [];\n    for (let [l, r] of queries) {\n        results.push(rangeSum(prefix, l - 1, r - 1)); \/\/ 1-based to 0-based\n    }\n    return results;\n}\n\n\/\/ Example usage\nconst arr = [1, 2, 3, 4, 5];\nconst queries = [[1, 3], [2, 5], [0, 2]];\nconst results = calculateRangeSums(arr, queries);\nconsole.log(results); \/\/ [9, 14, 6]\n<\/code><\/pre>\n<h2>Results Analysis<\/h2>\n<p>The above code is a program that efficiently calculates and outputs the range sum for each query based on the given array and queries. As a result, it can quickly compute range sums and does not suffer performance degradation depending on the size of the array or the number of queries.<\/p>\n<h3>Time Complexity<\/h3>\n<p>The time complexity of this algorithm is as follows:<\/p>\n<ul>\n<li>Creating the prefix sum array: O(N)<\/li>\n<li>Processing each query: O(1) (If you combine processing all M queries, it becomes O(M))<\/li>\n<\/ul>\n<p>As a result, the overall time complexity is O(N + M), which is very efficient.<\/p>\n<h2>Conclusion<\/h2>\n<p>Now we have learned how to efficiently solve the range sum problem using a prefix sum array. Since range sum problems are frequently featured topics in coding tests, understanding and utilizing optimization techniques like this is important. Practice solving various types of problems!<\/p>\n<h3>Additional Practice Problems<\/h3>\n<p>Try practicing the following variant problems:<\/p>\n<ul>\n<li>Problems that find the maximum value of the range instead of the sum<\/li>\n<li>Problems that find the product of the range (note that division operations may need to be considered)<\/li>\n<li>Consider how to handle different queries (e.g., range increment, decrement, etc.)<\/li>\n<\/ul>\n<p>Based on the above content, I hope you solve various problems. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this tutorial, we will explore the &#8220;range sum&#8221; problem, which is frequently featured in JavaScript coding tests, in depth. The range sum problem involves efficiently calculating the sum of a specific range within a given sequence, and it can be solved using various optimization techniques. The range sum problems we will discuss can &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34440\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Javascript Coding Test Course, Interval Sum&#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-34440","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, Interval Sum - \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\/34440\/\" \/>\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, Interval Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this tutorial, we will explore the &#8220;range sum&#8221; problem, which is frequently featured in JavaScript coding tests, in depth. The range sum problem involves efficiently calculating the sum of a specific range within a given sequence, and it can be solved using various optimization techniques. The range sum problems we will discuss can &hellip; \ub354 \ubcf4\uae30 &quot;Javascript Coding Test Course, Interval Sum&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34440\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:15+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\/34440\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34440\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Javascript Coding Test Course, Interval Sum\",\"datePublished\":\"2024-11-01T09:28:08+00:00\",\"dateModified\":\"2024-11-01T11:41:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34440\/\"},\"wordCount\":555,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34440\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34440\/\",\"name\":\"Javascript Coding Test Course, Interval Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:08+00:00\",\"dateModified\":\"2024-11-01T11:41:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34440\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34440\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34440\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Javascript Coding Test Course, Interval Sum\"}]},{\"@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, Interval Sum - \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\/34440\/","og_locale":"ko_KR","og_type":"article","og_title":"Javascript Coding Test Course, Interval Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this tutorial, we will explore the &#8220;range sum&#8221; problem, which is frequently featured in JavaScript coding tests, in depth. The range sum problem involves efficiently calculating the sum of a specific range within a given sequence, and it can be solved using various optimization techniques. The range sum problems we will discuss can &hellip; \ub354 \ubcf4\uae30 \"Javascript Coding Test Course, Interval Sum\"","og_url":"https:\/\/atmokpo.com\/w\/34440\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:08+00:00","article_modified_time":"2024-11-01T11:41:15+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\/34440\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34440\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Javascript Coding Test Course, Interval Sum","datePublished":"2024-11-01T09:28:08+00:00","dateModified":"2024-11-01T11:41:15+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34440\/"},"wordCount":555,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34440\/","url":"https:\/\/atmokpo.com\/w\/34440\/","name":"Javascript Coding Test Course, Interval Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:08+00:00","dateModified":"2024-11-01T11:41:15+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34440\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34440\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34440\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Javascript Coding Test Course, Interval Sum"}]},{"@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\/34440","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=34440"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34440\/revisions"}],"predecessor-version":[{"id":34441,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34440\/revisions\/34441"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}