Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 181b1a6

Browse files
Merge pull request #9 from knaxus/fix-sum-upto-N
Minor fixes
2 parents 878b928 + d1d0bd2 commit 181b1a6

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
// the best case using a data structure - SET
2-
function findTwoNumsAddingToN(arr, sum) {
3-
const nums = [];
1+
// the best case [O(n)] using SET data structure
2+
function findTwoNumsAddingToN(arr, number) {
3+
const pair = [];
44
const store = new Set();
55

6-
for (let i = 0; i < arr.length; i++) {
7-
// check if the set contains one of the pir that sum upto given sum
8-
if (store.has(sum - arr[i])) {
9-
nums.push(sum - arr[i]);
10-
nums.push(arr[i]);
6+
for (let i = 0; i < arr.length; i+=1) {
7+
// check if the set contains one of the element that sum upto the given number
8+
if (store.has(number - arr[i])) {
9+
pair.push(number - arr[i]);
10+
pair.push(arr[i]);
1111
break;
1212
}
1313
// push the element in the set
1414
store.add(arr[i]);
1515
}
16-
return nums.length ? nums : false;
16+
return pair.length ? pair : false;
1717
}
1818

1919
// the Brute force approach
20-
function findTwoNumsAddingToN2(arr, sum) {
21-
const nums = [];
22-
for (let i = 0; i < arr.length; i++) {
23-
for (let j = i + 1; j < arr.length; j++) {
24-
if (arr[i] + arr[j] === sum) {
25-
nums.push(arr[i], arr[j]);
20+
function findTwoNumsAddingToN2(arr, number) {
21+
const pair = [];
22+
for (let i = 0; i < arr.length; i+=1) {
23+
for (let j = i + 1; j < arr.length; j+=1) {
24+
if (arr[i] + arr[j] === number) {
25+
pair.push(arr[i], arr[j]);
2626
break;
2727
}
2828
}
2929
}
3030

31-
return nums.length ? nums : false;
31+
return pair.length ? pair : false;
3232
}

‎src/_Problems_/find-2nd-max/index.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* You may find it easy but it's tricky for few
3+
* Input - [9, 2, 3, 6]
4+
* Output - 6
5+
*/
6+
7+
function findSecondMax(arr) {
8+
let max = arr[0];
9+
let max2 = Number.MIN_SAFE_INTEGER;
10+
11+
for (let el of arr) {
12+
if (el > max) {
13+
max2 = max;
14+
max = el;
15+
}
16+
17+
if (el < max && el > max2) {
18+
max2 = el;
19+
}
20+
}
21+
return max2;
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /