{"id":34138,"date":"2024-11-01T09:24:39","date_gmt":"2024-11-01T09:24:39","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34138"},"modified":"2024-11-01T10:58:27","modified_gmt":"2024-11-01T10:58:27","slug":"c-coding-test-course-range-sum","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34138\/","title":{"rendered":"C++ Coding Test Course, Range Sum"},"content":{"rendered":"<p>The range sum problem is an algorithm problem that efficiently calculates the sum of a specific range within a given array. This problem frequently appears in various programming languages, particularly with important implementations in C++. In this tutorial, we will set a specific problem for the range sum problem and explain the process to solve it in detail.<\/p>\n<h2>1. Problem Description<\/h2>\n<p>Let&#8217;s assume we are given an array composed of integers. The size of the integer array <code>A<\/code> is <code>N<\/code>. We need to calculate the sum of specific ranges in the array through <code>M<\/code> queries. The starting index of the range is provided as <code>L<\/code>, and the ending index is provided as <code>R<\/code>. We need to calculate <code>A[L] + A[L+1] + ... + A[R]<\/code>.<\/p>\n<p>For example, suppose the array <code>A<\/code> is as follows:<\/p>\n<pre>\nA = [10, 20, 30, 40, 50]\n<\/pre>\n<p>And let&#8217;s assume the following queries are given:<\/p>\n<pre>\n1. L = 1, R = 3\n2. L = 0, R = 4\n3. L = 2, R = 2\n<\/pre>\n<p>Calculating the range sum for the given queries yields the following results:<\/p>\n<pre>\n1. A[1] + A[2] + A[3] = 20 + 30 + 40 = 90\n2. A[0] + A[1] + A[2] + A[3] + A[4] = 10 + 20 + 30 + 40 + 50 = 150\n3. A[2] = 30\n<\/pre>\n<h2>2. Problem Solving Approach<\/h2>\n<p>The traditional method to solve the range sum problem is to iterate through the array directly. However, this can be inefficient. For example, if each query among <code>M<\/code> queries spends <code>O(R - L + 1)<\/code> time, in the worst case, it will take <code>O(N * M)<\/code> time. To improve this, we can use the following method.<\/p>\n<h3>2.1. Preprocessing Method &#8211; Cumulative Sum Array<\/h3>\n<p>To quickly calculate the sum of all queries, we can utilize a cumulative sum array. First, we define the cumulative sum array <code>S<\/code> for the given array <code>A<\/code>. The cumulative sum array is calculated as follows:<\/p>\n<pre>\nS[i] = S[i - 1] + A[i]\n<\/pre>\n<p>Here, <code>S[0]<\/code> is initialized to 0. Using the cumulative sum array, the sum of a range can be calculated easily as follows:<\/p>\n<pre>\nsum(L, R) = S[R] - S[L - 1]\n<\/pre>\n<p>Now we can calculate the range sum with a time complexity of <code>O(1)<\/code>. Below is a C++ solution that uses the cumulative sum array.<\/p>\n<h2>3. C++ Code Example<\/h2>\n<pre>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n    int N, M;\n    cin &gt;&gt; N &gt;&gt; M;\n\n    vector<int> A(N);\n    vector<long long=\"\"> S(N + 1, 0); \/\/ S[0] is initialized to 0\n\n    for (int i = 0; i &lt; N; i++) {\n        cin &gt;&gt; A[i];\n        S[i + 1] = S[i] + A[i]; \/\/ Calculate cumulative sum\n    }\n\n    for (int i = 0; i &lt; M; i++) {\n        int L, R;\n        cin &gt;&gt; L &gt;&gt; R;\n        \/\/ Output the range sum\n        cout &lt;&lt; S[R] - S[L - 1] &lt;&lt; endl;\n    }\n    return 0;\n}\n<\/long><\/int><\/vector><\/iostream><\/pre>\n<h2>4. Code Explanation<\/h2>\n<p>Let\u2019s explain the above code step by step:<\/p>\n<ol>\n<li><strong>Input Gathering:<\/strong> We receive the size of the array <code>N<\/code> and the number of queries <code>M<\/code>, and we input the values of the array <code>A<\/code>.<\/li>\n<li><strong>Cumulative Sum Array Creation:<\/strong> We calculate the cumulative sum for each index and store it in the <code>S<\/code> array. This array helps us easily find the sums of the values of the original array <code>A<\/code>.<\/li>\n<li><strong>Query Processing:<\/strong> We input the starting and ending indices <code>L<\/code> and <code>R<\/code> for each query, and we calculate and output the sum of the range using the cumulative sum array <code>S<\/code>.<\/li>\n<\/ol>\n<h2>5. Performance Analysis<\/h2>\n<p>The time complexity of the above C++ code is <code>O(N)<\/code> for calculating the cumulative sum of the array <code>A<\/code> and <code>O(1)<\/code> for processing each query, making the overall time complexity <code>O(N + M)<\/code>. This is a very efficient method, allowing for quick results even with thousands of queries.<\/p>\n<h2>6. Various Modified Problems<\/h2>\n<p>The range sum problem can have various modifications. For example:<\/p>\n<ul>\n<li><strong>Range Minimum\/Maximum:<\/strong> It can be modified to find the minimum or maximum value for a given parameter.<\/li>\n<li><strong>Range Updates:<\/strong> In cases where a specific range of values needs to be changed and the range sum must be recalculated. In this case, segment trees can be utilized.<\/li>\n<li><strong>2D Range Sum:<\/strong> A problem of calculating the sum of a specific range in a 2D array, where double cumulative sums can be used.<\/li>\n<\/ul>\n<h2>7. Conclusion<\/h2>\n<p>The range sum problem is one of the very useful techniques in programming and provides significant efficiency, especially when large-scale data processing is required. In this tutorial, we learned how to solve the range sum problem using a cumulative sum array. We can further enhance this technique through various modified problems. Practice solving various problems to build experience and achieve high scores on C++ coding tests.<\/p>\n<p>As we conclude the tutorial, please leave any additional questions or requests in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The range sum problem is an algorithm problem that efficiently calculates the sum of a specific range within a given array. This problem frequently appears in various programming languages, particularly with important implementations in C++. In this tutorial, we will set a specific problem for the range sum problem and explain the process to solve &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34138\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Range 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":[111],"tags":[],"class_list":["post-34138","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, Range 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\/34138\/\" \/>\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, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The range sum problem is an algorithm problem that efficiently calculates the sum of a specific range within a given array. This problem frequently appears in various programming languages, particularly with important implementations in C++. In this tutorial, we will set a specific problem for the range sum problem and explain the process to solve &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Range Sum&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34138\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:27+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\/34138\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34138\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Range Sum\",\"datePublished\":\"2024-11-01T09:24:39+00:00\",\"dateModified\":\"2024-11-01T10:58:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34138\/\"},\"wordCount\":587,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34138\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34138\/\",\"name\":\"C++ Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:39+00:00\",\"dateModified\":\"2024-11-01T10:58:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34138\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34138\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34138\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Range 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":"C++ Coding Test Course, Range 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\/34138\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The range sum problem is an algorithm problem that efficiently calculates the sum of a specific range within a given array. This problem frequently appears in various programming languages, particularly with important implementations in C++. In this tutorial, we will set a specific problem for the range sum problem and explain the process to solve &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Range Sum\"","og_url":"https:\/\/atmokpo.com\/w\/34138\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:39+00:00","article_modified_time":"2024-11-01T10:58:27+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\/34138\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34138\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Range Sum","datePublished":"2024-11-01T09:24:39+00:00","dateModified":"2024-11-01T10:58:27+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34138\/"},"wordCount":587,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34138\/","url":"https:\/\/atmokpo.com\/w\/34138\/","name":"C++ Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:39+00:00","dateModified":"2024-11-01T10:58:27+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34138\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34138\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34138\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Range 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\/34138","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=34138"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34138\/revisions"}],"predecessor-version":[{"id":34139,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34138\/revisions\/34139"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}