chore(JavaScript): add two sum (#1031)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/1046/merge
Manik Rana 2022-10-21 08:29:44 +05:30 committed by GitHub
parent 2da3cda5b9
commit ec8bdb7c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -3,6 +3,7 @@
## Arrays
- [Counting Inversions](src/arrays/counting-inversions.js)
- [Two Sum](src/arrays/two-sum.js)
- [Single Occurring Element](src/arrays/single-occurring-element.js)
## Linked Lists

View File

@ -0,0 +1,52 @@
// Documentation credit: Himanshu from two-sum.go
/*
Problem Statement : Given an array of integers nums and an integer target,
return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
Input: An array of integers and a target (int)
Output: array of indexes of len(2) with sum of element
at that index equal to target or nil
*/
/*
Using Brute Force : For every element check for another element if
it exist in the array such that sum of both the element is equals
to the target
Time Complexity : O(n^2)
*/
const twoSumBrute = (arr, target) => {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] === target) {
return [i, j];
}
}
}
return [];
};
/*
Using Map := While traversing every element add the element as key
and its position as its value in a map Check the required value
(i.e target - arr[i]) in the map If the map contains the required value
then we have two elements with the required sum and return the positions.
Time Complexity : O(n)
*/
const twoSum = (arr, target) => {
const map = new Map();
for (let i = 0; i < arr.length; i++) {
if (map.has(arr[i])) return [map.get(arr[i]), i];
map.set(target - arr[i], i);
}
return [];
};
const nums = [2, 7, 11, 15];
const target = 9;
console.log(`Two Sum brute: ${twoSumBrute(nums, target)}`);
console.log(`Two sum optimized: ${twoSum(nums, target)}`);