{"id":34960,"date":"2024-11-01T09:34:02","date_gmt":"2024-11-01T09:34:02","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34960"},"modified":"2024-11-01T11:45:34","modified_gmt":"2024-11-01T11:45:34","slug":"kotlin-coding-test-course-finding-the-sum-of-intervals-3","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34960\/","title":{"rendered":"kotlin coding test course, finding the sum of intervals 3"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today, we will discuss the coding test problem &#8220;Finding the Sum of Intervals 3&#8221; using Kotlin. This problem requires efficiently calculating the sum of a specific interval of a given array. Such efficient algorithms are particularly important when needing to calculate interval sums repeatedly.<\/p>\n<h2>Problem Description<\/h2>\n<p>\n        You are given an array A consisting of N integers and Q queries. Each query contains two integers L and R, which require calculating the sum from the L-th index to the R-th index of array A. Note that the array starts from index 1. You are to output the result for all Q queries.\n    <\/p>\n<h3>Input Format<\/h3>\n<pre>\n    First line: N (1 \u2264 N \u2264 100,000)\n    Second line: N integers (each integer is -10,000,000 \u2264 A[i] \u2264 10,000,000)\n    Third line: Q (1 \u2264 Q \u2264 100,000)\n    The following Q lines contain each query in the form of L R.\n    <\/pre>\n<h3>Output Format<\/h3>\n<pre>\n    Output the interval sum for each of the Q lines.\n    <\/pre>\n<h2>Approach<\/h2>\n<p>\n        The basic method is to directly traverse the array to calculate the sum for the given L and R. However, this has a time complexity of O(Q * N), which could involve up to 1 billion operations in the worst case when both Q and N reach their maximum of 100,000. Therefore, this approach is inefficient. This problem requires a method to handle queries with a time complexity of O(1) through preprocessing.\n    <\/p>\n<p>\n        One of the efficient methods to solve the interval sum problem is to use a <strong>cumulative sum array<\/strong>. By using the cumulative sum array, the sum for a specific interval L ~ R can be easily calculated.\n    <\/p>\n<h3>Cumulative Sum Array<\/h3>\n<p>\n        Define the cumulative sum array P. P[i] stores the sum from the first element of array A to the i-th element.\n    <\/p>\n<pre>\n    P[i] = A[1] + A[2] + ... + A[i]\n    <\/pre>\n<p>\n        The sum for the interval L ~ R can then be calculated as follows:\n    <\/p>\n<pre>\n    sum(L, R) = P[R] - P[L - 1]\n    <\/pre>\n<p>\n        This method allows us to compute the cumulative sum array in O(N), and subsequently obtain results for each query in O(1). The overall time complexity becomes O(N + Q).\n    <\/p>\n<h2>Code Implementation<\/h2>\n<h3>Kotlin Code<\/h3>\n<pre>\n    fun main() {\n        val reader = System.`in`.bufferedReader()\n        val n = reader.readLine().toInt()\n        val a = reader.readLine().split(\" \").map { it.toInt() }\n        val q = reader.readLine().toInt()\n        \n        \/\/ Initialize cumulative sum array\n        val p = LongArray(n + 1)\n        \n        \/\/ Calculate cumulative sum\n        for (i in 1..n) {\n            p[i] = p[i - 1] + a[i - 1]\n        }\n\n        val result = StringBuilder()\n        for (i in 0 until q) {\n            val (l, r) = reader.readLine().split(\" \").map { it.toInt() }\n            \/\/ Calculate interval sum\n            result.append(p[r] - p[l - 1]).append(\"\\n\")\n        }\n\n        \/\/ Print results\n        println(result.toString())\n    }\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        The above code is a solution to the &#8220;Finding the Sum of Intervals 3&#8221; problem written in Kotlin. Let&#8217;s explain each part.\n    <\/p>\n<h3>1. Input Handling<\/h3>\n<p>\n        First, it receives the values for N, A, and Q. N signifies the size of the array, A is the integer array, and Q represents the number of given queries.\n    <\/p>\n<h3>2. Cumulative Sum Array Creation<\/h3>\n<p>\n        A cumulative sum array P is created. P[0] is initialized to 0 and the cumulative sums from P[1] to P[n] are calculated. This will store the results of adding each element of the array A.\n    <\/p>\n<h3>3. Query Processing<\/h3>\n<p>\n        For each query, the L and R values are received, allowing the interval sum to be computed within O(1) time using the cumulative sum array. This result is added to a StringBuilder and printed at the end.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        Through the &#8220;Finding the Sum of Intervals 3&#8221; problem, we learned the importance of using cumulative sum arrays. This technique allows the efficient handling of queries even with large datasets. When solving algorithm problems, it is essential to find the optimal method by considering time complexity. I hope this helps you prepare for your coding tests.\n    <\/p>\n<h3>Learn More<\/h3>\n<p>\n        In the next lecture, we will cover a variety of algorithm problems. Keep preparing for coding tests and practice a lot. Thank you!\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today, we will discuss the coding test problem &#8220;Finding the Sum of Intervals 3&#8221; using Kotlin. This problem requires efficiently calculating the sum of a specific interval of a given array. Such efficient algorithms are particularly important when needing to calculate interval sums repeatedly. Problem Description You are given an array A consisting of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34960\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, finding the sum of intervals 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":[106],"tags":[],"class_list":["post-34960","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>kotlin coding test course, finding the sum of intervals 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\/34960\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, finding the sum of intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today, we will discuss the coding test problem &#8220;Finding the Sum of Intervals 3&#8221; using Kotlin. This problem requires efficiently calculating the sum of a specific interval of a given array. Such efficient algorithms are particularly important when needing to calculate interval sums repeatedly. Problem Description You are given an array A consisting of &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, finding the sum of intervals 3&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34960\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:34+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\/34960\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34960\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, finding the sum of intervals 3\",\"datePublished\":\"2024-11-01T09:34:02+00:00\",\"dateModified\":\"2024-11-01T11:45:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34960\/\"},\"wordCount\":507,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34960\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34960\/\",\"name\":\"kotlin coding test course, finding the sum of intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:02+00:00\",\"dateModified\":\"2024-11-01T11:45:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34960\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34960\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34960\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, finding the sum of intervals 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":"kotlin coding test course, finding the sum of intervals 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\/34960\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, finding the sum of intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today, we will discuss the coding test problem &#8220;Finding the Sum of Intervals 3&#8221; using Kotlin. This problem requires efficiently calculating the sum of a specific interval of a given array. Such efficient algorithms are particularly important when needing to calculate interval sums repeatedly. Problem Description You are given an array A consisting of &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, finding the sum of intervals 3\"","og_url":"https:\/\/atmokpo.com\/w\/34960\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:02+00:00","article_modified_time":"2024-11-01T11:45:34+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\/34960\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34960\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, finding the sum of intervals 3","datePublished":"2024-11-01T09:34:02+00:00","dateModified":"2024-11-01T11:45:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34960\/"},"wordCount":507,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34960\/","url":"https:\/\/atmokpo.com\/w\/34960\/","name":"kotlin coding test course, finding the sum of intervals 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:02+00:00","dateModified":"2024-11-01T11:45:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34960\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34960\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34960\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, finding the sum of intervals 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\/34960","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=34960"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34960\/revisions"}],"predecessor-version":[{"id":34961,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34960\/revisions\/34961"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}