Problem Statement
Problem Sources: LeetCode
Input: variable A: Array of Number and variable B: Number
Process: Return two Index of Variable A that if those two value of the Index summed up it should be equal to the variable B. Business Rule: Can only use one same element or element index
Test Cases
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
reasoning: nums[0] + nums [1] = 2+7 = 9
Solution
below solution still has O(N^2) performance
function twoSum(nums: number[], target: number): number[] {
let output: Array<number> = [];
let loopProcess: boolean = true;
for(var x = 0;x<nums.length && loopProcess == true;x++){
for(var y = 0; y<nums.length && loopProcess;y++){
if(y !== x){
if(nums[x]+nums[y] == target){
output.push(x);
output.push(y);
loopProcess = false;
}
}
}
}
return output;
};