Instantly share code, notes, and snippets.

# lalkmim/codility_solutions.txt

Last active May 9, 2023 15:14
Codility Solutions in JavaScript
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

### nkreiger commented Jul 7, 2018 • edited

LOVE THIS!
Note Lesson 3 - TapeEquilibrium
A simpler way to do it with higher-order functions.

function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
var reducer = (accumulator, currentValue) => accumulator + currentValue;
var sum = A.reduce(reducer) - A;
var arrSum = 0;
var min = Math.abs(A - sum);

``````for (var i = 0; i < A.length - 1; i++) {
arrSum = arrSum + A[i];
var diff = Math.abs(arrSum - sum);
sum = sum - A[i + 1];
if (diff < min) {
min = diff;
}
}
return min;
``````

}

idk why it pastes like this every time
https://app.codility.com/demo/results/trainingHQRMXH-P6C/

### HusDev commented Nov 1, 2018

Thanks Lilkimim,
Can you explain "OddOccurrencesInArray" to me, why it works?
I didn't understand this part:
` agg ^= A[i];`

### Synkevych commented Nov 7, 2018

it is operator ХОР, if you try to transmit two equal numbers, you get 0, if you have 1 number without pair XOR return them.
Simple:
bar = 5; //00000000000000000000000000000101
bar ^= 5; // 00000000000000000000000000000101
//bar = 0, or 00000000000000000000000000000000

bar = 5;
bar ^= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// 7: 00000000000000000000000000000111

👏

### SuchismitaGoswami commented Dec 14, 2018 • edited

FrogJmp Problem can be solved in O(1) time .

Solution:
function solution(X, Y, D) {
return Math.ceil(((Y-X)/D));
}

https://app.codility.com/demo/results/trainingQGSXX6-DX2/

### wzup commented Apr 4, 2019

Why do you use `%` (division with remainder) for BinaryGap task?
https://app.codility.com/demo/results/trainingU2FQPQ-7Y4/

### tonifirnandes commented Jul 21, 2019

Thank for this great work, so we can learn from each other for better/perfect solution. Anyway for this case i only got 90% using your code, maybe would you like to check it again or update it thanks.

https://codility.com/demo/results/trainingSREAJE-B4Q/

### Maravanna commented Aug 28, 2019

Lesson 15 - CountTriangles
This code does not get 100% all of the time. (At least I couldn't get it, even once).
The code can be improved by moving the line
end = mid+1;
to before the second For, like
end = start + 2;
because you don't have to go all through the end part of the array again since once the array is ordered if (start + lastMid > end) then start + newMid will always be > end.
You just have to test the rest of it...
This will make the code 100%.
like with the modification - https://app.codility.com/demo/results/trainingPS6YB4-DQ3/

### boutahlilsoufiane commented Sep 14, 2019 • edited

The solution of MaxProductOfThree is not correct 100%:
This is an example:
A = -5
A = 4
A = 4
A = 3
A = -1
The program should return 48, but the result is 20

### Maravanna commented Sep 14, 2019

The solution of MaxProductOfThree is not correct 100%:
This is an example:
A = -5
A = 4
A = 4
A = 3
A = -1
The program should return 48, but the result is 20

Try this solution:
https://app.codility.com/demo/results/trainingESHJN9-ACJ/

### ghost commented Sep 19, 2019 • edited by ghost

Thank you for the amazing work!

Lesson 6 - Distinct can be solved by using ES6.

function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
return (new Set(A)).size;
}

https://app.codility.com/demo/results/trainingRSRTET-8F2/

### arammkr commented Sep 25, 2019

Hello, here is good solution for ArrayInversionCount, I'm using here merge sort to calculate inversion.

https://app.codility.com/demo/results/trainingYFRBTD-F58/

### lalkmim commented Oct 18, 2019

Hey all, first I'm very glad this is helpful for someone other than me :), and sorry for not being very responsive, just noticed all github emails were going to a spam folder.

I put this together because I was planning to change jobs and thought it was a good idea to get myself "sharp" again on those types of exercises and couldn't really find a single place with all the solutions.

Some of the solutions might have gotten 100% on the tests but that doesn't mean they are a 100% correct for all scenarios (as some of you noticed), once I have some time I'll try to update the original doc to add the other solutions as well.

:)

### ignaciodediego commented Mar 20, 2020

Thank you very much, it really helped me !

### sofinico commented Mar 29, 2020 • edited

Here's another way of solving Lesson 2 - CyclicRotation with recursion:

``````function solution(A, K) {
if (K === 0 || A === [])
return A;

const newFirstElement = A.pop();
A.unshift(newFirstElement);

return solution(A, K-1);
}
``````

When testing just forgot the empty array case:
https://app.codility.com/demo/results/trainingMS7V6F-D9B/

### julian916 commented May 5, 2020 • edited

Here a more performant solution for Lesson 1 - BinaryGap
https://app.codility.com/demo/results/trainingBSDAJV-WZW/

### priyakishore921 commented May 17, 2020

Hi,
I have another way of implementing Lesson 4 PermCheck
https://app.codility.com/demo/results/trainingEE6XV3-TRA/

### Jojr commented Jun 20, 2020

Thanks for sharing!

### Knyazik01 commented Jul 14, 2020

Lesson 4 - Couting Elements

This solution is faster :)

### deejoe79 commented Sep 5, 2020 • edited

Hi lalkmim,

I have another solution for the AbsDistinct (lesson 15). Take a look if you like it:

``````function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let distinctValues = {};
for (let i = 0; i < A.length; i++) {
let prop = Math.abs(A[i]).toString();
distinctValues[prop] = prop;
}
return Object.keys(distinctValues).length;
}
``````

### zeeshanullah commented Oct 23, 2020

Here is my solution to Lesson 2 (CyclicRotation) without using loops or recursion:
https://app.codility.com/demo/results/trainingEVYNBV-5FC/

### 2sagarraut2 commented Jan 18, 2021

Hi, Can you please explain Lesson 4 MissingInteger?

### MustafaSarhan commented May 18, 2021

Using JS filter you can distinct the array and get the length of it will be the number you looking for
`return A.filter((v,i,s)=> s.indexOf(v) === i ).length;`

### thekomx commented May 30, 2021 • edited

Lesson 5 - Prefix Sums

Lesson 9 - Maximum slice problem

### lumayara commented Oct 23, 2021

Oi, vascaino! tudo bem? Leonardo, queria falar com vc! vc tem Linkedin?

### MackKaputo commented Oct 28, 2021 • edited

Lesson 3 - PermMissingElem can be solved using the fact that sum of consecutive elements from 1 to n equals n*(n+1)/2. Subtract all elements from the sum, you're left with the missing number

function solution(A) {

``````let maxNumber = A.length + 1
let total_sum_assumed = maxNumber * (maxNumber + 1) / 2

for (let number of A){
total_sum_assumed = total_sum_assumed - number
}

``````

}

### Knyazik01 commented Jun 16, 2022

Lesson 5 - Prefix Sums
PassingCars

This solution is easer

https://app.codility.com/demo/results/trainingJTC3QW-P7C/

### flsouto commented Sep 24, 2022

PermCheck:

```function solution(A){
const mem = {}
for(const v of A){
if(v < 1 || v > A.length) return 0
mem[v] = 1
}
return Object.keys(mem).length === A.length ? 1 : 0
}```

### flsouto commented Sep 24, 2022

FrogRiverOne:

```function solution(A){
let mem = {};
for(let i=0;i<A.length;i++){
if(A[i] < X) mem[A[i]] = true
if(A[i] === X && Object.keys(mem).length === X-1){
return i
}
}
return -1
}```

### flsouto commented Sep 24, 2022

MaxCounters:

```function solution(N,A){
let counters = Array.from({length:N}).map(_=>0);
for(const X of A){
if(X >= 1 && X <= N){
counters[X-1]++
} else if(X === N+1){
const greatest = Math.max(...counters)
counters = counters.map(_ => greatest)
}
}
return counters
}```