settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Magic Sum - PHP Array Task

+2 votes

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

asked in PHP category by user mitko

1 Answer

+1 vote

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;
        }
    }
}
answered by user nikole
...