Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript Programs

The document outlines several coding challenges and their solutions, including finding a missing element in a permutation, minimizing the difference between two parts of an array, checking if an array is a permutation, and counting passing cars. Each challenge includes a task description, example inputs and outputs, and a JavaScript function implementation. The document also provides links to the respective solutions on Codility.

Uploaded by

rajmohd mohd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript Programs

The document outlines several coding challenges and their solutions, including finding a missing element in a permutation, minimizing the difference between two parts of an array, checking if an array is a permutation, and counting passing cars. Each challenge includes a task description, example inputs and outputs, and a JavaScript function implementation. The document also provides links to the respective solutions on Codility.

Uploaded by

rajmohd mohd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PermMissingElem

Find the missing element in a given permutation.

Task description
An array A consisting of N different integers is given. The array contains
integers in the range [1..(N + 1)], which means that exactly one element is
missing.

Your goal is to find that missing element.

Write a function:

function solution(A);

that, given an array A, returns the value of the missing element.

For example, given array A such that:

A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [0..100,000];


the elements of A are all distinct;
each element of array A is an integer within the range [1..(N + 1)].

Score - 30%

Solution - https://app.codility.com/demo/results/training99Z8YY-9VQ/

// you can write to stdout for debugging purposes, e.g.


// console.log('this is a debug message');

function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if(A.length <= 1){
return 1;
}
var missing;
A.sort();

for(var i=1;i<= A.length ;i++) {


if(A.indexOf(i) == -1){
missing = i;
}
}
//console.log(missing); // to check the result.
return missing;
}

2. TapeEquilibrium

Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|


Task description

A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.

Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P - 1]
and A[P], A[P + 1], ..., A[N - 1].

The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P - 1]) - (A[P] + A[P + 1]
+ ... + A[N - 1])|

In other words, it is the absolute difference between the sum of the first part and the sum of the
second part.

For example, consider array A such that:

A[0] = 3

A[1] = 1

A[2] = 2

A[3] = 4

A[4] = 3

We can split this tape in four places:

P = 1, difference = |3 - 10| = 7

P = 2, difference = |4 - 9| = 5

P = 3, difference = |6 - 7| = 1

P = 4, difference = |10 - 3| = 7

Write a function:

function solution(A);

that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.

For example, given:


A[0] = 3

A[1] = 1

A[2] = 2

A[3] = 4

A[4] = 3

the function should return 1, as explained above.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [2..100,000];

each element of array A is an integer within the range [-1,000..1,000].

Score - 84%

Solution - https://app.codility.com/demo/results/training72S2G5-TH3/

// you can write to stdout for debugging purposes, e.g.

// console.log('this is a debug message');

function solution(A) {

// write your code in JavaScript (Node.js 8.9.4)

// write your code in JavaScript (Node.js 8.9.4)

var sumright = 0;

var sumleft = 0;

var ans = 0;

for (var i =1;i<A.length;i++)

sumright += A[i];

sumleft = A[0];
ans =Math.abs(sumright + sumleft);

for (var P=1; P<A.length; P++)

if (Math.abs(sumleft - sumright) < ans)

ans = Math.abs(sumleft - sumright);

sumleft += A[P];

sumright -=A[P];

return ans;

//old code - Got 53%

//var length = A.length;

//var i;

//var j=1;

//var absoluteValue = -1;

//for(j=1; j<length; j++){

// var firstValue = 0;

// var secondValue = 0;

// for(i=0; i<length; i++){

// if(i < j){

// firstValue = firstValue + A[i];

// }

// else{

// secondValue = secondValue + A[i];

// }

// }

// //console.log(Math.abs(firstValue - secondValue));

// if( Math.abs(firstValue - secondValue) < absoluteValue || j==1 ){

// absoluteValue = Math.abs(firstValue - secondValue);

// }
//}

//return absoluteValue;

3. PermCheck

Check whether array A is a permutation.

https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/

Score - 75%

Solution - https://app.codility.com/demo/results/training6V86MP-N68/

https://app.codility.com/demo/results/trainingSJR8NF-76C/

function solution(A) {

// write your code in JavaScript (Node.js 8.9.4)

A.sort();

for(var i=1; i<=A.length; i++){

if(A[i-1] != i){

return 0;

return 1;

}
4. FrogRiverOne
Find the earliest time when a frog can jump to the other side of a river.

https://app.codility.com/programmers/lessons/4-counting_elements/
frog_river_one/

Score - 81%

Solution - https://app.codility.com/demo/results/trainingY3HJ5Y-C2V/
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(X, A) {
// write your code in JavaScript (Node.js 8.9.4)
let result = 0;
for(let i=1; i<=X; i++){
//console.log(A.indexOf(i) + '_' + i);
let index = A.indexOf(i);
if( index != -1){
if(index > result){
result = index;
}
}
else {
return -1;
}
}
return result;
}

MaxCounters

Calculate the values of counters after applying all alternating operations: increase counter by 1; set
value of all counters to current maximum.

https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/

Score - 100%

Solution - https://app.codility.com/demo/results/trainingY767U6-ETR/

function solution(N, A) {

// write your code in JavaScript (Node.js 8.9.4)

var max = 0;

var min =0;

let my_array = new Array(N + 1).join('0').split('').map(parseFloat);

for(let i=0; i<A.length; i++){


if( A[i] >= 1 && A[i] <= N){ //case 1

//console.log(my_array[ A[i] -1] );

if(my_array[ A[i] -1] < min){

my_array[ A[i] -1] = min;

my_array[ A[i] -1 ] ++;

if( my_array[ A[i] -1 ] > max){

max = my_array[ A[i] -1 ];

else if( A[i] == N+1){ // case 2

min = max;

//console.log(my_array.length);

for(let j=0; j<my_array.length; j++){

if(my_array[j] < min){

my_array[j] = min;

return my_array;

--------------------------------------------------------------------------------------------------------------------------------------

MissingInteger

Find the smallest positive integer that does not occur in a given sequence.

https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/
Score - 66%

Solution - https://app.codility.com/demo/results/trainingNE3NBX-H6H/

function solution(A) {

// write your code in JavaScript (Node.js 8.9.4)

A.sort();

let length = A.length + 1;

for(let i=1;i< length;i++){

if(A.indexOf(i) < 0){

return i;

return length;

PassingCars

Count the number of passing cars on the road

https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/

Score - 100%

Solution - https://app.codility.com/demo/results/trainingR3CTEK-7KU/

function solution(A) {

// write your code in JavaScript (Node.js 8.9.4)

let num_east = 0;

let num_pass = 0;

for(let i=0; i< A.length; i++){

if(A[i] ==0){ // to east


num_east++;

if(A[i] ==1){ // to west

num_pass = num_pass + num_east;

if(num_pass > 1000000000 || num_pass < 0)

return -1;

else

return num_pass;

You might also like