{"id":9,"date":"2019-02-05T14:40:31","date_gmt":"2019-02-05T14:40:31","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/coding\/?p=9"},"modified":"2019-02-05T17:19:58","modified_gmt":"2019-02-05T17:19:58","slug":"question-1-find-pair-with-given-sum-in-an-array","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/","title":{"rendered":"Question 1: Find Pair With Given Sum in an Array"},"content":{"rendered":"<h4><strong>Question 1<\/strong><\/h4>\n<p>Given an array of integers, you must find a pair within the array that sums up to a given sum. We assume the array is unsorted.<\/p>\n<p>For example:<\/p>\n<p><strong>Input:\u00a0 <\/strong><\/p>\n<p>Array A = {4, 9, 7, 3, 6, 8}<\/p>\n<p>Sum = 11<\/p>\n<p><strong>Output: <\/strong><\/p>\n<p>Pair found at 0 and\u00a0 2 (4 +\u00a0 7)<\/p>\n<p>Pair found at 3 and 5 (3 + 8)<\/p>\n<ol>\n<li><a href=\"#t1\">Solution 1: Inefficient Solution<\/a><\/li>\n<li><a href=\"#t2\">Solution 2: Start with Sorting<\/a><\/li>\n<li><a href=\"#t3\">Solution 3: Use a hashmap<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">Solution 1: Inefficient Solution (Quadratic time)<\/strong><\/h4>\n<p>In this solution, we loop through the array and for each element, we calculate the sum with every other element and compare with the given sum. If the sum equals the given sum. We output the indexes.<\/p>\n<p>The Java program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Question 1: Given an unsorted array of integers<\/span>\r\n<span style=\"color: #888888;\">\/\/ find a pair of integers with the given sum in it<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">package<\/span> examples<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FindPairSum1<\/span><span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findPair1<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> A<span style=\"color: #333333;\">[],<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/loop until element before the last<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/loop until the last element<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> j <span style=\"color: #333333;\">=<\/span> i<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span> j <span style=\"color: #333333;\">&lt;<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span> j<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\t\r\n\t\t\t\t<span style=\"color: #888888;\">\/\/if given sum is found, print the indexes<\/span>\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>A<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>j<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">==<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Pair found at \"<\/span> <span style=\"color: #333333;\">+<\/span> i <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\" and \"<\/span> <span style=\"color: #333333;\">+<\/span> j<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/End of findPair1<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> ar<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">8<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">11<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\tfindPair1<span style=\"color: #333333;\">(<\/span>ar<span style=\"color: #333333;\">,<\/span> sum<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/End of main\t\t<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p><strong>Solution 1:<\/strong> Inefficient Solution &#8211; Quadratic Time<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">Solution 2: Start by Sorting (nlog(n) time)<\/strong><\/h4>\n<p>In this solution, we first sort the array. As such, the largest elements\u00a0 are are the end of the array while the smallest elements are at the beginning of the array. Then we use two iterators. The first counting from the beginning of the array while the second counting from the end of the array.<\/p>\n<p>For each iteration,\u00a0 we calculate the sum to check if it equals the given sum<\/p>\n<p>The Java program is given below.<\/p>\n<p>Note: The index returned with this method is the index of the elements in the sorted array which is not the same as the indexes in the original array.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/findPair2: Sorts the array. Maintain two indices of the two end-points<\/span>\r\n<span style=\"color: #888888;\">\/\/Complexity: O(nlogn)<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">package<\/span> examples<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Arrays<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FindPairSum2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findPair2<\/span> <span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> A<span style=\"color: #333333;\">[],<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tArrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">sort<\/span><span style=\"color: #333333;\">(<\/span>A<span style=\"color: #333333;\">);<\/span> <span style=\"color: #888888;\">\/\/First sort the array<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> low <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> high <span style=\"color: #333333;\">=<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span> <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>low <span style=\"color: #333333;\">&lt;<\/span> high<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>A<span style=\"color: #333333;\">[<\/span>low<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>high<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">==<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Sum found are \"<\/span> <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>low<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\" and \"<\/span> <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>high<span style=\"color: #333333;\">]);<\/span>\r\n\t\t\t\tlow<span style=\"color: #333333;\">++;<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>A<span style=\"color: #333333;\">[<\/span>low<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>high<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">&lt;<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tlow <span style=\"color: #333333;\">=<\/span> low <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\thigh <span style=\"color: #333333;\">=<\/span> high  <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span> <span style=\"color: #888888;\">\/\/end of while loop<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t\t\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> ar<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">8<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">11<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\tfindPair2<span style=\"color: #333333;\">(<\/span>ar<span style=\"color: #333333;\">,<\/span> sum<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Solution 2: Maintain two indexes<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">Solution 3 &#8211; Use of HashMap (Linear time O(n))<\/strong><\/h4>\n<p>In this solution, we iterate though the array just once and store each element in a hash set.. For each element, we would check if the complement of the element is already\u00a0 in the hashset. If it is there, then we have\u00a0 a sum. If it is not there then we insert it.<\/p>\n<p>The Java implementation is given below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Find pair with given sum using a hashma[<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">package<\/span> examples<span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.HashMap<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Map<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FindPairSum3<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findPair<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> A<span style=\"color: #333333;\">[],<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> sum<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\tMap<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">,<\/span> Integer<span style=\"color: #333333;\">&gt;<\/span> map <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> HashMap<span style=\"color: #333333;\">&lt;&gt;();<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888;\">\/\/if complement is already in map, print the pair<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>map<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">containsKey<\/span><span style=\"color: #333333;\">(<\/span>sum<span style=\"color: #333333;\">-<\/span>A<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">]))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Pair found at \"<\/span> <span style=\"color: #333333;\">+<\/span> map<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">(<\/span>sum<span style=\"color: #333333;\">-<\/span>A<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">])<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\" and \"<\/span> <span style=\"color: #333333;\">+<\/span> i<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t\r\n\t\t\tmap<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span>A<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">],<\/span> i<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of for<\/span>\r\n\t\t\r\n\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end for function<\/span>\r\n\t\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> ar<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">9<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">22<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">29<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n         findPair<span style=\"color: #333333;\">(<\/span>ar<span style=\"color: #333333;\">,<\/span> sum<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p><strong>Solution 3<\/strong> : Using a HashMap<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question 1 Given an array of integers, you must find a pair within the array that sums up to a given sum. We assume the &hellip; <\/p>\n","protected":false},"author":395,"featured_media":14,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,2,3],"tags":[4,5],"class_list":["post-9","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array","category-coding-challenge","category-java","tag-hackerrank","tag-java-problems-and-solutions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Question 1: Find Pair With Given Sum in an Array - Coding Challenge<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Question 1: Find Pair With Given Sum in an Array - Coding Challenge\" \/>\n<meta property=\"og:description\" content=\"Question 1 Given an array of integers, you must find a pair within the array that sums up to a given sum. We assume the &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Coding Challenge\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-05T14:40:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-05T17:19:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"927\" \/>\n\t<meta property=\"og:image:height\" content=\"495\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Question 1: Find Pair With Given Sum in an Array\",\"datePublished\":\"2019-02-05T14:40:31+00:00\",\"dateModified\":\"2019-02-05T17:19:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/\"},\"wordCount\":307,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Pair-with-given-sum.jpg\",\"keywords\":[\"Hackerrank\",\"Java Problems and Solutions\"],\"articleSection\":[\"Array\",\"Coding Challenge\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/\",\"name\":\"Question 1: Find Pair With Given Sum in an Array - Coding Challenge\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Pair-with-given-sum.jpg\",\"datePublished\":\"2019-02-05T14:40:31+00:00\",\"dateModified\":\"2019-02-05T17:19:58+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Pair-with-given-sum.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Pair-with-given-sum.jpg\",\"width\":927,\"height\":495,\"caption\":\"Pair with a given sume\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-1-find-pair-with-given-sum-in-an-array\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Question 1: Find Pair With Given Sum in an Array\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/\",\"name\":\"Coding Challenge\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Question 1: Find Pair With Given Sum in an Array - Coding Challenge","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/","og_locale":"en_US","og_type":"article","og_title":"Question 1: Find Pair With Given Sum in an Array - Coding Challenge","og_description":"Question 1 Given an array of integers, you must find a pair within the array that sums up to a given sum. We assume the &hellip;","og_url":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/","og_site_name":"Coding Challenge","article_published_time":"2019-02-05T14:40:31+00:00","article_modified_time":"2019-02-05T17:19:58+00:00","og_image":[{"width":927,"height":495,"url":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Question 1: Find Pair With Given Sum in an Array","datePublished":"2019-02-05T14:40:31+00:00","dateModified":"2019-02-05T17:19:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/"},"wordCount":307,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg","keywords":["Hackerrank","Java Problems and Solutions"],"articleSection":["Array","Coding Challenge","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/","url":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/","name":"Question 1: Find Pair With Given Sum in an Array - Coding Challenge","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg","datePublished":"2019-02-05T14:40:31+00:00","dateModified":"2019-02-05T17:19:58+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Pair-with-given-sum.jpg","width":927,"height":495,"caption":"Pair with a given sume"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-1-find-pair-with-given-sum-in-an-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/coding\/"},{"@type":"ListItem","position":2,"name":"Question 1: Find Pair With Given Sum in an Array"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#website","url":"https:\/\/www.kindsonthegenius.com\/coding\/","name":"Coding Challenge","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/coding\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/coding\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":10,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/9\/revisions\/10"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/media\/14"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}