# Approaching Algorithms with JavaScript

Two Sum is the name for one of the most common questions asked in coding interviews. Given an array of integers and a number, return the indexes where the sum of two values within the array equal the given number. Since Two Sum is such a common problem, it is important to understand how to approach solving the problem so our solution can be flexible enough to accommodate any given permutation that the interviewer might throw us.

Two Sum is a variation of the subset sum problem (SSP) in computer science. For the purposes of this example, I will reference a LeetCode framing of this problem.

`Given an array of integers `*nums* and an integer target, return *indices of the two numbers such that they add up to **target*.

We are given an example of what the input will look is, as well as what we can expect as a result.

**Input:** nums = [2,7,11,15], target = 9

**Output:** [0,1]

**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].

# Problem Solving Approach

Like most algorithmic problems that interviewers ask, there are many possible scenarios within with Two Sum can be invoked. For instance, one important unknown is whether or not the array of integers is sorted — such a difference will greatly change our approach to solving the problem. It is important to have a methodology in place in order to comfortably deal with unknown algorithm questions. Here are a few steps I like to follow when approaching problem solving:

**Understand the problem**. Take the problem and restate it in your own words. Make sure you know what the inputs are. Try to imagine edge cases, such what if negative numbers are input? What about extremely large numbers? Other important questions include do we have enough information to solve the problem (can we derive an output from our input data?), and what is the best way to label data relevant to our problem? Labeling our data is particularly important since it helps us structure our solution. Naming arguments, functions, and the return statement will serve as a framework for the next steps in our problem solving approach.

In the case of our example from LeetCode, we are given an array of integers called `nums`

, as well as a `target`

integer we will be checking against our `nums`

array. To summarize the problem, given an array of numbers and a target, find any pair of numbers which sum to the target number and return their indices. The inputs are the `nums`

array and `target`

integer. Negative number as inputs should not make a difference. JavaScript has some limitations on summing extremely large numbers. By default a number of a certain size will return `infinity`

so there may be so issues here if we are summing an array of extremely large numbers, however, for the scope of this particular problem we can assume that we will be given numbers which we can calculate the sums of.

**Explore concrete examples**. Taking a moment to conceptualize the problem and thinking of a personal way to illustrate the problem can help when developing a strategy for how to approach solving the problem. In the case of Two Sum I will visualize how if I were to perform the task on my own, without a computer.

Here I will individually sum each input with each other input, highlighting each combination which sums to our `target`

of `9`

.

for example:nums = [2,7,11,15], target = 92+7=9 -- return these indices!

2+11!=9

2+15!=9

7+11!=9

7+15!=9

11+15!=9return [0, 1]

This particular approach to solving the Two Sum problem is called the Brute Force approach. It is the simplest way of solving the problem, but not the most efficient.

**Break down the problem**. Once we have an understanding of our initial approach to this problem we will explicitly write out the steps which we will take in the form of pseudocode. This will serve as a blueprint for when we solve our problem and is an essential component of our problem solving strategy since it lays out our solution without having to Explicitly write out the steps you need to take.

Here is the corresponding pseudocode for our strategy outlined above:

// Define a function with 2 arguments (nums and target)// For the currentIndice in the array, check against the sum of the currentIndice and the nextIndice against the target.// If sum of currentIndice and nextIndice === target, return the indices of currentIndice and nextIndice.// Iterate through each element of the array.

**Solve/simplify the problem**. Solve a simpler problem. Ignore the part of the problem that is giving you a hard time and focus on everything else. Find the core difficulty in what you’re trying to do. Temporarily ignore that difficulty. Write a simplified solution. Then incorporate that difficulty back in.

const twoSum = (nums, target) => {

let indices = []; for(let i = 0; i < nums.length; i++){

for(let j = i + 1; j < nums.length; J++}{

if(nums[i] + nums[j] === target}{

indices.push(i);

indices.push(j);

}

}

}

return indices;

}

**Look back and refactor the solution**.

`const twoSum = (nums, target) {`

for (let i = 0; j = nums.length - 1; i < j;) {

let sum = nums[i] + nums[j];

if (sum === target) {

return new Array(i + 1, j + 1);

} else if (sum < target) {

i++;

} else {

j--;

}

}

};