Swap Two Numbers (No Temp)
PHP
Easy
4 views
Problem Description
Two integers a and b are given. Swap them without using a third variable and print the result.
Input Format
One line: a b.
Output Format
One line: swapped_a swapped_b.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b]=array_map('intval',preg_split('/\\s+/', $inputText));
$a=$a+$b; $b=$a-$b; $a=$a-$b;
echo $a.' '.$b;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!