{"id":33608,"date":"2024-11-01T09:18:27","date_gmt":"2024-11-01T09:18:27","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33608"},"modified":"2024-11-01T11:47:26","modified_gmt":"2024-11-01T11:47:26","slug":"python-coding-test-course-sum-of-remainders","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33608\/","title":{"rendered":"python coding test course, sum of remainders"},"content":{"rendered":"<div class=\"post\">\n<p>Hello everyone! In this lecture, we will look at one of the algorithm problems frequently encountered in coding tests using Python, which is &#8216;Calculating the Remainder of the Sum&#8217;. This problem is a good example that helps in understanding and using modular operations and processing large amounts of data.<\/p>\n<h2>Problem Description<\/h2>\n<p>You are tasked with solving the problem of finding the remainder when the sum of subarrays of an array A consisting of N integers is divided by K. A subarray is defined as a contiguous set of numbers defined by a starting index and an ending index of the array.<\/p>\n<p>For example, if the array A is [3, 1, 4, 1, 5] and K is 2, you need to find the remainder when the sum of all subarrays is divided by K. This problem requires basic math and programming skills and can be solved using various approaches.<\/p>\n<h2>Input<\/h2>\n<ul>\n<li>The first line contains two integers N (1 \u2264 N \u2264 100,000) and K (1 \u2264 K \u2264 10,000).<\/li>\n<li>The second line contains N integers A[1], A[2], &#8230;, A[N] (0 \u2264 A[i] \u2264 10<sup>9<\/sup>).<\/li>\n<\/ul>\n<h2>Output<\/h2>\n<p>Print the number of subarrays whose sums give a remainder of 0 when divided by K.<\/p>\n<h2>Example<\/h2>\n<pre>\n    Input\n    5 2\n    3 1 4 1 5\n\n    Output\n    4\n    <\/pre>\n<h2>Approach<\/h2>\n<p>To solve this problem, the following approaches can be utilized.<\/p>\n<h3>1. Understanding the Definition of Subarrays<\/h3>\n<p>A subarray is a set of consecutive elements from the original array, so you need to generate all possible subarrays from the given array, then calculate the sum of each subarray and check the remainder when divided by K.<\/p>\n<h3>2. Efficient Calculation Method<\/h3>\n<p>Directly computing subarrays can result in a time complexity of O(N<sup>2<\/sup>), which is inefficient in the worst case. Therefore, you can solve this problem in O(N) using cumulative sums and hash maps.<\/p>\n<h3>3. Using Cumulative Sums and Modular Operations<\/h3>\n<p>Calculate the cumulative sum and store the remainder when divided by K. If the same remainder value appears, you can utilize the fact that the sum of the subarray between those indices can be divided by K.<\/p>\n<h2>Code Example<\/h2>\n<pre>\n    <code>\n    def count_subarrays_with_zero_modulo(n, k, arr):\n        count = 0\n        mod_count = [0] * k\n        current_sum = 0\n        \n        for num in arr:\n            current_sum += num\n            mod = current_sum % k\n            \n            if mod < 0:  # Python's mod can be negative, adjust it\n                mod += k\n            \n            # If current modulo is zero, we can count it\n            if mod == 0:\n                count += 1\n            \n            # Add the number of times this modulo has appeared before\n            count += mod_count[mod]\n            mod_count[mod] += 1\n            \n        return count\n\n    # Example usage\n    n = 5\n    k = 2\n    arr = [3, 1, 4, 1, 5]\n    result = count_subarrays_with_zero_modulo(n, k, arr)\n    print(result)  # Output: 4\n    <\/code>\n    <\/pre>\n<h2>Result Analysis<\/h2>\n<p>In the above code, the function count_subarrays_with_zero_modulo counts the number of subarrays in the array whose sum is divisible by K. In this process, it calculates the sum at each index using cumulative sums, and counts occurrences of the same remainder using a hash map. By doing this, we can solve the problem with a time complexity of O(N).<\/p>\n<h2>Conclusion<\/h2>\n<p>Through this lecture, you learned about the approach to solving the remainder sum problem and acquired efficient coding techniques. These techniques can be applied in various situations requiring large-scale data processing and will significantly enhance your algorithm problem-solving skills.<\/p>\n<p>Additionally, try to gain experience by attempting solutions for similar problems. In the next lecture, we will meet with another interesting topic. Thank you!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this lecture, we will look at one of the algorithm problems frequently encountered in coding tests using Python, which is &#8216;Calculating the Remainder of the Sum&#8217;. This problem is a good example that helps in understanding and using modular operations and processing large amounts of data. Problem Description You are tasked with &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33608\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python coding test course, sum of remainders&#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-33608","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, sum of remainders - \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\/33608\/\" \/>\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, sum of remainders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello everyone! In this lecture, we will look at one of the algorithm problems frequently encountered in coding tests using Python, which is &#8216;Calculating the Remainder of the Sum&#8217;. This problem is a good example that helps in understanding and using modular operations and processing large amounts of data. Problem Description You are tasked with &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, sum of remainders&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33608\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47: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\/33608\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33608\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, sum of remainders\",\"datePublished\":\"2024-11-01T09:18:27+00:00\",\"dateModified\":\"2024-11-01T11:47:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33608\/\"},\"wordCount\":457,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33608\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33608\/\",\"name\":\"python coding test course, sum of remainders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:27+00:00\",\"dateModified\":\"2024-11-01T11:47:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33608\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33608\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33608\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python coding test course, sum of remainders\"}]},{\"@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, sum of remainders - \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\/33608\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, sum of remainders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello everyone! In this lecture, we will look at one of the algorithm problems frequently encountered in coding tests using Python, which is &#8216;Calculating the Remainder of the Sum&#8217;. This problem is a good example that helps in understanding and using modular operations and processing large amounts of data. Problem Description You are tasked with &hellip; \ub354 \ubcf4\uae30 \"python coding test course, sum of remainders\"","og_url":"https:\/\/atmokpo.com\/w\/33608\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:27+00:00","article_modified_time":"2024-11-01T11:47: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\/33608\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33608\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, sum of remainders","datePublished":"2024-11-01T09:18:27+00:00","dateModified":"2024-11-01T11:47:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33608\/"},"wordCount":457,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33608\/","url":"https:\/\/atmokpo.com\/w\/33608\/","name":"python coding test course, sum of remainders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:27+00:00","dateModified":"2024-11-01T11:47:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33608\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33608\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33608\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python coding test course, sum of remainders"}]},{"@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\/33608","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=33608"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33608\/revisions"}],"predecessor-version":[{"id":33609,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33608\/revisions\/33609"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}