1 / 20

210 likes | 391 Vues

4 sum, 3 sum, 3 sum closest , and 2 sum. 4 sum problem description. Given an array S of n integers, are there elements a , b , c , and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Télécharger la présentation
## 4 sum, 3 sum, 3 sum closest , and 2 sum

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**4 sum problem description**• Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. • Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a <= b <= c <= d) • The solution set must not contain duplicate quadruplets. • For example, given array S = {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)**Key ideas**• Sort the array. • Build a hash map (anytime we need to use two or more loops, consider build a hash map to save one loop) on each element and number of its appearances. • Find all quadruplets where all four elements are equal, like a, a, a, a. • Find all quadruplets where three elements are equal. These can be in two formats: a, a, a, b or a, b, b, b (assume that a < b)**Key ideas: II**• Find all quadruplets where two elements are equal. These can be in the following formats: a, a, b, b (a < b) a, a, b, c (a < b < c) a, b, b, c (a < b < c) a, b, c, c (a < b < c) 6. Find all elements that are unique, as a, b, c, d**Warning: when using continue; in while loop**• Do not forget to update the loop variable before we use continue; in while statement. Otherwise, endless loop may happen, as shown in the following piece of code. while(i < len && num[i] < target - 2*elm - num[i]) { val= target - 2*elm - num[i]; if(num[i] == elm || val == elm) { i++; //Warning: do not miss the i++; statement, //or endless loop for [0, 0, 0, 0], 1 continue; } … }**Hash map on element and # of its appearances**HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int elm: num) { if( !map.containsKey(elm) ) map.put(elm, 1); elsemap.put(elm, map.get(elm) +1); }**Add a quadruplet to array list**//Cannot use the default version of add from lists. //That version does not check for redundancy. voidadd(ArrayList<ArrayList<Integer>> lists, inta, int b, int c, int d) { ArrayList<Integer> list = newArrayList<Integer>(); list.add(a); list.add(b); list.add(c); list.add(d); if( !lists.contains(list) ) lists.add( list ); }**Quadruplets with four equal items**• Search in each element in the array, add those appears at least four times and add that value four times equals the target. for (int elm: num) { if(map.get(elm) >= 4 && 4*elm == target) add(lists, elm, elm, elm, elm);**Quadruplets with three equal items**//Find quadruplets in form of a, b, b, b or //a, a, a, b,where a < b if(map.get(elm) >= 3) { intval = target - 3*elm; if (val!= elm && map.containsKey(val)) { if(val < elm) add(lists, val, elm, elm, elm); elseadd(lists, elm, elm, elm, val); } }**Quadruplets with two equal items**if(map.get(elm) >= 2) { //Find quadruplets in form of a, a, b, b if(target % 2 == 0) {//target is even //may find integer val such that //2*val + 2*elm == target intval = (target - 2*elm)/2; if( val != elm && map.containsKey(val) && map.get(val) >= 2 ) { if(val < elm) add(lists, val, val,elm, elm); elseadd(lists, elm, elm, val, val); } } To be continued**Quadruplets with two equal items: II**//Warning: the following code //cannot be the else part of target % 2 == 0 //Reason: a + b + elm + elm == target no requirement on the parity of target. //Find out num[i] such that //val = target - num[i] - 2*elm is an element in the array, //and num[i] < val and num[i] != elm and val != elm i= 0; while(i < len && num[i] < target-num[i]-2*elm) { //For a group of equal value elements, select the last one. while( i+1 < len && num[i+1] == num[i] ) i++; if(num[i] == elm) { i++; continue; } To be continued**Quadruplets with two equal items: III**intval = target - num[i] - 2*elm; if(val != elm && map.containsKey(val)) { if(val < elm) add(lists, num[i], val, elm, elm); elseif (elm > num[i]) add(lists, num[i], elm, elm, val); elseadd(lists, elm, elm, num[i], val ); } i++; } } }**Quadruplet with all distinct items**• Use three loops. • Calculate the difference between target and the sum of three selections from those three loops. • If the different is in contained in the hash map, add them to the list.**Warning: same item can appear three times or twice in**resulting quadruplets • Given array {1, 2, 2, 3, 3, 3, 3, 6, 8} and target 15. • Both {1, 3, 3, 8} and {3, 3, 3, 6} are eligible quadruplets. for (int elm: num) { if(map.get(elm) >= 4 && 4*elm == target) … elseif(map.get(elm) >= 3) … else if (map.get(elm) >= 2) … } Complete code see the notes of this page.**3 sum**• Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. • Note: • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) • The solution set must not contain duplicate triplets. • For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)**3 sum: key idea**• Sort the array. • Build hash map between an element in the array and number of its appearances in the array. • Check whether there is triplet with all equal elements. • Check whether there is triplet with two equal elements.**3 sum: key idea: II**• The remaining triplets must have unique element. • Skip identical elements. • Since we build up hash map already, to select three elements, only two loops are needed. • For the complete code, see note page.**3 sum closest**• Key idea: use three loops to find I, j, k such that num[i] + num[j] + num[k] is the closest to the target. • Use absolute value to calculate the difference. • Return the value of num[i] + num[j] + num[k] that is closest to target. • Sort the array is not needed, the complexity is still o(n3), where n is the size of the array. • If we sort the array, the speed up is once num[i] + num[j] + num[k] is larger than target, we can use break; to exit the current loop.**2 sum**• Given an array of integers, find two numbers such that they add up to a specific target number. • The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. • You may assume that each input would have exactly one solution. • Input: numbers={2, 7, 11, 15}, target=9Output: index1=1, index2=2**Key idea for 2 sum**• No need to sort the array, otherwise, index might be different from the original. • Use two for loops. • No need to initialize the elements in an integer array.

More Related