Last active
February 3, 2022 06:39
-
-
Save maliMirkec/64cb7815e2124a97270963b3b1ea6438 to your computer and use it in GitHub Desktop.
codility tasks
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 80% | |
function binary(N) { | |
var half = Math.floor(N / 2); | |
var r = (N % 2).toString(); | |
if(N >= 2) { | |
r += binary(half); | |
} | |
return r; | |
} | |
function solution(N) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var bin = binary(N).split(''); | |
var solution = []; | |
bin.map(function(val, key) { | |
if(val === '0') { | |
if(!solution.length) { | |
solution.push(1); | |
} else { | |
solution[solution.length -1] = solution[solution.length - 1] + 1; | |
} | |
} else { | |
if(solution.length) { | |
solution.push(0); | |
} | |
} | |
}); | |
return !solution.length ? 0 : Math.max.apply(null, solution); | |
} | |
solution(15); |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 11% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A !== 'object') { | |
return false; | |
} | |
var r = A.filter(function(val, key) { | |
var f = false; | |
if(A[key + 2] !== 'undefined') { | |
f = val !== A[key + 2]; | |
} | |
if(f && (key - 2) >= 0) { | |
f = val !== A[key - 2]; | |
} | |
return f; | |
}); | |
return (r[0] !== 'undefined') ? r[0] : false; | |
} | |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 50% | |
function solution(A, K) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if((K > 0 && K > 100) || A.length > 100 && Array.isArray(A)) { | |
return false; | |
} | |
var B = A.map(function(val, key) { | |
var a = A[key - K]; | |
if(typeof a === 'undefined') { | |
a = A[A.length + key - K]; | |
} | |
if(a > -1000 && a < 1000 && Number.isInteger(a)) { | |
return a; | |
} | |
}); | |
return B; | |
} | |
solution([3, 8, 9, 7, 6], 3); |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 100% | |
function solution(X, Y, D) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof X === 'undefined' || !Number.isInteger(X)) { | |
return false; | |
} | |
if(typeof Y === 'undefined' || !Number.isInteger(Y)) { | |
return false; | |
} | |
if(typeof D === 'undefined' || !Number.isInteger(D)) { | |
return false; | |
} | |
if(X > Y) { | |
return false; | |
} | |
if(X < 1 && X > 1000000000) { | |
return false; | |
} | |
if(Y < 1 && Y > 1000000000) { | |
return false; | |
} | |
if(D < 1 && D > 1000000000) { | |
return false; | |
} | |
return Math.ceil((Y - X) / D); | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 30% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(!Array.isArray(A)) { | |
return false; | |
} | |
var r = false; | |
A.map(function(val, key) { | |
if(val >= 1 && val <= A.length + 2 && A.indexOf(key + 1) === -1) { | |
r = key + 1; | |
} | |
}); | |
return r; | |
} |
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
// 50% |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 66% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
if(A.length < 1 || A.length > 100000) { | |
return false; | |
} | |
var r = false; | |
for(var i = 1; i < 100001; i++) { | |
if(A.indexOf(i) === -1) { | |
r = i; | |
break; | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 9% | |
function solution(X, A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
if(X < 1 || X > 100000) { | |
return false; | |
} | |
if(A.length < 1 || A.length > 100000) { | |
return false; | |
} | |
var r = A.indexOf(X); | |
for(var j = r - 1; j > 0; j--) { | |
if(A.indexOf(j) === -1) { | |
r = -1; | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 30% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
if(A.length < 1 || A.length > 100000) { | |
return false; | |
} | |
var r = 1; | |
var m = Math.max.apply(null, A); | |
if(m > 1000000000) { | |
return false; | |
} | |
for(var i = m - 1; i >= 1; i--) { | |
var s = A.indexOf(i); | |
if(s === -1) { | |
r = 0; | |
break; | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 66% | |
function solution(N, A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof N === 'undefined' || !Number.isInteger(N)) { | |
return false; | |
} | |
if(N < 1 || N > 100000) { | |
return false; | |
} | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
if(A.length < 1 || A.length > 100000) { | |
return false; | |
} | |
var r = Array.apply(null, Array(N)).map(Number.prototype.valueOf,0); | |
for(var i = 0; i < A.length; i++) { | |
if(typeof A[i] === 'undefined' || !Number.isInteger(A[i]) || A[i] < 1 || A[i] > N + 1) { | |
break; | |
} | |
if(A[i] >= 1 && A[i] <= N + 1) { | |
if(A[i] === N + 1) { | |
r = Array.apply(null, Array(N)).map(Number.prototype.valueOf, Math.max.apply(null, r)); | |
} else { | |
r[A[i] - 1] = r[A[i] - 1] + 1; | |
} | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 50% | |
function solution(A, B, K) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Number.isInteger(A)) { | |
return false; | |
} | |
if(A < 0 || A > 2000000000) { | |
return false; | |
} | |
if(typeof B === 'undefined' || !Number.isInteger(B)) { | |
return false; | |
} | |
if(B < 0 || B > 2000000000) { | |
return false; | |
} | |
if(A > B) { | |
return false; | |
} | |
if(typeof K === 'undefined' || !Number.isInteger(K)) { | |
return false; | |
} | |
if(K < 1 || K > 2000000000) { | |
return false; | |
} | |
var r = 0; | |
for(var i = A; i <= B; i++) { | |
if(i % K === 0) { | |
r++; | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 60% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
if(A.length < 1 || A.length > 100000) { | |
return false; | |
} | |
var r = 0; | |
for(var i = 0; i < A.length; i++) { | |
if(A[i] === 0) { | |
for(var j = i; j < A.length; j++) { | |
if(i !== j && A[i] !== A[j]) { | |
r++; | |
} | |
} | |
} | |
} | |
return r > 1000000000 ? -1 : r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 25% | |
function solution(S, P, Q) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof S !== 'string') { | |
return false; | |
} | |
if(S.length < 1 || S.length > 100000) { | |
return false; | |
} | |
if(typeof P === 'undefined' || !Array.isArray(P)) { | |
return false; | |
} | |
if(P.length < 1 || P.length > 50000) { | |
return false; | |
} | |
if(typeof Q === 'undefined' || !Array.isArray(Q)) { | |
return false; | |
} | |
if(Q.length < 1 || Q.length > 50000) { | |
return false; | |
} | |
if(P.length !== Q.length) { | |
return false; | |
} | |
if(!S.match(/[ACGT]+$/)) { | |
return false; | |
} | |
var s = { | |
A: 1, | |
C: 2, | |
G: 3, | |
T: 4 | |
}; | |
var r = []; | |
for(var i = 0; i < P.length; i++) { | |
if(P[i] < 0 || P[i] > S.length) { | |
continue; | |
} | |
if(Q[i] < 0 || Q[i] > S.length) { | |
continue; | |
} | |
if(P[i] > Q[i]) { | |
continue; | |
} else if(P[i] === Q[i]) { | |
r.push(s[S[P[i]]]); | |
} else { | |
var m = []; | |
for(var j = P[i]; j < Q[i]; j++) { | |
m.push(s[S[j]]); | |
} | |
r.push(Math.min.apply(null, m)); | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 30% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
var len = A.length; | |
if(len < 2 || len > 100000) { | |
return false; | |
} | |
var r1 = []; | |
var r2 = []; | |
for(var i = 0; i < len; i++) { | |
var val = A[i]; | |
if(val < -10000 || val > 10000 || (len - i) < 2) { | |
continue; | |
} | |
for(var j = i + 1; j < len; j++) { | |
val += A[j]; | |
r1.push(val); | |
r2.push(i); | |
} | |
} | |
return r2[r1.indexOf(Math.min.apply(null, r1))]; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 100% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
var len = A.length; | |
if(len < 0 || len > 100000) { | |
return false; | |
} | |
if(Math.min.apply(null, A) < -1000000 || Math.max.apply(null, A) > 1000000) { | |
return false; | |
} | |
var r = {}; | |
A.forEach(function(val, key) { | |
r[val] = val; | |
}); | |
return Object.keys(r).length; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 33% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0 | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
var len = A.length; | |
if(len < 3 || len > 100000) { | |
return false; | |
} | |
if(Math.min.apply(null, A) < -1000 || Math.max.apply(null, A) > 1000) { | |
return false; | |
} | |
var r = []; | |
for(var i = 0; i < len; i++) { | |
if(len - i < 3) { | |
continue; | |
} | |
for(var j = i + 1; j < len; j++) { | |
for(var k = j + 1; k < len; k++) { | |
r.push(A[i]*A[j]*A[k]); | |
} | |
} | |
} | |
return Math.max.apply(null, r); | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 62% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof A === 'undefined' || !Array.isArray(A)) { | |
return false; | |
} | |
var len = A.length; | |
if(len < 0 || len > 100000) { | |
return false; | |
} | |
if(Math.min.apply(null, A) < -2147483648 || Math.max.apply(null, A) > 2147483647) { | |
return false; | |
} | |
var r = 0; | |
for(var i = 0; i < len; i++) { | |
if(!Number.isInteger(A[i])) { | |
continue; | |
} | |
for(var j = i + 1; j < len; j++) { | |
if(!Number.isInteger(A[j])) { | |
continue; | |
} | |
for(var k = j + 1; k < len; k++) { | |
if(!Number.isInteger(A[k])) { | |
continue; | |
} | |
if(A[i] + A[j] > A[k] && A[j] + A[k] > A[i] && A[k] + A[i] > A[j]) { | |
r = 1; | |
break; | |
} | |
} | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
function solution(S) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(typeof S !== 'string') { | |
return 0; | |
} | |
if(S.length < 0 || S.length > 200000) { | |
return 0; | |
} | |
if(S === '') { | |
return 1; | |
} | |
var A = S.split(''); | |
for(var i = 0; i < S.length; i++) { | |
var j; | |
if(S[i] === '(') { | |
j = S.indexOf(')', i); | |
if(j - i > 1) { | |
console.log(i, j); | |
} | |
} | |
} | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 12% | |
function findPair(S, i, A) { | |
switch(S[i]) { | |
case '(': | |
j = S.indexOf(')', i); | |
break; | |
case '[': | |
j = S.indexOf(']', i); | |
break; | |
case '{': | |
j = S.indexOf('}', i); | |
break; | |
default: | |
break; | |
} | |
A.push(i, j); | |
i++; | |
var diff = j - i; | |
if(diff === 0) { | |
if(A.indexOf(j) === -1) { | |
return findPair(S, j + 1, A); | |
} else { | |
return 1; | |
} | |
} else if(diff > 1) { | |
if(diff % 2 !== 0) { | |
return 0; | |
} else { | |
return findPair(S, i, A); | |
} | |
} else { | |
return 0; | |
} | |
} | |
function solution(S) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(S === '') { | |
return 1; | |
} | |
var r = findPair(S, 0, []); | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 7% | |
function solution(H) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = []; | |
var i = 0; | |
while(i < H.length) { | |
for(var j = i + 1; j < H.length; j++) { | |
if(H[j] < H[i]) { | |
r.push(H[j]); | |
i = j - 1; | |
break; | |
} else if(H[j] > H[i]) { | |
r.push(H[j]); | |
break; | |
} | |
} | |
i++; | |
} | |
return r.length; | |
} | |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 12% | |
function solution(A, B) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = A.length; | |
var i = 0; | |
while(i < A.length) { | |
if(B[i] === 1) { | |
var j = i + 1; | |
while(j < A.length) { | |
if(B[i] > B[j]) { | |
r--; | |
} | |
j++; | |
} | |
} | |
i++; | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 75% | |
function solution(S) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
if(S === '') { | |
return 1; | |
} | |
var A = S.split(''); | |
var i = 0; | |
while(i < A.length) { | |
if(A[i] === '(' && A[i + 1] === ')') { | |
A.splice(i, 2); | |
i = 0; | |
} else { | |
i++; | |
} | |
if(S.length === 0) { | |
break; | |
} | |
} | |
return A.length === 0 ? 1 : 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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 100% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = {}; | |
for(var i = 0; i < A.length; i++) { | |
if(typeof r[A[i]] === 'undefined') { | |
r[A[i]] = [i]; | |
} else { | |
r[A[i]].push(i); | |
} | |
} | |
var l = false; | |
var d = false; | |
for(var rVal in r) { | |
var len = r[rVal].length; | |
if(!l) { | |
l = len; | |
d = r[rVal]; | |
} else if(len > l) { | |
l = len; | |
d = r[rVal]; | |
} | |
} | |
return (l <= (A.length / 2)) ? -1 : d[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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 0% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = {}; | |
var i = 0; | |
while(i < A.length) { | |
var j = i; | |
var s1 = {}; | |
while(j < A.length) { | |
if(typeof s1[A[j]] === 'undefined') { | |
s1[A[j]] = [A[j]]; | |
} else { | |
s1[A[j]].push(A[j]); | |
} | |
j++; | |
} | |
j = i; | |
var s2 = {}; | |
while(j > 0) { | |
if(typeof s2[A[j]] === 'undefined') { | |
s2[A[j]] = [A[j]]; | |
} else { | |
s2[A[j]].push(A[j]); | |
} | |
j--; | |
} | |
r[i] = { | |
s1: s1, | |
s2: s2 | |
}; | |
i++; | |
} | |
var t = 1; | |
for(var rVal in r) { | |
var l1 = false; | |
var l2 = false; | |
var d1 = false; | |
var d2 = false; | |
for(var s1Val in r[rVal]['s1']) { | |
if(!l1) { | |
l1 = r[rVal]['s1'][s1Val].length; | |
d1 = s1Val; | |
} else if(l1 < r[rVal]['s1'][s1Val].length) { | |
l1 = r[rVal]['s1'][s1Val].length; | |
d1 = s1Val; | |
} | |
} | |
for(var s2Val in r[rVal]['s2']) { | |
if(!l2) { | |
l2 = r[rVal]['s2'][s2Val].length; | |
d2 = s2Val; | |
} else if(l2 < r[rVal]['s2'][s2Val].length) { | |
l2 = r[rVal]['s2'][s2Val].length; | |
d2 = s2Val; | |
} | |
} | |
var rValParsed = parseInt(rVal); | |
if(d1 === d2 && l1 > (rValParsed / 2) && l2 > ((A.length - rValParsed) / 2)) { | |
t++; | |
} | |
} | |
return t; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 78% | |
function solution(N) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var i = 1; | |
var r = 1; | |
while(i <= N / 2) { | |
if(N % i === 0) { | |
r++; | |
} | |
i++; | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 60% | |
function solution(N) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var i = 0; | |
var r = false; | |
var A = []; | |
while(i <= N / 2) { | |
if(N % i === 0) { | |
var s = N / i; | |
var p = 2 * (i + s); | |
if(!r) { | |
r = p; | |
} else if (p < r) { | |
r = p; | |
} | |
A.push(s); | |
} | |
i++; | |
if(A.indexOf(i) !== -1) { | |
i++; | |
} | |
} | |
return r; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 20% | |
function solution(A) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = {}; | |
for(var i = 1; i < A.length - 1; i++) { | |
if(A[i - 1] < A[i] && A[i + 1] < A[i]) { | |
if(typeof r[A[i]] === 'undefined') { | |
r[A[i]] = [i] | |
} else { | |
r[A[i]].push(i); | |
} | |
} | |
} | |
var i = 0; | |
for(var rVal in r) { | |
i++; | |
} | |
return i; | |
} |
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
// 62% | |
function solution(N, M) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var r = []; | |
var i = 0; | |
while(i < N) { | |
var s = i * M; | |
var d = s % N; | |
if(r.indexOf(d) !== -1) { | |
break; | |
} | |
r.push(d); | |
i++; | |
} | |
return r.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment