{"id":33315,"date":"2024-11-01T09:15:28","date_gmt":"2024-11-01T09:15:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33315"},"modified":"2024-11-01T11:39:14","modified_gmt":"2024-11-01T11:39:14","slug":"java-coding-test-course-calculating-interval-sum-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33315\/","title":{"rendered":"Java Coding Test Course, Calculating Interval Sum 1"},"content":{"rendered":"<article>\n<p>Hello! Today, I would like to take an in-depth look at one of the algorithms frequently featured in Java coding tests, which is calculating the range sum. In particular, this course will select a problem related to computing the range sum and explain the solution process in detail.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Given an integer array <code>arr<\/code>, several queries are provided. Each query includes two integers <code>start<\/code> and <code>end<\/code>, and the task is to calculate <code>arr[start] + arr[start + 1] + ... + arr[end]<\/code>. What is required in this problem is to efficiently compute the range sum for each query.<\/p>\n<h3>Example Problem<\/h3>\n<pre>\n    Input:\n    arr = [1, 2, 3, 4, 5]\n    queries = [[0, 2], [1, 3], [2, 4]]\n    \n    Output:\n    [6, 9, 12]\n    <\/pre>\n<h2>Solution Algorithm<\/h2>\n<p>To solve this problem, we must efficiently handle multiple range sums on the array. Essentially, repeating the sum of parts of the array for each query may be inefficient. To reduce this inefficiency, I will introduce an approach using the &#8220;prefix sum array.&#8221;<\/p>\n<h3>Explanation of Prefix Sum Array<\/h3>\n<p>A prefix sum array is a method of preprocessing the elements of a given array by accumulating them. This allows for the calculation of each range sum in O(1) time complexity. The definition of the prefix sum array is as follows:<\/p>\n<pre>\n    prefix[i] = arr[0] + arr[1] + ... + arr[i]\n    <\/pre>\n<p>In other words, the sum of the range <code>arr[i] ~ arr[j]<\/code> can be computed as <code>prefixSum[j] - prefixSum[i-1]<\/code>. Here, <code>prefixSum[-1]<\/code> is assumed to be 0.<\/p>\n<h3>Implementation Steps<\/h3>\n<ol>\n<li><strong>Create the Prefix Sum Array<\/strong>: Generate the prefix sum array using the given array.<\/li>\n<li><strong>Process Queries<\/strong>: Calculate the range sum for each query with O(1) complexity.<\/li>\n<\/ol>\n<h2>Java Code Implementation<\/h2>\n<pre>\n    public class RangeSum {\n        public static void main(String[] args) {\n            int[] arr = {1, 2, 3, 4, 5};\n            int[][] queries = {{0, 2}, {1, 3}, {2, 4}};\n            int[] result = rangeSum(arr, queries);\n            \n            for (int sum : result) {\n                System.out.println(sum);\n            }\n        }\n\n        public static int[] rangeSum(int[] arr, int[][] queries) {\n            int n = arr.length;\n            int[] prefixSum = new int[n];\n            prefixSum[0] = arr[0];\n\n            \/\/ Create the cumulative sum array\n            for (int i = 1; i < n; i++) {\n                prefixSum[i] = prefixSum[i - 1] + arr[i];\n            }\n\n            int[] results = new int[queries.length];\n            for (int i = 0; i < queries.length; i++) {\n                int start = queries[i][0];\n                int end = queries[i][1];\n\n                \/\/ Calculate the range sum\n                results[i] = (start > 0) ? (prefixSum[end] - prefixSum[start - 1]) : prefixSum[end];\n            }\n\n            return results;\n        }\n    }\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>The Java code above creates a user-defined class <code>RangeSum<\/code> and defines the array and queries in the <code>main<\/code> method. The <code>rangeSum<\/code> method generates the prefix sum array from the given array and calculates the range sum for each query.<\/p>\n<h3>1. Creating the Cumulative Sum Array<\/h3>\n<p>First, we initialize the first element, then create the cumulative sum array through a loop. This process has a time complexity of O(n), where n is the length of the array.<\/p>\n<h3>2. Processing Queries<\/h3>\n<p>For each query, we use the cumulative sum array to compute the range sum in O(1). This method should perform well in terms of efficiency, especially when there is a large amount of data or many queries.<\/p>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of this code is O(n + m), where n is the length of the array and m is the number of queries. Thus, it consumes O(n) time complexity during the initialization process, and O(1) for each query, making the total time complexity O(n + m). This ensures satisfactory performance.<\/p>\n<h2>Conclusion<\/h2>\n<p>Today, we dealt with the problem of calculating range sums in Java coding tests. We learned that utilizing a cumulative sum array allows for efficient calculations of range sums for multiple queries. This is a useful algorithm to remember for actual coding tests! I will prepare more algorithm problem-solving courses in the future. Thank you!<\/p>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today, I would like to take an in-depth look at one of the algorithms frequently featured in Java coding tests, which is calculating the range sum. In particular, this course will select a problem related to computing the range sum and explain the solution process in detail. Problem Definition Given an integer array arr, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33315\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Calculating Interval Sum 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":[139],"tags":[],"class_list":["post-33315","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Calculating Interval Sum 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\/33315\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Calculating Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today, I would like to take an in-depth look at one of the algorithms frequently featured in Java coding tests, which is calculating the range sum. In particular, this course will select a problem related to computing the range sum and explain the solution process in detail. Problem Definition Given an integer array arr, &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Calculating Interval Sum 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33315\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:14+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\/33315\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33315\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Calculating Interval Sum 1\",\"datePublished\":\"2024-11-01T09:15:28+00:00\",\"dateModified\":\"2024-11-01T11:39:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33315\/\"},\"wordCount\":459,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33315\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33315\/\",\"name\":\"Java Coding Test Course, Calculating Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:28+00:00\",\"dateModified\":\"2024-11-01T11:39:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33315\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33315\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33315\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Calculating Interval Sum 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 Coding Test Course, Calculating Interval Sum 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\/33315\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Calculating Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today, I would like to take an in-depth look at one of the algorithms frequently featured in Java coding tests, which is calculating the range sum. In particular, this course will select a problem related to computing the range sum and explain the solution process in detail. Problem Definition Given an integer array arr, &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Calculating Interval Sum 1\"","og_url":"https:\/\/atmokpo.com\/w\/33315\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:28+00:00","article_modified_time":"2024-11-01T11:39:14+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\/33315\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33315\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Calculating Interval Sum 1","datePublished":"2024-11-01T09:15:28+00:00","dateModified":"2024-11-01T11:39:14+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33315\/"},"wordCount":459,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33315\/","url":"https:\/\/atmokpo.com\/w\/33315\/","name":"Java Coding Test Course, Calculating Interval Sum 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:28+00:00","dateModified":"2024-11-01T11:39:14+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33315\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33315\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33315\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Calculating Interval Sum 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\/33315","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=33315"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33315\/revisions"}],"predecessor-version":[{"id":33316,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33315\/revisions\/33316"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}