{"id":34956,"date":"2024-11-01T09:34:00","date_gmt":"2024-11-01T09:34:00","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34956"},"modified":"2024-11-01T11:45:35","modified_gmt":"2024-11-01T11:45:35","slug":"java-kotlin-coding-test-course-finding-the-sum-of-intervals-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34956\/","title":{"rendered":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Hello! In this post, we will take a closer look at how to solve the problem of calculating the sum of intervals using Kotlin.<br \/>\n        The interval sum problem is a common type encountered in programming contests and coding tests, and knowing how to solve it efficiently can help you quickly solve various problems.\n    <\/p>\n<h2>Problem Description<\/h2>\n<p>\n        The task is to calculate the sum of a specific interval when given an array. The aim is to solve the problem of<br \/>\n        calculating the sum for each interval given the array and several queries. For example, let&#8217;s assume the array <code>A<\/code> is as follows:\n    <\/p>\n<pre>\n        A = [10, 20, 30, 40, 50]\n    <\/pre>\n<p>\n        In this case, when we receive a query to find the sum in the interval (i, j) specified by the user,<br \/>\n        we need to return the corresponding sum.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        The first line contains the size of the array <code>N<\/code> and the number of queries <code>M<\/code>.<br \/>\n        Then, <code>N<\/code> integers are provided to form the array <code>A<\/code>.<br \/>\n        The next <code>M<\/code> lines each contain two integers <code>i<\/code> and <code>j<\/code>,<br \/>\n        indicating a query to calculate the sum from <code>A[i]<\/code> to <code>A[j]<\/code>.\n    <\/p>\n<h2>Output Format<\/h2>\n<p>\n        Print the sum for each query.\n    <\/p>\n<h3>Example Input<\/h3>\n<pre>\n    5 3\n    10 20 30 40 50\n    1 3\n    2 4\n    1 5\n    <\/pre>\n<h3>Example Output<\/h3>\n<pre>\n    60\n    90\n    150\n    <\/pre>\n<h2>Approach to Solve the Problem<\/h2>\n<p>\n        There are two ways to solve this problem.<br \/>\n        The first method is to simply iterate and calculate the sum for each query,<br \/>\n        and the second is to use the <strong>prefix sum array<\/strong>.<br \/>\n        The second method is much more efficient, so I will focus on explaining that.\n    <\/p>\n<h3>Prefix Sum Array<\/h3>\n<p>\n        By using a prefix sum array, we can calculate the interval sum in O(1) time complexity for each query.<br \/>\n        Since calculating the prefix sum array takes O(N) time,<br \/>\n        the total time complexity is O(N + M).<br \/>\n        This becomes more efficient as the number of queries increases.\n    <\/p>\n<h4>Definition of Prefix Sum Array<\/h4>\n<p>\n        A prefix sum array is an array that stores the cumulative sums of a given array.<br \/>\n        Specifically, we define an array <code>S<\/code> that stores the sum up to the i-th index of array <code>A<\/code>.<br \/>\n        It is represented as <code>S[i] = A[0] + A[1] + ... + A[i-1]<\/code>.\n    <\/p>\n<h4>How to Calculate Interval Sums<\/h4>\n<p>\n        The sum from <code>A[i]<\/code> to <code>A[j]<\/code> can be calculated as follows:<br \/>\n        <code>sum = S[j+1] - S[i]<\/code>\n<\/p>\n<p>\n        Here, <code>S[j+1]<\/code> is the sum from <code>A[0]<\/code> to <code>A[j]<\/code>,<br \/>\n        and <code>S[i]<\/code> is the sum from <code>A[0]<\/code> to <code>A[i-1]<\/code>.<br \/>\n        Therefore, by subtracting <code>S[i]<\/code> from <code>S[j+1]<\/code>, we obtain the sum from <code>A[i]<\/code> to <code>A[j]<\/code>.\n    <\/p>\n<h2>Code Implementation<\/h2>\n<p>\n        Now, let\u2019s write the code to solve this problem using Kotlin.\n    <\/p>\n<h3>Kotlin Code<\/h3>\n<pre>\n    fun main() {\n        val (n, m) = readLine()!!.split(\" \").map { it.toInt() }\n        val a = readLine()!!.split(\" \").map { it.toInt() }\n        \n        val s = IntArray(n + 1)\n        \n        for (i in 1..n) {\n            s[i] = s[i - 1] + a[i - 1]\n        }\n        \n        repeat(m) {\n            val (i, j) = readLine()!!.split(\" \").map { it.toInt() }\n            println(s[j] - s[i - 1])\n        }\n    }\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        1. The first line reads <code>N<\/code> and <code>M<\/code>.<br \/>\n        2. The second line reads the array <code>A<\/code>.<br \/>\n        3. The prefix sum array <code>S<\/code> is initialized with a size of <code>N + 1<\/code>.<br \/>\n        4. A loop calculates the values for the prefix sum array <code>S<\/code>.<br \/>\n        5. For each query, we calculate and print the sum for the respective interval.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        The interval sum problem is a useful technique that can be applied to various problems.<br \/>\n        Using a prefix sum array in Kotlin allows us to solve the problem much faster.<br \/>\n        I hope this tutorial helps you enhance your understanding of the interval sum problem and<br \/>\n        allows you to effectively utilize it in coding tests.\n    <\/p>\n<p>\n        In the next post, we will explore other types of problems extending the concept of interval sums.<br \/>\n        Keep continuing your learning!\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this post, we will take a closer look at how to solve the problem of calculating the sum of intervals using Kotlin. The interval sum problem is a common type encountered in programming contests and coding tests, and knowing how to solve it efficiently can help you quickly solve various problems. Problem Description &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34956\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Kotlin Coding Test Course, Finding the Sum of Intervals 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":[106],"tags":[],"class_list":["post-34956","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>Java Kotlin Coding Test Course, Finding the Sum of Intervals 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\/34956\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this post, we will take a closer look at how to solve the problem of calculating the sum of intervals using Kotlin. The interval sum problem is a common type encountered in programming contests and coding tests, and knowing how to solve it efficiently can help you quickly solve various problems. Problem Description &hellip; \ub354 \ubcf4\uae30 &quot;Java Kotlin Coding Test Course, Finding the Sum of Intervals 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34956\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:00+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\/34956\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34956\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1\",\"datePublished\":\"2024-11-01T09:34:00+00:00\",\"dateModified\":\"2024-11-01T11:45:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34956\/\"},\"wordCount\":507,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34956\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34956\/\",\"name\":\"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:00+00:00\",\"dateModified\":\"2024-11-01T11:45:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34956\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34956\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34956\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Kotlin Coding Test Course, Finding the Sum of Intervals 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":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 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\/34956\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this post, we will take a closer look at how to solve the problem of calculating the sum of intervals using Kotlin. The interval sum problem is a common type encountered in programming contests and coding tests, and knowing how to solve it efficiently can help you quickly solve various problems. Problem Description &hellip; \ub354 \ubcf4\uae30 \"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1\"","og_url":"https:\/\/atmokpo.com\/w\/34956\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:00+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\/34956\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34956\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1","datePublished":"2024-11-01T09:34:00+00:00","dateModified":"2024-11-01T11:45:35+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34956\/"},"wordCount":507,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34956\/","url":"https:\/\/atmokpo.com\/w\/34956\/","name":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:00+00:00","dateModified":"2024-11-01T11:45:35+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34956\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34956\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34956\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Kotlin Coding Test Course, Finding the Sum of Intervals 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\/34956","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=34956"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34956\/revisions"}],"predecessor-version":[{"id":34957,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34956\/revisions\/34957"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}