-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
826 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let map = {}; | ||
A.forEach(element => { | ||
if (map[element] === 1) { | ||
map[element] = 0; | ||
} else if(typeof map[element] === "undefined" || map[element] === 0) { | ||
map[element] = 1; | ||
} | ||
}); | ||
|
||
for (const key in map) { | ||
if (map.hasOwnProperty(key)) { | ||
const element = map[key]; | ||
if(element !== 0) { | ||
return key; | ||
} | ||
} | ||
} | ||
} | ||
console.log(solution([9, 3, 9, 3, 9, 7, 9])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. | ||
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. | ||
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| | ||
In other words, it is the absolute difference between the sum of the first part and the sum of the second part. | ||
*/ | ||
|
||
function solution(A) { | ||
var sumRight = A.reduce((pv, cv, idx) => (idx > 0) ? (pv + cv) : (0), 0); | ||
var sumLeft = 0; | ||
var substractions = []; | ||
var maxI = A.length - 1; | ||
|
||
for(var i = 0; i < maxI; i += 1){ | ||
sumLeft += A[i]; | ||
substractions.push(Math.abs(sumLeft - sumRight)); | ||
if (i + 1 <= maxI) sumRight -= A[i + 1]; | ||
} | ||
|
||
return substractions.reduce((pv, cv, idx) => (idx > 0) ? ((pv < cv)? pv : cv) : (cv), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function solution(A, B) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let numFishes = A.length; | ||
if (numFishes === 0) return 0; | ||
let fishScore = []; | ||
for (let i = 0; i < A.length; i++) { | ||
if (B[i] === 1) { | ||
fishScore.push(A[i]); | ||
} else { | ||
while (fishScore.length !== 0) { | ||
if (A[i] < fishScore[fishScore.length - 1]) { | ||
numFishes--; | ||
break; | ||
} else if (A[i] > fishScore[fishScore.length - 1]) { | ||
numFishes--; | ||
fishScore.pop(); | ||
} | ||
} | ||
} | ||
} | ||
return numFishes; | ||
} | ||
console.log(solution([4, 3, 2, 1, 5], [0, 1, 0, 0, 0])); | ||
console.log(solution([4, 6, 2, 1,7], [0, 1, 0, 0,1])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function solution(S) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let stack = [], | ||
pairs = { | ||
"{": "}", | ||
"[": "]", | ||
"(": ")", | ||
}; | ||
for (let index = 0; index < S.length; index++) { | ||
const element = S[index]; | ||
if (element === "{" || element === "(" || element === "[") { | ||
stack.push(element); | ||
continue; | ||
} | ||
if(stack.length === 0) return 0; | ||
|
||
if (element !== pairs[stack.pop()]) { | ||
return 0; | ||
} | ||
} | ||
return stack.length === 0 ? 1: 0; | ||
} | ||
console.log(solution("{[()()]}")); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let map = {}, | ||
uniqueCount = 0; | ||
A.forEach((element) => { | ||
if (map[element] === undefined) { | ||
map[element] = true; | ||
uniqueCount++; | ||
} | ||
}); | ||
return uniqueCount; | ||
} | ||
console.log(solution([2, 1, 1, 2, 3, 1])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let map = {}, | ||
requiredLength = Math.floor(A.length / 2) + 1; | ||
for (let index = 0; index < A.length; index++) { | ||
const element = A[index]; | ||
if (map[element] === undefined) { | ||
map[element] = [index]; | ||
} else { | ||
map[element].push(index); | ||
} | ||
if (map[element].length >= requiredLength) { | ||
return index; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
console.log(solution([3, 4, 3, 2, 3, -1, 3, 3, 2])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
function solution(X, Y, D) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
return Math.ceil((Y-X) / D); | ||
} | ||
|
||
|
||
|
||
// A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. | ||
|
||
// Count the minimal number of jumps that the small frog must perform to reach its target. | ||
|
||
// Write a function: | ||
|
||
// function solution(X, Y, D); | ||
|
||
// that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y. | ||
|
||
// For example, given: | ||
// X = 10 | ||
// Y = 85 | ||
// D = 30 | ||
|
||
// the function should return 3, because the frog will be positioned as follows: | ||
|
||
// after the first jump, at position 10 + 30 = 40 | ||
// after the second jump, at position 10 + 30 + 30 = 70 | ||
// after the third jump, at position 10 + 30 + 30 + 30 = 100 | ||
|
||
// Write an efficient algorithm for the following assumptions: | ||
|
||
// X, Y and D are integers within the range [1..1,000,000,000]; | ||
// X ≤ Y. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function solution(X, A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let map = {}, | ||
len = A.length; | ||
for (let second = 0; second < len; second++) { | ||
let val = A[second]; | ||
if (val > X) continue; | ||
if (map[val] === undefined || second < map[val]) { | ||
map[val] = second; | ||
} | ||
} | ||
console.log(map); | ||
let maxSeconds = 0; | ||
for (let index = 1; index < X + 1; index++) { | ||
const element = map[index]; | ||
if(element === undefined) { | ||
return -1; | ||
} else { | ||
maxSeconds = Math.max(maxSeconds, element); | ||
} | ||
} | ||
return maxSeconds; | ||
} | ||
console.log(solution(5, [1, 3, 1, 4, 2, 3, 5, 4])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// console.log([[[[[[[22]]]]]]] == 22); | ||
// console.log(["1", "2", "3"].map(parseInt)); | ||
|
||
// fetch("http://starlord.hackerearth.com/kickstarter") | ||
// .then((response) => { | ||
// console.log(response); | ||
// }) | ||
// .then((data) => console.log(data)); | ||
|
||
//Insert code below | ||
|
||
// console.log("Hello World!"); | ||
fetch("http://starlord.hackerearth.com/kickstarter") | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
let body = `<tr> | ||
<th>S. No.</th> | ||
<th>Percentage Funded</th> | ||
<th>Amount Pledged</th> | ||
</tr>`; | ||
for (let i = 0; i < data.length; i++) { | ||
let row = `<tr><td>${data[i]["s.no"]}</td><td>${data[i]["percentage.funded"]}</td><td>${data[i]["amt.pledged"]}</td></tr>`; | ||
body = body + row; | ||
} | ||
console.log(body); | ||
document.getElementById("table").innerHTML = body; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function solution() { | ||
console.log(variableA); // undefined | ||
console.log(letA); // Cannot access 'letA' before initialization | ||
var variableA = 20; | ||
let letA = 30; | ||
} | ||
|
||
// solution(); | ||
|
||
function solutionWithTimeout() { | ||
setTimeout(() => { | ||
console.log(variableA); // 20 | ||
console.log(letA); // 30 | ||
}, 0); | ||
var variableA = 20; | ||
let letA = 30; | ||
} | ||
|
||
solutionWithTimeout(); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function solution(N, A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let counters = Array(N).fill(0), | ||
max = 0; | ||
for (let index = 0; index < A.length; index++) { | ||
const element = A[index]; | ||
if (element <= N) { | ||
counters[element - 1] += 1; | ||
max = Math.max(max, counters[element - 1]); | ||
} else { | ||
counters.fill(max); | ||
} | ||
} | ||
return counters; | ||
} | ||
console.log(solution(5, [3, 4, 4, 6, 1, 4, 4])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
// if size is less than 3, no triplet exists | ||
let n = A.length; | ||
if (n < 3) return -1; | ||
let max1 = A[0], max2 = -1, max3 = -1, min1 = A[0], min2 = -1; | ||
for(let i = 1; i < n; i++) { | ||
if(A[i] > max1) { | ||
max3 = max2; | ||
max2 = max1; | ||
max1 = A[i]; | ||
} else if(max2 === -1 || A[i] > max2) { | ||
max3 = max2; | ||
max2 = A[i]; | ||
} else if( max3 === -1 || A[i] > max3) { | ||
max3 = A[i]; | ||
} | ||
if(A[i] < min1) { | ||
min2 = min1; | ||
min1 = A[i]; | ||
} else if(min2 === -1 || A[i] < min2) { | ||
min2 = A[i]; | ||
} | ||
} | ||
console.log(max1, max2, max3, min1, min2); | ||
let prod1 = max1 * max2 * max3, prod2 = max1 * min1 * min2; | ||
return Math.max(prod1, prod2); | ||
} | ||
console.log(solution([-3, -2, -16, -20, -25])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let maxSum = A[0], currentSum = 0; | ||
for (let index = 0; index < A.length; index++) { | ||
const element = A[index]; | ||
currentSum += element; | ||
maxSum = Math.max(maxSum, currentSum); | ||
if(currentSum < 0) currentSum = 0; | ||
} | ||
return maxSum; | ||
} | ||
|
||
console.log(solution([3, 2, -6, 4, 0])); | ||
console.log(solution([-10])); | ||
console.log(solution([-10, -1])); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function solution(A) { | ||
// write your code in JavaScript (Node.js 8.9.4) | ||
let min = A[0], | ||
maxProfit = 0; | ||
for (let index = 1; index < A.length; index++) { | ||
const element = A[index]; | ||
maxProfit = Math.max(element - min, maxProfit); | ||
min = Math.min(element, min); | ||
} | ||
return maxProfit; | ||
} | ||
console.log(solution([23171, 21011, 21123, 21366, 21013, 21367])); | ||
console.log(solution([2, 3, 10, 6, 4, 8, 1])); | ||
console.log(solution([7, 9, 5, 6, 3, 2])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
let merge_sorted = function(head1, head2) { | ||
// if both lists are empty then merged list is also empty | ||
// if one of the lists is empty then other is the merged list | ||
if (!head1) { | ||
return head2; | ||
} else if (!head2) { | ||
return head1; | ||
} | ||
|
||
let mergedHead = null; | ||
if (head1.data <= head2.data) { | ||
mergedHead = head1; | ||
head1 = head1.next; | ||
} else { | ||
mergedHead = head2; | ||
head2 = head2.next; | ||
} | ||
|
||
let mergedTail = mergedHead; | ||
|
||
while (head1 && head2) { | ||
let temp = null; | ||
if (head1.data <= head2.data) { | ||
temp = head1; | ||
head1 = head1.next; | ||
} else { | ||
temp = head2; | ||
head2 = head2.next; | ||
} | ||
|
||
mergedTail.next = temp; | ||
mergedTail = temp; | ||
} | ||
|
||
if (head1) { | ||
mergedTail.next = head1; | ||
} else if (head2) { | ||
mergedTail.next = head2; | ||
} | ||
|
||
return mergedHead; | ||
}; | ||
|
||
console.log(""); | ||
console.log(""); | ||
console.log("+++++++++++++++++++++++++++++++++++++++"); | ||
console.log("Insertion Sort"); | ||
console.log("---------------------------------------"); | ||
|
||
let merge_sort_node_1 = create_linked_list([1, 3, 5, 6]); | ||
let merge_sort_node_2 = create_linked_list([2, 4, 6, 20, 34]); | ||
let merged_sort = create_linked_list([1, 2, 3, 4, 5, 6, 6, 20, 34]); | ||
|
||
|
||
let temp_head = merge_sort_node_1; | ||
console.log("1st Linked List"); | ||
while (temp_head) { | ||
console.log(temp_head.data); | ||
temp_head = temp_head.next; | ||
} | ||
|
||
temp_head = merge_sort_node_2; | ||
console.log("2nd Linked List"); | ||
while (temp_head) { | ||
console.log(temp_head.data); | ||
temp_head = temp_head.next; | ||
} | ||
let result = merge_sorted(merge_sort_node_1, merge_sort_node_2); | ||
|
||
temp_head = result; | ||
console.log("Result Merge Sorted List"); | ||
while (temp_head) { | ||
console.log(temp_head.data); | ||
temp_head = temp_head.next; | ||
} |
Oops, something went wrong.