{"id":6,"date":"2023-03-10T16:54:25","date_gmt":"2023-03-10T16:54:25","guid":{"rendered":"https:\/\/vmlogger.com\/algorithms\/?p=6"},"modified":"2023-03-27T13:00:43","modified_gmt":"2023-03-27T13:00:43","slug":"two-number-sum","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/algorithms\/2023\/03\/10\/two-number-sum\/","title":{"rendered":"Two Number sum from an Array of Integers"},"content":{"rendered":"
This is a very common algorithm question. Although it sounds very easy at first while writing the code for such problems might trick you a little bit. This problem is commonly used in technical interviews and is a great way to demonstrate a programmer’s problem-solving skills. In this article, we will explore the problem in depth, discuss various approaches to solving it, and provide some tips on how to tackle similar algorithm problems.<\/p>\n
Before we go to the solutions for each of these problems, let me explain the problem statement with an example. Also, this problem can be of summing 3 numbers, 4 numbers, etc. As the total count of numbers increases, complexity increases. <\/p>\n
Example:<\/strong> In this approach, we compare every pair of numbers in the array until we find the two numbers that add up to the target sum. This method has a time complexity of O(n^2) as it requires two nested loops to traverse the array.<\/p>\n \nAt first, you are traversing the entire array for each of its elements and comparing the sum if it is equal to the given targetSum. The moment, it matches, breaks out inner and outer loops, and returns the result array.<\/p>\n The following is a better solution from a time complexity perspective. Instead of traversing the entire array for each element, traverse the array only once. For each number, calculate the expected second number needed in order to make the sum equal to targetSum. Now using, python’s built function check if that number exists in the array. If yes, then return those two values as an array. At last, if found nothing, an empty array will be returned. <\/p>\n The classic two-number sum algorithm problem is a fundamental problem in computer science that can be solved using multiple approaches. The brute force method is the simplest but least efficient approach, while the second solution is more efficient. By understanding the problem statement and using the right approach, you can solve this problem efficiently and demonstrate your problem-solving skills to potential employers.<\/p>\n \nIf you like these solutions, share them with your friends, and colleagues. If you find any other solutions, please post them in the comment, I may put them in the main article here so that others can also get benefitted from all different types of solutions.<\/p>\n<\/div>\n<\/span>","protected":false},"excerpt":{"rendered":" An Introduction This is a very common algorithm question. Although it sounds very easy at first while writing the code for such problems might trick you a little bit. This problem is commonly used in technical interviews and is a great way to demonstrate a programmer’s problem-solving skills. In this article, we will explore the […]<\/p>\n","protected":false},"author":45,"featured_media":137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-easy"],"yoast_head":"\n
\nintArray = [1, 4, 3, 6, 8, 2]
\nTargetSum = 11
\nOutput = [3, 8] or [8, 3]\n<\/div>\nSolution – 1 – Brute Force Method<\/h2>\n
\r\n# Time Complexity = O(n^2)\r\ndef twoNumberSum(array, targetSum):\r\n result = []\r\n for i in range(0, len(array),1):\r\n for j in range(i+1, len(array), 1):\r\n if array[i] + array[j] == targetSum:\r\n result.append(array[i])\r\n result.append(array[j])\r\n break\r\n if result != []:\r\n break\r\n return result\r\n<\/pre>\n
Solution – 2 [Better Solution]<\/h2>\n
\r\n# Time Complexity = O(n)\r\ndef twoNumberSum(array, targetSum):\r\n for iNum in array:\r\n expectedNum = targetSum - iNum\r\n if (expectedNum in array) and (expectedNum is not iNum):\r\n return [iNum, temp]\r\n return []\r\n<\/pre>\n
Info<\/h3>\n