Today we talk about how to use recursion to solve a specific algorithm problem.

I recently did a coding challenge as part of a job interview and they asked an algorithm problem I hadn’t come across before. I thought this one was particularly interesting and at the time I had no clue how to solve it, so today let’s break it down together. Here’s the problem:

``````Have the function PlusMinus(num) read the num parameter being passed which will be a combination of 1 or
more single digits, and determine if it's possible to separate the digits with either a plus or minus sign
to get the final expression to equal zero. For example: if num is 35132 then it's possible to separate the
digits the following way, 3 - 5 + 1 + 3 - 2, and this expression equals zero. Your program should return a
string of the signs you used, so for this example your program should return -++-. If it's not possible to
get the digit expression to equal zero, return the string not possible.If there are multiple ways to get the
final expression to equal zero, choose the one that contains more minus characters. For example: if num is
26712 your program should return -+-- and not +-+-.
``````

Ok, there’s a lot of information in this problem so let’s break it down:

## What are we trying to do?

I think this problem phrases it oddly but we are going to take in a number that will be one or more digits. First we need to strip the number down to the individual integers that make up the number. Then we want to figure out if it is possible to put plus signs or minus signs between the numbers in some combination to get the whole thing to equal zero. Finally we return that list of pluses and minuses or we return “not possible” if it isn’t possible for those numbers to equal zero in any combination.

## So where do we start?

After we make sure we understand the problem, the next step is to figure out some examples. There is a good one provided in the problem so let’s use that one, and I also added one that gives us our ‘not possible’ outcome:

``````let num = 35132
//expected output: '-++-'

let anotherNum = 199
//expected output: 'not possible'
``````

## Let’s figure out a plan:

1. First, we need to find a way to get our input into the correct form. We are given an integer so we need to break that integer apart into its individual numbers.
2. Let’s make an array to hold our possible solution strings
3. We’re going to write a helper function to traverse through our array of numbers. This is where our recursion comes in.
4. We’re going to write another helper function to figure out the best possible solution from the ones we have come up with.
5. Then we return the possibility with the most minuses or we return ‘not possible’

I know I kind of glazed over the juicy details above, I’ll break it down below each code snippet.

``````function PlusMinus(num) {
let array = String(num).split('').map((num) => parseInt(num));
const possibilities = []
``````

#javascript #job-interview #algorithms #programming

17.35 GEEK