Three Number Sum Coding Challenge
This weeks coding problem is called three number sum. The concept of this challenge is you are given an array, and a targetSum value. The array contains integers that are not sorted and a target sum is the target give to see if any 3 values in our array add up to the value of our target sum. If they do add up then we must return a nested list of integers with each list in our list containing 3 values that were in our array that equal our target. This problem is similar to a two target sum problem I have done before but since its 3 number sum this makes it a lot more difficult.
In a 2 target sum coding challenge all you would have to do is use 2 for loops. The first for loop you keep track of the first_index and in your second for loop you loop through the list again but starting at first_index + 1. Then you simply keep track of how many times the sum of those 2 values add the target sum and append those values to your list. However this is a 3 target sum. We could use 3 for loops to go through the list 3 times but that would not be optimal since you would be going to 3 different loops. This is where a new technique I learned called pointers come into place. I have used pointers before but never in this way. I attached a video link of a video I made so you guys can visually see what I am doing.
Comments
Post a Comment