{"id":34040,"date":"2024-11-01T09:23:26","date_gmt":"2024-11-01T09:23:26","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34040"},"modified":"2024-11-01T10:53:53","modified_gmt":"2024-11-01T10:53:53","slug":"c-coding-test-course-finding-the-product-of-a-range","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34040\/","title":{"rendered":"C# Coding Test Course, Finding the Product of a Range"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>There is a given integer array <code>nums<\/code> and a query array <code>queries<\/code>. Each query consists of two integers <code>(l, r)<\/code>, requesting the value of <code>nums[l] * nums[l + 1] * ... * nums[r]<\/code>. The size of the <code>nums<\/code> array can be up to <code>10^5<\/code>, and the length of <code>queries<\/code> can be up to <code>10^4<\/code>. Since the result of the multiplication can be very large, the output must be the remainder when divided by <code>10^9 + 7<\/code>.<\/p>\n<h2>Input Format<\/h2>\n<p>The first line contains the size of the array <code>n<\/code> and the number of queries <code>q<\/code>. The second line contains an array <code>nums<\/code> of <code>n<\/code> integers, followed by <code>q<\/code> lines where each line contains the query values <code>l<\/code> and <code>r<\/code>.<\/p>\n<h2>Output Format<\/h2>\n<p>Output the results of the calculations for each query, one result per line.<\/p>\n<h2>Example<\/h2>\n<div class=\"example\">\n<strong>Input<\/strong><br \/>\n        5 3<br \/>\n        2 3 4 5 6<br \/>\n        0 2<br \/>\n        1 3<br \/>\n        2 4<br \/>\n<strong>Output<\/strong><br \/>\n        24<br \/>\n        60<br \/>\n        120\n<\/div>\n<h2>Solution Explanation<\/h2>\n<p>To solve this problem, we need a method to quickly compute the product over a range. Simply iterating through the range for each query to calculate the product would lead to a time complexity of <code>O(q * n)<\/code>, which could result in up to <code>10^9<\/code> operations in the worst case. This is impractical, so we use a Prefix Product strategy to reduce time complexity.<\/p>\n<h3>Prefix Product Method<\/h3>\n<p>First, we store the cumulative product of the elements in the <code>nums<\/code> array to quickly calculate the results of the queries.<\/p>\n<pre><code>C#\nint mod = 1000000007;\nint[] prefixProduct = new int[n + 1];\nprefixProduct[0] = 1;\n\nfor (int i = 1; i &lt;= n; i++) {\n    prefixProduct[i] = (int)((1L * prefixProduct[i - 1] * nums[i - 1]) % mod);\n}\n<\/code><\/pre>\n<p>Here, <code>prefixProduct[i]<\/code> stores the value of <code>nums[0] * nums[1] * ... * nums[i - 1]<\/code>. With this structure, we can easily and quickly compute the product over a range.<\/p>\n<h3>Calculating the Range Product<\/h3>\n<p>Now, for each query <code>(l, r)<\/code>, the range product <code>nums[l] * nums[l + 1] * ... * nums[r]<\/code> can be calculated as follows:<\/p>\n<pre><code>C#\nint result = (int)((1L * prefixProduct[r + 1] * modInverse(prefixProduct[l]) % mod) % mod);\n<\/code><\/pre>\n<p>Here, the <code>modInverse<\/code> function calculates the modular inverse. The inverse can be implemented as follows:<\/p>\n<pre><code>C#\nint modInverse(int a) {\n    return power(a, mod - 2);\n}\n\nint power(int x, int y) {\n    int res = 1;\n    while (y &gt; 0) {\n        if ((y &amp; 1) == 1) {\n            res = (int)((1L * res * x) % mod);\n        }\n        y &gt;&gt;= 1;\n        x = (int)((1L * x * x) % mod);\n    }\n    return res;\n}\n<\/code><\/pre>\n<h3>Final Code<\/h3>\n<p>Based on the above explanation, the complete code would look like this:<\/p>\n<pre><code>C#\nusing System;\n\nclass Program {\n    static int mod = 1000000007;\n\n    static void Main() {\n        string[] firstLine = Console.ReadLine().Split();\n        int n = int.Parse(firstLine[0]);\n        int q = int.Parse(firstLine[1]);\n        int[] nums = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);\n        int[] prefixProduct = new int[n + 1];\n        prefixProduct[0] = 1;\n\n        for (int i = 1; i &lt;= n; i++) {\n            prefixProduct[i] = (int)((1L * prefixProduct[i - 1] * nums[i - 1]) % mod);\n        }\n\n        for (int i = 0; i &lt; q; i++) {\n            string[] query = Console.ReadLine().Split();\n            int l = int.Parse(query[0]);\n            int r = int.Parse(query[1]);\n            int result = (int)((1L * prefixProduct[r + 1] * modInverse(prefixProduct[l]) % mod) % mod);\n            Console.WriteLine(result);\n        }\n    }\n\n    static int modInverse(int a) {\n        return power(a, mod - 2);\n    }\n\n    static int power(int x, int y) {\n        int res = 1;\n        while (y &gt; 0) {\n            if ((y &amp; 1) == 1) {\n                res = (int)((1L * res * x) % mod);\n            }\n            y &gt;&gt;= 1;\n            x = (int)((1L * x * x) % mod);\n        }\n        return res;\n    }\n}\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we learned how to effectively calculate the product over an array range by using the Prefix Product method and the concept of modular inverses. This method can be applied to handle more complex query processes as well. By utilizing the features of the C# language, efficient code can be written, further enhancing algorithm problem-solving skills.<\/p>\n<p>Additionally, the Prefix Product technique used in this problem can be useful in various forms of query problems, so it is good to try applying it to different problems. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description There is a given integer array nums and a query array queries. Each query consists of two integers (l, r), requesting the value of nums[l] * nums[l + 1] * &#8230; * nums[r]. The size of the nums array can be up to 10^5, and the length of queries can be up to &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34040\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Finding the Product of a Range&#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":[90],"tags":[],"class_list":["post-34040","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Coding Test Course, Finding the Product of a Range - \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\/34040\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Finding the Product of a Range - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description There is a given integer array nums and a query array queries. Each query consists of two integers (l, r), requesting the value of nums[l] * nums[l + 1] * ... * nums[r]. The size of the nums array can be up to 10^5, and the length of queries can be up to &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Finding the Product of a Range&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34040\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:53+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\/34040\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34040\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Finding the Product of a Range\",\"datePublished\":\"2024-11-01T09:23:26+00:00\",\"dateModified\":\"2024-11-01T10:53:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34040\/\"},\"wordCount\":359,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34040\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34040\/\",\"name\":\"C# Coding Test Course, Finding the Product of a Range - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:26+00:00\",\"dateModified\":\"2024-11-01T10:53:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34040\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34040\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34040\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Finding the Product of a Range\"}]},{\"@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":"C# Coding Test Course, Finding the Product of a Range - \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\/34040\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Finding the Product of a Range - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description There is a given integer array nums and a query array queries. Each query consists of two integers (l, r), requesting the value of nums[l] * nums[l + 1] * ... * nums[r]. The size of the nums array can be up to 10^5, and the length of queries can be up to &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Finding the Product of a Range\"","og_url":"https:\/\/atmokpo.com\/w\/34040\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:26+00:00","article_modified_time":"2024-11-01T10:53:53+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\/34040\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34040\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Finding the Product of a Range","datePublished":"2024-11-01T09:23:26+00:00","dateModified":"2024-11-01T10:53:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34040\/"},"wordCount":359,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34040\/","url":"https:\/\/atmokpo.com\/w\/34040\/","name":"C# Coding Test Course, Finding the Product of a Range - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:26+00:00","dateModified":"2024-11-01T10:53:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34040\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34040\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34040\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Finding the Product of a Range"}]},{"@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\/34040","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=34040"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34040\/revisions"}],"predecessor-version":[{"id":34041,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34040\/revisions\/34041"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}