3v4l.org

run code in 300+ PHP versions simultaneously
<?php function numberOfPairs($a,$k){ $a=array_count_values($a); // enable use of the very fast isset() function and avoid iterating duplicates ksort($a); // order so that only the lower values need to be iterated foreach($a as $num=>$occur){ if(($double=$num*2)>=$k){ // we are at or past the middle if($double==$k && $occur>1) $result[]=[$num,$k-$num]; // addends are equal and 2 exist, store before break break; }elseif(isset($a[$k-$num])){ // matching addend found, store & continue $result[]=[$num,$k-$num]; } } var_export($result); } $a = [6,6,3,9,3,5,1]; $k = 12; numberOfPairs($a,$k);
Output for git.master, git.master_jit, rfc.property-hooks
array ( 0 => array ( 0 => 3, 1 => 9, ), 1 => array ( 0 => 6, 1 => 6, ), )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
173.06 ms | 405 KiB | 5 Q