Algorithm Problem Solving: Two-Sum and Three-Sum Problem
By: Kailas Walldoddi
Tags: JavaScript, Algorithms, Programming, Dynamic Programming, Problem Solving

Problem Solving : Two-Sum and Three-Sum Problem

Algorithm Problem Solving: Two-Sum and Three-Sum Problem

UPDATE : The Article currently contains Brute-Force Approaches for Three-sum Problem, Will update the better approaches soon.

I have started exploring Advent of Code for the year 2020. For Day 1, The scenario included writing a program to find the product of ’n’ numbers that add up to the given target sum value. So, Ideally finding those two/three elements is the key. This Article explains various ways you can achieve this.

Problem Statement for Two sum Problem

Given an array of integers nums and an integer target_sum , return two numbers such that they add up to target_sum .

For example, suppose your sample input contained the following and the target sum provided is 2020

1721

979

366

299

675

1456

In this list, the two entries that sum to 2020 are 1721 and 299 .

Approach 1: Brute Force

The brute force approach is simple. Loop through each element x and find if there is another value y , that equals to y = target_sum — x .

Brute Force way to solve two sum problem using JavaScript

Approach 2: Using Hash Maps.

Tradeoff between time complexity and space complexity are vice-versa, in our second approach we will create a hashMap. in the first for-loop we loop through the array and assign the push the elements in the hashMap.

Reducing the time complexity in internal for loop from O(n*2) to O(n)

In our second for-loop, we use hasOwnProperty and try to look for the differential value element between target_sum and current index element.

Problem Statement for Three sum Problem

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = target_sum ? Find all unique triplets in the array which gives the sum of target_sum .

For example, suppose your sample input contained the following and the target sum provided is 2020

1721

979

366

299

675

1456

In the above list, the three entries that sum to 2020 are 979 , 366 , and 675 .

Approach 1: Brute Force

In this approach, we try to calculate sum for current element (obtained using first for-loop) with next two subsequent elements (obtained using following for-loops).

Solving Three sum problem using Brute Force

Making some amendments

In the above approach the indexes for each i, j and k variable are visited more then required number of times. Thus creating duplicate entries. We could avoid this by simply keeping track of indexes visited as follows. The time taken for execution would be nearly same but we would at least get rid off duplicates.

Removing Duplicate entries in three sum solution set

Prettifying the code in the innermost loop

Just like we did in Two Sum approach 2, we can use Array.find() method in the inner most for-loop to make it look pretty (it has no effect on current time complexity).

Reducing the time complexity in innermost for loop from O(n) to O(1)

The above solutions are not optimized to use minimum time for execution. however, they will at least get you started regrading how do you think in terms of dynamic programming.

Explore more about the adventOfCode problems statements and their solutions in my GitHub repo by same name. here is the link