Skip to content

Instantly share code, notes, and snippets.

@james-ff
Created March 6, 2014 01:00
Show Gist options
  • Save james-ff/9380138 to your computer and use it in GitHub Desktop.
Save james-ff/9380138 to your computer and use it in GitHub Desktop.
<?php
$input_data = array();
$i = 0;
while (($line = fgets(STDIN)) !== false) {
$input_data[$i] = explode(" ", $line);
$i++;
}
$k = $input_data[0][1];
$numbers = $input_data[1];
$hashMap = array();
$pairs = 0;
foreach ($numbers as $number) {
$upper_difference = $number + $k;
$lower_difference = $number - $k;
if ($hashMap[$upper_difference] == 1) {
$pairs++;
}
if ($hashMap[$lower_difference] == 1) {
$pairs++;
}
$hashMap[$number] = 1;
}
echo $pairs;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment