Skip to content

Instantly share code, notes, and snippets.

@haroldtreen
Created September 27, 2016 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haroldtreen/f01fc1108db690b33883f4e5c2431d2a to your computer and use it in GitHub Desktop.
Save haroldtreen/f01fc1108db690b33883f4e5c2431d2a to your computer and use it in GitHub Desktop.
function getValues(line) {
return line.split(' ').map((i) => Number(i));
}
function processData(input) {
var lines = input.split('\n');
var inputValues = getValues(lines[0]);
var array = getValues(lines[1]);
fastest(inputValues[0], inputValues[1], array);
}
function fastest(N, K, array) {
var set = array.reduce((obj, val) => {
obj[val] = true;
return obj;
}, {});
var count = array.reduce((count, val) => {
return set[val + K] ? count + 1 : count;
}, 0);
console.log(count);
return count;
}
function fast(N, K, array) {
var count = 0;
var sorted = array.sort((a, b) => a - b);
for (var j = 0; j < sorted.length; j++) {
for (var i = j + 1; i < sorted.length; i++) {
var diff = sorted[i] - sorted[j];
if (diff === K) {
count++;
break;
} else if(diff > K) {
break;
}
}
}
console.log(count);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment