<?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>4sum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/4sum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/4sum/</link>
	<description></description>
	<lastBuildDate>Thu, 20 Sep 2018 06:36:52 +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>4sum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/4sum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 18. 4Sum</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-18-4sum/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-18-4sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 20 Sep 2018 06:27:38 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[4sum]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4052</guid>

					<description><![CDATA[<p>Problem Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-18-4sum/">花花酱 LeetCode 18. 4Sum</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><strong>Problem</strong></h1>
<p>Given an array <code>nums</code> of <em>n</em> integers and an integer <code>target</code>, are there elements <em>a</em>, <em>b</em>, <em>c</em>, and <em>d</em> in <code>nums</code> such that <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> = <code>target</code>? Find all unique quadruplets in the array which gives the sum of <code>target</code>.</p>
<p><strong>Note:</strong></p>
<p>The solution set must not contain duplicate quadruplets.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]</pre>
<h1><strong>Solution 1: Sorting + Binary Search</strong></h1>
<p>Time complexity: O(n^3 log n + klogk)</p>
<p>Space complexity: O(k)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {
    set&lt;vector&lt;int&gt;&gt; h; 

    sort(num.begin(), num.end());

    int n = num.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++) {
          int t = target - num[i] - num[j] - num[k];
          if (t &lt; num[k]) break;
          if (!std::binary_search(num.begin() + k + 1, num.end(), t)) continue;          
          h.insert({num[i], num[j], num[k], t});          
        }
      }
    }
    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));
  }
};</pre><p></div><h2 class="tabtitle">C++ opt</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {        
    sort(num.begin(), num.end());
    if (target &gt; 0 &amp;&amp; target &gt; 4 * num.back()) return {};
    if (target &lt; 0 &amp;&amp; target &lt; 4 * num.front()) return {};
    
    set&lt;vector&lt;int&gt;&gt; h;    
    int n = num.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++) {
          int t = target - num[i] - num[j] - num[k];
          if (t &lt; num[k]) break;
          if (!std::binary_search(num.begin() + k + 1, num.end(), t)) continue;          
          h.insert({num[i], num[j], num[k], t});          
        }           
      }
    }

    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: Sorting + HashTable</strong></h1>
<p>Time complexity: O(n^3 + klogk)</p>
<p>Space complexity: O(n + k)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; fourSum(vector&lt;int&gt; &amp;num, int target) {        
    sort(num.begin(), num.end());
    if (target &gt; 0 &amp;&amp; target &gt; 4 * num.back()) return {};
    if (target &lt; 0 &amp;&amp; target &lt; 4 * num.front()) return {};
    
    unordered_map&lt;int, int&gt; index;
    for (int i = 0; i &lt; num.size(); ++i)
      index[num[i]] = i;
    
    set&lt;vector&lt;int&gt;&gt; h;    
    int n = num.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++) {
          int t = target - num[i] - num[j] - num[k];
          if (t &lt; num[k]) break;
          auto it = index.find(t);
          if (it == index.end() || it-&gt;second &lt;= k) continue;
          h.insert({num[i], num[j], num[k], t});
        }           
      }
    }

    return vector&lt;vector&lt;int&gt;&gt;(begin(h), end(h));
  }
};</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1-two-sum/">花花酱 LeetCode 1. Two Sum</a></li>
<li><a href="https://zxi.mytechroad.com/blog/algorithms/binary-search/167-two-sum-ii-input-array-is-sorted/">花花酱 167. Two Sum II &amp;#8211; Input array is sorted</a></li>
<li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-653-two-sum-iv-input-is-a-bst/">花花酱 LeetCode 653. Two Sum IV &amp;#8211; Input is a BST</a></li>
<li><a href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/">花花酱 LeetCode 15. 3Sum</a></li>
<li><a href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-16-3sum-closest/">花花酱 LeetCode 16. 3Sum Closest</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-18-4sum/">花花酱 LeetCode 18. 4Sum</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-18-4sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
