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
Code: 14:11:05 UTC,
java,
autosave
Code: 14:11:15 UTC,
js,
autosave
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Code: 14:11:37 UTC,
js,
autosave
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Code: 14:11:40 UTC,
js,
verify,
result: Passed
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Analysis
Code: 14:12:04 UTC,
js,
autosave
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Code: 14:12:18 UTC,
js,
autosave
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Code: 14:12:33 UTC,
js,
verify,
result: Passed
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
User test case 1:
[]
User test case 2:
[1, 2, 3, 4, 5, 6]
User test case 3:
[6, 5, 4, 3, 2, 1]
User test case 4:
[-2147483648, 0, 2147483647]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 15
function result: 15
1.
0.068 s
OK
function result: 0
function result: 0
Code: 14:12:46 UTC,
js,
autosave
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
Code: 14:12:54 UTC,
js,
verify,
result: Passed
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
User test case 1:
[]
User test case 2:
[1, 2, 3, 4, 5, 6]
User test case 3:
[6, 5, 4, 3, 2, 1]
User test case 4:
[-2147483648, 0, 2147483647]
User test case 5:
[2147483647, 0, -2147483648]
Analysis
Code: 14:12:59 UTC,
js,
verify,
result: Passed
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
User test case 1:
[]
User test case 2:
[1, 2, 3, 4, 5, 6]
User test case 3:
[6, 5, 4, 3, 2, 1]
User test case 4:
[-2147483648, 0, 2147483647]
User test case 5:
[2147483647, 0, -2147483648]
Analysis
Code: 14:13:02 UTC,
js,
final,
score: 
100
// 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)
return mergeSortAndCount(A, 0, A.length - 1)
}
function mergeSortAndCount(A, start, end) {
let count = 0;
if (start < end) {
let mid = parseInt((start + end) / 2);
count += mergeSortAndCount(A, start, mid);
count += mergeSortAndCount(A, mid + 1, end);
count += mergeAndCount(A, start, mid, end);
if (count > 1000000000) {
return -1;
}
}
return count;
}
function mergeAndCount(A, start, mid, end) {
let left = A.slice(start, mid + 1);
let right = A.slice(mid + 1, end + 1);
let i = 0;
let j = 0;
let k = start;
let swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
A[k++] = left[i++];
} else {
A[k++] = right[j++];
swaps += (mid + 1) - (start + i)
}
}
while (i < left.length) {
A[k++] = left[i++];
}
while (j < right.length) {
A[k++] = right[j++]
}
return swaps;
}
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.072 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.072 s
OK
1.
0.068 s
OK