[Solved] Magic Sum - PHP Array Task

  

5
Topic starter

Write a program, which prints all unique pairs in an array of integers whose sum is equal to a given number.

Examples:

magic sum php array task

1 Answer
4

My solution:

<?php
 
$array = array_map('intval', explode(' ', readline()));
$number = intval(readline());
$count = count($array);
 
for ($i = 0; $i < $count; $i++) {
    for ($j = $i + 1; $j < $count; $j++) {
        if ($array[$i] + $array[$j] == $number) {
            echo $array[$i] . ' ' . $array[$j] . PHP_EOL;
        }
    }
}
Share: