22 September 2014

Quiz 32: Find symmetric point

Problem:
Given two points, find the symmetric point (C is symmetric point of A and B, if B is mid point of A and C)

Input Format: 
first line, T, contains number of test cases
T lines contains coordinates of 2 points A and B

Output Format: 
Each line, containing symmetric point of A and B

Constraints: 
none

Sample Input
2
0 0 2 2
1 1 3 3

Sample Output:
4 4
5 5

Explanations:
for first test case, 2,2 is midpoint of 4,4 and 0,0


Solution:

@out=();
chomp($t=<STDIN>);
for($i=0;$i<$t;$i++)
    {
    chomp($c=<STDIN>);
    @arr=split(" ",$c);
    $a=(2*$arr[2])-$arr[0];
    $b=(2*$arr[3])-$arr[1];
    push(@out,$a);
    push(@out,$b);
    }
$len=@out;
for($j=0;$j<$len;$j++)
    {
    print "$out[$j] $out[$j+1]\n";
    $j++;
    }


Tips:
Simple, C= 2B-1

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.

14 September 2014

Quiz 30: Find number of keys which need to get repaired

Problem:
Given number of broken keyboard keys and a string. Find how many keys need to be repair to type this string

Input Format: 
Broken keys without any spaces
String to be typed

Output Format: 
Number of keys which need to be repaired

Constraints: 
none

Sample Input
aspzq
happiness

Sample Output:
3

Explanations:
keys a,s and p need to be repaired


Solution:

chomp($n=<STDIN>);
@br=split("",$n);
$len=@br;
$count=0;
chomp($str=<STDIN>);
@arr=split("",$str);
$len1=@arr;
for($j=0;$j<$len;$j++)
{
if($str =~ /$br[$j]/)
{
$count++;
}
}
print "$count";


Tips:
To type p two times, key which need to repaired is key p, so count will not be doubled.

Quiz 29: Find number of people with weight above average weight

Problem:
Given weight of N number of people, find number of people whose weight is above average weight

Input Format: 
N followed by N weights

Output Format: 
Number of people whose weight is above average

Constraints: 
none

Sample Input
5 42 50 54 58 46

Sample Output:
2

Explanations:
Average is 50 and only 54 & 58 are above average, so answer is 2


Solution:

chomp($n=<STDIN>);
@arr=split(" ",$n);
$len=$arr[0];
$first=shift(@arr);
$sum=0;
foreach(@arr)
{
$sum+=$_;
}
$av=$sum/$n;
@arr=sort{$a<=>$b}(@arr);
$count=0;
for($j=$n-1;$j>=0;$j--)
{
if($arr[$j]>$av)
{
$count++;
}
else
{
last;
}
}
print "$count";


Tips:
Shift is used to take out first element of array

Quiz 28: Find minimum distance to reach the border of rectangle

Problem:
Given a current position and diagonally opposite coordinates of a  rectangle. Find the minimum distance to reach border of rectangle from current position

Input Format: 
String x y x1 y1 x2 y2 where x,y is current position, x1,y1 and x2,y2 are opposite coordinates of a rectangle.

Output Format: 
Minimum distance

Constraints: 
none

Sample Input
1 3 -4 -4 5 5

Sample Output:
2

Explanations:-
Minimum distance is 1,3 to 1,5 ie 2


Solution:

chomp($n=<STDIN>);
@br=split(" ",$n);
$d=$br[0]-$br[2];
if($d<0)
{
$d=$d * -1;
}
$tmp=$br[0]-$br[4];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
$tmp=$br[1]-$br[3];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
$tmp=$br[1]-$br[5];
if($tmp<0)
{
$tmp=$tmp * -1;
}
if($d>$tmp)
{
$d=$tmp;
}
print "$d";


Tips:
Just find the distance from all coordinates and select minimum

Quiz 27: Find the next bigger lexicographically string

Problem:
Given a String, find the next bigger lexicographically string.

Input Format: 
String

Output Format: 
next bigger lexicographically string
 

Constraints: 
none

Sample Input
zcfedba

Sample Output:
zdabcef

Explanations:-
when compare characters from right to left, c < f, next bigger available character to c is d and rest series is sorted order of remaining characters.


Solution:

chomp($str=<STDIN>);
@arr=split("",$str);
$len=@arr;
$count=1;
for($j=$len-1;$j>0;$j--)
{
$count++;
$c1=ord($arr[$j]);
$c2=ord($arr[$j-1]);
if($c1>$c2)
{
@r=splice(@arr,$j-1,$count);
@s=sort(@r);
for($m=0;$m<@s;$m++)
{
$c3=ord($s[$m]);
$c4=ord($r[0]);
$c5=ord($s[$m+1]);
if($c3 == $c4 and $c3!=$c5)
{
$arr[$j-1] = $s[$m+1];
@s1=splice(@s,$m+1,1);
push(@arr,@s);
last;
}
}
last;
}
}
$ans=join("",@arr);
print "$ans";


Tips:
Splice is used to remove some elements of array and store them somewhere else.

Quiz 26: Minimum time for all rats to hide

Problem:
There are n rats and n holes in a straight line. Each hole can accommodate only 1 rat. A rat may move to its right or left. To move 1 block, he needs 1 minute. Find minimum time required for all rats to occupy a hole from their current position.

Input Format: 
Line1: position of rats separated by space
Line2: position of holes separated by space

Output Format: 
minimum time required

Constraints: 
none

Sample Input
1 2 7
0 8 9

Sample Output:
6

Explanations:-
Rat and hole initial positions: O R R _ _ _ _ R O O
So rat 1 will move 1 position left, rat 2 will move 6 position right and rat 3 will move 2 position right, so minimum time for all rats to occupy holes is 6


Solution:

chomp($mouse=<STDIN>);
@arrm=split(" ",$mouse);
chomp($holes=<STDIN>);
@arrh=split(" ",$holes);
@arrh1=sort{$a<=>$b}@arrh;
@min=();
@h=();
@arrm1=sort{$a<=>$b}@arrm;
$c=0;
for($m=0;$m<$n;$m++)
{
if($arrm1[$m] == $arrh1[$m])
{
$c++;
}
}
$diff=0;
$n1=$n;
@arrm=sort{$a<=>$b}@arrm;
$k=0;
foreach(@arrm1)
{
if($c == $n)
{
push(@min,$diff);
}
$diff1 = $_ - $arrh1[$k];
$k++;
if($diff1<0)
{
$diff1 = $diff1 * -1;
}
push(@min,$diff1);
}
@ans=sort{$a<=>$b}@min;
print "$ans[$n-1]";



Tips:
Sort holes and rat positions and max(h(i)-r(i)) is the answer