## 22 August 2014

### Quiz 18: Find number of chocolates in exchange of wrappers

Problem:
A boy have $N money in his pocket. The price of each chocolate is$C. The store offers a discount: for every M wrappers he gives to the store, he gets one chocolate for free. How many chocolates does boy get to eat?

Input Format:
The first line contains the number of test cases T(<=1000).
T lines follow, each of which contains three integers N, C and M

Output Format:
Print the total number of chocolates Boy eats.

Constraints:
2N105

1CN
2MN

Sample Input
3
6 2 2
8 3 4
20 2 2

Sample Output:
5
2
19

Explanations:-
case 1, With 6$, he gets 3 chocolates. So he have 3 wrappers. He exchange 2 wrappers out of 3 to get 4th chocolate and now he have a new wrapper from the 4th chocolate, so total wrappers are 2. He again exchanges them and get 5th chocolate. So answer is 5. Solution: chomp($t=<STDIN>);
@output=();
if($t>1000) { exit; } for($i=0;$i<$t;$i++) { chomp($line=<STDIN>);
@arr=split(" ",$line); if($arr[0]<2 or $arr[0]>100000 or$arr[1]<1 or $arr[1]>$arr[0] or $arr[2]<2 or$arr[2]>$arr[0]) { exit; }$c=$arr[0]/$arr[1];
$c= int$c;
$w=$c;
while($w >=$arr[2])
{
$c1=$w/$arr[2];$c1= int $c1;$c= $c+$c1;
$w=$w%$arr[2]+$c1;
}
push(@output,$c); } foreach(@output) { print "$_\n";
}

Tips:
Dont forget to round off number of chocolates, like with$7, if cost of each chocolate is$2, you will get 3 chocolates and not 3.5

### Quiz 17: Find average number of balls in each jar

Problem:
There are N empty candy jars, numbered from 1 to N, with infinite capacity. M operations are performed. Each operation is described by 3 integers a, b and k. Here, a and b are index of the jars, and k is the number of candies to be added inside each jar whose index lies between a and b (both inclusive). Can you tell the average number of candies after M operations?

Input format
The first line contains two integers N and M separated by a single space.
M lines follow. Each of the M lines contain three integers a, b and k separated by single space.

Output Format
A single line containing the average number of candies across N jars, rounded down to the nearest integer.

Note
Rounded down means finding the greatest integer which is less than or equal to given number. Eg, 13.65 and 13.23 is rounded down to 13, while 12.98 is rounded down to 12.

Constraints
3 <= N <= 10000000
1 <= M <= 100000
1 <= a <= b <= N

0 <= k <= 1000000

Sample Input
4 2
1 4 100
2 3 50

Sample Output:
125

Explanations:-
balls initially in 4 jars 0,0,0,0
1st operation -> 100,100,100,100
2nd operation->100,150,150,100
average = 500/4=125

Solution:

chomp($line1 =<STDIN>); @line = split(" ",$line1);
$n =$line[0];
$m =$line[1];
if($n<3 or$n>10000000 or $m<1 or$m>100000)
{
exit;
}
$output = 0;$tmp=0;
for($i=0;$i<$m;$i++)
{
chomp($line2 = <STDIN>); @op = split(" ",$line2);
if($op[0]<1 or$op[0]>$op[1] or$op[1]<1 or $op[1]>$n or $op[2]<0 or$op[2]>1000000)
{
exit;
}
$tmp=$op[2]*($op[1]-$op[0]+1);
$output=$output+$tmp; }$output = $output/$n;
$output = int$output;
print "$output"; Tips: Use int$string to round off to nearest interger, note 6.67 will return 6 and not 7.

## 21 August 2014

### Quiz 16: Find if a string can be converted to palindrome or not

Problem:
There is a string having lower case english alphabets only. Find out if the string can be changed into palindrome by rearranging alphabets.

Constraints
1<=length of string<=100000
String contains only lower case english language alphabets

Sample Input
poiuytrecwkqgqlpowieurytaaaxxckla

Sample Output:
poiuytrecwkqgqlpowieurytaaaxxckla can be converted to palindrome

Explanations:-
String can be changed to "poiuytrecwkqlaaxgxaalqkwrectyuiop" which is a palindrome

Solution:

chomp($a =<STDIN>); @output;$odd=0;
@arr = split("",$a); @arr = sort(@arr);$len = @arr;
if($len<1 or$len > 100000)
{
exit;
}
foreach(@arr)
{
$ascii = ord($_);
if($ascii<97 or$ascii > 122)
{
exit;
}
}
my $tmp = 1; for(my$i=0;$i<@arr;$i++)
{
if($arr[$i] eq $arr[$i+1])
{
$tmp++; } else{ push(@output,$tmp);
$tmp = 1; } } for(my$j=0;$j<@output;$j++)
{
if($output[$j] % 2 == 0)
{
}
else
{
$odd++; } if($odd > 1)
{
print "$a cannot be converted to palindrome"; exit; } } print "$a can be converted to palindrome";

Tips:
Each alphabet should occur even number of times, only 1 alphabet can occur odd number of times which can be in middle of string.

## 20 August 2014

### Quiz 15: Find all possible values for last ball

Problem:
There are N number of balls having numbers on them. Number of 1st ball is 0. Number of next ball is either +a or +b then previous ball. Number of next ball is again either +a o +b then previous ball. Find all possible values for last ball(in increasing order) seperated by space
Input is in form of 3 lines
N= no of balls
a
b

Constraints
1<=N,a,b<=15
a != b

Sample Input
4
100
10

Sample Output:
30 120 210 300

Explanations:-
possible series
0 10 20 30
0 10 20 120
0 10 110 120
0 10 110 210
0 100 110 120
0 100 110 210
0 100 200 210
0 100 200 300
so last ball number can be 30,120,210 or 300

Solution:

@output = ();
chomp($n=<STDIN>); if($n<1 or $n>1000) { exit; } chomp($a=<STDIN>);
if($a<1 or$a>1000)
{
exit;
}
chomp($b=<STDIN>); if($b<1 or $b>1000) { exit; } if($a == $b) { exit; }$o1 = $a*($n-1);
$t1=$b*($n-1);$x=($t1-$o1)/($n-1); for($j=0;$j<$n;$j++) {$o2=$o1+($x*$j); push(@output,$o2);
}
@output = sort {$a <=>$b}(@output);
foreach(@output)
{
print "$_ "; } Tips: There will be N possible values ranging from a*(N-1) to b*(N-1) having equal difference. To sort numerically, use sort {$a <=> $b}(@output); ### Quiz 14: Find number of positions where digit divides the number Problem: Given a number like 1234, you need to find number of digits from number which divides the number. Like 1234 when divided by 1,2,3,4->exact division is by digits 1,2 only, so output should be 2. Input is in form of T, where T is number of test cases followed by numbers N Constraints 1<=T<=15 0<N<10000000000 Sample Input: 3 121 123456789 12021 Sample Output: 2 3 2 Explanations:- T = 3, so 3 test cases. For 121, digits are 1,2,1->out of these only 1,1 exactly divides 121, so output is 2. Solution: chomp($t=<STDIN>);
if($t<1 or$t>15)
{
exit;
}
$tmp = 0; @output = (); for($i=0;$i<$t;$i++) #loop for each test case { chomp($a=<STDIN>);
if($a<=0 or$a>=10000000000)
{
exit;
}
@arr=split("",$a); for($j=0;$j<@arr;$j++)
{
if($arr[$j] == 0)                    #skipping %0 cases
{
next;
}
elsif($a%$arr[$j] == 0) #checking for division {$tmp++;
}
}
push(@output,$tmp);$tmp = 0;
}
foreach(@output)
{
print "$_\n"; } Tips: Take care of modulus with 0->it will give runtime error ## 16 August 2014 ### Quiz 13: Find minimum number of steps to convert a string into palindrome Problem: A Palindrome is a string with is exactly same as its reverse string like abcba. Given a string containing only alphabets from a-z, we have to find minimum number of steps required to convert the string to palindrome. allowed operations: - any alphabet can be reduced by 1 lower alphabet in a step like d can be reduced to c - alphabet a cannot be reduced further Sample Input: abcd Sample Output: 4 Explanations:- step1-> abcd -> abcc step2->abcc->abcb step3->abcb->abca step4->abca->abba-> so 4 steps are required Solution: chomp($a=<STDIN>);   #take input
@arr=split("",$a); #convert string to array$len = @arr;   #get length of array
$tmp = 0; #this variable will store our count for($i=0;$i<$len/2;$i++) {$a = ord($arr[$i]);   #ord will get ascii value
$b = ord($arr[$len-$i-1]);
if($a>$b)   #compare characters
{
$tmp =$tmp + $a -$b;
}
elsif($a<=$b)
{
$tmp =$tmp + $b -$a;
}
}
print "$tmp"; Tips: • compare 1st and last character, then 2nd and last-2 and so on. ### Quiz 12: Find number of elements common to all chemicals Problem: An element may be represented by any lower case alphabet from a-z. A chemical composition may contains one or more than one elements like a, aaz, abcddyy can be chemical composition Input: A number N which represents number of chemicals followed by composition of chemicals Output: Number of elements which are common to all given chemicals Sample Input: 4 agmagmdddz werfkjabbfmgaap mgaaaqqqqqqagm uaimpg Sample Output: 3 constraints: 1<=N<=100 compositions can contain only lower case alphabets 1<=length of chemical composition<=100 Explanations:- 3 elements a,g and m are present in all chemical compositions, so output is 3 Solution: chomp($n=<STDIN>);
if($n<1 or$n>100)
{
exit;
}
@arr1=();
@arr3=();
$count = 0; @arr6 = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0); for($i=0;$i<$n;$i++) { chomp($comp=<STDIN>);
@arr1 = split("",$comp); if(@arr1 < 1 or @arr1 > 100) { exit; } foreach(@arr1) {$asc=ord("$_"); if($asc < 97 or $asc > 122) { exit; } }$all = "abcdefghijklmnopqrstuvwxyz";
$a =$all.$comp; @arr2 = split("",$a);
@arr2 = sort(@arr2);
$a = join("",@arr2); my @arr = split("",$a);
my $tmp = 0; for(my$i=0;$i<@arr;$i++)
{
if($arr[$i] eq $arr[$i+1])
{
if($tmp < 1) {$tmp++;
}
}
else{
push(@arr3,$arr[$i].$tmp);$tmp = 0;
}
}
$c = ":"; push(@arr3,$c);
}
$b = join("",@arr3); @arr4 = split(/:/,$b);
foreach(@arr4)
{
@arr9 = split(/\D/,$_); for ($i=0;$i<@arr6;$i++){
$arr6[$i] = $arr6[$i] + $arr9[$i+1];
}
}
foreach(@arr6)
{
if($_ eq$n)
{
$count++; } } print "$count";

Tips:
• ord is used to get ascii values

## 15 August 2014

### Quiz 11: Service lane problem

Problem:
There is a Highway 20 km long. Along the highway, there are 20 service lanes numbered 1-20 and each service lane is 1 km long. Width of service lane can be either 1,2 or 3.
Our data of width is in form of array: 1 2 2 2 3 3 3 1 1 2 2 3 3 3 1 1 2 2 3 3
ie service lane 1 have width 1 and service lane 20 have width 3.
Now a vehicle can exit highway from any service lane and enter highway again from any lane, but we have a width constraint:
-- if width = 1, only bike can enter/exit that service lane.
-- if width = 2, bike or car can enter/exit that service lane.
-- if width = 3, bike, car or truck can enter/exit that service lane.

Input: Entering service lane and exiting service lane
Output: which type of vehicle can pass thru it

Sample Input:
4 6

Sample Output:
Only bike or car can enter by service lane 4 and exit by service lane 6

Explanations:- width of service lane 4,5,6 are 2,3,3--> so truck cannot enter by service lane 4

Solution:

@arr2 = qw(1 2 2 2 3 3 3 1 1 2 2 3 3 3 1 1 2 2 3 3);
@arr4 = ();
chomp($str3 =<STDIN>); @arr3 = split(" ",$str3);
if($arr3[0]<1 or$arr3[1]<=$arr3[0] or 20<$arr3[1])
{
exit;
}
for($j=$arr3[0]-1;$j<=$arr3[1]-1;$j++) { unshift(@arr4,$arr2[$j]); } @arr4 = sort(@arr4); if ($arr4[0] == 1)
{
print "Only Bike can enter from service lane $arr3[0] and exit from service lane$arr3[1]";
}
elsif ($arr4[0] == 2) { print "Only Bike and Car can enter from service lane$arr3[0] and exit from service lane $arr3[1]"; } else { print "Bike,Car or Truck can enter from service lane$arr3[0] and exit from service lane $arr3[1]"; } Tips: • if input is 4 6, solution is the minimum value of width of service lane 4 to 6. ## 13 August 2014 ### Quiz 10: Find height of Tree after Nth cycle Problem: There is a tree (initial height- 1mt) which grows twice its current length in 1st cycle and grows by 1 mt in 2nd cycle and then again twice its current length in 3rd cycle and by 1mt in 4th cycle and so on. Find the height after Nth cycle. Input is in form of T and N(i), where T is number of input and N(i) is the cycle number for which height is required. Also, 0<=T<=10 and 0<=N<=30 Sample Input: 2 0 3 Sample Output: 1 6 Explanations:- T=2, so we need to find height after 0th cycle and after 3rd cycle Solution:$T = <STDIN>;
if($T<0 or$T>10)
{
exit;
}
@arr;
$h=0; for(my$i=0;$i<$T;$i++) {$arr[$i] = <STDIN>; if($arr[$i]<0 or$arr[$i]>30) { exit; } } for(my$j=0;$j<@arr;$j++)
{
for(my $k=0;$k<=$arr[$j];$k++) { if($k%2 == 0)
{
$h =$h + 1;
}
else{
$h = 2 *$h;
}
}
print "$h\n";$h=0;
}

Tips:
• % is modulus, used to find if number is even or odd

## 12 August 2014

### Quiz 9: 500 closed doors problem

Problem:
There are 500 closed doors along a corridor, numbered from 1 to 500. A person walks through the corridor and opens each door. Another person walks through the corridor and closes every alternate door. Continuing in this manner, the i-th person comes and toggles the position of every i-th door starting from door i. You are to determine exactly how many doors are open after the 500-th person has walked through the corridor.

Sample Input:
500

Sample Output:
no of doors opened after 500-th person walked through the corridor having 500 doors is 22

Solution:

$num = 500; @arr; for(my$i=0;$i<$num;$i++) {$arr[$i] = 0; } for(my$i=0;$i<$num;$i++) { for(my$j=$i;$j<$num;$j++)
{
if($arr[$j] == 0)
{
$arr[$j]=1;
}
elsif($arr[$j] == 1)
{
$arr[$j]=0;
}
$j =$j + $i; } }$tmp = 0;
foreach(@arr){
if($_ == 1) {$tmp++;
}
}
print "no of doors opened after $num-th person walked through the corridor having$num doors is $tmp"; Tips: • Simple solution is nearest square root ie square root of 500 is 22.36, so answer is 22 ### Quiz 8: Find if a number is a perfect square without using inbuilt functions like sq root Problem: Find if a number is a perfect square without using inbuilt functions like sq root Sample Input: 999998000001 Sample Output: 999998000001 is a perfect square Solution: print "Enter the number\n";$num = <STDIN>;
chomp($num);$a = 1;
$b =$num;
$tmp=0; while($b > 0)
{
$b =$b-$a;$a = $a + 2; if($b == 0)
{
print "$num is a Perfect square";$tmp++;
}
}
if($tmp == 0) { print "$num is not a perfect square";
}

Tips:
• Try to understand the logic by taking a smaller value like 64 and finding a and b in while loop.

## 11 August 2014

### Quiz 7: Expand the string- Alphabet followed by number to alphabet only

Problem:
Expand the string- Alphabet followed by number to alphabet only

Sample Input:
a2b11c3d5

Sample Output:
aabbbbbbbbbbbcccddddd

Solution:

my $a = "a2b11c3d5"; my @arr = split(/(\d+)/,$a);
for(my $i=0;$i<@arr;$i++) { for(my$j=0;$j<$arr[$i+1];$j++)
{
print "$arr[$i]";
}
$i++; } Tips: • parenthesis() preserves digits also, without it split will only return alphabets ### Quiz 6: Print alphabet followed by number of times it appears continuously in a string. Problem: Print alphabet followed by number of times it appears continuously in a string Sample Input: aaabbccccddde Sample Output: a3b2c4d3e1 Solution: my$a = "aaabbccccddde";
my @arr = split("",$a); my$tmp = 1;
for(my $i=0;$i<@arr;$i++) { if($arr[$i] eq$arr[$i+1]) {$tmp++;
}
else{
print "$arr[$i]$tmp";$tmp = 1;
}
}

Tips:
• remember to set tmp variable as 1 whenever a new character is found ex char b after char a.

## 10 August 2014

### Quiz 5: Print Fibonacci series till N numbers.

Problem:
Print Fibonacci series till N numbers.

Sample Input:
5

Sample Output:
First 5 elements of Fibonacci series are: 1 1 2 3 5

Solution:

my $a = 1; my$b = 0;
my $c = 0; print "Enter the number of elements you want in fibonaaci series\n"; my$num = <STDIN>;
chomp($num); print "First$num elements of fibonacci series : ";
for (my $i=1;$i <= $num;$i++)
{
$c =$a + $b; print "$c ";
$a =$b;
$b =$c;
}

Tips:
• print "$c ", here a space character is used to print all elements in 1 line and separated by a space. ### Quiz 4: Search for a string in an array. Problem: Search for a string in an array. Sample Input: Array passed as argument like sun mon tue wed thu fri sat. String to search: wed Sample Output: wed is found at 4 position Solution: my$tmp = 0;
my $found = 0; print "Enter the string you need to find\n"; my$str = <STDIN>;
chomp($str); my$len = @ARGV;
while($tmp <$len)
{
if ($ARGV[$tmp] eq $str) { my$pos = $tmp + 1; print "$str is found at $pos position\n";$found = 1;
}
++$tmp; } if ($found == 0)
{
print "$str is not found\n"; } Tips: • Use chomp when take user input from <STDIN> to remove new line character ### Quiz 3: Take input till user presses "n". Problem: Keep taking input from user till user presses "n" and sum all inputs. Sample Input: Taken as <STDIN> Sample Output: Sum is xyz Solution: my$tmp = 1;
my $sum = 0; my$var = 0;
print "press n when you want to stop entering numbers\n";
do{
print "Enter number $tmp\n";$var = <STDIN>   ;
$sum =$sum+$var; ++$tmp;
}  until($var == "n") ; print "sum is$sum";

Tips:
• Use ==, when comparing

### Quiz 2: Find sum of all the numbers using STDIN.

Problem:

Sample Input:
Taken as <STDIN>

Sample Output:
Sum is xyz

Solution:

print "How many numbers you want to add\n";
my $len = <STDIN>; my$tmp = 1;
my $sum = 0; while($tmp <= $len) { print "Enter number$tmp\n";
my $var = <STDIN> ;$sum = $sum+$var;
++$tmp; } print "Sum is$sum";

Tips:

• <STDIN> is used to take user input and save it in a variable.

### Quiz 1: Find sum of all the numbers using ARGV.

Problem:
Give sum of all the numbers passed as command like argument.

Sample Input:
5 6

Sample Output:
Sum is 11

Solution:

my $tmp = 0; my$sum = 0;
my $len = @ARGV; while($tmp < $len) {$sum = $sum+$ARGV[$tmp]; ++$tmp;
}
print "Sum is $sum"; Tips: • @ARGV stores all command like arguments in array ie for input 5 6 ,$ARGV[0] = 5 and \$ARGV[1] = 6.

### PERL Quiz

Hello Everyone

Want to check your Perl coding skills.
Take up the quiz here and solve using Perl.
You will find 5-7 new quiz here every week.

Have fun!!!