{"id":33313,"date":"2024-11-01T09:15:26","date_gmt":"2024-11-01T09:15:26","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33313"},"modified":"2024-11-01T11:39:14","modified_gmt":"2024-11-01T11:39:14","slug":"java-coding-test-course-range-sum","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33313\/","title":{"rendered":"Java Coding Test Course, Range Sum"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this post, we will discuss the range sum problem for coding test preparation using Java. The range sum problem is one where we efficiently calculate the sum of elements in a specific range of a given array, and it is a common type of question in algorithm problems.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given an array <code>A<\/code>, there are <code>Q<\/code> queries. Each query consists of two integers <code>L<\/code> and <code>R<\/code>, which represent the indices of the array <code>A<\/code>. For each query, compute the range sum from index <code>L<\/code> to index <code>R<\/code>.<\/p>\n<h3>Input<\/h3>\n<pre>\n    The first line contains the size of the array <code>N<\/code> and the number of queries <code>Q<\/code>.\n    The second line contains <code>N<\/code> integers <code>A[1], A[2], ..., A[N]<\/code>.\n    Following that, <code>Q<\/code> lines are given with <code>L<\/code> and <code>R<\/code> separated by space.\n    <\/pre>\n<h3>Output<\/h3>\n<pre>\n    Print the range sum for each query, one per line.\n    <\/pre>\n<h2>Example Input<\/h2>\n<pre>\n    5 3\n    1 2 3 4 5\n    1 3\n    2 4\n    1 5\n    <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n    6\n    9\n    15\n    <\/pre>\n<h2>Problem Solving Strategy<\/h2>\n<p>There are various ways to handle the range sum problem, but an inefficient way is to directly loop through and calculate the range sum for each query. In this case, the worst-case time complexity could be <code>O(N * Q)<\/code>. Therefore, we need to find a method to calculate the range sum quickly.<\/p>\n<h3>Preprocessing Approach<\/h3>\n<p>One efficient method is to store the range sums in a cumulative array through preprocessing. This allows us to calculate the range sum for each query in <code>O(1)<\/code>.<\/p>\n<ol>\n<li>First, create a cumulative sum array <code>sum<\/code>. Initialize it with <code>sum[0] = 0<\/code>, and set <code>sum[i] = sum[i - 1] + A[i - 1]<\/code>.<\/li>\n<li>For each query, to find the sum in the range <code>[L, R]<\/code>, use the formula <code>sum[R] - sum[L - 1]<\/code>.<\/li>\n<\/ol>\n<h2>Java Code Implementation<\/h2>\n<pre>\n    import java.util.Scanner;\n\n    public class RangeSum {\n        public static void main(String[] args) {\n            Scanner sc = new Scanner(System.in);\n            \n            \/\/ Input the size of the array and the number of queries\n            int N = sc.nextInt();\n            int Q = sc.nextInt();\n            \n            \/\/ Input the array\n            int[] A = new int[N];\n            for (int i = 0; i < N; i++) {\n                A[i] = sc.nextInt();\n            }\n            \n            \/\/ Create cumulative sum array\n            long[] sum = new long[N + 1];\n            for (int i = 1; i <= N; i++) {\n                sum[i] = sum[i - 1] + A[i - 1];\n            }\n            \n            \/\/ Process each query\n            for (int i = 0; i < Q; i++) {\n                int L = sc.nextInt();\n                int R = sc.nextInt();\n                long rangeSum = sum[R] - sum[L - 1];\n                System.out.println(rangeSum);\n            }\n            \n            sc.close();\n        }\n    }\n    <\/pre>\n<h2>Code Explanation<\/h2>\n<p>The code above operates in the following manner:<\/p>\n<ol>\n<li>First, it reads the size of the array <code>N<\/code> and the number of queries <code>Q<\/code> from the user.<\/li>\n<li>Then, it initializes the elements of the array <code>A<\/code>.<\/li>\n<li>A <code>sum<\/code> array is created to store the cumulative sums of each element, initializing index 0 to 0 to facilitate easy access to <code>sum[i]<\/code>.<\/li>\n<li>For each query, it calculates the range sum using the provided <code>L<\/code> and <code>R<\/code> and outputs the result.<\/li>\n<\/ol>\n<h2>Testing and Validation<\/h2>\n<p>After writing the code, it is essential to verify that each query returns the correct result through various inputs. In addition to the example input, we should experiment with additional cases and review the results. Here are a few examples:<\/p>\n<ul>\n<li>Input: <code>1 1<\/code> \u2192 Output: <code>1<\/code><\/li>\n<li>Input: <code>2 5<\/code> \u2192 Output: <code>14<\/code><\/li>\n<li>Input: <code>3 3<\/code> \u2192 Output: <code>3<\/code><\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this post, we explored the process of solving the range sum problem. It is important to learn how to efficiently calculate range sums through preprocessing, and practicing with arrays and loops in Java will help develop good coding skills. I hope to tackle more algorithm problems in the future to enhance your technical abilities.<\/p>\n<p>Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this post, we will discuss the range sum problem for coding test preparation using Java. The range sum problem is one where we efficiently calculate the sum of elements in a specific range of a given array, and it is a common type of question in algorithm problems. Problem Description Given an array &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33313\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Range Sum&#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-33313","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, Range Sum - \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\/33313\/\" \/>\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, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this post, we will discuss the range sum problem for coding test preparation using Java. The range sum problem is one where we efficiently calculate the sum of elements in a specific range of a given array, and it is a common type of question in algorithm problems. Problem Description Given an array &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Range Sum&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33313\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:26+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Range Sum\",\"datePublished\":\"2024-11-01T09:15:26+00:00\",\"dateModified\":\"2024-11-01T11:39:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/\"},\"wordCount\":380,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33313\/\",\"name\":\"Java Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:26+00:00\",\"dateModified\":\"2024-11-01T11:39:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33313\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33313\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Range Sum\"}]},{\"@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, Range Sum - \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\/33313\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this post, we will discuss the range sum problem for coding test preparation using Java. The range sum problem is one where we efficiently calculate the sum of elements in a specific range of a given array, and it is a common type of question in algorithm problems. Problem Description Given an array &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Range Sum\"","og_url":"https:\/\/atmokpo.com\/w\/33313\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:26+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33313\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33313\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Range Sum","datePublished":"2024-11-01T09:15:26+00:00","dateModified":"2024-11-01T11:39:14+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33313\/"},"wordCount":380,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33313\/","url":"https:\/\/atmokpo.com\/w\/33313\/","name":"Java Coding Test Course, Range Sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:26+00:00","dateModified":"2024-11-01T11:39:14+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33313\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33313\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33313\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Range Sum"}]},{"@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\/33313","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=33313"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33313\/revisions"}],"predecessor-version":[{"id":33314,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33313\/revisions\/33314"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}