Entries from 2019-03-01 to 1 month
Question app.codility.com My Solution app.codility.com function solution(A) { if (A.length < 3) return 0; A.sort((a, b)=>(a - b)); for (let i = 2; i < A.length; i++) { if (A[i] < A[i-1] + A[i-2]) { return 1; } } return 0; }
Question app.codility.com My Solution I (O(NlogN)) app.codility.com function solution(A) { A.sort((a, b)=>(a - b)); // 2 types (--+) or (+++) let type1 = A[0]*A[1]*A[A.length-1]; let type2 = A[A.length-1]*A[A.length-2]*A[A.length-3]; retur…
Question app.codility.com My Solution app.codility.com function solution(A) { if (A.length === 0 || A.length === 1) return A.length; A.sort((a, b)=>(a - b)); let cur = A[0]; let count = 1; for (let i = 0; i < A.length; i++) { if (A[i] != c…
Question app.codility.com My Solution (O(1)) app.codility.com function solution(A, B, K) { // find the smallest multiple of K (A or above) let minKsMult = Math.ceil(A/K)*K; // there's none if (minKsMult > B) return 0; else return Math.floo…
Question app.codility.com My Solution This is the most challenging one I've ever seen in codility (if you also start from Q1 to here), the trick is that any slices of arbitrary length can be broken into sub-slices of length 2 or 3. The pro…
Question app.codility.com My Solution app.codility.com function solution(S, P, Q) { let prefixASum = []; let prefixCSum = []; let prefixGSum = []; let prefixTSum = []; let ASum = 0; let CSum = 0; let GSum = 0; let TSum = 0; let ans = []; f…
Question app.codility.com My Solution app.codility.com function solution(A) { let suffixSum = []; let sum = 0; let ans = 0; for (let i = A.length-1; i >= 0; i--) { sum += A[i]; suffixSum[i] = sum; } for (let i = 0; i < A.length; i++) { if …
Question app.codility.com My Solution app.codility.com function solution(A) { let count = []; let minMissing = 0; for (let i = 0; i < A.length; i++) { if (A[i] > 0) { if (count[A[i]] === undefined) { count[A[i]] = 1; } while (count[minMiss…
Question app.codility.com Solution app.codility.com function solution(N, A) { let setMax = 0; let knownMax = 0; let currentMax = 0; let count = []; for (let i = 0; i < N; i++) { count[i] = 0; } for (let i = 0; i < A.length; i++) { if (A[i] …
Question app.codility.com My Solution app.codility.com function solution(X, A) { let count = []; let maxDist = 0; for (let i = 0; i < A.length; i++) { if (count[A[i]] === undefined) count[A[i]] = 1; while(count[maxDist+1] === 1) maxDist++;…
Question app.codility.com My Solution app.codility.com function solution(A) { let count = []; for (let i = 0; i < A.length; i++) { if (A[i] > A.length+1) return 0; if (count[A[i]-1] === undefined) count[A[i]-1] = 1; else return 0; } for (l…
Question app.codility.com My Solution (O(n)) app.codility.com function solution(A) { let prefixSum = []; let suffixSum = []; prefixSum[0] = A[0]; for (let i = 1; i < A.length; i++) { prefixSum[i] = prefixSum[i-1]+A[i]; } suffixSum[A.length…
Question app.codility.com My Solution app.codility.com function solution(A) { if (A.length === 0) return 1; let arrSum = A.reduce( (prev, curr) => prev + curr ); let permSum = (1 + A.length + 1)*(A.length + 1)/2; return permSum-arrSum; } N…
Question app.codility.com My Solution app.codility.com function solution(X, Y, D) { return Math.ceil((Y-X)/D); }
brew upgrade node brew link node
Question app.codility.com My Solution (O(nlogn)) app.codility.com function solution(A) { A.sort((a, b)=>(a-b)); for (let i = 0; i < A.length; i+=2) { if (A[i] != A[i+1]) { return A[i]; } } } Online Solution (O(n)) Odd Occurrences In Array …
Question app.codility.com My Solution app.codility.com function solution(A, K) { K = K%A.length; if (K === 0) return A; let AA = A.concat(A); return AA.slice(A.length-K, A.length-K+A.length); } Note The rotation of string A is a substring …
Question app.codility.com My Solution app.codility.com function solution(N) { let input = N; let count = 0; let maxCount = 0; let startCount = false; while (input > 0) { let bit = input%2; if (bit === 0) { if (startCount) count++; } else {…