{"id":34144,"date":"2024-11-01T09:24:43","date_gmt":"2024-11-01T09:24:43","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34144"},"modified":"2024-11-01T10:58:26","modified_gmt":"2024-11-01T10:58:26","slug":"c-coding-test-course-finding-interval-sums-3","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34144\/","title":{"rendered":"C++ Coding Test Course, Finding Interval Sums 3"},"content":{"rendered":"<p><body><\/p>\n<div class=\"problem\">\n<h2>Problem<\/h2>\n<p>\n            Given an integer array <code>arr<\/code> and two integers <code>left<\/code> and <code>right<\/code>,<br \/>\n            write a function to calculate the value of <code>arr[left] + arr[left + 1] + ... + arr[right]<\/code>.<\/p>\n<p>            Note that <code>arr<\/code> has a length constraint of 1 \u2264 <code>arr.length<\/code> \u2264 100,000,<br \/>\n            and each element is within the range of -10^9 \u2264 <code>arr[i]<\/code> \u2264 10^9.<\/p>\n<p>            Your goal is to find an efficient method to calculate the range sum with a time complexity of O(log N) or O(1).\n        <\/p>\n<\/div>\n<h2>Approach to the Problem<\/h2>\n<p>\n        The range sum problem can be solved in various ways, but an efficient approach is to use<br \/>\n        <strong>Prefix Sum<\/strong>. With prefix sums, you can calculate the sum for the given range<br \/>\n        <code>[left, right]<\/code> in O(1) time complexity.<\/p>\n<p>        The basic idea is to precompute and store the sum for each index in the array.<br \/>\n        With this, the sum of a specific range can be calculated as follows.<\/p>\n<p><code>sum(left, right) = prefixSum[right] - prefixSum[left - 1]<\/code><\/p>\n<p>        Here, <code>prefixSum[i]<\/code> is the sum from the start of the array to the <code>i<\/code>-th element.<br \/>\n        This method has a time complexity of O(N) since it involves one traversal of the array, and afterwards, each range sum can be computed in O(1).\n    <\/p>\n<h2>Implementation<\/h2>\n<h3>Code<\/h3>\n<pre>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n\nclass SubarraySum {\npublic:\n    SubarraySum(const vector&lt;int&gt;&amp; arr) {\n        int n = arr.size();\n        prefixSum.resize(n + 1);\n        prefixSum[0] = 0; \/\/ prefixSum[0] is initialized to 0.\n        \n        for (int i = 1; i &lt;= n; i++) {\n            prefixSum[i] = prefixSum[i - 1] + arr[i - 1];\n        }\n    }\n\n    int getSum(int left, int right) {\n        return prefixSum[right] - prefixSum[left - 1];\n    }\n\nprivate:\n    vector&lt;int&gt; prefixSum; \/\/ Array to store the prefix sum.\n};\n\nint main() {\n    int n; \/\/ Size of the array\n    cout &lt;&lt; \"Enter the size of the array: \";\n    cin &gt;&gt; n;\n    vector&lt;int&gt; arr(n);\n    cout &lt;&lt; \"Enter the elements of the array: \";\n    for (int i = 0; i &lt; n; i++) {\n        cin &gt;&gt; arr[i];\n    }\n\n    SubarraySum subarray(arr);\n\n    int left, right;\n    cout &lt;&lt; \"Enter the left boundary of the range (starting from 1): \";\n    cin &gt;&gt; left;\n    cout &lt;&lt; \"Enter the right boundary of the range: \";\n    cin &gt;&gt; right;\n\n    int result = subarray.getSum(left, right);\n    cout &lt;&lt; \"The sum of the range [\" &lt;&lt; left &lt;&lt; \", \" &lt;&lt; right &lt;&lt; \"] is: \" &lt;&lt; result &lt;&lt; endl;\n\n    return 0;\n}\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<h3>Class Definition<\/h3>\n<p>\nThe <code>SubarraySum<\/code> class provides the functionality to calculate range sums.<br \/>\n        The constructor of this class initializes the prefix sum based on the array elements provided.<br \/>\n        It defines a variable <code>n<\/code> to store the size of the array and creates the <code>prefixSum<\/code> array (including one for [0]).\n    <\/p>\n<h3>Prefix Sum Calculation<\/h3>\n<p>\n        Through the <code>for<\/code> loop inside the constructor, the array is traversed to store<br \/>\n        the sums up to each index in the <code>prefixSum<\/code> array.<br \/>\n        This is unique because it has a size of one more than the array size, starting from the first index.\n    <\/p>\n<h3>Range Sum Calculation<\/h3>\n<p>\nThe <code>getSum(int left, int right)<\/code> method returns the range sum using the provided <code>left<\/code> and <code>right<\/code> indices. It easily calculates the range sum using<br \/>\n        <code>prefixSum[right] - prefixSum[left - 1]<\/code>.\n    <\/p>\n<h2>Testing and Usage<\/h2>\n<p>\n        With this code, users can input the left and right boundaries of the range and easily calculate<br \/>\n        the sum of that range. This algorithm runs smoothly even as the input array size increases,<br \/>\n        allowing for the range sum to be processed quickly in O(1) time complexity.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        The range sum problem is widely used in various applications, and<br \/>\n        this prefix sum approach demonstrates its utility in areas like data analysis and statistical processing.<br \/>\n        This simple yet effective method can enhance the efficiency of problem-solving.<\/p>\n<p>        I hope this lecture aids you in preparing for your C++ coding tests!\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer array arr and two integers left and right, write a function to calculate the value of arr[left] + arr[left + 1] + &#8230; + arr[right]. Note that arr has a length constraint of 1 \u2264 arr.length \u2264 100,000, and each element is within the range of -10^9 \u2264 arr[i] \u2264 10^9. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34144\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding Interval Sums 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":[111],"tags":[],"class_list":["post-34144","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, Finding Interval Sums 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\/34144\/\" \/>\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, Finding Interval Sums 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Given an integer array arr and two integers left and right, write a function to calculate the value of arr[left] + arr[left + 1] + ... + arr[right]. Note that arr has a length constraint of 1 \u2264 arr.length \u2264 100,000, and each element is within the range of -10^9 \u2264 arr[i] \u2264 10^9. &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding Interval Sums 3&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34144\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:26+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\/34144\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34144\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding Interval Sums 3\",\"datePublished\":\"2024-11-01T09:24:43+00:00\",\"dateModified\":\"2024-11-01T10:58:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34144\/\"},\"wordCount\":385,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34144\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34144\/\",\"name\":\"C++ Coding Test Course, Finding Interval Sums 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:43+00:00\",\"dateModified\":\"2024-11-01T10:58:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34144\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34144\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34144\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding Interval Sums 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":"C++ Coding Test Course, Finding Interval Sums 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\/34144\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding Interval Sums 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Given an integer array arr and two integers left and right, write a function to calculate the value of arr[left] + arr[left + 1] + ... + arr[right]. Note that arr has a length constraint of 1 \u2264 arr.length \u2264 100,000, and each element is within the range of -10^9 \u2264 arr[i] \u2264 10^9. &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding Interval Sums 3\"","og_url":"https:\/\/atmokpo.com\/w\/34144\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:43+00:00","article_modified_time":"2024-11-01T10:58:26+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\/34144\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34144\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding Interval Sums 3","datePublished":"2024-11-01T09:24:43+00:00","dateModified":"2024-11-01T10:58:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34144\/"},"wordCount":385,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34144\/","url":"https:\/\/atmokpo.com\/w\/34144\/","name":"C++ Coding Test Course, Finding Interval Sums 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:43+00:00","dateModified":"2024-11-01T10:58:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34144\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34144\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34144\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding Interval Sums 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\/34144","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=34144"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34144\/revisions"}],"predecessor-version":[{"id":34145,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34144\/revisions\/34145"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}