Created
March 6, 2014 01:00
-
-
Save james-ff/9380138 to your computer and use it in GitHub Desktop.
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
<?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