{"id":7552,"date":"2020-10-24T22:31:32","date_gmt":"2020-10-25T05:31:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7552"},"modified":"2020-10-24T22:35:58","modified_gmt":"2020-10-25T05:35:58","slug":"leetcode-1630-arithmetic-subarrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1630-arithmetic-subarrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1630. Arithmetic Subarrays"},"content":{"rendered":"\n<p>A sequence of numbers is called&nbsp;<strong>arithmetic<\/strong>&nbsp;if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence&nbsp;<code>s<\/code>&nbsp;is arithmetic if and only if&nbsp;<code>s[i+1] - s[i] == s[1] - s[0]&nbsp;<\/code>for all valid&nbsp;<code>i<\/code>.<\/p>\n\n\n\n<p>For example, these are&nbsp;<strong>arithmetic<\/strong>&nbsp;sequences:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1, 3, 5, 7, 9\n7, 7, 7, 7\n3, -1, -5, -9<\/pre>\n\n\n\n<p>The following sequence is not&nbsp;<strong>arithmetic<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">1, 1, 2, 5, 7<\/pre>\n\n\n\n<p>You are given an array of&nbsp;<code>n<\/code>&nbsp;integers,&nbsp;<code>nums<\/code>, and two arrays of&nbsp;<code>m<\/code>&nbsp;integers each,&nbsp;<code>l<\/code>&nbsp;and&nbsp;<code>r<\/code>, representing the&nbsp;<code>m<\/code>&nbsp;range queries, where the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;query is the range&nbsp;<code>[l[i], r[i]]<\/code>. All the arrays are&nbsp;<strong>0-indexed<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list of&nbsp;<\/em><code>boolean<\/code>&nbsp;<em>elements<\/em>&nbsp;<code>answer<\/code><em>, where<\/em>&nbsp;<code>answer[i]<\/code>&nbsp;<em>is<\/em>&nbsp;<code>true<\/code>&nbsp;<em>if the subarray<\/em>&nbsp;<code>nums[l[i]], nums[l[i]+1], ... , nums[r[i]]<\/code><em>&nbsp;can be&nbsp;<strong>rearranged<\/strong>&nbsp;to form an&nbsp;<strong>arithmetic<\/strong>&nbsp;sequence, and<\/em>&nbsp;<code>false<\/code>&nbsp;<em>otherwise.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = <code>[4,6,5,9,3,7]<\/code>, l = <code>[0,0,2]<\/code>, r = <code>[2,3,5]<\/code>\n<strong>Output:<\/strong> <code>[true,false,true]<\/code>\n<strong>Explanation:<\/strong>\nIn the 0<sup>th<\/sup> query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.\nIn the 1<sup>st<\/sup> query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.\nIn the 2<sup>nd<\/sup> query, the subarray is <code>[5,9,3,7]. This<\/code> can be rearranged as <code>[3,5,7,9]<\/code>, which is an arithmetic sequence.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]\n<strong>Output:<\/strong> [false,true,false,false,true,true]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>m == l.length<\/code><\/li><li><code>m == r.length<\/code><\/li><li><code>2 &lt;= n &lt;= 500<\/code><\/li><li><code>1 &lt;= m &lt;= 500<\/code><\/li><li><code>0 &lt;= l[i] &lt; r[i] &lt; n<\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Sort the range of each query and check.<\/p>\n\n\n\n<p>Time complexity: O(nlogn * m)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n    vector<bool> ans(l.size(), true);\n    for (int i = 0; i < l.size(); ++i) {\n      vector<int> arr(begin(nums) + l[i], begin(nums) + r[i] + 1);\n      sort(begin(arr), end(arr));\n      const int d = arr[1] - arr[0];\n      for (int j = 2; j < arr.size() &#038;&#038; ans[i]; ++j)\n        ans[i] = ans[i] &#038;&#038; (arr[j] - arr[j - 1] == d);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A sequence of numbers is called&nbsp;arithmetic&nbsp;if it consists of at least two elements, and the difference between every two consecutive elements is the same. More&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[662,20,177,664,15],"class_list":["post-7552","post","type-post","status-publish","format-standard","hentry","category-array","tag-arithmetic","tag-array","tag-medium","tag-omnlogn","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7552"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7552\/revisions"}],"predecessor-version":[{"id":7556,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7552\/revisions\/7556"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}