{"id":33592,"date":"2024-11-01T09:18:15","date_gmt":"2024-11-01T09:18:15","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33592"},"modified":"2024-11-01T11:47:30","modified_gmt":"2024-11-01T11:47:30","slug":"python-coding-test-course-finding-interval-sum-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33592\/","title":{"rendered":"Python Coding Test Course, Finding Interval Sum 1"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Problem Definition<\/h2>\n<p>The interval sum problem, which is frequently asked in coding tests, is about efficiently calculating the sum of elements in a specific range within a given array. In this post, we will discuss the &#8216;Calculating Interval Sum 1&#8217; problem.<\/p>\n<h3>Problem Description<\/h3>\n<p>An integer N and M are given, along with an array consisting of N integers. Next, M queries are provided, each consisting of two integers S and E. The task is to calculate the sum from S to E. Note that S and E range from 1 to N.<\/p>\n<h2>2. Input and Output Format<\/h2>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains N and M separated by a space. (1 \u2264 N, M \u2264 100,000)<\/li>\n<li>The second line contains N integers separated by spaces. (Each integer is between 1 and 100,000)<\/li>\n<li>From the third line onward, M lines contain two queries. (S, E)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<ul>\n<li>Output the answer for each query over M lines.<\/li>\n<\/ul>\n<h2>3. Sample Input and Output<\/h2>\n<h4>Sample Input<\/h4>\n<pre>\n    5 3\n    1 2 3 4 5\n    1 3\n    2 4\n    3 5\n    <\/pre>\n<h4>Sample Output<\/h4>\n<pre>\n    6\n    9\n    12\n    <\/pre>\n<h2>4. Approach to the Problem<\/h2>\n<p>To calculate the interval sum, the simplest method can be used first. However, since the maximum values of N and M are 100,000, it is impossible with a time complexity of O(N * M). Therefore, we need to find an efficient way to calculate the interval sum.<\/p>\n<h3>4.1. Simple Approach<\/h3>\n<p>The simplest method is to iterate from S to E to calculate the sum. However, this method has a complexity of O(N * M).<\/p>\n<h3>4.2. Utilizing Cumulative Sum Array<\/h3>\n<p>One of the efficient approaches is to use a Cumulative Sum array. By creating the cumulative sum array first, we quickly calculate the answer for each query using the values of S and E.<\/p>\n<p>When defining the cumulative sum array, the i-th element of the array represents the sum from 1 to i. That is, the i-th value of the cumulative sum array is calculated as:<\/p>\n<pre>\n    sum[i] = sum[i - 1] + array[i - 1]\n    <\/pre>\n<p>When processing queries, the following formula can be used:<\/p>\n<pre>\n    result = sum[E] - sum[S - 1]\n    <\/pre>\n<h2>5. Algorithm Implementation<\/h2>\n<p>Now let&#8217;s implement the algorithm using the cumulative sum array described above.<\/p>\n<h3>5.1. Python Code<\/h3>\n<pre class=\"algorithm\">\ndef solve(N, M, array, queries):\n    # Create cumulative sum array\n    sum_array = [0] * (N + 1)\n    \n    # Calculate cumulative sum\n    for i in range(1, N + 1):\n        sum_array[i] = sum_array[i - 1] + array[i - 1]\n    \n    result = []\n    \n    # Process queries\n    for S, E in queries:\n        query_sum = sum_array[E] - sum_array[S - 1]\n        result.append(query_sum)\n    \n    return result\n\n# Sample input\nN, M = 5, 3\narray = [1, 2, 3, 4, 5]\nqueries = [(1, 3), (2, 4), (3, 5)]\n\n# Output results\noutput = solve(N, M, array, queries)\nfor res in output:\n    print(res)\n    <\/pre>\n<h2>6. Code Explanation<\/h2>\n<p>The above code works in the following order:<\/p>\n<ol>\n<li>First, the cumulative sum array is initialized. The size of the cumulative sum array is set to N + 1 to easily calculate the sum from 1 to N.<\/li>\n<li>Next, each element of the array is iterated to calculate the cumulative sum.<\/li>\n<li>While going through the queries, the sum for the given S and E is quickly calculated using the cumulative sum array.<\/li>\n<li>The calculated results are stored in a list to be output at the end.<\/li>\n<\/ol>\n<h2>7. Time Complexity Analysis<\/h2>\n<p>The time complexity of the above algorithm is as follows:<\/p>\n<ul>\n<li>O(N) to create the cumulative sum array<\/li>\n<li>O(M) to process M queries<\/li>\n<\/ul>\n<p>Thus, the overall time complexity is O(N + M), allowing us to solve the problem efficiently.<\/p>\n<h2>8. Space Complexity Analysis<\/h2>\n<p>The space complexity is O(N) to store the cumulative sum array. Additional variables use constant space, so the overall space complexity is O(N).<\/p>\n<h2>9. Conclusion<\/h2>\n<p>In this post, we discussed the interval sum problem. We learned how to solve the problem efficiently using a cumulative sum array and provided a Python code example to aid understanding. This method can be applied in various problems requiring interval sums and can serve as a basis to tackle more complex issues.<\/p>\n<h2>10. Additional Exercises<\/h2>\n<p>Now, try to solve some exercises on your own. Below are a few additional practice problems.<\/p>\n<ul>\n<li>Write a program to calculate the interval sum from 1 to 100.<\/li>\n<li>Implement a program that calculates the sum of the interval each time an element changes.<\/li>\n<li>Write a program to calculate the sum of a specific area in a two-dimensional array.<\/li>\n<\/ul>\n<h2>11. Questions and Feedback<\/h2>\n<p>If you have any questions or feedback regarding the content discussed in this article, feel free to leave a comment. Let&#8217;s solve problems together!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Problem Definition The interval sum problem, which is frequently asked in coding tests, is about efficiently calculating the sum of elements in a specific range within a given array. In this post, we will discuss the &#8216;Calculating Interval Sum 1&#8217; problem. Problem Description An integer N and M are given, along with an array &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33592\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Finding Interval Sum 1&#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-33592","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 Interval Sum 1 - \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\/33592\/\" \/>\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 Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Problem Definition The interval sum problem, which is frequently asked in coding tests, is about efficiently calculating the sum of elements in a specific range within a given array. In this post, we will discuss the &#8216;Calculating Interval Sum 1&#8217; problem. Problem Description An integer N and M are given, along with an array &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding Interval Sum 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33592\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:30+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\/33592\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33592\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding Interval Sum 1\",\"datePublished\":\"2024-11-01T09:18:15+00:00\",\"dateModified\":\"2024-11-01T11:47:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33592\/\"},\"wordCount\":625,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33592\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33592\/\",\"name\":\"Python Coding Test Course, Finding Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:15+00:00\",\"dateModified\":\"2024-11-01T11:47:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33592\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33592\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33592\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Finding Interval Sum 1\"}]},{\"@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 Interval Sum 1 - \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\/33592\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Problem Definition The interval sum problem, which is frequently asked in coding tests, is about efficiently calculating the sum of elements in a specific range within a given array. In this post, we will discuss the &#8216;Calculating Interval Sum 1&#8217; problem. Problem Description An integer N and M are given, along with an array &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding Interval Sum 1\"","og_url":"https:\/\/atmokpo.com\/w\/33592\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:15+00:00","article_modified_time":"2024-11-01T11:47:30+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\/33592\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33592\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding Interval Sum 1","datePublished":"2024-11-01T09:18:15+00:00","dateModified":"2024-11-01T11:47:30+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33592\/"},"wordCount":625,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33592\/","url":"https:\/\/atmokpo.com\/w\/33592\/","name":"Python Coding Test Course, Finding Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:15+00:00","dateModified":"2024-11-01T11:47:30+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33592\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33592\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33592\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Finding Interval Sum 1"}]},{"@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\/33592","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=33592"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33592\/revisions"}],"predecessor-version":[{"id":33593,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33592\/revisions\/33593"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}