Tasks Details
medium
Compute number of inversion in an array.
Task Score
100%
Correctness
100%
Performance
100%
An array A consisting of N integers is given. An inversion is a pair of indexes (P, Q) such that P < Q and A[Q] < A[P].
Write a function:
function solution(A);
that computes the number of inversions in A, or returns −1 if it exceeds 1,000,000,000.
For example, in the following array:
A[0] = -1 A[1] = 6 A[2] = 3 A[3] = 4 A[4] = 7 A[5] = 4there are four inversions:
(1,2) (1,3) (1,5) (4,5)so the function should return 4.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 13:54:53 UTC,
java,
autosave
Code: 13:55:02 UTC,
js,
autosave
var total = 0;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
mergeSort(A);
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Code: 13:55:16 UTC,
js,
autosave
var total = 0;
const LIMIT =
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
mergeSort(A);
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Code: 13:55:30 UTC,
js,
autosave
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
mergeSort(A);
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Code: 13:55:44 UTC,
js,
autosave
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
mergeSort(A);
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
if (total >)
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Code: 13:56:14 UTC,
js,
autosave
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
try {
mergeSort(A);
} catc
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
if (total > LIMIT) {
throw new Error('Limit exceeded');
}
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Code: 13:56:22 UTC,
js,
verify,
result: Passed
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
try {
mergeSort(A);
} catch (e) {
return -1;
}
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
if (total > LIMIT) {
throw new Error('Limit exceeded');
}
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Analysis
Code: 13:56:27 UTC,
js,
verify,
result: Passed
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
try {
mergeSort(A);
} catch (e) {
return -1;
}
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
if (total > LIMIT) {
throw new Error('Limit exceeded');
}
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Analysis
Code: 13:56:29 UTC,
js,
final,
score: 
100
var total = 0;
const LIMIT = 1000000000;
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
try {
mergeSort(A);
} catch (e) {
return -1;
}
return total;
}
function mergeSort(unsortedArray) {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const mid = parseInt(unsortedArray.length / 2);
const left = unsortedArray.slice(0, mid);
const right = unsortedArray.slice(mid);
return merge(
mergeSort(left),
mergeSort(right),
);;
}
function merge(left, right) {
const res = [];
let rightIndex = 0, leftIndex = 0;
let indif = 0;
while (rightIndex < right.length && leftIndex < left.length) {
if (left[leftIndex] <= right[rightIndex]) {
res.push(left[leftIndex]);
leftIndex++
} else {
res.push(right[rightIndex]);
rightIndex++;
const restInLeft = left.length - leftIndex;
total += restInLeft;
if (total > LIMIT) {
throw new Error('Limit exceeded');
}
}
}
return [...res, ...left.slice(leftIndex), ...right.slice(rightIndex)]
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N))
expand all
Correctness tests
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.080 s
OK
4.
0.068 s
OK
1.
0.072 s
OK
1.
0.072 s
OK