- An indexes array is created to store the index integers.
- A double loop is created to check which numbers when added are equal to the target.
- if the sum of the numbersisequal to the target, "1" is added to their indexes which is then pushed to the indexes array.
-
Time complexity: O(N^2)
-
Space complexity: O(1)
function twoSum(numbers: number[], target: number): number[] {
let indexes: number[] = []
for (let i = 0; i < numbers.length; i++) {
let j = i + 1
while (j < numbers.length) {
if (numbers[j] + numbers[i] === target) {
indexes.push( i + 1,j + 1)
}
j++
}
}
return indexes
};