<?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>abs Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/abs/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/abs/</link>
	<description></description>
	<lastBuildDate>Sun, 16 Oct 2022 20:50:07 +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>abs Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/abs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2441. Largest Positive Integer That Exists With Its Negative</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2441-largest-positive-integer-that-exists-with-its-negative%ef%bf%bc%ef%bf%bc/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2441-largest-positive-integer-that-exists-with-its-negative%ef%bf%bc%ef%bf%bc/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 16 Oct 2022 04:56:53 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[abs]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9866</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums&#160;that&#160;does not contain&#160;any zeros, find&#160;the largest positive&#160;integer&#160;k&#160;such that&#160;-k&#160;also exists in the array. Return&#160;the positive integer&#160;k. If there is no such integer, return&#160;-1.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2441-largest-positive-integer-that-exists-with-its-negative%ef%bf%bc%ef%bf%bc/">花花酱 LeetCode 2441. Largest Positive Integer That Exists With Its Negative</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2441. Largest Positive Integer That Exists With Its Negative - 刷题找工作 EP404" width="500" height="281" src="https://www.youtube.com/embed/HItywpd0-yY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given an integer array&nbsp;<code>nums</code>&nbsp;that&nbsp;<strong>does not contain</strong>&nbsp;any zeros, find&nbsp;<strong>the largest positive</strong>&nbsp;integer&nbsp;<code>k</code>&nbsp;such that&nbsp;<code>-k</code>&nbsp;also exists in the array.</p>



<p>Return&nbsp;<em>the positive integer&nbsp;</em><code>k</code>. If there is no such integer, return&nbsp;<code>-1</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li><li><code>nums[i] != 0</code></li></ul>



<h2><strong>Solution 1: Hashtable</strong></h2>



<p>We can do in one pass by checking whether -x in the hashtable and update ans with abs(x) if so.</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="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findMaxK(vector&lt;int&gt;&amp; nums) {
    unordered_set&lt;int&gt; s;
    int ans = -1;
    for (int x : nums) {
      if (abs(x) &gt; ans &amp;&amp; s.count(-x))
        ans = abs(x);
      s.insert(x);
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Sorting</strong></h2>



<p>Sort the array by abs(x) in descending order.</p>



<p>[-1,10,6,7,-7,1] becomes = [-1, 1, 6, -7, 7, 10]</p>



<p>Check whether arr[i] = -arr[i-1].</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findMaxK(vector&lt;int&gt;&amp; nums) {
    sort(begin(nums), end(nums), [](int a, int b){
      return abs(a) == abs(b) ? a &lt; b : abs(a) &gt; abs(b);
    });    
    for (int i = 1; i &lt; nums.size(); ++i)
      if (nums[i] == -nums[i - 1]) return nums[i];
    return -1;
  }
};</pre>
</div></div>



<h2><strong>Solution 3: Two Pointers</strong></h2>



<p>Sort the array.</p>



<p>Let sum = nums[i] + nums[j], sum == 0, we find one pair, if sum &lt; 0, ++i else &#8211;j.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findMaxK(vector&lt;int&gt;&amp; nums) {
    sort(begin(nums), end(nums));
    int ans = -1;
    for (int i = 0, j = nums.size() - 1; i &lt; j; ) {
      const int s = nums[i] + nums[j];
      if (s == 0) {
        ans = max(ans, nums[j]);
        ++i, --j;      
      } else if (s &lt; 0) {
        ++i;
      } else {
        --j;
      }      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2441-largest-positive-integer-that-exists-with-its-negative%ef%bf%bc%ef%bf%bc/">花花酱 LeetCode 2441. Largest Positive Integer That Exists With Its Negative</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/hashtable/leetcode-2441-largest-positive-integer-that-exists-with-its-negative%ef%bf%bc%ef%bf%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1818. Minimum Absolute Sum Difference</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1818-minimum-absolute-sum-difference/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1818-minimum-absolute-sum-difference/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Apr 2021 00:27:27 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[abs]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8325</guid>

					<description><![CDATA[<p>You are given two positive integer arrays&#160;nums1&#160;and&#160;nums2, both of length&#160;n. The&#160;absolute sum difference&#160;of arrays&#160;nums1&#160;and&#160;nums2&#160;is defined as the&#160;sum&#160;of&#160;&#124;nums1[i] - nums2[i]&#124;&#160;for each&#160;0 &#60;= i &#60; n&#160;(0-indexed). You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1818-minimum-absolute-sum-difference/">花花酱 LeetCode 1818. Minimum Absolute Sum Difference</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>You are given two positive integer arrays&nbsp;<code>nums1</code>&nbsp;and&nbsp;<code>nums2</code>, both of length&nbsp;<code>n</code>.</p>



<p>The&nbsp;<strong>absolute sum difference</strong>&nbsp;of arrays&nbsp;<code>nums1</code>&nbsp;and&nbsp;<code>nums2</code>&nbsp;is defined as the&nbsp;<strong>sum</strong>&nbsp;of&nbsp;<code>|nums1[i] - nums2[i]|</code>&nbsp;for each&nbsp;<code>0 &lt;= i &lt; n</code>&nbsp;(<strong>0-indexed</strong>).</p>



<p>You can replace&nbsp;<strong>at most one</strong>&nbsp;element of&nbsp;<code>nums1</code>&nbsp;with&nbsp;<strong>any</strong>&nbsp;other element in&nbsp;<code>nums1</code>&nbsp;to&nbsp;<strong>minimize</strong>&nbsp;the absolute sum difference.</p>



<p>Return the&nbsp;<em>minimum absolute sum difference&nbsp;<strong>after</strong>&nbsp;replacing at most oneelement in the array&nbsp;<code>nums1</code>.</em>&nbsp;Since the answer may be large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p><code>|x|</code>&nbsp;is defined as:</p>



<ul><li><code>x</code>&nbsp;if&nbsp;<code>x &gt;= 0</code>, or</li><li><code>-x</code>&nbsp;if&nbsp;<code>x &lt; 0</code>.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,7,5], nums2 = [2,3,5]
<strong>Output:</strong> 3
<strong>Explanation: </strong>There are two possible optimal solutions:
- Replace the second element with the first: [1,<strong>7</strong>,5] =&gt; [1,<strong>1</strong>,5], or
- Replace the second element with the third: [1,<strong>7</strong>,5] =&gt; [1,<strong>5</strong>,5].
Both will yield an absolute sum difference of <code>|1-2| + (|1-3| or |5-3|) + |5-5| = </code>3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
<strong>Output:</strong> 0
<strong>Explanation: </strong>nums1 is equal to nums2 so no replacement is needed. This will result in an 
absolute sum difference of 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
<strong>Output:</strong> 20
<strong>Explanation: </strong>Replace the first element with the second: [<strong>1</strong>,10,4,4,2,7] =&gt; [<strong>10</strong>,10,4,4,2,7].
This yields an absolute sum difference of <code>|10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20</code>
</pre>



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



<ul><li><code>n == nums1.length</code></li><li><code>n == nums2.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Binary Search</strong></h2>



<p>Greedy won&#8217;t work, e.g. finding the max diff pair and replace it. Counter example:<br>nums1 = [7, 5], nums2 = [1, -2]<br>pair1 = abs(7 &#8211; 1) = 6<br>pair2 = abs(5 &#8211; (-2)) = 7<br>If we replace 5 with 7, we got pair2&#8242; = abs(7 &#8211; (-2)) = 9 &gt; 7.</p>



<p>Every pair of numbers can be the candidate, we just need to find the closest number for each nums2[i].</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minAbsoluteSumDiff(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {
    constexpr int kMod = 1e9 + 7;
    const int n = nums1.size();
    long ans = 0;
    int gain = 0;
    set&lt;int&gt; s(begin(nums1), end(nums1));
    for (int i = 0; i &lt; n; ++i) {
      const int diff = abs(nums1[i] - nums2[i]);
      ans += diff;
      if (diff &lt;= gain) continue;
      auto it = s.lower_bound(nums2[i]);      
      if (it != s.end()) 
        gain = max(gain, diff - abs(*it - nums2[i]));
      if (it != s.begin()) 
        gain = max(gain, diff - abs(*prev(it) - nums2[i])); 
    }
    return (ans - gain) % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1818-minimum-absolute-sum-difference/">花花酱 LeetCode 1818. Minimum Absolute Sum Difference</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/binary-search/leetcode-1818-minimum-absolute-sum-difference/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
