<?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>pairs Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pairs/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pairs/</link>
	<description></description>
	<lastBuildDate>Thu, 25 Nov 2021 20:43:25 +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>pairs Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pairs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2006. Count Number of Pairs With Absolute Difference K</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2006-count-number-of-pairs-with-absolute-difference-k/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2006-count-number-of-pairs-with-absolute-difference-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 25 Nov 2021 20:42:56 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[pairs]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8763</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums&#160;and an integer&#160;k, return&#160;the number of pairs&#160;(i, j)&#160;where&#160;i &#60; j&#160;such that&#160;&#124;nums[i] - nums[j]&#124; == k. The value of&#160;&#124;x&#124;&#160;is defined as: x&#160;if&#160;x &#62;=&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2006-count-number-of-pairs-with-absolute-difference-k/">花花酱 LeetCode 2006. Count Number of Pairs With Absolute Difference K</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>Given an integer array&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>, return&nbsp;<em>the number of pairs</em>&nbsp;<code>(i, j)</code>&nbsp;<em>where</em>&nbsp;<code>i &lt; j</code>&nbsp;<em>such that</em>&nbsp;<code>|nums[i] - nums[j]| == k</code>.</p>



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



<ul><li><code>x</code>&nbsp;if&nbsp;<code>x &gt;= 0</code>.</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> nums = [1,2,2,1], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> The pairs with an absolute difference of 1 are:
- [<strong><u>1</u></strong>,<strong><u>2</u></strong>,2,1]
- [<strong><u>1</u></strong>,2,<strong><u>2</u></strong>,1]
- [1,<strong><u>2</u></strong>,2,<strong><u>1</u></strong>]
- [1,2,<strong><u>2</u></strong>,<strong><u>1</u></strong>]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3], k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no pairs with an absolute difference of 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,2,1,5,4], k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> The pairs with an absolute difference of 2 are:
- [<strong><u>3</u></strong>,2,<strong><u>1</u></strong>,5,4]
- [<strong><u>3</u></strong>,2,1,<strong><u>5</u></strong>,4]
- [3,<strong><u>2</u></strong>,1,5,<strong><u>4</u></strong>]
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 200</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li><li><code>1 &lt;= k &lt;= 99</code></li></ul>



<h2><strong>Solution: Hashtable<br></strong>|y &#8211; x| = k<br>y = x + k or y = x &#8211; k</h2>



<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 countKDifference(vector&lt;int&gt;&amp; nums, int k) {
    unordered_map&lt;int, int&gt; m;
    int ans = 0;
    for (int x : nums) {
      ans += m[x - k];
      ans += m[x + k];
      ++m[x];
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2006-count-number-of-pairs-with-absolute-difference-k/">花花酱 LeetCode 2006. Count Number of Pairs With Absolute Difference K</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-2006-count-number-of-pairs-with-absolute-difference-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1711. Count Good Meals</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jan 2021 07:35:38 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[pairs]]></category>
		<category><![CDATA[target sum]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7893</guid>

					<description><![CDATA[<p>A&#160;good meal&#160;is a meal that contains&#160;exactly two different food items&#160;with a sum of deliciousness equal to a power of two. You can pick&#160;any&#160;two different foods&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/">花花酱 LeetCode 1711. Count Good Meals</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&nbsp;<strong>good meal</strong>&nbsp;is a meal that contains&nbsp;<strong>exactly two different food items</strong>&nbsp;with a sum of deliciousness equal to a power of two.</p>



<p>You can pick&nbsp;<strong>any</strong>&nbsp;two different foods to make a good meal.</p>



<p>Given an array of integers&nbsp;<code>deliciousness</code>&nbsp;where&nbsp;<code>deliciousness[i]</code>&nbsp;is the deliciousness of the&nbsp;<code>i<sup>​​​​​​th</sup>​​​​</code>​​​​ item of food, return&nbsp;<em>the number of different&nbsp;<strong>good meals</strong>&nbsp;you can make from this list modulo</em>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p>Note that items with different indices are considered different even if they have the same deliciousness value.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> deliciousness = [1,3,5,7,9]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The good meals are (1,3), (1,7), (3,5) and, (7,9).
Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> deliciousness = [1,1,1,3,3,3,7]
<strong>Output:</strong> 15
<strong>Explanation: </strong>The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.</pre>



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



<ul><li><code>1 &lt;= deliciousness.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= deliciousness[i] &lt;= 2<sup>20</sup></code></li></ul>



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



<p>Same idea as LeetCode 1: Two Sum</p>



<p>Use a hashtable to store the occurrences of all the numbers added so far. For a  new number x, check all possible 2^i &#8211; x. ans += freq[2^i &#8211; x] 0 &lt;= i &lt;= 21</p>



<p>Time complexity: O(22n)<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 countPairs(vector&lt;int&gt;&amp; A) {
    constexpr int kMod = 1e9 + 7;
    unordered_map&lt;int, int&gt; m;    
    long ans = 0;
    for (int x : A) {
      for (int t = 1; t &lt;= 1 &lt;&lt; 21; t *= 2) {
        auto it = m.find(t - x);
        if (it != end(m)) ans += it-&gt;second;
      }
      ++m[x];
    }
    return ans % kMod;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def countPairs(self, deliciousness: List[int]) -&gt; int:
    sums = [1&lt;&lt;i for i in range(22)]
    m = defaultdict(int)
    ans = 0
    for x in deliciousness:
      for t in sums:
        if t - x in m: ans += m[t - x]
      m[x] += 1
    return ans % (10**9 + 7)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/">花花酱 LeetCode 1711. Count Good Meals</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-1711-count-good-meals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 447. Number of Boomerangs</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-447-number-of-boomerangs/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-447-number-of-boomerangs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 24 Mar 2018 19:11:52 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[pairs]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2347</guid>

					<description><![CDATA[<p>Problem Given n points in the plane that are all pairwise distinct, a &#8220;boomerang&#8221; is a tuple of points (i, j, k) such that the distance between i and j equals the distance&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-447-number-of-boomerangs/">花花酱 LeetCode 447. Number of Boomerangs</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<div class="question-description">
<div>
<p>Given <i>n</i> points in the plane that are all pairwise distinct, a &#8220;boomerang&#8221; is a tuple of points <code>(i, j, k)</code> such that the distance between <code>i</code> and <code>j</code> equals the distance between <code>i</code> and <code>k</code> (<b>the order of the tuple matters</b>).</p>
<p>Find the number of boomerangs. You may assume that <i>n</i> will be at most <b>500</b> and coordinates of points are all in the range <b>[-10000, 10000]</b> (inclusive).</p>
<p><b>Example:</b></p>
<pre class="crayon:false"><b>Input:</b>
[[0,0],[1,0],[2,0]]

<b>Output:</b>
2

<b>Explanation:</b>
The two boomerangs are <b>[[1,0],[0,0],[2,0]]</b> and <b>[[1,0],[2,0],[0,0]]</b>
</pre>
</div>
</div>
<h1><strong>Solution: HashTable</strong></h1>
<p>For each point, compute the distance to the rest of the points and count.</p>
<p>if there are k points that have the same distance to current point, then there are P(k,2) = k*k-1 Boomerangs.</p>
<p>for example, p1, p2, p3 have the same distance to p0, then there are P(3,2) = 3 * (3-1) = 6 Boomerangs</p>
<p>(p1, p0, p2), (p1, p0, p3)</p>
<p>(p2, p0, p1), (p2, p0, p3)</p>
<p>(p3, p0, p1), (p3, p0, p2)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 210 ms (beats 83.33%)
class Solution {
public:
  int numberOfBoomerangs(vector&lt;pair&lt;int, int&gt;&gt;&amp; points) {
    int ans = 0;
    for (int i = 0; i &lt; points.size(); ++i) {
      unordered_map&lt;int, int&gt; dist(points.size());
      for (int j = 0; j &lt; points.size(); ++j) {
        const int dx = points[i].first - points[j].first;
        const int dy = points[i].second - points[j].second;        
        ++dist[dx * dx + dy * dy];
      }
      for (const auto&amp; kv : dist)
        ans += kv.second * (kv.second - 1);      
    }
    return ans;
  }
};</pre><p></p>
<h2><strong>Solution 2: Sorting</strong></h2>
<p>dist = [1, 2, 1, 2, 1, 5]</p>
<p>sorted_dist = [1, 1, 1, 2, 2, 5], 1*3, 2*2, 5*1</p>
<p>ans = 3*(3-1) + 2 * (2 &#8211; 1) * 1 * (1 &#8211; 1) = 8</p>
<p>Time complexity: O(n*nlogn)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 150 ms (beats 98.52%)
class Solution {
public:
  int numberOfBoomerangs(vector&lt;pair&lt;int, int&gt;&gt;&amp; points) {
    const int n = points.size();
    int ans = 0;
    vector&lt;int&gt; dist(points.size());
    for (int i = 0; i &lt; n; ++i) {   
      for (int j = 0; j &lt; n; ++j) {
        const int dx = points[i].first - points[j].first;
        const int dy = points[i].second - points[j].second;    
        dist[j] = dx * dx + dy * dy;
      }
      
      std::sort(dist.begin(), dist.end());
      
      for (int j = 1; j &lt; n; ++j) {
        int k = 1;
        while (j &lt; n &amp;&amp; dist[j] == dist[j - 1]) { ++j; ++k; }
        ans += k * (k - 1);
      }
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-447-number-of-boomerangs/">花花酱 LeetCode 447. Number of Boomerangs</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-447-number-of-boomerangs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
