Thursday 17 December 2015

Question 4

Problem : Water Management There are N tubs of water, numbered from 1 to N. Initially there is a few litres of water in each tub. Each water tub has 2 taps attached to it. Incoming Tap with speed x litres / second and Outgoing Tap with speed y litres / second. Let water(i) denote the final volume of water in ith tub. Amit wants to attain such a situation that water(i) < water(i+1) for 1<=i <=N. i.e. water in each tub must be less than water in the tub next to it. He wants to do this as quickly as possible. You task is to find out and tell Amit, what is the minimum number of seconds required to attain in this situation.

  Input Format: First line will contains the number of tubs, denoted by N Next N lines contain a tuple with 3 integers delimited by white space. The tuple contents are 
  • Wi - volume of water present initially in ith tub (in litres)
  • x - speed of incoming tap of ith tub ( in litres/second)
  • y - speed of outgoing tap of ith tub ( in litres/second)
  Output Format: Minimum time in seconds, needed to arrange water in N tubs, in ascending order of volume.
  Constraints: 2 <= N <= 100000 0 <= W <= 1000000000 (1 billion) for each tub 1 <= x <= 10000 for each tub1 <= y <= 10000 for each tub A tap can be used only for integral number of seconds i.e. you cannot use a tap for 0.3 or 0.5 seconds. It can be used either for 1,2,3.... Seconds Capacity of each tub is infinite.Volume of water in any tub cannot be less than zero at any point of time. Any number of taps can be turned on or off simultaneously.   Sample Input and Output 


  Explanation for sample input and output 1: Here we have 3 tubs with following information about each of them
Tub Number
Initial Volume
Incoming Tap speed
Outgoing Tap speed
Tub 1233
Tub 2345
Tub 3355
Initially tub 2 and 3 have same volume of water. So he will just turn on the Incoming Tap at Tub 3 for one second. After one second the water in 3rd tub will be 8 litres. Since 2 < 3 < 8, the answer is 1 second.


  Explanation for sample input and output 2: Here we have 3 tubs with following information about each of them

Tub Number
Initial Volume
Incoming Tap speed
Outgoing Tap speed
Tub 1621
Tub 2413
Tub 3414
As we can see that it is impossible to do the task in one second. But it can be done in two second as below. Turn on the outgoing tap at tub 1. So after two seconds water level will reduce to 4 litres. Turn on the incoming tap at tub 2 only for one second. So water level will be 5 litres. Turn on the incoming tap at tub 3 for two seconds. So water level will be 6 litres. Since 4 < 5 < 6, the answer is 2 seconds.

program code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 int i,j=0,n,t=0;
 long *w,*x,*y;
 scanf("%d",&n);
 w=(long*)calloc(n,sizeof(long));
 x=(long*)calloc(n,sizeof(long));
 y=(long*)calloc(n,sizeof(long));
 for(i=0;i<n;i++)
 {
  scanf("%d%d%d",&w[i],&x[i],&y[i]);
 }
 for(i=0;i<n;i++)
 {
  if(w[i]<w[i+1])
  {
   j=0;
   continue;
  }
  else
  {
    goto loop;
  }
 }
 if(j==0)
 {
  goto ans;
 }
loop: for(i=0;i<n;i++)
 {
  t=1;
 cmp:  if(w[i]-y[i]>0&&i==0)
  {
   w[i]=w[i]-y[i];
  }
  else if(w[i]-y[i]>0 && w[i]-y[i]>w[i-1])
  {
   w[i]=w[i]-y[i];
  }
  if(w[i+1]<=w[i])
  {
    w[i+1]=w[i+1]+x[i+1];
    if(w[i+1]<=w[i])
    {
     t++;
     goto cmp;
    }
  }
  if(j<=t)
   j=t;
  }
ans:  printf("%d",j);
  getch();
}


Problem : Super ASCII String Checker

In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.

In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.

Your task is to find out whether the given string is a super ascii string or not.

Input Format:


First line contains number of test cases T, followed by T lines, each containing a string "S".

Output Format:


For each test case print "Yes" if the String "S" is super ascii, else print "No"
Constraints:

1<=T<=100
1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').

Sample Input and Output

SNo.InputOutput
1
2
bba
scca

Yes
No


program code:

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
 int a[26],l,i,k,j,c;
 char s[50];
 clrscr();
 printf("enter the number of test cases\n");
 scanf("%d",&j);
 for(l=0;l<j;l++)
 {
  for(i=0;i<26;i++)
  {
   a[i]=0;
  }
  printf("enter the string\n");
  scanf("%s",&s);
  for(i=0;i<strlen(s);i++)
  {
   k=(int)s[i]-97;
   a[k]++;
  }
  for(i=0;i<26;i++)
  {
   if(a[i]==i+1||a[i]==0)
    c=1;
   else
   {
    c=0;
    break;
   }
  }
  if(c!=0)
   printf("\tyes\n");
  else
   printf("\tno\n");
 }
 getch();
}


Question 3

Problem : Sheldon Cooper and his beverage paradigm Sheldon Cooper, Leonard Hofstadter and Penny decide to go for drinks at Cheese cake factory. Sheldon proposes to make a game out of this. Sheldon proposes as follows, 
  • To decide the amount of beverage they plan to consume, say X.
  • Then order for a random number of different drinks, say {A, B, C, D, E, F} of quantities {a, b, c, d, e, f} respectively.
  • If quantity of any three drinks add up to X then we'll have it else we'll return the order. E.g. If a + d + f = X then True else False
You are given 
  1. Number of bottles N corresponding to different beverages and hence their sizes
  2. Next N lines, contain a positive integer corresponding to the size of the beverage
  3. Last line consists of an integer value, denoted by X above
Your task is to help find out if there can be any combination of three beverage sizes that can sum up to the quantity they intend to consume. If such a combination is possible print True else False
  Input Format: 
  1. First line contains number of bottles ordered denoted by N
  2. Next N lines, contains a positive integer Ai, the size of the ith bottle
  3. Last line contains the quantity they intend to consume denoted by X in text above
  Output Format: True, if combination is possible False, if combination is not possible

  Constraints: N >= 3 Ai > 0 1 <= i <= N X > 0   Sample Input and Output 
SNo.
Input
Output
1
6 1 4 45 6 10 8 22
True
2
4 1 3 12 4 14
False

  Explanation for sample input and output 1: The sum of 2nd, 5th and 6th beverage size is equal to 22. So the output will be True.
  Explanation for sample input and output 2: Since no combination of given beverage sizes sum up to X i.e. 14, the output will be False

program code:(hint:set of all possible combinations with some condition)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 int i,n,q,l,k,j,*a=NULL;
 clrscr();
 printf("enter the no of entries: ");
 scanf("%d",&n);
 a=(int*)calloc(n,sizeof(int));
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("enter the quantity to be consumed\n");
 scanf("%d",&q);
 for(i=0;i<n;i++)
 {
  if(a[i]>q)
  {
   a[i]=a[n-1];
   n--;
   i--;
  }
 }
 for(i=0;i<n;i++)
 {
        k=a[i];  //selecting 1st element
        for(j=i+1;k+a[j]<q&&j<n;j++)
        {
   k=k+a[j];      //2nd element decided
   for(l=j+1;l<n;l++)
   {
    if(k+a[l]==q)         //checking for 3rd element
    {
     printf("\tyes\n");
     goto end;
    }
   }
  k=k-a[j];
        }
 k=k-a[i];
  }
  printf("\tno");
      end:getch();
      exit(0);
}

Saturday 5 September 2015

for more codevita round 1 solutions comment below indicating the question

Question 8

Problem : Milk Man and His Bottles A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply desired quantity using as less bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand of milk.
  Input Format: First line contains number of test cases N Next N lines, each contain a positive integer Liwhich corresponds to the demand of milk. 
  Output Format: For each input Li, print the minimum number of bottles required to fulfill the demand 
 Constraints: 1 <= N <= 1000 Li > 0 1 <= i <= N Sample Input and Output
SNo.
Input
Output
12 17 652 7
  Explanation: Number of test cases is 2
  1. In first test case, demand of milk is 17 litres which can be supplied using minimum of 2 bottles as follows
    • 1 x 10 litres and
    • 1 x 7 litres
  2. In second test case, demand of milk is 65 litres which can be supplied using minimum of 7 bottles as follows
    • 6 x 10 litres and
    • 1 x 5 litres

 here is the code in  c

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,nb,q,n;
clrscr();
printf("no of tests : ");
scanf("%d",&n);//no of test cases
for(i=0;i<n;i++)
{
printf("quantity of milk =");
scanf("%d",&q);
nb=0;
r=q%10;
if(q>=10)
{
if(r==2||r==9||r==3||r==4)
{
j=q/10;
nb=nb+j-1;
q=q%10+10;
}
else
{
j=q/10;
nb=nb+j;
q=q-(j*10);
}
}
if(q>=7)
{
j=q/7;
nb=nb+j;
q=q%7;
}
if(q>=5)
{
j=q/5;
nb=nb+j;
q=q%5;
}
nb=nb+q;
printf("minimum no of bottles = %d\n",nb);
}
getch();
}


hope this helps

Question 2

Problem : Saving for a rainy day By nature, an average Indian believes in saving money. Some reports suggest that an average Indian manages to save approximately 30+% of his salary. Dhaniram is one such hard working fellow. With a view of future expenses, Dhaniram resolves to save a certain amount in order to meet his cash flow demands in the future. Consider the following example. Dhaniram wants to buy a TV. He needs to pay Rs 2000/- per month for 12 installments to own the TV. If let's say he gets 4% interest per annum on his savings bank account, then Dhaniram will need to deposit a certain amount in the bank today, such that he is able to withdraw Rs 2000/- per month for the next 12 months without requiring any additional deposits throughout. Your task is to find out how much Dhaniram should deposit today so that he gets assured cash flows for a fixed period in the future, given the rate of interest at which his money will grow during this period.
  Input Format: First line contains desired cash flow M Second line contains period in months denoted by T Third line contains rate per annum R expressed in percentage at which deposited amount will grow
  Output Format: Print total amount of money to be deposited now rounded off to the nearest integerConstraints: M > 0 T > 0 R >= 0 Calculation should be done upto 11-digit precision   Sample Input and Output 
SNo.
Input
Output
1
500 3 12
1470
2
6000 3 5.9
17824
3
500 2 0
1000


here is my code freinds  (this problem gave me some trouble)

by looking at the code you may think this was easy but frankly this took the most amount of my time to get it right(because of  a small trick)

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float m,p,r;
double x,b;
int c;
scanf("%f%f%f",&p,&m,&r);
b=1200+r*(m-1);
x=p*m*1200/b;
c=(int)x;
printf("the amount to be deposited is :%d\n",c);
getch();
}

codevita 2015 solutions in c

Question 1

Problem : Catch-22 A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot. Your task is to calculate amount of time taken, before the Robot falls in either ditch, if at all it falls in a ditch. Input Format: First line contains total number of test cases, denoted by N Next N lines, contain a tuple containing 5 values delimited by space F B T FD BD, where 
  1. F denotes forward displacement in meters
  2. B denotes backward displacement in meters
  3. T denotes time taken to cover 1 meter
  4. FD denotes distance from Robot's starting position and the ditch in forward direction
  5. BD denotes distance from Robot's starting position and the ditch in backward direction
  Output Format: For each test case print time taken by the Robot to fall in the ditch and also state which ditch he falls into. Print F for forward and B for backward. Both the outputs must be delimited by whitespace OR Print No Ditch if the Robot does not fall in either ditch

  Constraints: First move will always be in forward direction 1 <= N <= 100 forward displacement > 0 backward displacement > 0 time > 0 distance of ditch in forward direction (FD) > 0 distance of ditch in backward direction (BD) > 0 All input values must be positive integers only   Sample Input and Output 
SNo.
Input
Output
1
3 9 4 3 13 10 9 7 1 11 13 4 4 3 8 12
63 F 25 F No Ditch
2
5 8 4 7 11 22 4 5 4 25 6 4 9 3 6 29 7 10 6 24 12 10 10 1 9 7
133 F 216 B 231 B 408 B 9 F


here is my code in c

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
int f,b,fd,bd,t,c=0,tc=0;
clrscr();
printf("enter no of test cases ");
scanf("%d",&n);
if(n>=1 && n<=100)
{
printf("f b t fd bd\n");
for(i=0;i<n;i++)
{
scanf("%d %d %d %d %d",&f,&b,&t,&fd,&bd);
if(fd>0 && bd>0 &&  t>0 && f>0 && b >0)
{
if(f>=fd)
{
c=fd*t;
printf("output:%d F\n",c);
}
else if(f==b)
{
printf("output:NO Ditch\n");
}
else if(f>b)
{
while(!0)
{
tc=tc+f;
c=c+f;
if(c>fd) break;
c=c-b;
tc=tc+b;
}
tc=tc-(c-fd);
printf("output:%d F\n",tc*t);
}
else
{

while(!0)
{
tc=tc+f;
c=c-f;
if(c>bd) break;
c=c+b;
tc=tc+b;
}
tc=tc-(c-bd);
printf("output:%d B\n",tc*t);
}
}
}
}
}