├── .gitignore ├── LICENSE ├── README.md └── js ├── add-two-numbers.js ├── binary-gap.js ├── brackets.js ├── change-calculation.js ├── chocolates-by-numbers.js ├── city-network.js ├── collect-cards.js ├── count-div.js ├── count-factors.js ├── count-semiprimes.js ├── count-triangles.js ├── counting-legionaries.js ├── cyclic-rotation.js ├── decimal-representation.js ├── distinct.js ├── dominator.js ├── equi-leader.js ├── fibonacci.js ├── find-all-disappeared-numbers.js ├── find-max-in-dynamicmatris.js ├── find-word.js ├── fix-tree-pattern.js ├── flight-seat-organise.js ├── flip-card copy.js ├── flip-card.js ├── frog-jmp.js ├── frog-river-one.js ├── index.js ├── letter-combinations-of-phone-number.js ├── max-counters.js ├── max-product-of-three.js ├── max-profit.js ├── max-slice-sum.js ├── merge-sort-two-arrays-into-one.js ├── min-avg-two-slice.js ├── min-perimeter-rectangle.js ├── minimum-cost-energy.js ├── minimum-hits.js ├── minimum-operations-to-reduce-x-to-zero.js ├── missing-integer.js ├── multiplies-3-5.js ├── nesting.js ├── number-of-disc-intersections.js ├── odd-occurence-in-array.js ├── palindromes.js ├── passing-cars.js ├── perm-check.js ├── perm-missing-elements.js ├── rabbits-in-forest.js ├── reordering-groups.js ├── stone-wall.js ├── tape-equilibrium.js ├── temp.js ├── travel-favourite.js └── triangle.js /.gitignore: -------------------------------------------------------------------------------- 1 | /temp 2 | /node_modules 3 | 4 | /.vscode 5 | 6 | .DS_Store 7 | Thumbs.db 8 | 9 | package-lock.json 10 | 11 | /js/fish.js -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Burak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # code-challenges 2 | 3 | Javascript solutions for Code Challanges and Interview Questions. 4 | 5 | ## Run 6 | ```bash 7 | node js/ 8 | ``` 9 | 10 | Each challange is imported in `index.js`. 11 | Enable only one `.js` import line that should run. 12 | 13 | Recommended global npm packages: 14 | `nodemon` 15 | 16 | JS version: `ES6`. 17 | 18 | 19 | ## websites 20 | [projecteuler.net](http://projecteuler.net) 21 | [hackerrank.com](http://hackerrank.com) 22 | [coderbyte.com](https://coderbyte.com) 23 | [leetcode.com](http://leetcode.com) 24 | [codility.com](http://codility.com) 25 | [topcoder.com](http://topcoder.com) 26 | [codechef.com](http://codechef.com) 27 | [hackerearth.com](http://hackerearth.com) 28 | [codeeval.com](http://codeeval.com) 29 | [spoj.com](http://spoj.com) 30 | [codingame.com](http://codingame.com) 31 | [codewars.com](http://codewars.com) 32 | [geeksforgeeks.org](https://www.geeksforgeeks.org) -------------------------------------------------------------------------------- /js/add-two-numbers.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 3. stage 4 | 5 | 65 6 | 55 7 | +---- 8 | 120 9 | 10 | 5 + 5 = 10 take 0 carry 1 -- 0 11 | 6 + 5 = 11 take 1 plus carried one = 2 and carry 1 -- 20 12 | finalise > 120 13 | 14 | */ 15 | 16 | function solution(A, B) { 17 | 18 | let strA = A.toString(); 19 | let strB = B.toString(); 20 | 21 | strA = strA.padStart(strB.length, '0'); 22 | strB = strB.padStart(strA.length, '0'); 23 | 24 | console.log('strA:', strA) 25 | console.log('strB:', strB) 26 | 27 | let i = strA.length - 1; 28 | 29 | let additional = 0; 30 | 31 | let result = ''; 32 | 33 | while (i >= 0) { 34 | let sum = Number(strA[i]) + Number(strB[i]) + additional; 35 | if (sum >= 10) { 36 | additional = 1; 37 | sum = sum - 10; 38 | } else { 39 | additional = 0; 40 | } 41 | 42 | result = String(sum) + result; 43 | i--; 44 | } 45 | 46 | return result; 47 | 48 | } 49 | 50 | test(20, 145); 51 | 52 | test(98397, 37234226); 53 | 54 | function test(...params) { 55 | console.log('\n(', ...params, ')\n'); 56 | console.log('\n=>', solution(...params), '\n\n'); 57 | } 58 | -------------------------------------------------------------------------------- /js/binary-gap.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/1-iterations/ 4 | 5 | Find longest sequence of zeros in binary representation of an integer. 6 | 7 | */ 8 | 9 | console.log('// BINARY GAP //'); 10 | 11 | function solution(N) { 12 | 13 | let b = N.toString(2); 14 | 15 | for (let i = b.length - 2; i > 0; i--) { 16 | 17 | let zeros = padZero(i); 18 | if (b.indexOf(zeros) !== -1) { 19 | return i; 20 | } 21 | 22 | } 23 | 24 | return 0; 25 | 26 | } 27 | 28 | function padZero(len) { 29 | let s = ''; 30 | for (let i = 0; i < len; i++) { 31 | s = s + '0'; 32 | } 33 | return '1' + s + '1'; 34 | } 35 | 36 | 37 | test(1041) 38 | // 5 39 | 40 | 41 | 42 | 43 | 44 | 45 | function test(...params) { 46 | console.log('\n(', ...params, ')\n'); 47 | console.log('\n=>', solution(...params), '\n\n'); 48 | } 49 | -------------------------------------------------------------------------------- /js/brackets.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/ 4 | 5 | Determine whether a given string of parentheses (multiple types) is properly nested. 6 | 7 | Note: Using recrusive method to clear string is a slower method. Got up to 87% max. 8 | However, This solution below is 100% although seems more complicated. 9 | 10 | */ 11 | 12 | console.log('// BRACKETS //'); 13 | 14 | function solution(S) { 15 | 16 | let i = 0; 17 | let len = S.length; 18 | 19 | if (len === 0) { return 1; } 20 | if (len % 2 !== 0) { return 0; } 21 | 22 | len = S.length; 23 | 24 | let arrChar = ['(', ')', '{', '}', '[', ']']; 25 | 26 | let arrIndex = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 27 | 28 | while (i < len) { 29 | 30 | let ch = S.charAt(i); 31 | let charIndex = arrChar.indexOf(ch) 32 | let even = Number(i % 2 === 0); 33 | 34 | arrIndex[(charIndex * 2) + even]++; 35 | 36 | // console.log(arrIndex) 37 | 38 | if (((arrIndex[2] + arrIndex[3]) > (arrIndex[0] + arrIndex[1])) || 39 | ((arrIndex[6] + arrIndex[7]) > (arrIndex[4] + arrIndex[5])) || 40 | ((arrIndex[10] + arrIndex[11]) > (arrIndex[8] + arrIndex[9]))) { 41 | return 0; 42 | } 43 | 44 | i++; 45 | } 46 | 47 | console.log(arrIndex); 48 | 49 | return Number( 50 | (arrIndex[0] === arrIndex[3]) && 51 | (arrIndex[1] === arrIndex[2]) && 52 | (arrIndex[4] === arrIndex[7]) && 53 | (arrIndex[5] === arrIndex[6]) && 54 | (arrIndex[8] === arrIndex[11]) && 55 | (arrIndex[9] === arrIndex[10]) 56 | ); 57 | } 58 | 59 | 60 | 61 | test('{[()()]}'); 62 | // 1 63 | 64 | test('([)()]'); 65 | // 0 66 | 67 | test(''); 68 | // 1 69 | 70 | test(')('); 71 | // 0 72 | 73 | test('({{({}[]{})}}[]{})'); 74 | // 1 75 | 76 | test('()(()())((()())(()()))'); 77 | // 1 78 | 79 | 80 | 81 | 82 | 83 | 84 | function test(...params) { 85 | console.log('\n(', ...params, ')\n'); 86 | console.log('\n=>', solution(...params), '\n\n'); 87 | } -------------------------------------------------------------------------------- /js/change-calculation.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1. 4 | Your task is to write a program that will be used in a vending machine to return change. 5 | Assume that the vending machine will always want to return the least number of coins or notes. 6 | Devise a function getChange(M, P) where M is how much money was inserted into the machine 7 | and P the price of the item selected, that returns an array of integers representing the number of 8 | each denomination to return. 9 | 10 | Example: 11 | getChange(5, 0.99) should return [1,0,0,0,0,4] 12 | 13 | */ 14 | 15 | console.log('// CHANGE CALCULATION //') 16 | 17 | function solution(M, P) { 18 | 19 | const arrDenominations = [1, 5, 10, 25, 50, 100]; 20 | let arrChangeBack = [0, 0, 0, 0, 0, 0] 21 | let change = (M - P) * 100; 22 | 23 | for (let i = arrDenominations.length - 1; i >= 0; i--) { 24 | const element = arrDenominations[i]; 25 | 26 | if (change >= element) { 27 | 28 | arrChangeBack[i] = Math.floor(change / element); 29 | change = (change % element); 30 | 31 | } 32 | } 33 | 34 | return arrChangeBack; 35 | 36 | } 37 | 38 | 39 | test(5, 0.99); 40 | // [ 1, 0, 0, 0, 0, 4 ] 41 | 42 | test(3, 1); 43 | // [ 0, 0, 0, 0, 0, 2 ] 44 | 45 | test(10, 2.5); 46 | // => [ 0, 0, 0, 0, 1, 7 ] 47 | 48 | 49 | function test(...params) { 50 | console.log('\n(', ...params, ')\n'); 51 | console.log('\n=>', solution(...params), '\n\n'); 52 | } -------------------------------------------------------------------------------- /js/chocolates-by-numbers.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | ChocolatesByNumbers 4 | There are N chocolates in a circle. Count the number of chocolates you will eat. 5 | 6 | https://app.codility.com/programmers/lessons/12-euclidean_algorithm/chocolates_by_numbers/ 7 | 8 | */ 9 | 10 | function solution(N, M) { 11 | 12 | let i = 0; 13 | let count = 1; 14 | 15 | if (N % M === 0) { 16 | return N / M; 17 | } 18 | 19 | while (i !== -1) { 20 | 21 | i = i + M; 22 | 23 | if (i >= N) { 24 | i = i % N; 25 | 26 | if (i === 0) { 27 | return count; 28 | } 29 | } 30 | 31 | count++; 32 | 33 | } 34 | 35 | return 1; 36 | } 37 | 38 | 39 | 40 | test(10, 4); 41 | // 5 42 | 43 | test(20, 5); 44 | // 4 45 | 46 | test(24, 18); 47 | // 4 48 | 49 | 50 | function test(...params) { 51 | console.log('\n(', ...params, ')\n'); 52 | console.log('\n=>', solution(...params), '\n\n'); 53 | } -------------------------------------------------------------------------------- /js/city-network.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | City Network 4 | 5 | */ 6 | 7 | function solution(T) { 8 | 9 | let i = 0; 10 | let len = T.length; 11 | 12 | let capital = -1; 13 | let result = []; 14 | 15 | while (i < len) { 16 | 17 | result[i] = 0; 18 | 19 | if (i === T[i]) { 20 | capital = i; 21 | } 22 | i++; 23 | 24 | } 25 | 26 | 27 | i = 0; 28 | while (i < len) { 29 | 30 | let resultIndex = 0; 31 | if (i !== capital) { 32 | if (T[i] === capital) { 33 | resultIndex = 0; 34 | } else { 35 | 36 | resultIndex++; 37 | let nextIndex = T[i]; 38 | let condition = false; 39 | while (condition === false) { 40 | 41 | if (T[nextIndex] === capital) { 42 | condition = true; 43 | } else { 44 | nextIndex = T[nextIndex]; 45 | resultIndex++; 46 | } 47 | } 48 | 49 | 50 | } 51 | 52 | result[resultIndex] = result[resultIndex] + 1; 53 | } 54 | i++; 55 | 56 | } 57 | 58 | console.log('capital: ', capital) 59 | console.log('result:', result) 60 | 61 | return result; 62 | } 63 | 64 | 65 | 66 | test([9, 1, 4, 9, 0, 4, 8, 9, 0, 1]); 67 | // [1, 3, 2, 3, 0, 0, 0, 0, 0] 68 | 69 | 70 | 71 | function test(...params) { 72 | console.log('\n(', ...params, ')\n'); 73 | console.log('\n=>', solution(...params), '\n\n'); 74 | } -------------------------------------------------------------------------------- /js/collect-cards.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | T: In a casino, all the playing cards got mixed up, and some of them got lost. 4 | You have to collect as many full decks as possible. 5 | 6 | You get N mixed up French playing cards as your input. 7 | 8 | The cards are of the following ranks: 9 | 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A 10 | 11 | The four suits are: 12 | S - Spade (♠), C - Club(♣), H - Heart(♥), D - Diamond(♦) 13 | 14 | The cards are given using their rank followed by their suit: 15 | 16 | 2 of Spades: 2S 17 | Ace of Clubs: AC 18 | 10 of Hearts: TH 19 | 20 | Write a function that will accept an array of cards and return the number of full 21 | decks contained in the array. 22 | 23 | Examples: 24 | a) ['9C', 'KS', 'AC', 'AH', '8D', '4C', 'KD', 'JC', '7D', '9D', '2H', 25 | '7C', '3C', '7S', '5C', '6H', 'TH'] 26 | 27 | -> 0 28 | 29 | b) ['2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 30 | '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 31 | '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 32 | 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD', 'AH', 33 | '2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 34 | '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 35 | '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 36 | 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD', 'AH', 37 | '2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 38 | '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 39 | '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 40 | 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD'] 41 | 42 | -> 2 43 | 44 | 45 | */ 46 | 47 | console.log('// COLLECT CARD //'); 48 | 49 | function solution(A) { 50 | 51 | let i = 0; 52 | let decks = {} 53 | let count = 0; 54 | while (i < A.length) { 55 | 56 | const type = A[i].charAt(A[i].length - 1); 57 | let value = A[i].substring(0, A[i].length - 1); 58 | 59 | value = value.replace('A', 1) 60 | .replace('T', 10) 61 | .replace('J', 11) 62 | .replace('Q', 12) 63 | .replace('K', 13); 64 | 65 | if (!decks[type]) { 66 | decks[type] = []; 67 | } 68 | 69 | decks[type][value - 1] = 1; 70 | 71 | const totalType = decks[type].reduce((x, y) => x + y); 72 | if (totalType === 13) { 73 | console.log('decks:', decks) 74 | count++; 75 | decks[type] = []; 76 | } 77 | 78 | i++; 79 | } 80 | 81 | return count; 82 | 83 | } 84 | 85 | 86 | // test(['9C', 'KS', 'AC', 'AH', '8D', '4C', 'KD', 'JC', '7D', '9D', '2H', 87 | // '7C', '3C', '7S', '5C', '6H', 'TH', '5C'] 88 | // ); 89 | 90 | // test(['2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 91 | // '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 92 | // '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 93 | // 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD', 'AH', 94 | // '2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 95 | // '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 96 | // '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 97 | // 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD', 'AH', 98 | // '2S', '2C', '2D', '2H', '3S', '3C', '3D', '3H', '4S', '4C', '4D', '4H', '5S', 99 | // '5C', '5D', '5H', '6S', '6C', '6D', '6H', '7S', '7C', '7D', '7H', '8S', '8C', 100 | // '8D', '8H', '9S', '9C', '9D', '9H', 'TS', 'TC', 'TD', 'TH', 'JS', 'JC', 'JD', 101 | // 'JH', 'QS', 'QC', 'QD', 'QH', 'KS', 'KC', 'KD', 'KH', 'AS', 'AC', 'AD'] 102 | // ); 103 | 104 | 105 | 106 | 107 | test(['AC', '2C', '3C', '4C', '5C', '6C', 'KD', '7C', '8C', '9C', 'TC', 108 | 'JC', 'QC', 'KC', '5C', '6H', 'TH', '5C'] 109 | ); 110 | 111 | 112 | 113 | function test(...params) { 114 | console.log('\n(', ...params, ')\n'); 115 | console.log('\n=>', solution(...params), '\n\n'); 116 | } -------------------------------------------------------------------------------- /js/count-div.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/5-prefix_sums/count_div/ 4 | 5 | Compute number of integers divisible by k in range [a..b]. 6 | 7 | */ 8 | 9 | console.log('// COUNT DIV //'); 10 | 11 | function solution(A, B, K) { 12 | 13 | let first = (A % K) === 0 ? A : A + (K - (A % K)); 14 | let last = (B % K) === 0 ? B : B - (B % K); 15 | 16 | return ((last - first) / K) + 1; 17 | 18 | } 19 | 20 | 21 | test(6, 11, 2); 22 | // 3 23 | 24 | test(4, 22, 3); 25 | // 6 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | function test(...params) { 36 | console.log('\n(', ...params, ')\n'); 37 | console.log('\n=>', solution(...params), '\n\n'); 38 | } -------------------------------------------------------------------------------- /js/count-factors.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/count_factors/ 4 | 5 | Count Factors 6 | Count factors of given number n. 7 | 8 | https://app.codility.com/demo/results/trainingBM656B-8HX/ 9 | 10 | */ 11 | 12 | console.log('COUNT FACTORS'); 13 | 14 | function solution(N) { 15 | 16 | if (N === 1) { 17 | return 1; 18 | } 19 | 20 | let counter = 2; 21 | let i = 2; 22 | let len = N; 23 | let dividable = false; 24 | 25 | while (i < len) { 26 | 27 | if (N % i === 0) { 28 | 29 | dividable = true; 30 | 31 | let result = N / i; 32 | 33 | if (result === i) { 34 | counter += 1; 35 | } else { 36 | counter += 2; 37 | } 38 | 39 | len = result; 40 | 41 | } 42 | else { 43 | 44 | if (!dividable) { 45 | len = Math.floor(N / i); 46 | } 47 | 48 | } 49 | 50 | i++; 51 | } 52 | 53 | return counter; 54 | } 55 | 56 | 57 | 58 | 59 | // test(780291637) 60 | // 2 61 | 62 | // test(1); 63 | // // 1 64 | 65 | // test(7); 66 | // // 2 67 | 68 | // test(24); 69 | // 8 70 | 71 | // test(19); 72 | // 2 73 | 74 | test(1111); 75 | // 4 76 | 77 | // test(4); 78 | // 3 79 | 80 | // test(42); 81 | // 8 82 | 83 | 84 | 85 | 86 | function test(params) { 87 | console.log('\n(', params, ')\n'); 88 | console.log('\n=>', solution(params), '\n\n'); 89 | } -------------------------------------------------------------------------------- /js/count-semiprimes.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_semiprimes/ 4 | 5 | Count the semiprime numbers in the given range [a..b] 6 | 7 | */ 8 | 9 | console.log('CountSemiprimes'); 10 | 11 | function solution(N, P, Q) { 12 | 13 | let i = 0; 14 | let arr = []; 15 | 16 | while (i < P.length) { 17 | 18 | console.log(i); 19 | 20 | arr[i] = 0; 21 | 22 | let p = P[i]; 23 | let q = Q[i]; 24 | 25 | let j = p; 26 | while (j <= q) { 27 | console.log('j:', j) 28 | 29 | arr[i] += isSemiPrime(j); 30 | j++; 31 | 32 | console.log('arr:', arr) 33 | } 34 | 35 | i++; 36 | 37 | console.log('----'); 38 | 39 | } 40 | 41 | return arr; 42 | } 43 | 44 | function isSemiPrime(N) { 45 | return 1; 46 | } 47 | 48 | 49 | 50 | // test(1, [1], [1]) 51 | // 1 52 | 53 | test(26, 54 | [1, 4, 16], 55 | [26, 10, 20]); 56 | // [10, 4, 0] 57 | 58 | 59 | function test(...params) { 60 | console.log('\n(', ...params, ')\n'); 61 | console.log('\n=>', solution(...params), '\n\n'); 62 | } -------------------------------------------------------------------------------- /js/count-triangles.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | CountTriangles 4 | Count the number of triangles that can be built from a given set of edges. 5 | 6 | https://app.codility.com/programmers/lessons/15-caterpillar_method/count_triangles/ 7 | 8 | */ 9 | 10 | function solution(A) { 11 | 12 | let i = 0; 13 | let len = A.length; 14 | let arr = []; 15 | 16 | let results = []; 17 | let count = 0; 18 | 19 | while (i < (len - 2)) { 20 | 21 | console.log(i) 22 | 23 | arr[0] = i; 24 | 25 | let j = i + 1; 26 | while (j < len) { 27 | 28 | console.log(j, ' j........ ') 29 | arr[1] = j; 30 | 31 | 32 | console.log('arr: ', arr) 33 | 34 | let y = j + 1; 35 | 36 | while (y < len) { 37 | 38 | arr[2] = y; 39 | 40 | if ( 41 | ((arr[0] + arr[1]) > arr[2]) && 42 | ((arr[1] + arr[2]) > arr[0]) && 43 | ((arr[2] + arr[0]) > arr[1]) 44 | ) { 45 | results.push([i, j, y]); 46 | count++; 47 | } 48 | 49 | console.log('y: ', y, arr) 50 | y++; 51 | } 52 | 53 | j++; 54 | 55 | } 56 | 57 | 58 | i++; 59 | } 60 | 61 | console.log('result', results); 62 | 63 | return count; 64 | } 65 | 66 | 67 | 68 | test([10, 2, 5, 1, 8, 12]); 69 | // 4 70 | 71 | 72 | 73 | function test(params) { 74 | console.log('\n(', params, ')\n'); 75 | console.log('\n=>', solution(params), '\n\n'); 76 | } -------------------------------------------------------------------------------- /js/counting-legionaries.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Counting Legionaries 4 | In the range 1 - 13 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) the digit 1 occurs 6 times. 5 | In the range 1 - 2,660 (half the number of Romans in a legion), expressed in Roman numerals, how many times does the numeral "X" occur? 6 | 7 | */ 8 | 9 | console.log(''); 10 | 11 | function solution(A) { 12 | 13 | return 0 14 | } 15 | 16 | 17 | 18 | test(); 19 | // 20 | 21 | 22 | 23 | function test(params) { 24 | console.log('\n(', params, ')\n'); 25 | console.log('\n=>', solution(params), '\n\n'); 26 | } -------------------------------------------------------------------------------- /js/cyclic-rotation.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/ 4 | 5 | Rotate an array to the right by a given number of steps 6 | 7 | */ 8 | 9 | console.log('// CYCLIC ROTATION //'); 10 | 11 | function solution(A, K) { 12 | 13 | K = (A.length > K) ? K : K % A.length; 14 | 15 | var d = A.slice(0, A.length - K); 16 | var e = A.splice(A.length - K); 17 | return e.concat(d); 18 | 19 | // K = K % A.length; 20 | 21 | // if (K === 0 || K === A.length) { 22 | // return A; 23 | // } 24 | 25 | // const sliceIndex = A.length - K; 26 | // return [...A.slice(sliceIndex), ...A.slice(0, sliceIndex)] 27 | } 28 | 29 | 30 | test([5, -1000], 1); 31 | // [ -1000, 5 ] 32 | 33 | 34 | test([3, 8, 9, 7, 6], 3); 35 | // [9, 7, 6, 3, 8] 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | function test(...params) { 44 | console.log('\n(', ...params, ')\n'); 45 | console.log('\n=>', solution(...params), '\n\n'); 46 | } -------------------------------------------------------------------------------- /js/decimal-representation.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A[0] = 3 A[1] = 5 A[2] = 1 4 | 5 | represents the number V = 153; 6 | 7 | Write a function that should return 9, because: 8 | 9 | - array A represents the number 153, 10 | - 17 * 153 = 2601 (17 is given) 11 | - the sum of the digits in the decimal representation of 2601 is 2 + 6 + 0 + 1 =9 12 | 13 | */ 14 | 15 | console.log('// DECIMAL REPRESENTATION //'); 16 | 17 | function solution(A) { 18 | 19 | let newNumber = 17 * Number(A.reverse().toString().replace(/,/g, '')); 20 | 21 | let total = 0; 22 | newNumber.toString().split('').map(e => total += Number(e)); 23 | return total; 24 | } 25 | 26 | 27 | test([3, 5, 1]); 28 | // 9 29 | 30 | 31 | 32 | function test(...params) { 33 | console.log('\n(', ...params, ')\n'); 34 | console.log('\n=>', solution(...params), '\n\n'); 35 | } -------------------------------------------------------------------------------- /js/distinct.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/6-sorting/distinct/ 4 | 5 | Compute number of distinct values in an array. 6 | 7 | */ 8 | 9 | function solution(A) { 10 | 11 | var seen = []; 12 | var count = 0; 13 | var len = A.length; 14 | for (var i = 0; i < len; i++) { 15 | var item = A[i]; 16 | if (seen[item] !== 1) { 17 | seen[item] = 1; 18 | count++ 19 | } 20 | } 21 | return count; 22 | 23 | } 24 | 25 | 26 | test([2, 1, 1, 2, 3, 1]); 27 | // 3 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | function test(...params) { 41 | console.log('\n(', ...params, ')\n'); 42 | console.log('\n=>', solution(...params), '\n\n'); 43 | } -------------------------------------------------------------------------------- /js/dominator.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/8-leader/dominator/ 4 | 5 | Dominator 6 | Find an index of an array such that its value occurs at more than half of indices in the array. 7 | 8 | */ 9 | 10 | console.log('DOMINATOR'); 11 | 12 | function solution(A) { 13 | 14 | const B = []; 15 | const threshold = Math.floor(A.length / 2) + 1; 16 | let i = 0; 17 | while (i < A.length) { 18 | B[A[i]] = B[A[i]] ? B[A[i]] + 1 : 1; 19 | if (B[A[i]] === threshold) { 20 | return i; 21 | } 22 | i++; 23 | } 24 | 25 | return -1; 26 | } 27 | 28 | test([3, 4, 3, 3, 3, -1, 3, 3]); 29 | // 0, 2, 4, 6 or 7 30 | 31 | test([1, 2, 3]); 32 | // -1 33 | 34 | 35 | function test(...params) { 36 | console.log('\n(', ...params, ')'); 37 | console.log('\n=>', solution(...params), '\n\n'); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /js/equi-leader.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/8-leader/equi_leader/ 4 | 5 | EquiLeader 6 | Find the index S such that the leaders of the sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same. 7 | 8 | */ 9 | 10 | console.log('EQUILEADER'); 11 | 12 | function solution(A) { 13 | 14 | let count = 0; 15 | let i = 0; 16 | let currThreshold = -1; 17 | const B = []; 18 | const C = []; 19 | 20 | while (i < A.length) { 21 | C[A[i]] = C[A[i]] ? C[A[i]] + 1 : 1; 22 | i++; 23 | } 24 | 25 | i = 0; 26 | 27 | while (i < A.length) { 28 | 29 | B[A[i]] = B[A[i]] ? B[A[i]] + 1 : 1; 30 | C[A[i]] = C[A[i]] ? C[A[i]] - 1 : 0; 31 | 32 | let threshold1 = Math.floor((i + 1) / 2) + 1; 33 | let threshold2 = Math.floor(((A.length - (i + 1)) / 2)) + 1; 34 | 35 | if (B[A[i]] === threshold1) { 36 | currThreshold = i; 37 | } 38 | if (currThreshold !== -1 && 39 | B[A[currThreshold]] >= threshold1 && 40 | C[A[currThreshold]] >= threshold2 41 | ) { 42 | count++ 43 | } 44 | 45 | i++; 46 | 47 | } 48 | 49 | return count; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | test([4, 3, 4, 4, 4, 2]); 60 | // 2 61 | 62 | test([4, 4, 2, 5, 3, 4, 4, 4]); 63 | // 3 64 | 65 | 66 | 67 | 68 | function test(...params) { 69 | console.log('\n(', ...params, ')'); 70 | console.log('\n=>', solution(...params), '\n\n'); 71 | } 72 | 73 | -------------------------------------------------------------------------------- /js/fibonacci.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Fibonacci 4 | The Fibonnoci sequence begins like this: 5 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 6 | (each number is the sum of the previous two) 7 | 8 | What is the sum of all odd numbers in the Fibonnoci sequence that are less than 10,000? 9 | 10 | */ 11 | 12 | console.log(''); 13 | 14 | function solution(A) { 15 | 16 | return 0 17 | } 18 | 19 | 20 | 21 | test(); 22 | // 23 | 24 | 25 | 26 | function test(params) { 27 | console.log('\n(', params, ')\n'); 28 | console.log('\n=>', solution(params), '\n\n'); 29 | } -------------------------------------------------------------------------------- /js/find-all-disappeared-numbers.js: -------------------------------------------------------------------------------- 1 | 2 | // https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 3 | 4 | function solution(nums) { 5 | 6 | const output = []; 7 | 8 | for (let i = 1; i <= nums.length; i++) { 9 | 10 | if (nums.indexOf(i) === -1) { 11 | output.push(i); 12 | } 13 | 14 | } 15 | 16 | 17 | return output; 18 | } 19 | 20 | test([4, 3, 2, 7, 8, 2, 3, 1]); 21 | 22 | test([1, 1]); 23 | 24 | function test(...params) { 25 | console.log('\n(', ...params, ')\n'); 26 | console.log('\n=>', solution(...params), '\n\n'); 27 | } 28 | -------------------------------------------------------------------------------- /js/find-max-in-dynamicmatris.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Q3 4 | 5 | Find the maximum 4 digit number in dynamicmatris 6 | 7 | */ 8 | 9 | function solution(Board) { 10 | 11 | let N = Board.substring(3, Board.indexOf(', M')); 12 | let M = Board.substring(Board.indexOf('M=') + 2, Board.length - 1); 13 | 14 | let max = 0; 15 | let arr = []; 16 | let i = 0; 17 | let maxNumberIds = []; 18 | topNumbers = []; 19 | 20 | while (i < N) { 21 | 22 | arr[i] = []; 23 | Array.from(new Array(Number(M)), (val, index) => { 24 | const rnd = Math.floor(Math.random() * 10); 25 | 26 | arr[i].push(rnd); 27 | 28 | if (rnd > max) { 29 | max = rnd; 30 | maxNumberIds = [[i, index]]; 31 | } else if (rnd === max) { 32 | maxNumberIds.push([i, index]); 33 | } 34 | 35 | return rnd; 36 | }) 37 | 38 | console.log(arr[i]) 39 | 40 | i++; 41 | } 42 | 43 | for (const [x, y] of maxNumberIds) { 44 | 45 | const list = [ 46 | getValue(arr, [[x, y], [x - 1, y], [x - 2, y], [x - 3, y]]), 47 | getValue(arr, [[x, y], [x - 1, y], [x - 2, y], [x - 2, y - 1]]), 48 | getValue(arr, [[x, y], [x - 1, y], [x - 2, y], [x - 3, y + 1]]), 49 | 50 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y - 1], [x - 2, y - 1]]), 51 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y - 1], [x - 1, y - 2]]), 52 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y - 1], [x, y - 1]]), 53 | 54 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y + 1], [x - 2, y + 1]]), 55 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y + 1], [x - 1, y + 2]]), 56 | getValue(arr, [[x, y], [x - 1, y], [x - 1, y + 1], [x, y + 1]]), 57 | 58 | 59 | getValue(arr, [[x, y], [x, y - 1], [x, y - 2], [x, y - 1]]), 60 | getValue(arr, [[x, y], [x, y - 1], [x, y - 2], [x - 1, y - 2]]), 61 | getValue(arr, [[x, y], [x, y - 1], [x, y - 2], [x + 1, y - 2]]), 62 | 63 | getValue(arr, [[x, y], [x, y - 1], [x - 1, y - 1], [x - 2, y - 1]]), 64 | getValue(arr, [[x, y], [x, y - 1], [x - 1, y - 1], [x - 1, y - 2]]), 65 | getValue(arr, [[x, y], [x, y - 1], [x - 1, y - 1], [x - 1, y]]), 66 | 67 | getValue(arr, [[x, y], [x, y - 1], [x + 1, y - 1], [x + 2, y - 1]]), 68 | getValue(arr, [[x, y], [x, y - 1], [x + 1, y - 1], [x + 1, y - 2]]), 69 | getValue(arr, [[x, y], [x, y - 1], [x + 1, y - 1], [x + 1, y]]), 70 | 71 | 72 | getValue(arr, [[x, y], [x, y + 1], [x, y + 2], [x, y + 3]]), 73 | getValue(arr, [[x, y], [x, y + 1], [x, y + 2], [x - 1, y + 2]]), 74 | getValue(arr, [[x, y], [x, y + 1], [x, y + 2], [x + 1, y + 2]]), 75 | 76 | getValue(arr, [[x, y], [x, y + 1], [x - 1, y + 1], [x - 2, y + 1]]), 77 | getValue(arr, [[x, y], [x, y + 1], [x - 1, y + 1], [x - 1, y]]), 78 | getValue(arr, [[x, y], [x, y + 1], [x - 1, y + 1], [x - 1, y + 2]]), 79 | 80 | getValue(arr, [[x, y], [x, y + 1], [x + 1, y + 1], [x + 1, y + 2]]), 81 | getValue(arr, [[x, y], [x, y + 1], [x + 1, y + 1], [x + 2, y + 1]]), 82 | getValue(arr, [[x, y], [x, y + 1], [x + 1, y + 1], [x + 1, y]]), 83 | 84 | 85 | getValue(arr, [[x, y], [x + 1, y], [x + 2, y], [x + 3, y]]), 86 | getValue(arr, [[x, y], [x + 1, y], [x + 2, y], [x + 2, y - 1]]), 87 | getValue(arr, [[x, y], [x + 1, y], [x + 2, y], [x + 2, y + 1]]), 88 | 89 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y - 1], [x + 1, y - 2]]), 90 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y - 1], [x, y - 1]]), 91 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y - 1], [x + 2, y - 1]]), 92 | 93 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y + 1], [x, y + 1]]), 94 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y + 1], [x + 1, y + 2]]), 95 | getValue(arr, [[x, y], [x + 1, y], [x + 1, y + 1], [x + 2, y + 1]]) 96 | ]; 97 | 98 | 99 | topNumbers.push(Math.max(...list)); 100 | 101 | } 102 | 103 | console.log('topNumbers:', topNumbers) 104 | 105 | return Math.max(...topNumbers); 106 | } 107 | 108 | 109 | function getValue(arr, indexes) { 110 | 111 | strNumber = ''; 112 | for (const [x, y] of indexes) { 113 | if ((arr[x] !== undefined) && (arr[x][y] !== undefined)) { 114 | strNumber += String(arr[x][y]); 115 | } else { 116 | return -1; 117 | } 118 | } 119 | 120 | return Number(strNumber); 121 | } 122 | 123 | 124 | test('(N=3, M=5)'); 125 | 126 | 127 | function test(...params) { 128 | console.log('\n(', ...params, ')\n'); 129 | console.log('\n=>', solution(...params), '\n\n'); 130 | } 131 | -------------------------------------------------------------------------------- /js/find-word.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Find the encrypted word that in the array 4 | 5 | for example; 6 | A = [ 'U>N', 'G>A', 'R>Y', 'H>U', 'N>G', 'A>R' ] 7 | Word = HUNGARY 8 | 9 | */ 10 | 11 | console.log('// FIND WORD //') 12 | 13 | function solution(A) { 14 | let arrStr = A.toString(); 15 | let W = ''; 16 | 17 | for (const key in A) { 18 | const value = A[key]; 19 | 20 | if (arrStr.indexOf('>' + value.substr(0, 1)) === -1) { 21 | W = value.replace('>', ''); 22 | arrStr = arrStr.replace(value, ''); 23 | break; 24 | } 25 | } 26 | 27 | for (let index = 0; index < A.length - 1; index++) { 28 | 29 | let ws = W.substring(W.length - 1) + '>'; 30 | W += arrStr.substr(arrStr.indexOf(ws) + 2, 1); 31 | 32 | } 33 | 34 | return W; 35 | } 36 | 37 | test(['U>N', 'G>A', 'R>Y', 'H>U', 'N>G', 'A>R']); 38 | // HUNGARY 39 | 40 | 41 | function test(...params) { 42 | console.log('\n(', ...params, ')\n'); 43 | console.log('\n=>', solution(...params), '\n\n'); 44 | } -------------------------------------------------------------------------------- /js/fix-tree-pattern.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Q2 4 | 5 | | 6 | | | 7 | | | | 8 | | | | | 9 | | | | | | 10 | | | | | | 11 | 12 | */ 13 | 14 | function solution(A) { 15 | 16 | let i = 1; 17 | let len = A.length; 18 | count = 0; 19 | 20 | while (i < len - 1) { 21 | 22 | let prev = A[i - 1]; 23 | let curr = A[i]; 24 | let next = A[i + 1]; 25 | 26 | console.log('curr:', curr) 27 | 28 | if ((curr < prev) && (curr > next)) { 29 | if (A[i + 2]) { 30 | if (next > A[i + 2]) { 31 | A[i] = next - 1; 32 | } else { 33 | A[i] = prev + 1; 34 | } 35 | } 36 | count++; 37 | } else if ((curr > prev) && (curr < next)) { 38 | if (A[i + 2]) { 39 | if (next > A[i + 2]) { 40 | A[i] = prev - 1; 41 | } else { 42 | A[i] = next + 1; 43 | } 44 | } 45 | count++; 46 | } 47 | 48 | console.log('A', A) 49 | 50 | i++; 51 | } 52 | 53 | return count; 54 | } 55 | 56 | 57 | test([1, 4, 1]); 58 | // 0 59 | 60 | test([3, 7, 4, 5]); 61 | // 0 62 | 63 | test([5, 4, 3, 2, 6]); // 2 64 | // 1 65 | 66 | test([5, 4, 3, 6, 5]); // 6 67 | // 1 68 | 69 | test([3, 5, 7, 8]); // 8 70 | // 1 71 | 72 | test([3, 5, 7, 6]); // 2 73 | // 1 74 | 75 | function test(...params) { 76 | console.log('\n(', ...params, ')\n'); 77 | console.log('\n=>', solution(...params), '\n\n'); 78 | } 79 | 80 | -------------------------------------------------------------------------------- /js/flight-seat-organise.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Q1 4 | 5 | A B C D E F G H I J 6 | _ _ _ _ _ _ _ _ _ _ 7 | 8 | */ 9 | 10 | function solution(N, S) { 11 | 12 | let count = N * 2; 13 | 14 | let i = 1; 15 | while (i <= N) { 16 | 17 | const strPattern = `${i}A|${i}B|${i}C|${i}D|${i}E|${i}F|${i}G|${i}H|${i}I|${i}J`; 18 | let seats = S.match(new RegExp(strPattern, 'g')); 19 | 20 | if (seats !== null) { 21 | 22 | console.log('seats:', seats) 23 | 24 | count -= 2; 25 | 26 | if (seats.indexOf(i + 'B') === -1 && 27 | seats.indexOf(i + 'C') === -1 && 28 | seats.indexOf(i + 'D') === -1 && 29 | seats.indexOf(i + 'E') === -1 && 30 | seats.indexOf(i + 'F') === -1 && 31 | seats.indexOf(i + 'G') === -1 && 32 | seats.indexOf(i + 'H') === -1 && 33 | seats.indexOf(i + 'I') === -1 34 | ) { 35 | count += 2; 36 | } else { 37 | 38 | if ( 39 | ( 40 | seats.indexOf(i + 'B') === -1 && 41 | seats.indexOf(i + 'C') === -1 && 42 | seats.indexOf(i + 'D') === -1 && 43 | seats.indexOf(i + 'E') === -1 44 | ) || 45 | ( 46 | seats.indexOf(i + 'D') === -1 && 47 | seats.indexOf(i + 'E') === -1 && 48 | seats.indexOf(i + 'F') === -1 && 49 | seats.indexOf(i + 'G') === -1 50 | ) || 51 | ( 52 | seats.indexOf(i + 'F') === -1 && 53 | seats.indexOf(i + 'G') === -1 && 54 | seats.indexOf(i + 'H') === -1 && 55 | seats.indexOf(i + 'I') === -1 56 | ) 57 | ) { 58 | count += 1; 59 | } 60 | 61 | } 62 | } 63 | 64 | i++; 65 | } 66 | 67 | return count; 68 | } 69 | 70 | 71 | 72 | 73 | test(6, '1A 1G 2F 1C 2D 3B 3C 4B 1B 6D 6I'); 74 | // 10 75 | 76 | // test(23, '1G 2F 3C 2C 17F 2D 2E 20B 2B 2G 2H 2J 5D 5H'); 77 | // 39 78 | 79 | test(1, ''); 80 | // 2 81 | 82 | 83 | // test(4, '4C 4F'); 84 | // 7 85 | 86 | // test(2, ''); 87 | // 0 88 | 89 | function test(...params) { 90 | console.log('\n(', ...params, ')\n'); 91 | console.log('\n=>', solution(...params), '\n\n'); 92 | } 93 | 94 | -------------------------------------------------------------------------------- /js/flip-card copy.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Flip Cards 4 | 5 | */ 6 | 7 | function solution(A, B) { 8 | 9 | let i = 0; 10 | let len = A.length; 11 | let minIndex = -1; 12 | let max = A[0]; 13 | while (i < len) { 14 | 15 | if (A[i] !== B[i]) { 16 | if ((A[minIndex] > A[i]) || (minIndex === -1)) { 17 | minIndex = i; 18 | } 19 | } 20 | 21 | if (max < A[i]) { 22 | max = A[i]; 23 | } 24 | 25 | i++; 26 | 27 | } 28 | 29 | return minIndex !== -1 ? A[minIndex] : (max + 1); 30 | } 31 | 32 | 33 | 34 | test([1, 2, 4, 3], [1, 3, 2, 3]); 35 | // 2 36 | 37 | test([3, 2, 1, 6, 5], [4, 2, 1, 3, 3]); 38 | // 3 39 | 40 | test([1, 2], [1, 2]); 41 | // 3 42 | 43 | 44 | function test(...params) { 45 | console.log('\n(', ...params, ')\n'); 46 | console.log('\n=>', solution(...params), '\n\n'); 47 | } -------------------------------------------------------------------------------- /js/flip-card.js: -------------------------------------------------------------------------------- 1 | // /* 2 | 3 | // Flip Cards 4 | 5 | // */ 6 | 7 | // function solution(A, B) { 8 | 9 | // let i = 0; 10 | // let len = A.length; 11 | // let minIndex = -1; 12 | // let max = A[0]; 13 | // while (i < len) { 14 | 15 | // if (A[i] !== B[i]) { 16 | // if ((A[minIndex] > A[i]) || (minIndex === -1)) { 17 | // minIndex = i; 18 | // } 19 | // } 20 | 21 | // console.log('minIndex:', minIndex) 22 | // if (max < A[i]) { 23 | // max = A[i]; 24 | // } 25 | 26 | // console.log('mx', max); 27 | 28 | 29 | // i++; 30 | 31 | // } 32 | 33 | // return minIndex !== -1 ? A[minIndex] : (max + 1); 34 | // } 35 | 36 | 37 | 38 | // test([1, 2, 4, 3], [1, 3, 2, 3]); 39 | // // 2 40 | 41 | // test([3, 2, 1, 6, 5], [4, 2, 1, 3, 3]); 42 | // // 3 43 | 44 | // test([1, 2], [1, 2]); 45 | // // 3 46 | 47 | 48 | // function test(...params) { 49 | // console.log('\n(', ...params, ')\n'); 50 | // console.log('\n=>', solution(...params), '\n\n'); 51 | // } -------------------------------------------------------------------------------- /js/frog-jmp.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/3-time_complexity/frog_jmp/ 4 | 5 | Count minimal number of jumps from position X to Y. 6 | 7 | */ 8 | 9 | 10 | function solution(X, Y, D) { 11 | return Math.ceil((Y - X) / D); 12 | } 13 | 14 | 15 | test(10, 85, 30); 16 | // 3 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | function test(...params) { 28 | console.log('\n(', ...params, ')\n'); 29 | console.log('\n=>', solution(...params), '\n\n'); 30 | } -------------------------------------------------------------------------------- /js/frog-river-one.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/4-counting_elements/frog_river_one/ 4 | 5 | Find the earliest time when a frog can jump to the other side of a river. 6 | 7 | */ 8 | 9 | console.log('// FROG RIVER ONE //'); 10 | 11 | function solution(X, A) { 12 | 13 | var leaves = []; 14 | var i = 0; 15 | var result = -1; 16 | 17 | for (i = 0; i < A.length; i++) { 18 | if (typeof leaves[A[i]] == 'undefined') { 19 | leaves[A[i]] = i; 20 | } 21 | } 22 | 23 | if (leaves.length <= X) { 24 | return -1; 25 | } 26 | 27 | for (i = 1; i <= X; i++) { 28 | if (typeof leaves[i] == 'undefined') { 29 | return -1; 30 | } else { 31 | result = Math.max(result, leaves[i]); 32 | } 33 | } 34 | 35 | return result; 36 | 37 | } 38 | 39 | 40 | 41 | test(5, [1, 3, 1, 4, 2, 3, 5, 4]); 42 | 43 | 44 | 45 | 46 | 47 | function test(...params) { 48 | console.log('\n(', ...params, ')\n'); 49 | console.log('\n=>', solution(...params), '\n\n'); 50 | } -------------------------------------------------------------------------------- /js/index.js: -------------------------------------------------------------------------------- 1 | console.log('\n\n\n\n\n\n\n'); 2 | 3 | // require('./add-two-numbers'); 4 | // require('./binary-gap'); 5 | // require('./brackets'); 6 | // require('./change-calculation'); 7 | // require('./chocolates-by-numbers.js'); 8 | // require('./collect-cards'); 9 | // require('./count-div'); 10 | // require('./count-factors'); 11 | // require('./count-semiprimes'); 12 | // require('./counting-legionaries'); 13 | // require('./cyclic-rotation'); 14 | // require('./decimal-representation'); 15 | // require('./count-triangles'); 16 | // require('./distinct'); 17 | // require('./dominator'); 18 | // require('./equi-leader'); 19 | // require('./fibonacci'); 20 | // require('./find-word'); 21 | // require('./fish'); 22 | // require('./frog-jmp'); 23 | // require('./frog-river-one'); 24 | // require('./letter-combinations-of-phone-number'); 25 | // require('./max-counters'); 26 | // require('./max-product-of-three'); 27 | // require('./max-profit'); 28 | // require('./max-slice-sum'); 29 | // require('./merge-sort-two-arrays-into-one'); 30 | // require('./minimum-hits'); 31 | // require('./min-avg-two-slice'); 32 | // require('./min-perimeter-rectangle'); 33 | // require('./missing-integer'); 34 | // require('./multiplies-3-5'); 35 | // require('./nesting'); 36 | // require('./number-of-disc-intersections.js'); 37 | // require('./odd-occurence-in-array'); 38 | // require('./passing-cars'); 39 | // require('./palindromes'); 40 | // require('./perm-check.js'); 41 | // require('./perm-missing-elements'); 42 | // require('./reordering-groups'); 43 | // require('./stone-wall'); 44 | // require('./tape-equilibrium'); 45 | // require('./triangle'); 46 | 47 | 48 | // require('./temp'); 49 | 50 | 51 | // require('./flip-card'); 52 | // require('./city-network'); 53 | // require('./travel-favourite'); 54 | 55 | // require('./q1'); 56 | // require('./q2'); 57 | // require('./q3'); 58 | 59 | 60 | // require('./test'); 61 | 62 | 63 | console.log('\n\n\n\n\n\n\n\n\n\n\n'); -------------------------------------------------------------------------------- /js/letter-combinations-of-phone-number.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 5 | 6 | */ 7 | 8 | 9 | function solution(digits) { 10 | 11 | if (digits.length === 0) return [] 12 | else if (digits.length === 1) return map(digits[0]) 13 | 14 | prev = map(digits[0]) 15 | console.log('prev:', prev) 16 | // loop on all tree levels 17 | for (let i = 1; i < digits.length; i++) { 18 | next = [] 19 | currDigits = map(digits[i]) 20 | console.log('currDigits:', currDigits) 21 | // loop on previous level 22 | for (let j = 0; j < prev.length; j++) { 23 | // loop on current level 24 | for (let k = 0; k < currDigits.length; k++) { 25 | next.push(prev[j] + currDigits[k]) 26 | } 27 | } 28 | 29 | console.log('next:', next) 30 | prev = next 31 | console.log('prev:', prev) 32 | console.log('--------'); 33 | 34 | } 35 | return prev 36 | }; 37 | 38 | function map(d) { 39 | return { 40 | 2: ['a', 'b', 'c'], 41 | 3: ['d', 'e', 'f'], 42 | 4: ['g', 'h', 'i'], 43 | 5: ['j', 'k', 'l'], 44 | 6: ['m', 'n', 'o'], 45 | 7: ['p', 'q', 'r', 's'], 46 | 8: ['t', 'u', 'v'], 47 | 9: ['w', 'x', 'y', 'z'] 48 | }[d] 49 | } 50 | 51 | 52 | 53 | test('234'); 54 | // 55 | 56 | 57 | 58 | function test(params) { 59 | console.log('\n(', params, ')\n'); 60 | console.log('\n=>', solution(params), '\n\n'); 61 | } -------------------------------------------------------------------------------- /js/max-counters.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/ 4 | 5 | Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. 6 | 7 | */ 8 | 9 | console.log('// MAX COUNTERS //'); 10 | 11 | function solution(N, A) { 12 | 13 | var counter = new Array(N); 14 | let i = 0; 15 | 16 | while (i < N) { 17 | counter[i] = 0; 18 | i++; 19 | } 20 | 21 | // A = A.filter(e => (e <= A.length) || (e === N + 1)); 22 | 23 | let currMax = 0; 24 | let lastMax = 0; 25 | let threshold = N + 1; 26 | 27 | i = 0; 28 | while (A[i]) { 29 | const element = A[i]; 30 | 31 | if (element < threshold) { 32 | 33 | if (counter[element - 1] < lastMax) { 34 | counter[element - 1] = lastMax + 1; 35 | } 36 | else { 37 | counter[element - 1]++; 38 | } 39 | 40 | if (counter[element - 1] > currMax) { 41 | currMax = counter[element - 1]; 42 | } 43 | 44 | } else { 45 | lastMax = currMax; 46 | } 47 | 48 | i++; 49 | } 50 | 51 | let j = 0; 52 | while (j < N) { 53 | if (counter[j] < lastMax) counter[j] = lastMax; 54 | j++ 55 | } 56 | 57 | return counter; 58 | 59 | } 60 | 61 | 62 | test(5, [3, 4, 4, 6, 1, 4, 4]); 63 | 64 | test(1, [2, 1, 1, 2, 1]); 65 | 66 | test(1, [1]); 67 | 68 | 69 | 70 | 71 | function test(...params) { 72 | console.log('\n(', ...params, ')\n'); 73 | console.log('\n=>', solution(...params), '\n\n'); 74 | } -------------------------------------------------------------------------------- /js/max-product-of-three.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/ 4 | 5 | Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R). 6 | 7 | */ 8 | 9 | console.log('// MAX PRODUCT OF THREE //'); 10 | 11 | function solution(A) { 12 | 13 | let B = A.sort((a, b) => b - a); 14 | let max = B[0] * B[1] * B[2]; 15 | 16 | if ((B[B.length - 1] < 0) && (B[B.length - 2] < 0)) { 17 | let minusTotal = B[B.length - 1] * B[B.length - 2] * B[0]; 18 | max = Math.max(max, minusTotal); 19 | } 20 | 21 | return max; 22 | 23 | } 24 | 25 | 26 | test([-3, 1, 2, -2, 5, 6]); 27 | // 60 28 | 29 | test([-5, 5, -5, 4]); 30 | // 125 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | function test(...params) { 40 | console.log('\n(', ...params, ')\n'); 41 | console.log('\n=>', solution(...params), '\n\n'); 42 | } -------------------------------------------------------------------------------- /js/max-profit.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/ 4 | 5 | Max Prodit 6 | Given a log of stock prices compute the maximum possible earning. 7 | 8 | https://app.codility.com/demo/results/training9NTTAN-694/ 9 | 10 | */ 11 | 12 | console.log('MAX PROFIT'); 13 | 14 | 15 | function solution(A) { 16 | 17 | let i = 0; 18 | let min = 0; 19 | let max = 0; 20 | let profit = 0; 21 | while (i < A.length) { 22 | 23 | let value = A[i]; 24 | 25 | if (i === 0 || value < min) { 26 | min = value; 27 | max = value; 28 | } 29 | 30 | if (value > max) { 31 | max = value; 32 | 33 | if (max - min > profit) { 34 | profit = max - min; 35 | } 36 | } 37 | 38 | i++; 39 | } 40 | 41 | return profit; 42 | } 43 | 44 | test([ 45 | 23171, 46 | 21011, 47 | 21123, 48 | 21366, 49 | 21013, 50 | 21367 51 | ]); 52 | // 356 53 | 54 | 55 | test([ 56 | 6, 57 | 1, 58 | 3, 59 | 4, 60 | 2, 61 | 5 62 | ]); 63 | // 4 64 | 65 | test([]); 66 | // 0 67 | 68 | 69 | test([ 70 | 8, 9, 3, 6, 1, 2 71 | ]); 72 | // 3 73 | 74 | 75 | 76 | function test(...params) { 77 | console.log('\n(', ...params, ')'); 78 | console.log('\n=>', solution(...params), '\n\n'); 79 | } 80 | 81 | -------------------------------------------------------------------------------- /js/max-slice-sum.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_slice_sum/ 4 | 5 | Find a maximum sum of a compact subsequence of array elements. 6 | 7 | */ 8 | 9 | console.log('MAX SLICE SUM'); 10 | 11 | // function solution(A) { 12 | 13 | // let maxTotal = A[0]; 14 | // let sum = 0 15 | 16 | // for (var i = 0; i < A.length; i++) { 17 | // console.log(A[i]) 18 | // sum = 0; 19 | // for (var j = i; j < A.length; j++) { 20 | // sum += A[j]; 21 | // if (sum > maxTotal) { 22 | // maxTotal = sum; 23 | // // console.log('maxTotal:', maxTotal) 24 | // } 25 | // console.log('A[j]:', A[j]) 26 | // // console.log('sum:', sum) 27 | // // console.log('...') 28 | // } 29 | 30 | // console.log('---------') 31 | 32 | // } 33 | 34 | 35 | // return maxTotal; 36 | // } 37 | 38 | 39 | 40 | 41 | function solution(A) { 42 | 43 | let sum = -1000000; 44 | let total = -1000000; 45 | let i = 0; 46 | 47 | while (i < A.length) { 48 | 49 | sum = Math.max(A[i], sum + A[i]); 50 | total = Math.max(sum, total); 51 | 52 | i++; 53 | 54 | console.log('----'); 55 | 56 | } 57 | 58 | return total; 59 | } 60 | 61 | 62 | 63 | test([3, 2, -6, 4, 0]); 64 | // 5 65 | 66 | test([-10]); 67 | // -10 68 | 69 | // test([1, 1, -2]) 70 | // 2 71 | 72 | // test([-2, -4, -1, -5, -3]); 73 | // -2 74 | 75 | 76 | // test([-2, 1]) 77 | // 1 78 | 79 | 80 | 81 | function test(...params) { 82 | console.log('\n(', ...params, ')\n'); 83 | console.log('\n=>', solution(...params), '\n\n'); 84 | } -------------------------------------------------------------------------------- /js/merge-sort-two-arrays-into-one.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Merge and sort pre-sorted array into one array 4 | 5 | */ 6 | 7 | 8 | const l1 = [4, 5, 6, 8, 9, 10]; 9 | const l2 = [1, 2, 3, 9]; 10 | const l3 = []; 11 | 12 | let i1 = 0; 13 | let i2 = 0; 14 | 15 | while (l3.length < l1.length + l2.length) { 16 | const a = l1[i1]; 17 | const b = l2[i2]; 18 | 19 | if (a <= b) { 20 | l3.push(a); 21 | i1++; 22 | } else { 23 | l3.push(b); 24 | i2++; 25 | } 26 | 27 | if (l1.length === i1) { 28 | l3.push(...l2.slice(i2)); 29 | } else if (l2.length === i2) { 30 | l3.push(...l1.slice(i1)); 31 | } 32 | } 33 | 34 | console.log(l3); 35 | -------------------------------------------------------------------------------- /js/min-avg-two-slice.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/5-prefix_sums/min_avg_two_slice/ 4 | 5 | Find the minimal average of any slice containing at least two elements. 6 | 7 | */ 8 | 9 | console.log('// MIN AVG TWO SLICE //'); 10 | 11 | function solution(A) { 12 | 13 | let len = A.length; 14 | let resultIndex = 0; 15 | 16 | let minAvg = (A[0] + A[1]) / 2; 17 | 18 | for (let i = 0; i < len - 1; i++) { 19 | 20 | let currAvg = A[i]; 21 | 22 | var str = '(' + currAvg; 23 | 24 | for (let j = i + 1; j < len; j++) { 25 | 26 | if ((j === i + 1) || (A[j] < minAvg)) { 27 | 28 | str += ' + ' + A[j]; 29 | 30 | currAvg += A[j]; 31 | let divide = (j - i + 1); 32 | 33 | if ((currAvg / divide) < minAvg) { 34 | minAvg = (currAvg / divide); 35 | resultIndex = i; 36 | } 37 | 38 | console.log(str + ') = ' + currAvg + ' / ' + divide + ' >> ' + (currAvg / divide).toString().substr(0, 4)) 39 | 40 | } else { 41 | j = len; 42 | } 43 | } 44 | 45 | console.log('---\nmin: ' + minAvg.toString().substr(0, 4) + ' i: ' + resultIndex + '\n\n') 46 | } 47 | 48 | return resultIndex; 49 | 50 | } 51 | 52 | 53 | test([4, 2, 2, 5, 1, 5, 8]); 54 | // 1 55 | 56 | test([10, 10, -1, 2, 4, -1, 2, -1]); 57 | // 5 58 | 59 | test([-3, -5, -8, -4, -10]); 60 | // 2 61 | 62 | test([5, 6, 3, 4, 9]); 63 | // 2 64 | 65 | 66 | 67 | 68 | 69 | 70 | function test(...params) { 71 | console.log('\n(', ...params, ')\n'); 72 | console.log('\n=>', solution(...params), '\n\n'); 73 | } -------------------------------------------------------------------------------- /js/min-perimeter-rectangle.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/10-prime_and_composite_numbers/min_perimeter_rectangle/ 4 | 5 | Find the minimal perimeter of any rectangle whose area equals N. 6 | 7 | */ 8 | 9 | console.log('Min Perimeter Rectangle'); 10 | 11 | function solution(N) { 12 | 13 | let i = 2; 14 | let len = N; 15 | let dividable = false; 16 | let total = N + 1; 17 | // let arr = []; 18 | 19 | while (i < len) { 20 | 21 | if (N % i === 0) { 22 | 23 | dividable = true; 24 | 25 | let result = N / i; 26 | 27 | len = result; 28 | 29 | // arr.push([i, result]); 30 | 31 | total = Math.min(total, i + result); 32 | 33 | } 34 | else { 35 | 36 | if (!dividable) { 37 | len = Math.floor(N / i); 38 | } 39 | 40 | console.log('len', len) 41 | 42 | } 43 | 44 | i++; 45 | } 46 | 47 | // console.log('sorted:', sorted) 48 | 49 | // console.log('arr:', arr) 50 | return total * 2; 51 | } 52 | 53 | 54 | 55 | test(1) 56 | // 1 57 | 58 | test(30) 59 | // 22 60 | 61 | // test(982451653) 62 | // 63 | 64 | 65 | function test(params) { 66 | console.log('\n(', params, ')\n'); 67 | console.log('\n=>', solution(params), '\n\n'); 68 | } -------------------------------------------------------------------------------- /js/minimum-cost-energy.js: -------------------------------------------------------------------------------- 1 | function solution(A, X, Y) { 2 | 3 | let totalCost = 0; 4 | 5 | let sorted = A.sort((a, b) => a - b); 6 | 7 | let sIndex = 0; 8 | let lastIndex = A.length; 9 | while (sIndex < lastIndex) { 10 | 11 | tmpSum = 0; 12 | let i = sIndex; 13 | let max = sorted[lastIndex - 1]; 14 | console.log('max:', max) 15 | 16 | while (tmpSum < max && i < lastIndex - 1) { 17 | tmpSum += A[i]; 18 | i++; 19 | } 20 | 21 | 22 | console.log('tmpSum:', tmpSum) 23 | if (tmpSum >= max) { 24 | if (Y < ((i + 1) * X)) { 25 | totalCost += Y; 26 | sIndex = i; 27 | const extra = tmpSum - max; 28 | if (extra > 0) { 29 | sorted = [extra, ...sorted]; 30 | sIndex -= 1; 31 | } 32 | } else { 33 | totalCost += X; 34 | } 35 | } else { 36 | totalCost += X < Y ? X : Y; 37 | } 38 | 39 | lastIndex--; 40 | 41 | 42 | } 43 | 44 | 45 | return totalCost; 46 | 47 | 48 | } 49 | 50 | 51 | test( 52 | [5, 3, 8, 3, 2], 53 | 2, 54 | 5 55 | ); 56 | 57 | // 7 58 | 59 | // test( 60 | // [4, 2, 7], 61 | // 4, 62 | // 100 63 | // ); 64 | 65 | // // 12 66 | 67 | // test( 68 | // [2, 2, 1, 2, 2], 69 | // 2, 70 | // 3 71 | // ); 72 | 73 | // // 8 74 | 75 | 76 | // test( 77 | // [4, 1, 5, 3], 78 | // 5, 79 | // 2 80 | // ); 81 | 82 | // 4 83 | 84 | 85 | 86 | function test(...params) { 87 | console.log(solution(...params)) 88 | } 89 | -------------------------------------------------------------------------------- /js/minimum-hits.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | T: Given an array of points A, representing the lcations of the obstacles, 4 | returns the minimal number of hit 5 | 6 | A[0].x = -1 A[0].y = -2 7 | A[1].x = 1 A[1].y = 2 8 | A[2].x = 2 A[2].y = 4 9 | A[3].x = -3 A[3].y = 2 10 | A[4].x = 2 A[4].y = -2 11 | 12 | should return 4. 13 | 14 | */ 15 | 16 | function solution(A) { 17 | 18 | 19 | let LT = []; 20 | let RT = []; 21 | let LB = []; 22 | let RB = []; 23 | 24 | for (const i of A) { 25 | 26 | let x = Number(i[0]) > 0 ? 1 : -1; 27 | let y = Number(i[1]) > 0 ? 1 : -1; 28 | 29 | if (x === -1 && y === -1) { 30 | if (!checkPath(LB, i)) { 31 | LB.push(i); 32 | } 33 | } 34 | if (x === -1 && y === 1) { 35 | if (!checkPath(LT, i)) { 36 | LT.push(i); 37 | } 38 | } 39 | if (x === 1 && y === -1) { 40 | if (!checkPath(RB, i)) { 41 | RB.push(i); 42 | } 43 | } 44 | if (x === 1 && y === 1) { 45 | if (!checkPath(RT, i)) { 46 | RT.push(i); 47 | } 48 | } 49 | 50 | } 51 | 52 | return LT.length + RT.length + LB.length + RB.length; 53 | } 54 | 55 | function checkPath(arr, value) { 56 | 57 | for (const key in arr) { 58 | let arrX = Math.abs(Number(arr[key][0])); 59 | let arrY = Math.abs(Number(arr[key][1])); 60 | 61 | let bigX = Math.max(arrX, Number(value[0])); 62 | let smallX = Math.min(arrX, Number(value[0])); 63 | 64 | let bigY = Math.max(arrY, Number(value[1])); 65 | let smallY = Math.min(arrY, Number(value[1])); 66 | 67 | if ((bigX % smallX === 0) && (bigY % smallY === 0)) { 68 | return true; 69 | } 70 | } 71 | 72 | return false; 73 | 74 | } 75 | 76 | 77 | test([ 78 | ['-1', '-2'], 79 | ['1', '-2'], 80 | ['2', '4'], 81 | ['-3', '2'], 82 | ['2', '-2'] 83 | ]); 84 | 85 | 86 | 87 | function test(...params) { 88 | console.log('\n(', ...params, ')\n'); 89 | console.log('\n=>', solution(...params), '\n\n'); 90 | } -------------------------------------------------------------------------------- /js/minimum-operations-to-reduce-x-to-zero.js: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/ 2 | 3 | function solution(nums, x) { 4 | 5 | let count = 0; 6 | const len = nums.length; 7 | 8 | while ((x > 0) && (count <= len)) { 9 | 10 | const left = nums[0]; 11 | const right = nums[nums.length - 1]; 12 | 13 | if (left > x && right > x) { 14 | return -1; 15 | } 16 | 17 | if ((left >= right) && (left <= x)) { 18 | count++; 19 | x -= nums[0]; 20 | nums.shift(); 21 | console.log('numsSS:', nums) 22 | } else { 23 | count++; 24 | x -= nums[nums.length - 1]; 25 | nums.pop(); 26 | console.log('numsPP:', nums) 27 | } 28 | 29 | console.log('x: ', x, ' count: ', count) 30 | } 31 | 32 | if (x === 0) { 33 | return count; 34 | } 35 | 36 | return -1; 37 | 38 | } 39 | 40 | test([1, 1, 4, 2, 3], 5); 41 | 42 | test([5, 6, 7, 8, 9], 4); 43 | 44 | test([3, 2, 20, 1, 1, 3], 10); 45 | 46 | function test(...params) { 47 | console.log('\n(', ...params, ')\n'); 48 | console.log('\n=>', solution(...params), '\n\n'); 49 | } 50 | -------------------------------------------------------------------------------- /js/missing-integer.js: -------------------------------------------------------------------------------- 1 | console.log('// MISSING INTEGER //\n\n'); 2 | 3 | /* 4 | 5 | https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/ 6 | 7 | Find the smallest positive integer that does not occur in a given sequence. 8 | 9 | */ 10 | 11 | 12 | function solution(A) { 13 | 14 | let missing = 0; 15 | let condition = true; 16 | 17 | A = uniq(A.filter(i => i > 0)).sort((a, b) => a - b); 18 | console.log('A:', A) 19 | 20 | if (A[0] !== 1) { return 1; } 21 | 22 | if (A[A.length - 1] === A.length) { return A.length + 1; } 23 | 24 | while (condition) { 25 | missing++; 26 | condition = A[missing - 1] === missing; 27 | } 28 | 29 | return missing; 30 | 31 | } 32 | 33 | 34 | function uniq(a) { 35 | var seen = {}; 36 | var out = []; 37 | var len = a.length; 38 | var j = 0; 39 | for(var i = 0; i < len; i++) { 40 | var item = a[i]; 41 | if(seen[item] !== 1) { 42 | seen[item] = 1; 43 | out[j++] = item; 44 | } 45 | } 46 | return out; 47 | } 48 | 49 | 50 | test([1, 3, 6, 4, 1, 2]); 51 | 52 | test([-1, -3]); 53 | 54 | test([1, 2, 3]); 55 | 56 | test([2]); 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | function test(...params) { 66 | console.log('\n(', ...params, ')\n'); 67 | console.log('\n=>', solution(...params), '\n\n'); 68 | } -------------------------------------------------------------------------------- /js/multiplies-3-5.js: -------------------------------------------------------------------------------- 1 | console.log('// MULTIPLIES of 3 and 5//'); 2 | 3 | /* 4 | https://projecteuler.net/problem=1 5 | 6 | If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 7 | Find the sum of all the multiples of 3 or 5 below 1000. 8 | */ 9 | 10 | 11 | function solution() { 12 | 13 | let total = 3 + 5; 14 | for (let i = 6; i < 1000; i++) { 15 | 16 | if ((i % 3 === 0) || (i % 5 === 0)) { 17 | total = total + i; 18 | } 19 | } 20 | 21 | console.log(total); 22 | 23 | } 24 | 25 | solution(); -------------------------------------------------------------------------------- /js/nesting.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/ 4 | 5 | Nesting 6 | Determine whether a given string of parentheses (single type) is properly nested. 7 | 8 | */ 9 | 10 | console.log('NESTING'); 11 | 12 | function solution(S) { 13 | let temp = ''; 14 | while (S !== '') { 15 | temp = S.replace(/\(\)|(\(\(\)\))|(\(\(\(\)\)\))/g, ''); 16 | if (temp === S) { 17 | return 0; 18 | } 19 | S = temp; 20 | } 21 | return 1; 22 | } 23 | 24 | 25 | test('(()(())())'); 26 | // 1 27 | 28 | test('())'); 29 | // 0 30 | 31 | 32 | function test(...params) { 33 | console.log('\n( ', ...params, ' )'); 34 | console.log('\n=>', solution(...params), '\n\n'); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /js/number-of-disc-intersections.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | https://app.codility.com/programmers/lessons/6-sorting/number_of_disc_intersections/ 5 | 6 | */ 7 | 8 | console.log(''); 9 | 10 | function solution(A) { 11 | 12 | let i = 0; 13 | let len = A.length; 14 | let count = 0; 15 | 16 | let arr = []; 17 | // let arr2 = []; 18 | 19 | while (i < len) { 20 | 21 | arr.push([i - A[i], i + A[i]]); 22 | 23 | i++; 24 | } 25 | 26 | // console.log('arr', arr) 27 | // console.log('-----') 28 | 29 | 30 | i = 0; 31 | len = arr.length; 32 | let j = 0; 33 | while (i < len) { 34 | 35 | // console.log('i', arr[i]) 36 | 37 | j = i + 1; 38 | while (j < len) { 39 | 40 | if ( 41 | ((arr[j][0] >= arr[i][0]) && (arr[j][0] <= arr[i][1])) || 42 | ((arr[j][1] <= arr[i][1]) && (arr[j][1] >= arr[i][0])) || 43 | ((arr[i][0] >= arr[j][0]) && (arr[i][0] <= arr[j][1])) || 44 | ((arr[i][1] <= arr[j][1]) && (arr[i][1] >= arr[j][0])) 45 | ) { 46 | // arr2.push(arr[j]); 47 | count++; 48 | isMet = true; 49 | 50 | // console.log('arr2push', arr[j]); 51 | } 52 | 53 | j++; 54 | } 55 | 56 | isMet = false; 57 | i++; 58 | 59 | // console.log('....') 60 | 61 | if (count > 10000000) { 62 | return -1; 63 | } 64 | 65 | } 66 | 67 | // console.log('arr2', arr2) 68 | 69 | return count; 70 | } 71 | 72 | 73 | 74 | test([1, 5, 2, 1, 4, 0]); 75 | // 11 76 | 77 | 78 | 79 | function test(params) { 80 | console.log('\n(', params, ')\n'); 81 | console.log('\n=>', solution(params), '\n\n'); 82 | } -------------------------------------------------------------------------------- /js/odd-occurence-in-array.js: -------------------------------------------------------------------------------- 1 | console.log('// ODD OCCURRENCES IN ARRAY //'); 2 | 3 | /* 4 | 5 | https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/ 6 | 7 | Find value that occurs in odd number of elements. 8 | 9 | */ 10 | 11 | function solution(A) { 12 | 13 | return A.reduce((x, y) => x ^ y) 14 | 15 | } 16 | 17 | 18 | 19 | test([9, 3, 9, 3, 9, 7, 9]); 20 | // 7 21 | 22 | 23 | 24 | 25 | 26 | function test(...params) { 27 | console.log('\n(', ...params, ')\n'); 28 | console.log('\n=>', solution(...params), '\n\n'); 29 | } -------------------------------------------------------------------------------- /js/palindromes.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Palindromes 4 | A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar, or the number 10801. 5 | 6 | What is the sum of all numeric palindromes that are less than 10,000? 7 | 8 | */ 9 | 10 | console.log(''); 11 | 12 | function solution() { 13 | 14 | 15 | var max = 10000; 16 | max = Math.ceil(max.toString().length / 2) 17 | var sum = 0; 18 | var i = 1; 19 | var len = 999; 20 | while (i <= 99) { 21 | 22 | 23 | if (i < 10) { 24 | sum += i; 25 | console.log(i); 26 | 27 | } else { 28 | 29 | const f = i.toString().charAt(0); 30 | const s = i.toString().charAt(1); 31 | 32 | if (f === s) { 33 | sum += i; 34 | console.log(i); 35 | 36 | } 37 | 38 | console.log(Number(i.toString() + f.toString())) 39 | console.log(Number(i.toString() + s.toString() + f.toString())) 40 | 41 | sum += Number(i.toString() + f.toString()); 42 | sum += Number(i.toString() + s.toString() + f.toString()); 43 | 44 | } 45 | 46 | console.log('---'); 47 | 48 | i++; 49 | } 50 | 51 | return sum; 52 | 53 | } 54 | 55 | test(); 56 | // 57 | 58 | 59 | 60 | function test(params) { 61 | console.log('\n(', params, ')\n'); 62 | console.log('\n=>', solution(params), '\n\n'); 63 | } -------------------------------------------------------------------------------- /js/passing-cars.js: -------------------------------------------------------------------------------- 1 | console.log('// PASSING CARS //'); 2 | 3 | /* 4 | 5 | https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ 6 | 7 | Count the number of passing cars on the road. 8 | 9 | 10 | */ 11 | 12 | 13 | function solution(A) { 14 | 15 | let counter = 0; 16 | let zeroCount = 0; 17 | let len = A.length; 18 | 19 | let i = A.indexOf(0); 20 | while (i < len) { 21 | 22 | if (A[i] === 0) { 23 | zeroCount++; 24 | } else { 25 | counter += zeroCount 26 | if (counter > 1000000000) { return -1; } 27 | } 28 | 29 | i++; 30 | } 31 | 32 | return counter; 33 | 34 | } 35 | 36 | 37 | 38 | test([0, 1, 0, 1, 1]); 39 | // 5 40 | 41 | test([0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1]); 42 | // 22 43 | 44 | test([1]); 45 | // 0 46 | 47 | test([1, 1]); 48 | // 0 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | function test(...params) { 58 | console.log('\n(', ...params, ')\n'); 59 | console.log('\n=>', solution(...params), '\n\n'); 60 | } -------------------------------------------------------------------------------- /js/perm-check.js: -------------------------------------------------------------------------------- 1 | console.log('// PERMUTATION CHECK //'); 2 | 3 | /* 4 | 5 | https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/ 6 | 7 | Check whether array A is a permutation. 8 | 9 | */ 10 | 11 | 12 | function solution(A) { 13 | 14 | A = A.sort((a, b) => a - b); 15 | for (var i = 0; i < A.length; i++) { 16 | if (A[i] != i + 1) { 17 | return 0; 18 | } 19 | } 20 | 21 | return 1; 22 | 23 | } 24 | 25 | 26 | 27 | test([3, 2, 3, 2]); 28 | // 0 29 | 30 | 31 | 32 | 33 | 34 | function test(...params) { 35 | console.log('\n(', ...params, ')\n'); 36 | console.log('\n=>', solution(...params), '\n\n'); 37 | } -------------------------------------------------------------------------------- /js/perm-missing-elements.js: -------------------------------------------------------------------------------- 1 | console.log('// PERM MISSING ELEMENT //'); 2 | 3 | /* 4 | https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/ 5 | 6 | Find the missing element in a given permutation. 7 | 8 | */ 9 | 10 | function solution(A) { 11 | 12 | let len = A.length; 13 | 14 | if (len <= 1) return 1; 15 | 16 | let max = len + 1; 17 | console.log('max:', max) 18 | let total = A.reduce((x, y) => x + y); 19 | console.log('total:', total) 20 | let missTotal = total - max; 21 | console.log('missTotal:', missTotal) 22 | let actTotal = (len * (len + 1)) / 2; 23 | console.log('actTotal:', actTotal) 24 | let o = actTotal - missTotal; 25 | console.log('o:', o) 26 | 27 | return o; 28 | 29 | } 30 | 31 | test([2, 3, 1, 5]); 32 | // 4 33 | 34 | test([1]); 35 | // 1 36 | 37 | 38 | function test(...params) { 39 | console.log('\n(', ...params, ')\n'); 40 | console.log('\n=>', solution(...params), '\n\n'); 41 | } -------------------------------------------------------------------------------- /js/rabbits-in-forest.js: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/rabbits-in-forest/ 2 | 3 | function solution(answers) { 4 | 5 | const checked = []; 6 | 7 | let count = 0; 8 | 9 | for (let index = 0; index < answers.length; index++) { 10 | const element = answers[index]; 11 | 12 | if (checked.indexOf(element) === -1) { 13 | count += element + 1; 14 | checked.push(element); 15 | } 16 | 17 | } 18 | 19 | return count; 20 | } 21 | 22 | test([1, 1, 2]); 23 | 24 | test([10, 10, 10]); 25 | 26 | test([]); 27 | 28 | function test(...params) { 29 | console.log('\n(', ...params, ')\n'); 30 | console.log('\n=>', solution(...params), '\n\n'); 31 | } 32 | -------------------------------------------------------------------------------- /js/reordering-groups.js: -------------------------------------------------------------------------------- 1 | console.log('// REORDERING GROUPS //'); 2 | 3 | /* 4 | 5 | Write a function that returns the maximum number of groups that can be reordered independently 6 | 7 | A = [1, 5, 4, 9, 8, 7, 12, 13, 14] 8 | 9 | should return 6. 10 | 11 | Because, can be split into six groups: 12 | [1], [5, 4], [9, 8, 7], [12], [13], [14] 13 | 14 | */ 15 | 16 | function solution(A) { 17 | 18 | let B = []; 19 | 20 | let filtered = []; 21 | let len = A.length; 22 | 23 | for (let i = 0; i < len; i++) { 24 | 25 | if ((i > 0) && (A[i - 1] - 1 !== A[i])) { 26 | 27 | let minC = Math.min(...A.slice(i)); 28 | let maxB = Math.max(...A.slice(0, i)); 29 | 30 | if (minC > maxB) { 31 | B.push(filtered); 32 | filtered = []; 33 | } 34 | 35 | } 36 | filtered.push(A[i]); 37 | 38 | } 39 | 40 | B.push(filtered); 41 | return B.length; 42 | } 43 | 44 | 45 | 46 | test([1, 5, 4, 9, 8, 7, 12, 13, 14]); 47 | // 6 48 | 49 | 50 | test([4, 3, 2, 6, 1]); 51 | // 1 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | function test(...params) { 61 | console.log('\n(', ...params, ')\n'); 62 | console.log('\n=>', solution(...params), '\n\n'); 63 | } -------------------------------------------------------------------------------- /js/stone-wall.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/ 4 | 5 | StoneWall 6 | Cover "Manhattan skyline" using the minimum number of rectangles. 7 | 8 | */ 9 | 10 | console.log('STONE WALL'); 11 | 12 | 13 | // function solution(H) { 14 | 15 | // let i = 0; 16 | // let len = H.length; 17 | 18 | // let stones = []; 19 | // let inUse = 0; 20 | 21 | // while (i < len) { 22 | 23 | // let value = H[i]; 24 | 25 | // if (stones[value] === 1) { 26 | // inUse++; 27 | // } 28 | 29 | // // if (value < stones.length - 1) { 30 | // stones.splice(value) 31 | // // } 32 | 33 | // stones[value] = 1; 34 | 35 | // console.log('inUse', inUse) 36 | // console.log('stones:', stones) 37 | // console.log('--------------------') 38 | 39 | // i++; 40 | 41 | // } 42 | 43 | // return len - inUse; 44 | // } 45 | 46 | function solution(H) { 47 | // write your code in JavaScript (Node.js 4.0.0) 48 | 49 | var counter = 0; 50 | var height = 0; 51 | var blocks = []; 52 | var i = 0; 53 | 54 | while (i < H.length) { 55 | console.log(i); 56 | console.log('value: ', H[i]) 57 | if (H[i] > height) { 58 | var newBlock = H[i] - height; 59 | blocks.push(newBlock); 60 | height += newBlock; 61 | counter++; 62 | i++; 63 | } else if (H[i] < height) { 64 | var lastBlock = blocks.pop(); 65 | height -= lastBlock; 66 | } else { 67 | i++; 68 | } 69 | 70 | 71 | console.log('blocks:', blocks) 72 | console.log('height:', height) 73 | console.log('counter:', counter) 74 | console.log('--------------------') 75 | } 76 | 77 | return counter; 78 | } 79 | 80 | 81 | 82 | test([1, 1, 1]); 83 | // 1 84 | 85 | test([1]); 86 | // 1 87 | 88 | test([1, 2, 3, 3, 2, 1]); 89 | // 3 90 | 91 | test([8, 8, 5, 7, 9, 8, 7, 4, 8]); 92 | // 7 93 | 94 | test([3, 2, 1]); 95 | // 3 96 | 97 | test([2, 3, 2, 1]); 98 | // 3 99 | 100 | test([2, 5, 1, 4, 6, 7, 9, 10, 1]); 101 | // 8 102 | 103 | test([4, 1, 2, 3, 1]); 104 | // 4 105 | 106 | 107 | function test(...params) { 108 | console.log('\n(', ...params, ')'); 109 | console.log('\n=>', solution(...params), '\n\n'); 110 | } 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /js/tape-equilibrium.js: -------------------------------------------------------------------------------- 1 | console.log('// TAPE EQUILIBRIUM //'); 2 | 3 | /* 4 | https://app.codility.com/programmers/lessons/3-time_complexity/ 5 | 6 | Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|. 7 | */ 8 | 9 | 10 | function solution(A) { 11 | 12 | var sumRight = A.reduce(function (pv, cv, idx) { return (idx > 0) ? pv + cv : 0; }, 0); 13 | var sumLeft = 0; 14 | var maxI = A.length - 1; 15 | 16 | let min = null; 17 | 18 | for (var i = 0; i < maxI; i++) { 19 | sumLeft += A[i]; 20 | let d = Math.abs(sumLeft - sumRight); 21 | min = min === null ? d : Math.min(min, d); 22 | if (i + 1 <= maxI) sumRight -= A[i + 1]; 23 | } 24 | 25 | return min; 26 | 27 | } 28 | 29 | test([3, 1, 2, 4, 3]); 30 | // 1 31 | 32 | test([-10, -5, -3, -4, -5]); 33 | // 3 34 | 35 | test([-1000, 1000]); 36 | // 2000 37 | 38 | 39 | 40 | 41 | 42 | 43 | function test(...params) { 44 | console.log('\n(', ...params, ')\n'); 45 | console.log('\n=>', solution(...params), '\n\n'); 46 | } 47 | -------------------------------------------------------------------------------- /js/temp.js: -------------------------------------------------------------------------------- 1 | 2 | function fn(d) { 3 | 4 | let total = 0; 5 | 6 | for (let g = 10; g <= 20; g++) { 7 | 8 | let count = 0; 9 | const n = g; 10 | const arrN = n.toString().split(''); 11 | for (let i = 0; i < arrN.length; i++) { 12 | for (let j = i + 1; j <= arrN.length; j++) { 13 | const value = Number(arrN.slice(i, j).join('')); 14 | console.log('value:', value) 15 | if (value % 3 === 0) { 16 | console.log('>>>>>>>>>', value) 17 | count++; 18 | } 19 | } 20 | // console.log('---'); 21 | } 22 | console.log('---'); 23 | 24 | if (count % 3 === 0) { 25 | total++; 26 | } 27 | 28 | } 29 | return total; 30 | } 31 | 32 | console.log('COUNT: ', fn(2)); 33 | 34 | 35 | 36 | /* 37 | 2 38 | 25 39 | 257 40 | 2573 41 | 42 | 43 | 5 44 | 57 45 | 573 46 | 47 | 7 48 | 73 49 | 50 | 3 51 | */ 52 | -------------------------------------------------------------------------------- /js/travel-favourite.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Travel Favourite 4 | 5 | */ 6 | 7 | // function solution(A, B, C) { 8 | 9 | // let i = 0; 10 | // let len = A.length; 11 | // let count = 0; 12 | 13 | // while (i < len) { 14 | 15 | // let j = 0; 16 | 17 | // while (j < len) { 18 | 19 | // let y = 0; 20 | // while (y < len) { 21 | 22 | // if ((A[i] < B[j]) && (B[j] < C[y])) { 23 | // console.log('count:', count) 24 | // count++; 25 | // } 26 | // console.log('--- ', i, j , y, ' ---') 27 | // y++; 28 | // } 29 | 30 | // j++; 31 | // } 32 | 33 | // i++; 34 | // } 35 | 36 | // return count; 37 | // } 38 | 39 | 40 | function solution(A, B, C) { 41 | 42 | let i = 0; 43 | let len = A.length; 44 | let count = 0; 45 | let a = 0; 46 | let b = 0; 47 | let c = 0; 48 | 49 | while (i < len) { 50 | 51 | let j = 0; 52 | while (j < len) { 53 | 54 | // let y = 0; 55 | // while (y < len) { 56 | 57 | a = i; 58 | b = j; 59 | c = i; 60 | 61 | if ((A[a] < B[b]) && (B[b] < C[c])) { 62 | console.log('count:', count) 63 | count++; 64 | } 65 | console.log('--- ', i, j, i, ' ---') 66 | 67 | if ((i !== j)) { 68 | 69 | a = i; 70 | b = i; 71 | c = j; 72 | 73 | if ((A[a] < B[b]) && (B[b] < C[c])) { 74 | console.log('count:', count) 75 | count++; 76 | } 77 | 78 | a = i; 79 | b = j; 80 | c = j; 81 | 82 | if ((A[a] < B[b]) && (B[b] < C[c])) { 83 | console.log('count:', count) 84 | count++; 85 | } 86 | 87 | console.log('--- ', i, i, j, ' ---') 88 | console.log('--- ', i, j, j, ' ---') 89 | } 90 | // y++; 91 | // } 92 | 93 | j++; 94 | } 95 | 96 | 97 | i++; 98 | } 99 | 100 | return count; 101 | } 102 | 103 | 104 | test([29, 50], [61, 37], [37, 70]); 105 | // [1, 3, 2, 3, 0, 0, 0, 0, 0] 106 | 107 | test([29, 29], [61, 61], [70, 70]); 108 | // 8 109 | 110 | test([5], [5], [5]); 111 | // 0 112 | 113 | function test(...params) { 114 | console.log('\n(', ...params, ')\n'); 115 | console.log('\n=>', solution(...params), '\n\n'); 116 | } 117 | 118 | 119 | // A[0], B[0], C[0] 120 | // A[0], B[0], C[1] 121 | // A[0], B[1], C[0] 122 | // A[0], B[1], C[1] 123 | 124 | // A[1], B[0], C[0] 125 | // A[1], B[0], C[1] 126 | // A[1], B[1], C[0] 127 | // A[1], B[1], C[1] 128 | 129 | -------------------------------------------------------------------------------- /js/triangle.js: -------------------------------------------------------------------------------- 1 | console.log('// TRIANGLE //'); 2 | 3 | /* 4 | 5 | https://app.codility.com/programmers/lessons/6-sorting/triangle/ 6 | 7 | Determine whether a triangle can be built from a given set of edges. 8 | 9 | */ 10 | 11 | 12 | function solution(A) { 13 | 14 | let i = 0; 15 | let len = A.length; 16 | 17 | A = A.sort((a, b) => b - a); 18 | 19 | while (i < len) { 20 | 21 | let p = A[i]; 22 | 23 | let q = A[i + 1]; 24 | let r = A[i + 2]; 25 | 26 | if ((p + q > r) && (p + r > q) && (q + r > p)) { 27 | return 1; 28 | } 29 | 30 | i++; 31 | } 32 | 33 | return 0; 34 | 35 | } 36 | 37 | 38 | 39 | test([10, 2, 5, 1, 8, 20]); 40 | // 1 41 | 42 | test([10, 50, 5, 1]); 43 | // 0 44 | 45 | test([5, 3, 3]); 46 | // 1 47 | 48 | test([-2, -2, -2]); 49 | // 0 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | function test(...params) { 58 | console.log('\n(', ...params, ')\n'); 59 | console.log('\n=>', solution(...params), '\n\n'); 60 | } 61 | 62 | 63 | --------------------------------------------------------------------------------