Find array elements adding up to given sum
unknown
javascript
a year ago
843 B
3
Indexable
Never
Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Solution Brute Force: let num_array = [2, 7, 11, 15]; let target = 9; let index_1, index_2 = findIndicesAddingToTarget(num_array) function findIndicesAddingToTarget(num_array) { for (let i = 0; i < num_array.length; i++) { for (let j = 0; j < num_array.length; j++) { if (i !== j) { if (num_array[i] + num_array[j] === target) { console.log(`indices ${i} and ${j} are adding up to target`); return i,j } } } } }