{"id":33710,"date":"2024-11-01T09:19:30","date_gmt":"2024-11-01T09:19:30","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33710"},"modified":"2024-11-01T11:47:01","modified_gmt":"2024-11-01T11:47:01","slug":"python-coding-test-course-finding-continuous-sum","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33710\/","title":{"rendered":"Python Coding Test Course, Finding Continuous Sum"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this lecture, we will cover one of the algorithm problems using Python, <strong>Finding the Maximum Sum of Continuous Elements<\/strong>. This problem can be approached in various ways and is very useful for studying algorithms. We will explain the necessary basic theories and the solution process in detail.<\/p>\n<div class=\"problem\">\n<h2>Problem Description<\/h2>\n<p>Given an integer array <code>arr<\/code>, write an algorithm to maximize the sum of <code>k<\/code> continuous elements within this array. The length of the array is given as <code>n<\/code>, and <code>k<\/code> is an integer from 1 to <code>n<\/code>, inclusive. In other words, we need to calculate the sum of <code>k<\/code> continuous elements in the array and return the maximum value of this sum.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains the size of the array <code>n<\/code> (1 \u2264 n \u2264 100,000)<\/li>\n<li>The second line contains the array <code>arr<\/code> consisting of <code>n<\/code> integers (-10<sup>9<\/sup> \u2264 arr<sub>i<\/sub> \u2264 10<sup>9<\/sup>)<\/li>\n<li>The third line contains <code>k<\/code> (1 \u2264 k \u2264 n)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>Output the maximum sum of <code>k<\/code> continuous elements.<\/p>\n<h3>Example Input<\/h3>\n<pre class=\"code\">5\n1 2 3 4 5\n3<\/pre>\n<h3>Example Output<\/h3>\n<pre class=\"code\">12<\/pre>\n<h3>Explanation<\/h3>\n<p>If we calculate the sum of 3 continuous elements in the given array, the largest value is <code>3+4+5=12<\/code>.<\/p>\n<\/div>\n<div class=\"explanation\">\n<h2>Solution Process<\/h2>\n<p>To solve this problem, we need an algorithm that can calculate continuous sums. If we simply iterate through the array and sum all <code>k<\/code> continuous elements to find the maximum, the time complexity would be <code>O(n*k)<\/code>, which is not feasible if <code>n<\/code> is 100,000. Therefore, we need to solve it in a more efficient way.<\/p>\n<h3>Sliding Window Technique<\/h3>\n<p>One useful technique to solve this problem is the <strong>Sliding Window<\/strong>. The sliding window is a method to quickly calculate a continuous subarray of a specific size within a given array or list. By using this technique, we can reduce the time complexity to <code>O(n)<\/code>.<\/p>\n<h3>Algorithm Explanation<\/h3>\n<ol>\n<li>Initially, sum the first <code>k<\/code> elements and set it as the current maximum sum.<\/li>\n<li>Then, focus on the <code>k<\/code> elements by removing one element and adding a newly added element to update the sum.<\/li>\n<li>Repeat this process until the end of the array to update the maximum sum.<\/li>\n<\/ol>\n<h3>Implementation Code<\/h3>\n<p>Now, let&#8217;s implement this algorithm in Python. Here is an implementation using the sliding window:<\/p>\n<pre class=\"code\">\ndef max_sum_k_elements(n, arr, k):\n    # Initial window sum\n    current_sum = sum(arr[:k])\n    max_sum = current_sum\n\n    # Use sliding window to explore the rest of the elements\n    for i in range(k, n):\n        current_sum += arr[i] - arr[i - k]\n        max_sum = max(max_sum, current_sum)\n\n    return max_sum\n\n# Input handling\nn = int(input())\narr = list(map(int, input().split()))\nk = int(input())\n\n# Output the maximum continuous sum\nprint(max_sum_k_elements(n, arr, k))<\/pre>\n<h3>Code Explanation<\/h3>\n<p>Let\u2019s take a detailed look at each part of the code:<\/p>\n<ul>\n<li><code>def max_sum_k_elements(n, arr, k):<\/code> &#8211; Function declaration that uses the input array and <code>k<\/code> to calculate the maximum sum.<\/li>\n<li><code>current_sum = sum(arr[:k])<\/code> &#8211; Calculates the sum of the initial window.<\/li>\n<li><code>max_sum = current_sum<\/code> &#8211; Initializes the current maximum sum.<\/li>\n<li><code>for i in range(k, n):<\/code> &#8211; Loops from <code>k<\/code> to <code>n<\/code> for the sliding window.<\/li>\n<li><code>current_sum += arr[i] - arr[i - k]<\/code> &#8211; Includes the new element and subtracts the excluded element to update the current sum.<\/li>\n<li><code>max_sum = max(max_sum, current_sum)<\/code> &#8211; Updates the maximum sum.<\/li>\n<li><code>return max_sum<\/code> &#8211; Returns the final maximum sum.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have now solved the problem of maximizing the sum of <code>k<\/code> continuous elements in an array. By using the sliding window technique, we effectively reduced the time complexity, which is very useful in actual job coding tests.<\/p>\n<p>Additionally, this problem can be modified in other forms, so I recommend practicing with various examples.<\/p>\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this lecture, we will cover one of the algorithm problems using Python, Finding the Maximum Sum of Continuous Elements. This problem can be approached in various ways and is very useful for studying algorithms. We will explain the necessary basic theories and the solution process in detail. Problem Description Given an integer array &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33710\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Finding Continuous 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":[145],"tags":[],"class_list":["post-33710","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 Continuous 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\/33710\/\" \/>\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 Continuous Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this lecture, we will cover one of the algorithm problems using Python, Finding the Maximum Sum of Continuous Elements. This problem can be approached in various ways and is very useful for studying algorithms. We will explain the necessary basic theories and the solution process in detail. Problem Description Given an integer array &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding Continuous Sum&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33710\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:19:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:01+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\/33710\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33710\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding Continuous Sum\",\"datePublished\":\"2024-11-01T09:19:30+00:00\",\"dateModified\":\"2024-11-01T11:47:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33710\/\"},\"wordCount\":466,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33710\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33710\/\",\"name\":\"Python Coding Test Course, Finding Continuous Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:19:30+00:00\",\"dateModified\":\"2024-11-01T11:47:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33710\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33710\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33710\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Finding Continuous 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":"Python Coding Test Course, Finding Continuous 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\/33710\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding Continuous Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this lecture, we will cover one of the algorithm problems using Python, Finding the Maximum Sum of Continuous Elements. This problem can be approached in various ways and is very useful for studying algorithms. We will explain the necessary basic theories and the solution process in detail. Problem Description Given an integer array &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding Continuous Sum\"","og_url":"https:\/\/atmokpo.com\/w\/33710\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:19:30+00:00","article_modified_time":"2024-11-01T11:47:01+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\/33710\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33710\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding Continuous Sum","datePublished":"2024-11-01T09:19:30+00:00","dateModified":"2024-11-01T11:47:01+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33710\/"},"wordCount":466,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33710\/","url":"https:\/\/atmokpo.com\/w\/33710\/","name":"Python Coding Test Course, Finding Continuous Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:19:30+00:00","dateModified":"2024-11-01T11:47:01+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33710\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33710\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33710\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Finding Continuous 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\/33710","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=33710"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33710\/revisions"}],"predecessor-version":[{"id":33711,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33710\/revisions\/33711"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}