<?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>Array &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/algorithms/array/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 13 Apr 2025 03:01:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>Array &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2161. Partition Array According to Given Pivot</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2161-partition-array-according-to-given-pivot/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2161-partition-array-according-to-given-pivot/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Apr 2025 03:01:02 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[partition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10348</guid>

					<description><![CDATA[最直接的方法就是扫描三遍，&#60;, = , > pivot。 时间复杂度：O(n)空间复杂度：O(1) // no extra space used except for output [crayon-67ff9b4a13bbd407203167/]]]></description>
										<content:encoded><![CDATA[
<p>最直接的方法就是扫描三遍，&lt;, = , > pivot。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(1) // no extra space used except for output</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  vector&lt;int&gt; pivotArray(vector&lt;int&gt;&amp; nums, int pivot) {
    vector&lt;int&gt; ans;
    for (int x: nums)
      if (x &lt; pivot) ans.push_back(x);
    for (int x : nums)
      if (x == pivot) ans.push_back(x);
    for (int x : nums)
      if (x &gt; pivot) ans.push_back(x);
    return ans;
  }
};</pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2161-partition-array-according-to-given-pivot/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2016. Maximum Difference Between Increasing Elements</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2016-maximum-difference-between-increasing-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2016-maximum-difference-between-increasing-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 11 Apr 2025 15:44:23 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[prefix max]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10332</guid>

					<description><![CDATA[数据规模 n &#60;= 1000。O(n2) 的算法也是能过的。 方法1: Brute Force 双重循环枚举所有的(nums[i], nums[j])的组合。 时间复杂度：O(n2)空间复杂度：O(1) [crayon-67ff9b4a16236484991794/] 方法2: 空间换时间 使用prefix sum算法，预先计算num[i] ~ nums[n-1]的最大值，存储在right[i]中。 时间复杂度：O(n)空间复杂度：O(n) [crayon-67ff9b4a1624e090308808/] 方法3: Running Min 从左到右遍历，记录当前为止出现过最小的值。比较 nums[i]&#8230;]]></description>
										<content:encoded><![CDATA[
<p>数据规模 n &lt;= 1000。O(n<sup>2</sup>) 的算法也是能过的。</p>



<p>方法1: Brute Force</p>



<p>双重循环枚举所有的(nums[i], nums[j])的组合。</p>



<p>时间复杂度：O(n<sup>2</sup>)<br>空间复杂度：O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int maximumDifference(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans = max(ans, nums[j] - nums[i]);
    return ans ? ans : -1;
  }
};</pre>



<p>方法2: 空间换时间</p>



<p>使用prefix sum算法，预先计算num[i] ~ nums[n-1]的最大值，存储在right[i]中。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(n)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int maximumDifference(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int ans = 0;
    vector&lt;int&gt; right(n, nums.back());
    for (int i = n - 2; i &gt;= 0; --i)
      right[i] = max(right[i + 1], nums[i]);
    for (int i = 0; i &lt; n; ++i)
      ans = max(ans, right[i] - nums[i]);
    return ans ? ans : -1;
  }
};</pre>



<p>方法3: Running Min</p>



<p>从左到右遍历，记录当前为止出现过最小的值。比较 nums[i] &#8211; 最小值 和 最优解。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int maximumDifference(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int ans = 0;
    int smallest = nums[0];
    for (int i = 1; i &lt; n; ++i) {
      ans = max(ans, nums[i] - smallest);
      smallest = min(smallest, nums[i]);
    }
    return ans ? ans : -1;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2016-maximum-difference-between-increasing-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2012. Sum of Beauty in the Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2012-sum-of-beauty-in-the-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2012-sum-of-beauty-in-the-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 11 Apr 2025 04:21:38 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[prefix max]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10326</guid>

					<description><![CDATA[数据规模比较大，算法必须是O(n)的。 可以预先使用O(n)时间计算nums[0] .. nums[i]的最大值，存在left[i]中，以及nums[i] .. nums[n-1]的最小值存在right[i]中。然后再按题目的定义计算即可。 时间复杂度：O(n)空间复杂度：O(n) [crayon-67ff9b4a16ec6903046058/]]]></description>
										<content:encoded><![CDATA[
<p>数据规模比较大，算法必须是O(n)的。</p>



<p>可以预先使用O(n)时间计算nums[0] .. nums[i]的最大值，存在left[i]中，以及nums[i] .. nums[n-1]的最小值存在right[i]中。然后再按题目的定义计算即可。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(n)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int sumOfBeauties(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; left(n, nums[0]);
    vector&lt;int&gt; right(n, nums.back());
    for (int i = 1; i &lt; n; ++i)
      left[i] = max(left[i - 1], nums[i]);
    for (int i = n - 2; i &gt;= 0; --i)
      right[i] = min(right[i + 1], nums[i]);
    int ans = 0;
    for (int i = 1; i &lt; n - 1; ++i)
      if (left[i - 1] &lt; nums[i] &amp;&amp; nums[i] &lt; right[i + 1])
        ans += 2;
      else if (nums[i - 1] &lt; nums[i] &amp;&amp; nums[i] &lt; nums[i + 1])
        ans += 1;
    return ans;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2012-sum-of-beauty-in-the-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 462. Minimum Moves to Equal Array Elements II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Apr 2025 21:02:47 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[nth_element]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10289</guid>

					<description><![CDATA[不太喜欢这样的题目，你需要证明/猜测最优解是将所有数字转换成median/中位数。 使用nth_element找中位数，然后再循环一遍求合即可。 时间复杂度：O(n)空间复杂度：O(1) [crayon-67ff9b4a181c4945255943/]]]></description>
										<content:encoded><![CDATA[
<p>不太喜欢这样的题目，你需要证明/猜测最优解是将所有数字转换成median/中位数。</p>



<p>使用nth_element找中位数，然后再循环一遍求合即可。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int minMoves2(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    nth_element(begin(nums), begin(nums) + n / 2, end(nums));
    const int median = nums[n / 2];
    return accumulate(begin(nums), end(nums), 0, [median](int s, int x) {
      return s + abs(x - median);
    });
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-462-minimum-moves-to-equal-array-elements-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 3462. Maximum Sum With at Most K Elements</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Mar 2025 04:24:17 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[fast parition]]></category>
		<category><![CDATA[partial sorting]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10232</guid>

					<description><![CDATA[本题使用了两次 partial sorting / std::nth_element 来进行部分排序，可以作为经典教程了。 速度比全排序或者heap/pq快到不知道哪里去了～ Time complexity: O(m*n)Space complexity: O(m*n) 不用黑科技就达到99.77% [crayon-67ff9b4a1c2b0673607140/]]]></description>
										<content:encoded><![CDATA[
<p>本题使用了两次 partial sorting / std::nth_element 来进行部分排序，可以作为经典教程了。</p>



<p>速度比全排序或者heap/pq快到不知道哪里去了～</p>



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



<p>不用黑科技就达到99.77%</p>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM.png"><img fetchpriority="high" decoding="async" width="815" height="448" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM.png" alt="" class="wp-image-10233" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM.png 815w, https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM-300x165.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM-768x422.png 768w" sizes="(max-width: 815px) 100vw, 815px" /></a></figure>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  long long maxSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid, vector&lt;int&gt;&amp; limits, int k) {
    // Step 1: Sort each row using fast parition, collect largest limits[i] elements.
    vector&lt;int&gt; nums;
    nums.reserve(grid.size() * grid[0].size());
    for (int i = 0; i &lt; grid.size(); ++i) {
      nth_element(begin(grid[i]), begin(grid[i]) + limits[i], end(grid[i]), std::greater{});
      nums.insert(nums.end(), begin(grid[i]), begin(grid[i]) + limits[i]);
    }

    // Setp 2: fast partition to find the largest k elements. O(m*n).
    nth_element(begin(nums), begin(nums) + k, end(nums), std::greater{});
    
    // Step 3: Sum them up. O(k)
    return accumulate(begin(nums), begin(nums) + k, 0LL);
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2824. Count Pairs Whose Sum is Less than Target</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 24 Aug 2023 01:09:34 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10070</guid>

					<description><![CDATA[Given a&#160;0-indexed&#160;integer array&#160;nums&#160;of length&#160;n&#160;and an integer&#160;target, return&#160;the number of pairs(i, j)where0 &#60;= i &#60; j &#60; nandnums[i] + nums[j] &#60; target. Example 1: Input: nums&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>target</code>, return&nbsp;<em>the number of pairs</em><code>(i, j)</code><em>where</em><code>0 &lt;= i &lt; j &lt; n</code><em>and</em><code>nums[i] + nums[j] &lt; target</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,1,2,3,1], target = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = 0 &lt; target
- (0, 2) since 0 &lt; 2 and nums[0] + nums[2] = 1 &lt; target 
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = 0 &lt; target
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-6,2,5,-2,-7,-1,3], target = -2
<strong>Output:</strong> 10
<strong>Explanation:</strong> There are 10 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = -4 &lt; target
- (0, 3) since 0 &lt; 3 and nums[0] + nums[3] = -8 &lt; target
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = -13 &lt; target
- (0, 5) since 0 &lt; 5 and nums[0] + nums[5] = -7 &lt; target
- (0, 6) since 0 &lt; 6 and nums[0] + nums[6] = -3 &lt; target
- (1, 4) since 1 &lt; 4 and nums[1] + nums[4] = -5 &lt; target
- (3, 4) since 3 &lt; 4 and nums[3] + nums[4] = -9 &lt; target
- (3, 5) since 3 &lt; 5 and nums[3] + nums[5] = -3 &lt; target
- (4, 5) since 4 &lt; 5 and nums[4] + nums[5] = -8 &lt; target
- (4, 6) since 4 &lt; 6 and nums[4] + nums[6] = -4 &lt; target
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length == n &lt;= 50</code></li><li><code>-50 &lt;= nums[i], target &lt;= 50</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution 1: Brute force</strong></h2>



<p>Enumerate all pairs.</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; nums, int target) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans += nums[i] + nums[j] &lt; target;
    return ans;
  }
};</pre>
</div></div>



<h2 class="wp-block-heading"><strong>Solution 2: Two Pointers</strong></h2>



<p>Sort the numbers.</p>



<p>Use two pointers i, and j.<br>Set i to 0 and j to n &#8211; 1.<br>while (nums[i] + nums[j] >= target) &#8211;j<br>then we have nums[i] + nums[k] &lt; target (i &lt; k &lt;= j), in total (j &#8211; i) pairs.<br>++i, move to the next starting number.<br>Time complexity: O(nlogn + n)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; nums, int target) {
    const int n = nums.size();
    sort(begin(nums), end(nums));
    int ans = 0;
    for (int i = 0, j = n - 1; i &lt; n; ++i) {
      while (j &gt;= i &amp;&amp; nums[i] + nums[j] &gt;= target) --j;      
      ans += max(0, j - i);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2815. Max Pair Sum in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Aug 2023 21:40:33 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10067</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums. You have to find the&#160;maximum&#160;sum of a pair of numbers from&#160;nums&#160;such that the maximum&#160;digit&#160;in both numbers are equal. Return&#160;the maximum&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>. You have to find the&nbsp;<strong>maximum</strong>&nbsp;sum of a pair of numbers from&nbsp;<code>nums</code>&nbsp;such that the maximum&nbsp;<strong>digit&nbsp;</strong>in both numbers are equal.</p>



<p>Return&nbsp;<em>the maximum sum or</em>&nbsp;<code>-1</code><em>&nbsp;if no such pair exists</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [51,71,17,24,42]
<strong>Output:</strong> 88
<strong>Explanation:</strong> 
For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. 
For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.
It can be shown that there are no other pairs with equal maximum digits, so the answer is 88.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> -1
<strong>Explanation:</strong> No pair exists in nums with equal maximum digits.
</pre>



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



<ul class="wp-block-list"><li><code>2 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Brute Force</strong></h2>



<p>Enumerate all pairs of nums and check their sum and max digit.</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int maxSum(vector&lt;int&gt;&amp; nums) {
    auto maxDigit = [](int x) {
      int ans = 0;
      while (x) {
        ans = max(ans, x % 10);
        x /= 10;
      }
      return ans;
    };
    int ans = -1;
    const int n = nums.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        if (nums[i] + nums[j] &gt; ans 
            &amp;&amp; maxDigit(nums[i]) == maxDigit(nums[j]))
          ans = nums[i] + nums[j];
    return ans;
  }
};</pre>
</div></div>

]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2657. Find the Prefix Common Array of Two Arrays</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Apr 2023 15:10:21 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bitset]]></category>
		<category><![CDATA[mask]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10027</guid>

					<description><![CDATA[You are given two&#160;0-indexed&#160;integer&#160;permutations&#160;A&#160;and&#160;B&#160;of length&#160;n. A&#160;prefix common array&#160;of&#160;A&#160;and&#160;B&#160;is an array&#160;C&#160;such that&#160;C[i]&#160;is equal to the count of numbers that are present at or before the index&#160;i&#160;in&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given two&nbsp;<strong>0-indexed&nbsp;</strong>integer<strong>&nbsp;</strong>permutations&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>&nbsp;of length&nbsp;<code>n</code>.</p>



<p>A&nbsp;<strong>prefix common array</strong>&nbsp;of&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>&nbsp;is an array&nbsp;<code>C</code>&nbsp;such that&nbsp;<code>C[i]</code>&nbsp;is equal to the count of numbers that are present at or before the index&nbsp;<code>i</code>&nbsp;in both&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>prefix common array</strong>&nbsp;of&nbsp;</em><code>A</code><em>&nbsp;and&nbsp;</em><code>B</code>.</p>



<p>A sequence of&nbsp;<code>n</code>&nbsp;integers is called a&nbsp;<strong>permutation</strong>&nbsp;if it contains all integers from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>&nbsp;exactly once.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> A = [1,3,2,4], B = [3,1,2,4]
<strong>Output:</strong> [0,2,3,4]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> A = [2,3,1], B = [3,1,2]
<strong>Output:</strong> [0,1,3]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= A.length == B.length == n &lt;= 50</code></li><li><code>1 &lt;= A[i], B[i] &lt;= n</code></li><li><code>It is guaranteed that A and B are both a permutation of n integers.</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution 1: Bitset</strong></h2>



<p>Use bitsets to store the numbers seen so far for each array, and use sA &amp; sB to count the common elements.</p>



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



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; findThePrefixCommonArray(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    bitset&lt;51&gt; sA;
    bitset&lt;51&gt; sB;
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; A.size(); ++i) {
      sA.set(A[i]);
      sB.set(B[i]);
      ans.push_back((sA &amp; sB).count());
    }
    return ans;
  }
};</pre>
</div></div>



<h2 class="wp-block-heading"><strong>Solution 2: Counter</strong></h2>



<p>Use a counter to track the frequency of each element, when the counter[x] == 2, we found a pair.</p>



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



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; findThePrefixCommonArray(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    vector&lt;int&gt; count(A.size() + 1);
    vector&lt;int&gt; ans;
    for (int i = 0, s = 0; i &lt; A.size(); ++i) {
      if (++count[A[i]] == 2) ++s;
      if (++count[B[i]] == 2) ++s;
      ans.push_back(s);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2640. Find the Score of All Prefixes of an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2640-find-the-score-of-all-prefixes-of-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2640-find-the-score-of-all-prefixes-of-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 15:30:47 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10010</guid>

					<description><![CDATA[We define the&#160;conversion array&#160;conver&#160;of an array&#160;arr&#160;as follows: conver[i] = arr[i] + max(arr[0..i])&#160;where&#160;max(arr[0..i])&#160;is the maximum value of&#160;arr[j]&#160;over&#160;0 &#60;= j &#60;= i. We also define the&#160;score&#160;of an&#8230;]]></description>
										<content:encoded><![CDATA[
<p>We define the&nbsp;<strong>conversion array</strong>&nbsp;<code>conver</code>&nbsp;of an array&nbsp;<code>arr</code>&nbsp;as follows:</p>



<ul class="wp-block-list"><li><code>conver[i] = arr[i] + max(arr[0..i])</code>&nbsp;where&nbsp;<code>max(arr[0..i])</code>&nbsp;is the maximum value of&nbsp;<code>arr[j]</code>&nbsp;over&nbsp;<code>0 &lt;= j &lt;= i</code>.</li></ul>



<p>We also define the&nbsp;<strong>score</strong>&nbsp;of an array&nbsp;<code>arr</code>&nbsp;as the sum of the values of the conversion array of&nbsp;<code>arr</code>.</p>



<p>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>, return&nbsp;<em>an array&nbsp;</em><code>ans</code><em>&nbsp;of length&nbsp;</em><code>n</code><em>&nbsp;where&nbsp;</em><code>ans[i]</code><em>&nbsp;is the score of the prefix</em>&nbsp;<code>nums[0..i]</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,7,5,10]
<strong>Output:</strong> [4,10,24,36,56]
<strong>Explanation:</strong> 
For the prefix [2], the conversion array is [4] hence the score is 4
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,2,4,8,16]
<strong>Output:</strong> [2,4,8,16,32,64]
<strong>Explanation:</strong> 
For the prefix [1], the conversion array is [2] hence the score is 2
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Prefix Sum</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;long long&gt; findPrefixScore(vector&lt;int&gt;&amp; nums) {
    vector&lt;long long&gt; ans(nums.size());
    for (int i = 0, m = 0; i &lt; nums.size(); ++i) {
      m = max(m, nums[i]);
      ans[i] = (i ? ans[i - 1] : 0) + nums[i] + m;
    }    
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2640-find-the-score-of-all-prefixes-of-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2644. Find the Maximum Divisibility Score</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 01:36:56 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[divisor]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10003</guid>

					<description><![CDATA[You are given two&#160;0-indexed&#160;integer arrays&#160;nums&#160;and&#160;divisors. The&#160;divisibility score&#160;of&#160;divisors[i]&#160;is the number of indices&#160;j&#160;such that&#160;nums[j]&#160;is divisible by&#160;divisors[i]. Return&#160;the integer&#160;divisors[i]&#160;with the maximum divisibility score. If there is more than&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given two&nbsp;<strong>0-indexed</strong>&nbsp;integer arrays&nbsp;<code>nums</code>&nbsp;and&nbsp;<code>divisors</code>.</p>



<p>The&nbsp;<strong>divisibility score</strong>&nbsp;of&nbsp;<code>divisors[i]</code>&nbsp;is the number of indices&nbsp;<code>j</code>&nbsp;such that&nbsp;<code>nums[j]</code>&nbsp;is divisible by&nbsp;<code>divisors[i]</code>.</p>



<p>Return&nbsp;<em>the integer</em>&nbsp;<code>divisors[i]</code>&nbsp;<em>with the maximum divisibility score</em>. If there is more than one integer with the maximum score, return the minimum of them.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,7,9,3,9], divisors = [5,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 5.
The divisibility score of divisors[1] is 1 since nums[0] is divisible by 2.
The divisibility score of divisors[2] is 3 since nums[2], nums[3], and nums[4] are divisible by 3.
Since divisors[2] has the maximum divisibility score, we return it.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [20,14,21,10], divisors = [5,7,5]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 2 since nums[0] and nums[3] are divisible by 5.
The divisibility score of divisors[1] is 2 since nums[1] and nums[2] are divisible by 7.
The divisibility score of divisors[2] is 2 since nums[0] and nums[3] are divisible by 5.
Since divisors[0], divisors[1], and divisors[2] all have the maximum divisibility score, we return the minimum of them (i.e., divisors[2]).
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [12], divisors = [10,16]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 10.
The divisibility score of divisors[1] is 0 since no number in nums is divisible by 16.
Since divisors[0] and divisors[1] both have the maximum divisibility score, we return the minimum of them (i.e., divisors[0]).
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length, divisors.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i], divisors[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Brute Force</strong></h2>



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



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int maxDivScore(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; divisors) {
    int maxScore = 0;
    int ans = INT_MAX;
    for (int d : divisors) {
      int score = 0;
      for (int x : nums) {
        score += x % d == 0;
      }
      if (score &gt; maxScore) {
        maxScore = score;
        ans = d;
      }
      if (score == maxScore)
        ans = min(ans, d);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2643. Row With Maximum Ones</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 01:28:50 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9999</guid>

					<description><![CDATA[Given a&#160;m x n&#160;binary matrix&#160;mat, find the&#160;0-indexed&#160;position of the row that contains the&#160;maximum&#160;count of&#160;ones,&#160;and the number of ones in that row. In case there are&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<code>m x n</code>&nbsp;binary matrix&nbsp;<code>mat</code>, find the&nbsp;<strong>0-indexed</strong>&nbsp;position of the row that contains the&nbsp;<strong>maximum</strong>&nbsp;count of&nbsp;<strong>ones,</strong>&nbsp;and the number of ones in that row.</p>



<p>In case there are multiple rows that have the maximum count of ones, the row with the&nbsp;<strong>smallest row number</strong>&nbsp;should be selected.</p>



<p>Return<em>&nbsp;an array containing the index of the row, and the number of ones in it.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,1],[1,0]]
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1]. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,0,0],[0,1,1]]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones <code>(2)</code>. So we return its index, <code>1</code>, and the count. So, the answer is [1,2].
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,0],[1,1],[0,0]]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
</pre>



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



<ul class="wp-block-list"><li><code>m == mat.length</code>&nbsp;</li><li><code>n == mat[i].length</code>&nbsp;</li><li><code>1 &lt;= m, n &lt;= 100</code>&nbsp;</li><li><code>mat[i][j]</code>&nbsp;is either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2 class="wp-block-heading"><strong>Solution: Counting</strong></h2>



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



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; rowAndMaximumOnes(vector&lt;vector&lt;int&gt;&gt;&amp; mat) {
    vector&lt;int&gt; ans(2);
    for (int i = 0; i &lt; mat.size(); ++i) {
      int ones = accumulate(begin(mat[i]), end(mat[i]), 0);
      if (ones &gt; ans[1]) {
        ans[0] = i;
        ans[1] = ones;
      }
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2574. Left and Right Sum Differences</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2574-left-and-right-sum-differences/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2574-left-and-right-sum-differences/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Feb 2023 05:54:12 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9964</guid>

					<description><![CDATA[Given a&#160;0-indexed&#160;integer array&#160;nums, find a&#160;0-indexed&#160;integer array&#160;answer&#160;where: answer.length == nums.length. answer[i] = &#124;leftSum[i] - rightSum[i]&#124;. Where: leftSum[i]&#160;is the sum of elements to the left of the&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>, find a&nbsp;<strong>0-indexed&nbsp;</strong>integer array&nbsp;<code>answer</code>&nbsp;where:</p>



<ul class="wp-block-list"><li><code>answer.length == nums.length</code>.</li><li><code>answer[i] = |leftSum[i] - rightSum[i]|</code>.</li></ul>



<p>Where:</p>



<ul class="wp-block-list"><li><code>leftSum[i]</code>&nbsp;is the sum of elements to the left of the index&nbsp;<code>i</code>&nbsp;in the array&nbsp;<code>nums</code>. If there is no such element,&nbsp;<code>leftSum[i] = 0</code>.</li><li><code>rightSum[i]</code>&nbsp;is the sum of elements to the right of the index&nbsp;<code>i</code>&nbsp;in the array&nbsp;<code>nums</code>. If there is no such element,&nbsp;<code>rightSum[i] = 0</code>.</li></ul>



<p>Return&nbsp;<em>the array</em>&nbsp;<code>answer</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [10,4,8,3]
<strong>Output:</strong> [15,1,11,22]
<strong>Explanation:</strong> The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1]
<strong>Output:</strong> [0]
<strong>Explanation:</strong> The array leftSum is [0] and the array rightSum is [0].
The array answer is [|0 - 0|] = [0].
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li></ul>



<p><strong>Solution: O(1) Space</strong></p>



<p>Pre-compute the sum of all numbers as right sum, and accumulate left sum on the fly then we can achieve O(1) space.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<p></p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; leftRigthDifference(vector&lt;int&gt;&amp; nums) {
    int right = accumulate(begin(nums), end(nums), 0);
    vector&lt;int&gt; ans;
    for (int i = 0, left = 0; i &lt; nums.size(); ++i) {
      right -= nums[i];
      ans.push_back(abs(left - right));
      left += nums[i];      
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2574-left-and-right-sum-differences/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2562. Find the Array Concatenation Value</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2562-find-the-array-concatenation-value/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2562-find-the-array-concatenation-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Feb 2023 16:03:16 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[concatenation]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9936</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums. The&#160;concatenation&#160;of two numbers is the number formed by concatenating their numerals. For example, the concatenation of&#160;15,&#160;49&#160;is&#160;1549. The&#160;concatenation value&#160;of&#160;nums&#160;is initially equal&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>.</p>



<p>The&nbsp;<strong>concatenation</strong>&nbsp;of two numbers is the number formed by concatenating their numerals.</p>



<ul class="wp-block-list"><li>For example, the concatenation of&nbsp;<code>15</code>,&nbsp;<code>49</code>&nbsp;is&nbsp;<code>1549</code>.</li></ul>



<p>The&nbsp;<strong>concatenation value</strong>&nbsp;of&nbsp;<code>nums</code>&nbsp;is initially equal to&nbsp;<code>0</code>. Perform this operation until&nbsp;<code>nums</code>&nbsp;becomes empty:</p>



<ul class="wp-block-list"><li>If there exists more than one number in&nbsp;<code>nums</code>, pick the first element and last element in&nbsp;<code>nums</code>&nbsp;respectively and add the value of their concatenation to the&nbsp;<strong>concatenation value</strong>&nbsp;of&nbsp;<code>nums</code>, then delete the first and last element from&nbsp;<code>nums</code>.</li><li>If one element exists, add its value to the&nbsp;<strong>concatenation value</strong>&nbsp;of&nbsp;<code>nums</code>, then delete it.</li></ul>



<p>Return<em>&nbsp;the concatenation value of the&nbsp;<code>nums</code></em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [7,52,2,4]
<strong>Output:</strong> 596
<strong>Explanation:</strong> Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.
 - In the first operation:
We pick the first element, 7, and the last element, 4.
Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.
Then we delete them from nums, so nums becomes equal to [52,2].
 - In the second operation:
We pick the first element, 52, and the last element, 2.
Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.
Then we delete them from the nums, so nums becomes empty.
Since the concatenation value is 596 so the answer is 596.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,14,13,8,12]
<strong>Output:</strong> 673
<strong>Explanation:</strong> Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.
 - In the first operation:
We pick the first element, 5, and the last element, 12.
Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.
Then we delete them from the nums, so nums becomes equal to [14,13,8].
 - In the second operation:
We pick the first element, 14, and the last element, 8.
Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.
Then we delete them from the nums, so nums becomes equal to [13].
 - In the third operation:
nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.
Then we delete it from nums, so nums become empty.
Since the concatenation value is 673 so the answer is 673.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Follow the rules</strong></h2>



<p>Time complexity: O(sum(log(nums[i]))<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  long long findTheArrayConcVal(vector&lt;int&gt;&amp; nums) {
    long long ans = 0;
    const int n = nums.size();
    for (int i = 0; i &lt; n / 2; ++i) {
      int cur  = nums[i];
      for (int t = nums[n - i - 1]; t &gt; 0; t /= 10)
        cur *= 10;    
      cur += nums[n - i - 1];
      ans += cur;
    }          
    if (n &amp; 1) ans += nums[n / 2];
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2562-find-the-array-concatenation-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2559. Count Vowel Strings in Ranges</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2559-count-vowel-strings-in-ranges/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2559-count-vowel-strings-in-ranges/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Feb 2023 05:25:04 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[prefix sum]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[vowel]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9929</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;array of strings&#160;words&#160;and a 2D array of integers&#160;queries. Each query&#160;queries[i] = [li, ri]&#160;asks us to find the number of strings present in&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array of strings&nbsp;<code>words</code>&nbsp;and a 2D array of integers&nbsp;<code>queries</code>.</p>



<p>Each query&nbsp;<code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;asks us to find the number of strings present in the range&nbsp;<code>l<sub>i</sub></code>&nbsp;to&nbsp;<code>r<sub>i</sub></code>&nbsp;(both&nbsp;<strong>inclusive</strong>) of&nbsp;<code>words</code>&nbsp;that start and end with a vowel.</p>



<p>Return an array&nbsp;<code>ans</code>&nbsp;of size&nbsp;<code>queries.length</code>, where&nbsp;<code>ans[i]</code>&nbsp;is the answer to the&nbsp;<code>i</code><sup>th</sup>&nbsp;query.</p>



<p><strong>Note</strong>&nbsp;that the vowel letters are&nbsp;<code>'a'</code>,&nbsp;<code>'e'</code>,&nbsp;<code>'i'</code>,&nbsp;<code>'o'</code>, and&nbsp;<code>'u'</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
<strong>Output:</strong> [2,3,0]
<strong>Explanation:</strong> The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
The answer to the query [0,2] is 2 (strings "aba" and "ece").
to query [1,4] is 3 (strings "ece", "aa", "e").
to query [1,1] is 0.
We return [2,3,0].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
<strong>Output:</strong> [3,2,1]
<strong>Explanation:</strong> Every string satisfies the conditions, so we return [3,2,1].</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= words[i].length &lt;= 40</code></li><li><code>words[i]</code>&nbsp;consists only of lowercase English letters.</li><li><code>sum(words[i].length) &lt;= 3 * 10<sup>5</sup></code></li><li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;&nbsp;words.length</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Prefix Sum</strong></h2>



<p>Let sum[i] := number of valid strings in words[0:i]</p>



<p>For each query [l, r], answer will be sum[r + 1] &#8211; sum[l]</p>



<p>Time complexity: O(n + q)<br>Space complexity: O(n)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; vowelStrings(vector&lt;string&gt;&amp; words, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    const size_t n = words.size();
    vector&lt;int&gt; sum(n + 1);
    auto check = [](char c) {
      return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    };
    for (size_t i = 0; i &lt; n; ++i)
      sum[i + 1] = sum[i] + 
        (check(words[i][0]) &amp;&amp; check(words[i][words[i].size() - 1]));
    vector&lt;int&gt; ans;
    for (const auto&amp; q : queries)
      ans.push_back(sum[q[1] + 1] - sum[q[0]]);
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2559-count-vowel-strings-in-ranges/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2475. Number of Unequal Triplets in Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2475-number-of-unequal-triplets-in-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2475-number-of-unequal-triplets-in-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 21 Nov 2022 23:45:24 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9886</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;array of positive integers&#160;nums. Find the number of triplets&#160;(i, j, k)&#160;that meet the following conditions: 0 &#60;= i &#60; j &#60; k&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array of positive integers&nbsp;<code>nums</code>. Find the number of triplets&nbsp;<code>(i, j, k)</code>&nbsp;that meet the following conditions:</p>



<ul class="wp-block-list"><li><code>0 &lt;= i &lt; j &lt; k &lt; nums.length</code></li><li><code>nums[i]</code>,&nbsp;<code>nums[j]</code>, and&nbsp;<code>nums[k]</code>&nbsp;are&nbsp;<strong>pairwise distinct</strong>.<ul><li>In other words,&nbsp;<code>nums[i] != nums[j]</code>,&nbsp;<code>nums[i] != nums[k]</code>, and&nbsp;<code>nums[j] != nums[k]</code>.</li></ul></li></ul>



<p>Return&nbsp;<em>the number of triplets that meet the conditions.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,4,2,4,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The following triplets meet the conditions:
- (0, 2, 4) because 4 != 2 != 3
- (1, 2, 4) because 4 != 2 != 3
- (2, 3, 4) because 2 != 4 != 3
Since there are 3 triplets, we return 3.
Note that (2, 0, 4) is not a valid triplet because 2 &gt; 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> No triplets meet the conditions so we return 0.
</pre>



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



<ul class="wp-block-list"><li><code>3 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 1000</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution 1: Brute Force</strong></h2>



<p>Enumerate i, j, k.</p>



<p>Time complexity: O(n<sup>3</sup>)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int unequalTriplets(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    const int n = nums.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        for (int k = j + 1; k &lt; n; ++k)
          if (nums[i] != nums[j] &amp;&amp; nums[j] != nums[k] &amp;&amp; nums[i] != nums[k])
            ++ans;
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2475-number-of-unequal-triplets-in-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
