Teaching Kids Programming – 3 Different Approaches to Solve Two-Sum Problem


Teaching Kids Programming: Videos on Data Structures and Algorithms

This is actually a very famous known problem for beginners to into Algorithms & Data Structures. Given a list of numbers, and a target, find out if there are two numbers in the list that sums up to the target.

For example: nums = [2, 1, 4, 6, 7], and target is 10 – we should return True as the 4+6 = 10.

Two Sum in Bruteforce Algorithm

We can bruteforce each possible pairs (j < i) and and check if nums[i] + nums[j] is the target. The complexity is O(N^2).

1
2
3
4
5
6
def twoSums(nums, target):
  for i in range(len(nums)):
    for j in range(i):
      if nums[i] + nums[j] == target:
        return True
  return False
def twoSums(nums, target):
  for i in range(len(nums)):
    for j in range(i):
      if nums[i] + nums[j] == target:
        return True
  return False

As you can imagine, this approach is inefficient and you may likely to get Time Limit Exceeded verdict when the nums contain a huge number of elements.

Using a Hash Table to Solve Two Sum Problem

We can remember the numbers that we’ve seen using a Hash Table (Hash Set). In Python, we can use a dictionary {} or a set()

1
2
3
4
5
6
7
def twoSums(nums, target):
   nb = {} # or nb = set()
   for i in nums:
      if target - i in nb:
         return True
      nb[i] = True # or nb.add(i)
   return False
def twoSums(nums, target):
   nb = {} # or nb = set()
   for i in nums:
      if target - i in nb:
         return True
      nb[i] = True # or nb.add(i)
   return False

Here, we only need to visit the array once thus the complexity is O(N). We use a hash set (or dictionary), and thus the space complexity is O(N).

Two Sum by Sorting and Two Pointer Algorithm

We can sort the array, and then apply the two-pointer algorithm.

1
2
3
4
5
6
7
8
9
10
11
def twoSums(nums, target):
  nums.sort()
  left, right = 0, len(nums) - 1
  while left < right:
    if nums[left] + nums[right] == target:
      return True
    if nums[left] + nums[right] < target:
      left += 1
    else:
      right -= 1
  return False
def twoSums(nums, target):
  nums.sort()
  left, right = 0, len(nums) - 1
  while left < right:
    if nums[left] + nums[right] == target:
      return True
    if nums[left] + nums[right] < target:
      left += 1
    else:
      right -= 1
  return False

As long as the left and right pointer not meeting in the middle, we move the left pointer one step to the right if the sum is less than the target, or the right pointer one step to the left if the sum is larger. Otherwise, when the sum equals to the target, we can just return True.

The overall complexity is O(NLogN) as the sorting dominates the O(N) two pointers.

Two Sum Variation Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
539 words
Last Post: Algorithm to Determine a Latin Square using a Hash Table
Next Post: Caesar Cipher Algorithm in C++

The Permanent URL is: Teaching Kids Programming – 3 Different Approaches to Solve Two-Sum Problem

Leave a Reply