{"id":33596,"date":"2024-11-01T09:18:18","date_gmt":"2024-11-01T09:18:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33596"},"modified":"2024-11-01T11:47:29","modified_gmt":"2024-11-01T11:47:29","slug":"python-coding-test-course-finding-range-sum-3","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33596\/","title":{"rendered":"Python Coding Test Course, Finding Range Sum 3"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        Given an integer array A and several pairs of integers L and R, write a program to find the sum of the interval from index L to index R for each pair.\n    <\/p>\n<p>\n        For example, if A = [1, 2, 3, 4, 5] and the interval pairs are (1, 3), (0, 2), (2, 4),<br \/>\n        the results should be 9, 6, and 12 respectively.\n    <\/p>\n<h2>Input Format<\/h2>\n<pre>\n    - The first line contains an integer N (1 \u2264 N \u2264 100,000): size of array A\n    - The second line contains N integers A[i] (1 \u2264 A[i] \u2264 10,000).\n    - The third line contains an integer M (1 \u2264 M \u2264 100,000): number of interval pairs.\n    - The following M lines contain the interval pairs L, R (0 \u2264 L &lt;= R &lt; N).\n    <\/pre>\n<h2>Output Format<\/h2>\n<pre>\n    - Output the sum of each interval over M lines.\n    <\/pre>\n<h2>Problem Solving Strategy<\/h2>\n<p>\n        While it is possible to solve this problem using simple loops,<br \/>\n        the worst-case scenario has N and M up to 100,000, making O(N * M) time complexity impossible.<br \/>\n        Therefore, a method with O(N + M) time complexity is needed.\n    <\/p>\n<p>\n        To achieve this, it is useful to create a <strong>prefix sum array<\/strong> as a <strong>preprocessing<\/strong> step.<br \/>\n        Using a prefix sum array allows for quick calculation of each interval&#8217;s sum.\n    <\/p>\n<h2>Prefix Sum Array Description<\/h2>\n<p>\n        First, calculate the prefix sum of array A to create the prefix_sum array.<br \/>\n        prefix_sum[i] stores the sum from A[0] to A[i].<br \/>\n        Thus, the sum from index L to index R can be calculated as follows:<br \/>\n        <br \/>\n<strong>sum(L, R) = prefix_sum[R] &#8211; prefix_sum[L &#8211; 1]<\/strong>, L &gt; 0<br \/>\n        <br \/>\n<strong>sum(0, R) = prefix_sum[R]<\/strong>, L = 0\n    <\/p>\n<h2>Code Implementation<\/h2>\n<pre>\n    <code>\ndef compute_prefix_sum(A):\n    prefix_sum = [0] * len(A)\n    prefix_sum[0] = A[0]\n    for i in range(1, len(A)):\n        prefix_sum[i] = prefix_sum[i - 1] + A[i]\n    return prefix_sum\n\ndef range_sum(prefix_sum, L, R):\n    if L == 0:\n        return prefix_sum[R]\n    else:\n        return prefix_sum[R] - prefix_sum[L - 1]\n\ndef main():\n    N = int(input())\n    A = list(map(int, input().split()))\n    M = int(input())\n    \n    prefix_sum = compute_prefix_sum(A)\n\n    results = []\n    for _ in range(M):\n        L, R = map(int, input().split())\n        results.append(range_sum(prefix_sum, L, R))\n    \n    for result in results:\n        print(result)\n\nif __name__ == \"__main__\":\n    main()\n    <\/code>\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        1. The <strong>compute_prefix_sum<\/strong> function calculates the prefix sum of the input array A and<br \/>\n        returns the prefix_sum array. It initializes the first value and calculates each index&#8217;s value by adding the previous value to the current value.\n    <\/p>\n<p>\n        2. The <strong>range_sum<\/strong> function quickly calculates the sum of the interval using the prefix sum array<br \/>\n        for the given L and R. If L is 0, it returns prefix_sum[R]; otherwise, it calculates the result by subtracting<br \/>\n        prefix_sum[L-1] from prefix_sum[R].\n    <\/p>\n<p>\n        3. The <strong>main<\/strong> function handles input and calls the range_sum function for each interval pair to display the results.\n    <\/p>\n<h2>Time Complexity Analysis<\/h2>\n<p>\n        &#8211; It takes O(N) time to calculate the prefix sum array.\n    <\/p>\n<p>\n        &#8211; Each of the M queries takes O(1) time.<br \/>\n        Thus, the overall time complexity is O(N + M).\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        In this lecture, we covered an efficient approach to finding interval sums.<br \/>\n        Utilizing prefix sums allows for reduced time complexity, enabling quick processing even for large inputs.<br \/>\n        This technique is useful in various algorithm problems, so it&#8217;s important to keep it in mind.\n    <\/p>\n<h2>Additional Practice Problems<\/h2>\n<ul>\n<li>1. Change the example array A and compute the interval sums.<\/li>\n<li>2. Research methods to calculate interval sums using other algorithms (Segment Tree or Fenwick Tree).<\/li>\n<li>3. Practice problems involving updating the value at a specific index in the array and recalculating the total interval sums.<\/li>\n<\/ul>\n<h2>References<\/h2>\n<p>\n        &#8211; Competitive Programming Problem Sets<br \/>\n        <br \/>\n        &#8211; Materials related to online coding tests<br \/>\n        <br \/>\n        &#8211; Interval sum problems from LeetCode and HackerRank\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Given an integer array A and several pairs of integers L and R, write a program to find the sum of the interval from index L to index R for each pair. For example, if A = [1, 2, 3, 4, 5] and the interval pairs are (1, 3), (0, 2), (2, 4), &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33596\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Finding Range Sum 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":[145],"tags":[],"class_list":["post-33596","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Coding Test Course, Finding Range Sum 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\/33596\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Finding Range Sum 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description Given an integer array A and several pairs of integers L and R, write a program to find the sum of the interval from index L to index R for each pair. For example, if A = [1, 2, 3, 4, 5] and the interval pairs are (1, 3), (0, 2), (2, 4), &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding Range Sum 3&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33596\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:29+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\/33596\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33596\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding Range Sum 3\",\"datePublished\":\"2024-11-01T09:18:18+00:00\",\"dateModified\":\"2024-11-01T11:47:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33596\/\"},\"wordCount\":445,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33596\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33596\/\",\"name\":\"Python Coding Test Course, Finding Range Sum 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:18+00:00\",\"dateModified\":\"2024-11-01T11:47:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33596\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33596\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33596\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Finding Range Sum 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":"Python Coding Test Course, Finding Range Sum 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\/33596\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding Range Sum 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description Given an integer array A and several pairs of integers L and R, write a program to find the sum of the interval from index L to index R for each pair. For example, if A = [1, 2, 3, 4, 5] and the interval pairs are (1, 3), (0, 2), (2, 4), &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding Range Sum 3\"","og_url":"https:\/\/atmokpo.com\/w\/33596\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:18+00:00","article_modified_time":"2024-11-01T11:47:29+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\/33596\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33596\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding Range Sum 3","datePublished":"2024-11-01T09:18:18+00:00","dateModified":"2024-11-01T11:47:29+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33596\/"},"wordCount":445,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33596\/","url":"https:\/\/atmokpo.com\/w\/33596\/","name":"Python Coding Test Course, Finding Range Sum 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:18+00:00","dateModified":"2024-11-01T11:47:29+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33596\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33596\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33596\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Finding Range Sum 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\/33596","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=33596"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33596\/revisions"}],"predecessor-version":[{"id":33597,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33596\/revisions\/33597"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}