<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(mnlogn) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/omnlogn/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/omnlogn/</link>
	<description></description>
	<lastBuildDate>Sun, 25 Oct 2020 05:35:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(mnlogn) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/omnlogn/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1630. Arithmetic Subarrays</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1630-arithmetic-subarrays/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1630-arithmetic-subarrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 25 Oct 2020 05:31:32 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Arithmetic]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(mnlogn)]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7552</guid>

					<description><![CDATA[<p>A sequence of numbers is called&#160;arithmetic&#160;if it consists of at least two elements, and the difference between every two consecutive elements is the same. More&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1630-arithmetic-subarrays/">花花酱 LeetCode 1630. Arithmetic Subarrays</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>For example, these are&nbsp;<strong>arithmetic</strong>&nbsp;sequences:</p>



<pre class="crayon-plain-tag">1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9</pre>



<p>The following sequence is not&nbsp;<strong>arithmetic</strong>:</p>



<pre class="wp-block-preformatted;crayon:false">1, 1, 2, 5, 7</pre>



<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>



<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>



<p><strong>Example 1:</strong></p>



<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>
<strong>Output:</strong> <code>[true,false,true]</code>
<strong>Explanation:</strong>
In 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.
In the 1<sup>st</sup> query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In 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>



<p><strong>Example 2:</strong></p>



<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]
<strong>Output:</strong> [false,true,false,false,true,true]
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Sort the range of each query and check.</p>



<p>Time complexity: O(nlogn * m)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;bool&gt; checkArithmeticSubarrays(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; l, vector&lt;int&gt;&amp; r) {
    vector&lt;bool&gt; ans(l.size(), true);
    for (int i = 0; i &lt; l.size(); ++i) {
      vector&lt;int&gt; arr(begin(nums) + l[i], begin(nums) + r[i] + 1);
      sort(begin(arr), end(arr));
      const int d = arr[1] - arr[0];
      for (int j = 2; j &lt; arr.size() &amp;&amp; ans[i]; ++j)
        ans[i] = ans[i] &amp;&amp; (arr[j] - arr[j - 1] == d);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1630-arithmetic-subarrays/">花花酱 LeetCode 1630. Arithmetic Subarrays</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1630-arithmetic-subarrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
