Created
September 27, 2016 22:14
-
-
Save haroldtreen/f01fc1108db690b33883f4e5c2431d2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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