Find possible numbers in array that can sum to a target value JavaScript


In this problem statement, we are required to find all the possible numbers in an array that can sum to a given target value with the help of Javascript functionalities. This task can be done with the help of some built in functions of Javascript or we can solve it by multiple for loops.

Logic for the given problem

The problem stated that we have to get the possible numbers in an array which can give the exact value as target value by summing them up with the help of Javascript functionalities.

As we have to find out the numbers and by adding them together we should get the sum equal to the target value. Ao for doing this operation we will initialize an array and a target value. Then we will loop through for every array value and again loop through the other remaining elements in the array to check that there is a pair which sums to get the target value. If that pair is found then add it to the new array. So at the end we will have an array of elements that the sum of the elements will be equal to the target value mentioned.

Algorithm

Step 1 − At the very first step we need to define a function which will find out the possible numbers of array to get the required sum as target value.

Step 2 − Now we will define a blank array which will store the numbers of resultant subarrays.

Step 3 − After the second step we will use a for loop to iterate through all the elements of the array till the length of the array.push

Step 4 − Now we use another for loop or nested for loop to get the required two elements. These two elements's sum should be equal to the target value.

Step 5 − If the total of those two elements equals the target value, we will add it to the array we have defined in step two.

Step 6 − In the next step, pass the array and target value in the function to execute and produce the result.

Step 7 − At the last step, get the output to the console after passing the numbers array and target value to the function.

Code for the algorithm

function possibleNums(array, targetValue) {
   const numbersArray = [];
   // nested loops to find the sum as target value
   for (let i = 0; i < array.length; i++) {
      for (let j = i + 1; j < array.length; j++) {
         if (array[i] + array[j] === targetValue) {
            numbersArray.push([array[i], array[j]]);
         }
      }
   }
   return numbersArray;
}
const array = [1, 2, 3, 4, 5, 6, 7];
const targetValue = 7;
const numbersArray = possibleNums(array, targetValue);
console.log(numbersArray);

Complexity

Assuming n is the length of the given array, so the time taken by the mentioned algorithm is O(n^2). Because we utilized two for nested loops to iterate and achieve the required result according to the problem given.

The space complexity of the algorithm is determined by the size of the input array and the number of pairs identified that total to the target value. If n is the number of array items, and the space needed by the pairs array is proportional to the number of pairs that total to the target value. As a result, the number of pairs will be n/2. So, the whole space complexity will be O(n).

Conclusion

In the above code we have used two nested loops and created a function to get the desired result as per the given problem statement. Basically we have to compare the sum or two elements to get the sum equal to the target value mentioned in the code. After finding those elements we have pushed these elements in the new array to show the output. So for executing this piece of code we need O(n^2) time to complete it. And the memory required to store the result is O(n).

Updated on: 18-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements