Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Count Inversion by flairplaya
"use strict";
function countInversion(sequence){
var x = 0;
for(var i = 0, l = sequence.length; i < l; i++) {
for(var j = i + 1; j < l; j++) {
sequence[i] > sequence[j] ? x++ : x;
}
}
return x;
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(countInversion([1, 2, 5, 3, 4, 7, 6]), 3, "Example");
assert.equal(countInversion([0, 1, 2, 3]), 0, "Sorted");
assert.equal(countInversion([99, -99]), 1, "Two numbers");
assert.equal(countInversion([5, 3, 2, 1, 0]), 10, "Reversed");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
July 3, 2018