Problem:
Given a series of integers such that each integer have a pair, only 1 integer is without pair. Find it
Input Format:
String of integers separated by space
Output Format:
Integer without pair
Integer without pair
Constraints:
none
none
Sample Input
1 3 5 7 3 5 2 1 2
Sample Output:
7
7
Explanations:
7 occurs only once, all other appears 2 ie in pair
7 occurs only once, all other appears 2 ie in pair
Solution:
@arr=split(" ",$line2);
@arr=sort{$a<=>$b}@arr;
$len=@arr;
for($i=0;$i<$len;$i++)
{
if($arr[$i] != $arr[$i+1])
{
print "$arr[$i]";
exit;
}
$i++;
}
Tips:
Simple enough, sort the list and compare pairs.
No comments:
Post a Comment