Find all combinations of pairs of elements
EASY
Given a string of unique (non-repeating) numbers separated by a single space.
Find all combinations of pairs of elements and print it in any order.
Solution
<?php
$line = trim(fgets(STDIN));
$nums = explode(' ', $line);
foreach ($nums as $i => $num1) {
foreach ($nums as $j => $num2) {
if ($i === $j) {
continue;
}
echo $num1 . ' ' . $num2 . PHP_EOL;
}
}
Tests
Test #1 |
Loading...
|
Test #2 |
Loading...
|
Test #3 |
Loading...
|
Test #4 |
Loading...
|