A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.
Array A contains only 0s and/or 1s:
- 0 represents a car traveling east,
- 1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
function solution(A);
that, given a non-empty array A of N integers, returns the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
For example, given:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer that can have one of the following values: 0, 1.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = carNum + 1; i < A.length - 1; i++) {
if (A[carNum] !== A[i]) {
count++
}
}
}
return count;
}
[0, 0, 0, 1, 1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (let i = carNum + 1; i < A.length - 1; i++) {
cons
}
for (carNum in A) {
for (let i = carNum + 1; i < A.length - 1; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (let i = carNum + 1; i < A.length - 1; i++) {
console.log(i);
}
for (carNum in A) {
for (let i = carNum + 1; i < A.length - 1; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
ReferenceError: carNum is not defined at solution (solution.js:7:18) at solutionWrapper (/tmp/exec_user_fjkup6av/exec.js:402:28) at /tmp/exec_user_fjkup6av/exec.js:428:24
ReferenceError: carNum is not defined at solution (solution.js:7:18) at solutionWrapper (/tmp/exec_user_fjkup6av/exec.js:402:28) at /tmp/exec_user_fjkup6av/exec.js:428:24
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (let i = 1; i < A.length - 1; i++) {
console.log(i);
}
for (carNum in A) {
for (let i = carNum + 1; i < A.length - 1; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
1 2 3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = carNum + 1; i < A.length; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = carNum + 1; i < A.length; i++) {
console.log(carNum, i)
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 01 0 2 0 3 0 4
function result: 3
0 01 0 2 0 3 0 4
function result: 3
0 01 0 2 0 3 0 4
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = ScarNum + 1; i < A.length; i++) {
console.log(carNum, i)
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i)
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 2 0 3 0 4 1 2 1 3 1 4 2 3 2 4 3 4
function result: 6
0 1 0 2 0 3 0 4 1 2 1 3 1 4 2 3 2 4 3 4
function result: 6
0 1 0 2 0 3 0 4 1 2 1 3 1 4 2 3 2 4 3 4
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i,)
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 1 2 1 0 1 3 1 1 1 4 1 1 2 3 0 1 2 4 0 1 3 4 1 1
function result: 6
0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1 3 4 1 1
function result: 6
0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0 3 4 0 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter()
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(A[0])
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(direction => direction === A[0]);
for (carNum in A) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(direction => direction === A[0]);
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 1 2 1 0 1 3 1 1 1 4 1 1
function result: 6
0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(direction => direction === A[0]);
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(direction => direction === A[0]);
console.log(basicDirectionCars)
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
[ 0, 0 ] 0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 1 2 1 0 1 3 1 1 1 4 1 1
function result: 6
[ 0, 0, 0 ] 0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
[ 1, 1, 1 ] 0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCars = A.filter(direction => direction === A[0]);
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter(direction => direction === A[0]);
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => direction === A[0]);
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
return index;
}
});
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCars) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
ReferenceError: basicDirectionCars is not defined at solution (solution.js:15:20) at solutionWrapper (/tmp/exec_user_osi_x6nc/exec.js:402:28) at /tmp/exec_user_osi_x6nc/exec.js:428:24stdout:
[ 0 ]
ReferenceError: basicDirectionCars is not defined at solution (solution.js:15:20) at solutionWrapper (/tmp/exec_user_osi_x6nc/exec.js:402:28) at /tmp/exec_user_osi_x6nc/exec.js:428:24stdout:
[ 0, 0 ]
ReferenceError: basicDirectionCars is not defined at solution (solution.js:15:20) at solutionWrapper (/tmp/exec_user_osi_x6nc/exec.js:402:28) at /tmp/exec_user_osi_x6nc/exec.js:428:24stdout:
[ 1, 1 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
[ 0 ] 0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1
function result: 4
[ 0, 0 ] 0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1
function result: 4
[ 1, 1 ] 0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
console.log()
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = A.filter((direction, index) => {
if (direction === A[0]) {
console.log(direction)
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 0 [ 0 ] 0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1
function result: 4
0 0 0 [ 0, 0 ] 0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1
function result: 4
1 1 1 [ 1, 1 ] 0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums =
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
console.log(direction)
return index;
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index;)
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
console.log(basicDirectionCarNums)
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
[ 0, 2 ] 0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 1 2 1 0 1 3 1 1 1 4 1 1
function result: 6
[ 0, 1, 2 ] 0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
[ 0, 1, 2 ] 0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum in basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
[0, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 0 0 0 2 0 0 0 3 0 1 0 4 0 1 1 2 0 0 1 3 0 1 1 4 0 1 2 3 0 1 2 4 0 1
function result: 6
0 1 1 1 0 2 1 1 0 3 1 0 0 4 1 0 1 2 1 1 1 3 1 0 1 4 1 0 2 3 1 0 2 4 1 0
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
console.log(carNum, i, A[carNum], A[i])
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
0 1 0 1 0 2 0 0 0 3 0 1 0 4 0 1 2 3 0 1 2 4 0 1
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
let count = 0;
const basicDirectionCarNums = [];
A.forEach((direction, index) => {
if (direction === A[0]) {
basicDirectionCarNums.push(index);
}
});
for (carNum of basicDirectionCarNums) {
for (let i = Number.parseInt(carNum) + 1; i < A.length; i++) {
if (A[carNum] !== A[i]) {
count++;
}
}
}
return count;
}
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [1, 0] the solution returned a wrong answer (got 1 expected 0).
random, length = ~10,000
running time: 3.560 sec., time limit: 0.256 sec.
0..01..1, length = ~100,000
Killed. Hard limit reached: 6.000 sec.
0101..01, length = ~100,000
Killed. Hard limit reached: 6.000 sec.
large test with all 1s/0s, length = ~100,000
Killed. Hard limit reached: 6.000 sec.