Skip to content

Commit fb20f58

Browse files
committed
added first solution with test case
1 parent 35478af commit fb20f58

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
import run1 from './solutions/1';
2+
3+
run1();
4+
15

src/solutions/1/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import twoSum from './twoSum';
2+
3+
const run = () => {
4+
const input : number[] = [2,7,11,15];
5+
const target : number = 9;
6+
const expectedOutput : number[] = [0,1];
7+
8+
9+
console.info("Input : ", input)
10+
console.info("Target: ", target)
11+
console.info("Expected Output: ", expectedOutput)
12+
console.log("=============================")
13+
console.time("execution")
14+
console.log("Output:::::", twoSum(input, target))
15+
console.timeEnd("execution")
16+
}
17+
18+
export default run;

src/solutions/1/twoSum.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import twoSum from "./twoSum";
2+
3+
test('solution 1 - twoSum', () => {
4+
expect(twoSum([2, 7, 11, 15], 9)).toEqual([0, 1]);
5+
expect(twoSum([3, 2, 4], 6)).toEqual([1, 2]);
6+
expect(twoSum([3, 3], 6)).toEqual([0, 1]);
7+
});

src/solutions/1/twoSum.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const twoSum = (nums: number[], target: number): number[] | undefined => {
2+
let output: number[] = [];
3+
for(let i = 0; i < nums.length; i++) {
4+
for(let j = i+1; j<nums.length; j++) {
5+
if(nums[i]+nums[j] === target) {
6+
output.push(i, j);
7+
return output;
8+
}
9+
}
10+
}
11+
}
12+
13+
export default twoSum;

0 commit comments

Comments
 (0)