Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Created September 27, 2016 21:25
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 vaibhavsagar/46f044eda78e89e26ee1583d010090ef to your computer and use it in GitHub Desktop.
Save vaibhavsagar/46f044eda78e89e26ee1583d010090ef to your computer and use it in GitHub Desktop.
hackerrank pairs code dojo with @sranso
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
function processData(input) {
function pair(k, set) {
var count = 0;
function countPair(value, _value, _set) {
if ( set.has(value + k) ) {
count += 1;
}
}
set.forEach(countPair);
return count;
}
function toInt(value, _index, _array) {
return parseInt(value)
};
var split = input.split('\n');
var k = parseInt(split[0].split(' ')[1]);
var inputSet = new Set(split[1].split(" ").map(toInt));
var result = pair(k, inputSet);
return result;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
console.log(processData(_input));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment