Created
September 27, 2016 21:25
-
-
Save vaibhavsagar/46f044eda78e89e26ee1583d010090ef to your computer and use it in GitHub Desktop.
hackerrank pairs code dojo with @sranso
This file contains 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 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