<?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>pair sum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pair-sum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pair-sum/</link>
	<description></description>
	<lastBuildDate>Sun, 06 Dec 2020 19:41:40 +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>pair sum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pair-sum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1679. Max Number of K-Sum Pairs</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1679-max-number-of-k-sum-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1679-max-number-of-k-sum-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Dec 2020 19:36:21 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[pair sum]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7768</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;and an integer&#160;k. In one operation, you can pick two numbers from the array whose sum equals&#160;k&#160;and remove them from&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1679-max-number-of-k-sum-pairs/">花花酱 LeetCode 1679. Max Number of K-Sum Pairs</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 an integer array&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>.</p>



<p>In one operation, you can pick two numbers from the array whose sum equals&nbsp;<code>k</code>&nbsp;and remove them from the array.</p>



<p>Return&nbsp;<em>the maximum number of operations you can perform on the array</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,1,3,4,3], k = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>



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



<ul><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><li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution 1: Frequency Map</strong></h2>



<p>For each x, check freq[x] and freq[k &#8211; x]. Note: there is a special case when x + x == k.</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">class Solution {
public:
  int maxOperations(vector&lt;int&gt;&amp; nums, int k) {
    unordered_map&lt;int, int&gt; m;
    int ans = 0;
    for (int x : nums) ++m[x];
    for (int x : nums) {      
      if (m[x] &lt; 1 || m[k - x] &lt; 1 + (x + x == k)) continue;
      --m[x];
      --m[k - x];
      ++ans;      
    }
    return ans;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def maxOperations(self, nums: List[int], k: int) -&gt; int:
    m = defaultdict(int)
    ans = 0
    for x in nums: m[x] += 1
    for x in nums:
      if m[x] &lt; 1 or m[k - x] &lt; 1 + (x + x == k): continue
      m[x] -= 1
      m[k - x] -= 1
      ans += 1
    return ans</pre>
</div></div>



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



<p>Sort the number, start from i = 0, j = n &#8211; 1, compare s = nums[i] + nums[j] with k and move i, j accordingly.</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">class Solution {
public:
  int maxOperations(vector&lt;int&gt;&amp; nums, int k) {
    sort(begin(nums), end(nums));
    int i = 0, j = nums.size() - 1;
    int ans = 0;
    while (i &lt; j) {
      const int s = nums[i] + nums[j];
      if (s == k) {
        ++ans;
        ++i; --j;
      } else if (s &lt; k) {
        ++i;
      } else {
        --j;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1679-max-number-of-k-sum-pairs/">花花酱 LeetCode 1679. Max Number of K-Sum Pairs</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-1679-max-number-of-k-sum-pairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
