Find maximum product of two integers
EASY
Given an input string with numbers separated by a single space.
Find a maximum product of two integers in this line.
INPUT:
string with integers; minimum count of integers = 2
OUTPUT:
integer; maximum product of two numbers
Solution
<?php
$line = trim(fgets(STDIN));
$nums = explode(' ', $line);
$max = $nums[0];
foreach($nums as $i => $num1) {
foreach($nums as $j => $num2) {
if ($i === $j) {
continue;
}
$product = $num1 * $num2;
if ($product > $max) {
$max = $product;
}
}
}
echo $max;
Tests
Test #1 
Loading...

Test #2 
Loading...

Test #3 
Loading...

Test #4 
Loading...

Test #5 
Loading...
