I recently had to do a code challenge where I was instructed that for a set of numbers, find the number of pairs whose difference was K. For example, given then numbers 1, 5, 3, 4, 2
, and the difference K (2) there are 3 pairs: (5,3) (4,2) (3,1). I tried this challenge in PHP. My code passed the test, but was inefficient i guess because some of the test timed out. Can anyone tell me how I could have improved it? I'm bashing my head because I can't figure out how I could make it more efficient.
Here's my code
<?php
// Open STDIN for reading
$stdin = fopen('php://stdin', 'r');
// Get the input
while(!feof($stdin)) {
$inputs[] = explode(' ', fgets($stdin));
}
fclose($handle);
$k = $inputs[0][1];
$values = array_map('intval', array_values($inputs[1]));
// Sort in decending order
rsort($values);
// Given the difference, K, find a pair for $left within
// $right whose difference is K
function findPair($k, $left, $right){
foreach($right as $n) {
if($left - $n == $k)
return $n;
// If the difference is greater than $k, there is no pair
if($left - $n > $k)
return false;
}
return false;
}
$pairs = 0;
while(count($values) > 1){
$left = array_shift($values);
$n = findPair($k, $left, $values);
if($n !== false)
$pairs++;
}
echo $pairs;
?>
Your code have
O(n^2)
complexity - and so it will be inefficient on large data sets. It'sO(n^2)
since you're looping through all array withforeach
inside your function and calling it inwhile
in external loop.But you can easily do the stuff with
O(n x log(N))
:-so, you will use standard
sort()
withO(n log(n))
complexity and then use binary searchN
times. Binary search hasO(log(n)) complexity
, so loop complexity will be alsoO(n log (n))
. Therefore, whole code complexity will beO(n log(n)) + O(n log(n)) = O(n log(n))
.Note: standard PHP's
in_array()
hasO(N)
complexity, so using it will produceO(N^2)
complexity estimation for loop, and, therefore,O(N^2)
code complexity.Note: sorting via
sort()
will produce quick sorting. This algorithm hasO(n log(n))
average complexity, it's worst case isO(N^2)
- so there may be cases of data sets, for which code above may be also inefficient. You may look into other sorting algorithms. For example, if your problem is time limit, you may try merge sort - it will be extremely faster (but it will take additional space).Note: If we're speaking about time complexity and space complexity does not matter, it's just simple hash-map that can be used. In PHP it's just array:
-that will result in
O(n)
time complexity andO(2n)=O(n)
space complexity.