{"id":34958,"date":"2024-11-01T09:34:01","date_gmt":"2024-11-01T09:34:01","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34958"},"modified":"2024-11-01T11:45:35","modified_gmt":"2024-11-01T11:45:35","slug":"android-coding-test-course-finding-interval-sum-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34958\/","title":{"rendered":"Android Coding Test Course, Finding Interval Sum 2"},"content":{"rendered":"<p><body><\/p>\n<p>Among the various algorithms dealt with in coding tests, the &#8216;interval sum&#8217; problem is frequently encountered.<br \/>\nIn this post, we will solve a problem titled <strong>Calculating Interval Sums 2<\/strong>.<br \/>\nWe will particularly utilize the Kotlin programming language for this.<\/p>\n<h2>Problem Description<\/h2>\n<p>This problem presents a method to efficiently calculate the sum of a specific interval given multiple integers.<br \/>\nHere is the definition of the problem:<\/p>\n<pre>\nGiven integers N and M, \nN integers will be provided, \nand M queries will be given. \nEach query consists of two integers i and j, \nand the sum of integers between i and j needs to be calculated.\n\nInput Format:\n- The first line will contain N (1 \u2264 N \u2264 100,000) and M (1 \u2264 M \u2264 100,000).\n- The second line contains N integers. Each integer does not exceed 1,000,000.\n- Following M lines, i and j will be given. (1 \u2264 i \u2264 j \u2264 N)\n\nOutput Format:\n- For each query, output the sum from the i-th to the j-th number.\n<\/pre>\n<h2>Problem Solving Process<\/h2>\n<p>To solve this problem, an efficient algorithm is needed. Simply looping through each query to calculate the sum would result in a time complexity of O(N*M), which may lead to a timeout failure if the input is large.<\/p>\n<h3>1. Using Cumulative Sum Array<\/h3>\n<p>Instead of calculating all sums each time from a checking perspective, there is the option of using a cumulative sum array. By utilizing the cumulative sum array, the sum of a specific interval can be determined with O(1) time complexity.<\/p>\n<p>Defining the cumulative sum array <code>prefixSum<\/code>, <code>prefixSum[k]<\/code> represents the sum from the start of the array to the k-th number.<br \/>\nTherefore, it holds the following relationship:<\/p>\n<pre>\nprefixSum[k] = prefixSum[k-1] + array[k-1] (1 \u2264 k \u2264 N)\n<\/pre>\n<p>In this case, the sum of a specific interval can be calculated as follows:<\/p>\n<pre>\nsum(i, j) = prefixSum[j] - prefixSum[i-1]\n<\/pre>\n<h3>2. Implementing the Algorithm<\/h3>\n<p>Now, let&#8217;s implement the code based on this idea.<br \/>\nThe following code is an example of solving the problem using Kotlin:<\/p>\n<pre><code>\nfun main() {\n    val (n, m) = readLine()!!.split(\" \").map { it.toInt() }\n    val numbers = readLine()!!.split(\" \").map { it.toInt() }.toIntArray()\n    val prefixSum = LongArray(n + 1)\n\n    for (i in 1..n) {\n        prefixSum[i] = prefixSum[i - 1] + numbers[i - 1].toLong()\n    }\n\n    for (j in 0 until m) {\n        val (i, j) = readLine()!!.split(\" \").map { it.toInt() }\n        println(prefixSum[j] - prefixSum[i - 1])\n    }\n}\n<\/code><\/pre>\n<h3>3. Code Explanation<\/h3>\n<p>Let me explain how the above code calculates the interval sum:<\/p>\n<ul>\n<li><code>val (n, m) = readLine()!!.split(\" \").map { it.toInt() }<\/code>:<br \/>\n        Reads N and M from the first line.<\/li>\n<li><code>val numbers = readLine()!!.split(\" \").map { it.toInt() }.toIntArray()<\/code>:<br \/>\n        Receives N integers from the second line and converts them into an array.<\/li>\n<li><code>val prefixSum = LongArray(n + 1)<\/code>:<br \/>\n        Creates an array to store cumulative sums. (The 0th index is initialized to 0)<\/li>\n<li><code>for (i in 1..n)<\/code>:<br \/>\n        Uses a loop to calculate the cumulative sums. Saves the sum to <code>prefixSum[i]<\/code> up to the i-th number.<\/li>\n<li><code>for (j in 0 until m)<\/code>:<br \/>\n        Receives input for each query and calculates the interval sum.<\/li>\n<li><code>println(prefixSum[j] - prefixSum[i - 1])<\/code>:<br \/>\n        Outputs the sum from the i-th index to the j-th index.<\/li>\n<\/ul>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of the above code is as follows:<\/p>\n<ul>\n<li>Time taken to create the cumulative sum array: O(N)<\/li>\n<li>Time taken to calculate the interval sum for each of the M queries: O(M)<\/li>\n<\/ul>\n<p>Therefore, the overall time complexity is <code>O(N + M)<\/code>. This is a good method for effectively handling the interval sums of the given problem.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we efficiently solved the problem of calculating interval sums 2 using cumulative sums.<br \/>\nThis approach can be effectively applied to many algorithm problems, so try to apply it to various challenges.<\/p>\n<p>We dealt with the problem of quickly and efficiently calculating the sum of a specific interval from an array consisting of multiple integers.<br \/>\nWe have learned that various algorithm problems can be solved using data structures such as arrays and lists in Kotlin.<\/p>\n<h2>Additional Practice Problems<\/h2>\n<p>Please practice the following additional problems:<\/p>\n<ul>\n<li>Finding the minimum and maximum value of intervals in various query forms<\/li>\n<li>Finding unusual interval sums: Summing only intervals that meet specific conditions<\/li>\n<li>Finding interval sums in multidimensional arrays<\/li>\n<\/ul>\n<p>I hope this post helps you prepare for your coding tests.<br \/>\nThank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Among the various algorithms dealt with in coding tests, the &#8216;interval sum&#8217; problem is frequently encountered. In this post, we will solve a problem titled Calculating Interval Sums 2. We will particularly utilize the Kotlin programming language for this. Problem Description This problem presents a method to efficiently calculate the sum of a specific interval &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34958\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Android Coding Test Course, Finding Interval Sum 2&#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":[106],"tags":[],"class_list":["post-34958","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Coding Test Course, Finding Interval Sum 2 - \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\/34958\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Coding Test Course, Finding Interval Sum 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Among the various algorithms dealt with in coding tests, the &#8216;interval sum&#8217; problem is frequently encountered. In this post, we will solve a problem titled Calculating Interval Sums 2. We will particularly utilize the Kotlin programming language for this. Problem Description This problem presents a method to efficiently calculate the sum of a specific interval &hellip; \ub354 \ubcf4\uae30 &quot;Android Coding Test Course, Finding Interval Sum 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34958\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:35+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\/34958\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34958\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Android Coding Test Course, Finding Interval Sum 2\",\"datePublished\":\"2024-11-01T09:34:01+00:00\",\"dateModified\":\"2024-11-01T11:45:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34958\/\"},\"wordCount\":492,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34958\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34958\/\",\"name\":\"Android Coding Test Course, Finding Interval Sum 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:01+00:00\",\"dateModified\":\"2024-11-01T11:45:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34958\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34958\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34958\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android Coding Test Course, Finding Interval Sum 2\"}]},{\"@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":"Android Coding Test Course, Finding Interval Sum 2 - \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\/34958\/","og_locale":"ko_KR","og_type":"article","og_title":"Android Coding Test Course, Finding Interval Sum 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Among the various algorithms dealt with in coding tests, the &#8216;interval sum&#8217; problem is frequently encountered. In this post, we will solve a problem titled Calculating Interval Sums 2. We will particularly utilize the Kotlin programming language for this. Problem Description This problem presents a method to efficiently calculate the sum of a specific interval &hellip; \ub354 \ubcf4\uae30 \"Android Coding Test Course, Finding Interval Sum 2\"","og_url":"https:\/\/atmokpo.com\/w\/34958\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:01+00:00","article_modified_time":"2024-11-01T11:45:35+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\/34958\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34958\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Android Coding Test Course, Finding Interval Sum 2","datePublished":"2024-11-01T09:34:01+00:00","dateModified":"2024-11-01T11:45:35+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34958\/"},"wordCount":492,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34958\/","url":"https:\/\/atmokpo.com\/w\/34958\/","name":"Android Coding Test Course, Finding Interval Sum 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:01+00:00","dateModified":"2024-11-01T11:45:35+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34958\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34958\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34958\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Android Coding Test Course, Finding Interval Sum 2"}]},{"@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\/34958","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=34958"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34958\/revisions"}],"predecessor-version":[{"id":34959,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34958\/revisions\/34959"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}