Skip to content

Commit d85571f

Browse files
committed
update
1 parent 79e5435 commit d85571f

13 files changed

+919
-0
lines changed

Project Euler/problem-13-large-sum.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const str = "37107287533902102798797998220837590246510135740250\n\
2+
46376937677490009712648124896970078050417018260538\n\
3+
74324986199524741059474233309513058123726617309629\n\
4+
91942213363574161572522430563301811072406154908250\n\
5+
23067588207539346171171980310421047513778063246676\n\
6+
89261670696623633820136378418383684178734361726757\n\
7+
28112879812849979408065481931592621691275889832738\n\
8+
44274228917432520321923589422876796487670272189318\n\
9+
47451445736001306439091167216856844588711603153276\n\
10+
70386486105843025439939619828917593665686757934951\n\
11+
62176457141856560629502157223196586755079324193331\n\
12+
64906352462741904929101432445813822663347944758178\n\
13+
92575867718337217661963751590579239728245598838407\n\
14+
58203565325359399008402633568948830189458628227828\n\
15+
80181199384826282014278194139940567587151170094390\n\
16+
35398664372827112653829987240784473053190104293586\n\
17+
86515506006295864861532075273371959191420517255829\n\
18+
71693888707715466499115593487603532921714970056938\n\
19+
54370070576826684624621495650076471787294438377604\n\
20+
53282654108756828443191190634694037855217779295145\n\
21+
36123272525000296071075082563815656710885258350721\n\
22+
45876576172410976447339110607218265236877223636045\n\
23+
17423706905851860660448207621209813287860733969412\n\
24+
81142660418086830619328460811191061556940512689692\n\
25+
51934325451728388641918047049293215058642563049483\n\
26+
62467221648435076201727918039944693004732956340691\n\
27+
15732444386908125794514089057706229429197107928209\n\
28+
55037687525678773091862540744969844508330393682126\n\
29+
18336384825330154686196124348767681297534375946515\n\
30+
80386287592878490201521685554828717201219257766954\n\
31+
78182833757993103614740356856449095527097864797581\n\
32+
16726320100436897842553539920931837441497806860984\n\
33+
48403098129077791799088218795327364475675590848030\n\
34+
87086987551392711854517078544161852424320693150332\n\
35+
59959406895756536782107074926966537676326235447210\n\
36+
69793950679652694742597709739166693763042633987085\n\
37+
41052684708299085211399427365734116182760315001271\n\
38+
65378607361501080857009149939512557028198746004375\n\
39+
35829035317434717326932123578154982629742552737307\n\
40+
94953759765105305946966067683156574377167401875275\n\
41+
88902802571733229619176668713819931811048770190271\n\
42+
25267680276078003013678680992525463401061632866526\n\
43+
36270218540497705585629946580636237993140746255962\n\
44+
24074486908231174977792365466257246923322810917141\n\
45+
91430288197103288597806669760892938638285025333403\n\
46+
34413065578016127815921815005561868836468420090470\n\
47+
23053081172816430487623791969842487255036638784583\n\
48+
11487696932154902810424020138335124462181441773470\n\
49+
63783299490636259666498587618221225225512486764533\n\
50+
67720186971698544312419572409913959008952310058822\n\
51+
95548255300263520781532296796249481641953868218774\n\
52+
76085327132285723110424803456124867697064507995236\n\
53+
37774242535411291684276865538926205024910326572967\n\
54+
23701913275725675285653248258265463092207058596522\n\
55+
29798860272258331913126375147341994889534765745501\n\
56+
18495701454879288984856827726077713721403798879715\n\
57+
38298203783031473527721580348144513491373226651381\n\
58+
34829543829199918180278916522431027392251122869539\n\
59+
40957953066405232632538044100059654939159879593635\n\
60+
29746152185502371307642255121183693803580388584903\n\
61+
41698116222072977186158236678424689157993532961922\n\
62+
62467957194401269043877107275048102390895523597457\n\
63+
23189706772547915061505504953922979530901129967519\n\
64+
86188088225875314529584099251203829009407770775672\n\
65+
11306739708304724483816533873502340845647058077308\n\
66+
82959174767140363198008187129011875491310547126581\n\
67+
97623331044818386269515456334926366572897563400500\n\
68+
42846280183517070527831839425882145521227251250327\n\
69+
55121603546981200581762165212827652751691296897789\n\
70+
32238195734329339946437501907836945765883352399886\n\
71+
75506164965184775180738168837861091527357929701337\n\
72+
62177842752192623401942399639168044983993173312731\n\
73+
32924185707147349566916674687634660915035914677504\n\
74+
99518671430235219628894890102423325116913619626622\n\
75+
73267460800591547471830798392868535206946944540724\n\
76+
76841822524674417161514036427982273348055556214818\n\
77+
97142617910342598647204516893989422179826088076852\n\
78+
87783646182799346313767754307809363333018982642090\n\
79+
10848802521674670883215120185883543223812876952786\n\
80+
71329612474782464538636993009049310363619763878039\n\
81+
62184073572399794223406235393808339651327408011116\n\
82+
66627891981488087797941876876144230030984490851411\n\
83+
60661826293682836764744779239180335110989069790714\n\
84+
85786944089552990653640447425576083659976645795096\n\
85+
66024396409905389607120198219976047599490197230297\n\
86+
64913982680032973156037120041377903785566085089252\n\
87+
16730939319872750275468906903707539413042652315011\n\
88+
94809377245048795150954100921645863754710598436791\n\
89+
78639167021187492431995700641917969777599028300699\n\
90+
15368713711936614952811305876380278410754449733078\n\
91+
40789923115535562561142322423255033685442488917353\n\
92+
44889911501440648020369068063960672322193204149535\n\
93+
41503128880339536053299340368006977710650566631954\n\
94+
81234880673210146739058568557934581403627822703280\n\
95+
82616570773948327592232845941706525094512325230608\n\
96+
22918802058777319719839450180888072429661980811197\n\
97+
77158542502016545090413245809786882778948721859617\n\
98+
72107838435069186155435662884062257473692284509516\n\
99+
20849603980134001723930671666823555245252804609722\n\
100+
53503534226472524250874054075591789781264330331690";
101+
102+
function large_sum(str) {
103+
return str.split('\n').map(Number).reduce((a,v) => a + v);
104+
}
105+
106+
console.log(large_sum(str).toString());

activity-selection.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function activitySelection(arr) {
2+
if (arr.length === 0) {
3+
return 0;
4+
}
5+
6+
// Sort by finish time
7+
arr.sort((a, b) => a[1] - b[1]);
8+
9+
// Always select the first activity
10+
let i = 0;
11+
let result = [arr[0]];
12+
13+
for (let j = 1, l = arr.length; j < l; j++) {
14+
if (arr[j][0] >= arr[i][1]) {
15+
i = j;
16+
result.push(arr[j]);
17+
}
18+
}
19+
return result;
20+
}
21+
22+
23+
24+
const arr1 = [
25+
// [start, finish]
26+
[12, 25],
27+
[20, 30],
28+
[10, 20]
29+
];
30+
31+
console.log(activitySelection(arr1)); // 2
32+
33+
const arr2 = [
34+
[8, 9],
35+
[5, 7],
36+
[3, 4],
37+
[0, 6],
38+
[5, 9],
39+
[1, 2],
40+
];
41+
42+
console.log(activitySelection(arr2)); // 4

big-sorting.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function bigSorting(arr) {
2+
return arr.sort((a, b) => {
3+
if (a.length !== b.length) {
4+
return a - b;
5+
}
6+
for (let i = 0; i < a.length; i++) {
7+
const a1 = parseInt(a[i]);
8+
const b1 = parseInt(b[i]);
9+
if (a1 !== b1) {
10+
return a1 - b1;
11+
}
12+
}
13+
});
14+
}
15+
16+
console.log(bigSorting([ '31415926535897932384626433832795', '1', '3', '10', '3', '5' ]));
17+
// 1, 3, 3, 5, 10, 31415926535897932384626433832795

find-element-in-array-divides-all.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Find element in array that divides all array elements
3+
*
4+
* Given an array of n non-negative integers. Find such element in the array, that all array elements are divisible by it.
5+
*
6+
* Examples :
7+
*
8+
* Input : arr[] = {2, 2, 4}
9+
* Output : 2
10+
*
11+
* Input : arr[] = {2, 1, 3, 1, 6}
12+
* Output : 1
13+
*
14+
* Input: arr[] = {2, 3, 5}
15+
* Output : -1
16+
*/
17+
18+
function findNumber(arr) {
19+
const n = arr.length;
20+
let ans = arr[0];
21+
22+
for (let i = 0; i < n; i++) {
23+
ans = greatestCommonDivider(ans, arr[i]);
24+
}
25+
26+
const index = arr.indexOf(ans);
27+
28+
return index === -1 ? -1 : ans;
29+
}
30+
31+
function greatestCommonDivider(a, b) {
32+
if (a === 0) {
33+
return b;
34+
}
35+
return greatestCommonDivider(b % a, a);
36+
}
37+
38+
39+
console.log(findNumber([2,2,4])); // 2
40+
console.log(findNumber([2,1,3,1,6])); // 1
41+
console.log(findNumber([2,3,5])); // -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* GCD of more than two (or array) numbers
3+
*
4+
* Given an array of numbers, find GCD of the array elements. In a previous post we find GCD of two number.
5+
*
6+
* Examples:
7+
*
8+
* Input : arr[] = {1, 2, 3}
9+
* Output : 1
10+
*
11+
* Input : arr[] = {2, 4, 6, 8}
12+
* Output : 2
13+
*/
14+
15+
function gcd(arr) {
16+
let ans = arr[0];
17+
18+
for (let num of arr) {
19+
ans = getGCD(ans, num);
20+
}
21+
22+
return ans;
23+
}
24+
25+
function getGCD(a, b) {
26+
if (a === 0) {
27+
return b;
28+
}
29+
return getGCD(b % a, a);
30+
}
31+
32+
console.log(gcd([1,2,3])); // 1
33+
console.log(gcd([2,4,6,8])); // 2
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Largest Continous Zero Sum Subarray
3+
* Given an array having both positive an negative integers . Your task is to complete the function maxLen which returns the length of maximum subarray with 0 sum . The function takes two arguments an array A and n where n is the size of the array A .
4+
*
5+
* Input:
6+
* The first line of input contains an element T denoting the No of test cases. Then T test cases follow. Each test case consist of 2 lines. The first line of each test case contains a number denoting the size of the array A. Then in the next line are space separated values of the array A .
7+
*
8+
* Output:
9+
* For each test case output will be the length of the largest subarray which has sum 0 .
10+
*
11+
* Constraints:
12+
* 1<=T<=100
13+
* 1<=N<=100
14+
* -1000<=A[]<=1000
15+
*
16+
* Example:
17+
* Input
18+
* 1
19+
* 8
20+
* 15 -2 2 -8 1 7 10 23
21+
*
22+
* Output
23+
* 5
24+
*
25+
* Explanation
26+
* In the above test case the largest subarray with sum 0 will be -2 2 -8 1 7
27+
*/
28+
29+
function longestContinousZeroSumSubArray(nums) {
30+
const n = nums.length;
31+
let max = 0;
32+
33+
// Accumulate the nums
34+
for (let i = 1; i < n; i++) {
35+
nums[i] += nums[i - 1];
36+
}
37+
38+
const map = {};
39+
40+
for (let i = 0; i < n; i++) {
41+
let num = nums[i];
42+
43+
if (num === 0) {
44+
max = Math.max(max, i + 1);
45+
} else if (map[num] !== undefined) {
46+
max = Math.max(max, i - map[num]);
47+
} else {
48+
map[num] = i;
49+
}
50+
}
51+
return max;
52+
}
53+
54+
function longestContinousSumSubarray(nums, target) {
55+
const n = nums.length;
56+
const sums = [nums[0]];
57+
let max = 0;
58+
let ans = [];
59+
60+
for (let i = 1; i < n; i++) {
61+
sums[i] = nums[i] + sums[i - 1];
62+
}
63+
64+
for (let i = 0; i < n; i++) {
65+
for (let j = i; j < n; j++) {
66+
let sum = sums[j] - (i > 0 ? sums[i - 1] : 0);
67+
68+
if (sum === target && j - i + 1 > max) {
69+
max = j - i + 1;
70+
ans = nums.slice(i, j+1);
71+
}
72+
}
73+
}
74+
75+
return ans;
76+
}
77+
78+
console.log(longestContinousZeroSumSubArray([15, -2, 2, -8, 1, 7, 10, 23]));
79+
console.log(longestContinousSumSubarray([15, -2, 2, -8, 1, 7, 10, 23], 10));

longest-palindrome.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Longest Palindrome
3+
*
4+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
5+
*
6+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
7+
*
8+
* Note: Assume the length of given string will not exceed 1,010.
9+
*
10+
* Example:
11+
*
12+
* Input: "abccccdd"
13+
* Output: 7
14+
*
15+
* Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @return {number}
21+
*/
22+
var longestPalindrome = function(s) {
23+
const map = {};
24+
let count = 0;
25+
26+
for (let i = 0, l = s.length; i < l; i++) {
27+
const c = s[i];
28+
if (map[c] === 1) {
29+
map[c] = 0;
30+
count += 2;
31+
} else {
32+
map[c] = 1;
33+
}
34+
}
35+
36+
return count < s.length ? count + 1 : count;
37+
}
38+
39+
40+
console.log(longestPalindrome('abccccdd')); // 7

0 commit comments

Comments
 (0)