16 September 2014

Quiz 31: Find the integer without pair

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

Constraints: 
none

Sample Input
1 3 5 7 3 5 2 1 2

Sample Output:
7

Explanations:
7 occurs only once, all other appears 2 ie in pair


Solution:

chomp($line2=<STDIN>);
@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