{"id":34406,"date":"2024-11-01T09:27:47","date_gmt":"2024-11-01T09:27:47","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34406"},"modified":"2024-11-01T11:41:23","modified_gmt":"2024-11-01T11:41:23","slug":"javascript-coding-test-course-finding-the-sum-of-intervals-3","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34406\/","title":{"rendered":"JavaScript Coding Test Course, Finding the Sum of Intervals 3"},"content":{"rendered":"<article>\n<header>\n<p>October 5, 2023<\/p>\n<\/header>\n<section>\n<h2>1. Problem Introduction<\/h2>\n<p>\n            The Range Sum Query 3 problem is a type of problem that you often encounter in algorithm problem-solving processes, especially demonstrating efficiency when calculating sums of large datasets.<br \/>\n            This problem deals with methods to quickly calculate the sum of a specific interval through queries.<br \/>\n            Computing the range sum algorithmically is particularly useful for database-related problems.<br \/>\n            Today, we will analyze various techniques to solve this problem and try to solve it using JavaScript.\n        <\/p>\n<\/section>\n<section>\n<h2>2. Problem Description<\/h2>\n<p>\n            Given an array <code>A<\/code> with length <code>N<\/code>, when a positive integer query <code>M<\/code> is provided,<br \/>\n            each query consists of two integers <code>i<\/code> and <code>j<\/code>, and we need to find the value of<br \/>\n            <code>A[i] + A[i+1] + ... + A[j]<\/code>. There can be up to <code>100,000<\/code> queries, and each number in <code>A<\/code> can be up to <code>1,000,000<\/code>.<br \/>\n            In other words, we need to efficiently calculate the sums of ranges based on the given array <code>A<\/code> and the queries.\n        <\/p>\n<\/section>\n<section>\n<h2>3. Problem Solving Strategy<\/h2>\n<p>\n            To solve the range sum problem, we will use two main methods.<br \/>\n            &#8211; First, the basic method which uses a double loop to calculate the sum for each query.<br \/>\n            &#8211; Second, a method that pre-computes the range sums and quickly derives results during the queries.<br \/>\n            In particular, the second method allows us to obtain query results in O(1) time through O(N) preprocessing.<br \/>\n            Thus, this will help us solve the problem more efficiently.\n        <\/p>\n<\/section>\n<section>\n<h2>4. Basic Method (O(N) x M)<\/h2>\n<p>\n            This method is very intuitive, but its time complexity is O(N * M).<br \/>\n            The implementation of this approach is as follows.\n        <\/p>\n<pre><code>\nfunction simpleRangeSum(A, queries) {\n    const results = [];\n    for (let [i, j] of queries) {\n        let sum = 0;\n        for (let k = i; k &lt;= j; k++) {\n            sum += A[k];\n        }\n        results.push(sum);\n    }\n    return results;\n}\n        <\/code><\/pre>\n<p>\n            This code calculates the sum at the respective index for each query by iterating through it. However, under the constraints of the problem,<br \/>\n            this method is inefficient. Therefore, we need to move on to a more efficient approach.\n        <\/p>\n<\/section>\n<section>\n<h2>5. Efficient Method (O(N) + O(1) per Query)<\/h2>\n<p>\n            In exploring the efficient method, we start by creating an array to store the range sums of the original array.<br \/>\n            First, the process of creating the range sum array is needed. Once the range sum array is created,<br \/>\n            the result of each query can be obtained simply by taking the difference of two cumulative sums.\n        <\/p>\n<pre><code>\nfunction prefixSum(A) {\n    const prefix = new Array(A.length + 1).fill(0);\n    for (let i = 0; i &lt; A.length; i++) {\n        prefix[i + 1] = prefix[i] + A[i];\n    }\n    return prefix;\n}\n\nfunction rangeSum(A, queries) {\n    const prefix = prefixSum(A);\n    const results = [];\n    for (let [i, j] of queries) {\n        results.push(prefix[j + 1] - prefix[i]);\n    }\n    return results;\n}\n        <\/code><\/pre>\n<p>\n            In the above implementation, the <code>prefixSum<\/code> function calculates the cumulative sums for the entire dataset and stores them in the <code>prefix<\/code> array.<br \/>\n            After that, each query can derive the range sum in O(1) time. This method is<br \/>\n            very efficient as it can process queries in O(N) + O(1).\n        <\/p>\n<\/section>\n<section>\n<h2>6. Code Explanation<\/h2>\n<p>\n            Analyzing the code above, we first create an empty array with one more than the length of the array in the <code>prefixSum<\/code> function,<br \/>\n            and compute the cumulative sums for each index of this array. Through this cumulative sum array,<br \/>\n            the <code>rangeSum<\/code> function quickly calculates the range sum by taking the starting index <code>i<\/code> and ending index <code>j<\/code> from the given queries.\n        <\/p>\n<p>\n            Now, when considering the large number of queries, we need to be mindful of the time complexity,<br \/>\n            as the solution above is very efficient in this regard.<br \/>\n            The unnecessary loops during the processing of each query were key,<br \/>\n            and deriving the results through this process improved performance.\n        <\/p>\n<\/section>\n<section>\n<h2>7. Example Test<\/h2>\n<p>\n            Let&#8217;s test the code above with an example.<br \/>\n            The array <code>A = [1, 2, 3, 4, 5]<\/code> and the queries are<br \/>\n            <code>[[0, 2], [1, 3], [2, 4]]<\/code>.\n        <\/p>\n<pre><code>\nconst A = [1, 2, 3, 4, 5];\nconst queries = [[0, 2], [1, 3], [2, 4]];\nconst results = rangeSum(A, queries);\nconsole.log(results); \/\/ [6, 9, 12]\n        <\/code><\/pre>\n<p>\n            The results of the above test correctly yield the cumulative sums for each query. By testing, we can confirm that our code is functioning accurately,<br \/>\n           which is an essential process for ensuring correctness.\n        <\/p>\n<\/section>\n<section>\n<h2>8. Conclusion<\/h2>\n<p>\n            The Range Sum Query 3 problem is an excellent example that demonstrates a variety of algorithm problem-solving skills.<br \/>\n            Solving the range sum problem through preprocessing is commonly used in everyday data processing tasks.<br \/>\n            Based on what we learned today, I hope you have developed the ability to solve similar problems and gained insight on how to structure algorithms when faced with problems.<br \/>\n            I encourage you to continue building your problem-solving experience through JavaScript.\n        <\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>October 5, 2023 1. Problem Introduction The Range Sum Query 3 problem is a type of problem that you often encounter in algorithm problem-solving processes, especially demonstrating efficiency when calculating sums of large datasets. This problem deals with methods to quickly calculate the sum of a specific interval through queries. Computing the range sum algorithmically &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34406\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Finding the Sum of Intervals 3&#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-34406","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, Finding the Sum of Intervals 3 - \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\/34406\/\" \/>\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, Finding the Sum of Intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"October 5, 2023 1. Problem Introduction The Range Sum Query 3 problem is a type of problem that you often encounter in algorithm problem-solving processes, especially demonstrating efficiency when calculating sums of large datasets. This problem deals with methods to quickly calculate the sum of a specific interval through queries. Computing the range sum algorithmically &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Finding the Sum of Intervals 3&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34406\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:23+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\/34406\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34406\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Finding the Sum of Intervals 3\",\"datePublished\":\"2024-11-01T09:27:47+00:00\",\"dateModified\":\"2024-11-01T11:41:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34406\/\"},\"wordCount\":624,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34406\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34406\/\",\"name\":\"JavaScript Coding Test Course, Finding the Sum of Intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:47+00:00\",\"dateModified\":\"2024-11-01T11:41:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34406\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34406\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34406\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Finding the Sum of Intervals 3\"}]},{\"@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, Finding the Sum of Intervals 3 - \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\/34406\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Finding the Sum of Intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"October 5, 2023 1. Problem Introduction The Range Sum Query 3 problem is a type of problem that you often encounter in algorithm problem-solving processes, especially demonstrating efficiency when calculating sums of large datasets. This problem deals with methods to quickly calculate the sum of a specific interval through queries. Computing the range sum algorithmically &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Finding the Sum of Intervals 3\"","og_url":"https:\/\/atmokpo.com\/w\/34406\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:47+00:00","article_modified_time":"2024-11-01T11:41:23+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\/34406\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34406\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Finding the Sum of Intervals 3","datePublished":"2024-11-01T09:27:47+00:00","dateModified":"2024-11-01T11:41:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34406\/"},"wordCount":624,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34406\/","url":"https:\/\/atmokpo.com\/w\/34406\/","name":"JavaScript Coding Test Course, Finding the Sum of Intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:47+00:00","dateModified":"2024-11-01T11:41:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34406\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34406\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34406\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Finding the Sum of Intervals 3"}]},{"@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\/34406","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=34406"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34406\/revisions"}],"predecessor-version":[{"id":34407,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34406\/revisions\/34407"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}