Sheldon Cooper and his beverage paradigm - Code Vita 2014 | round 1

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.InputOutput
16
1
4
45
6
10
8
22

True
24
1
3
12
4
14

False

Program:

#include <stdio.h>
int main() {
int n,i,a[100],t,s,sum=0,f,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&s);
for(i=0;i<n;i++)
{
    for(j=i;j<=n;j++)
        {
          if(a[i]<a[j])
          {
              t=a[i];
              a[i]=a[j];
              a[j]=t;
          }
        }   
}
    for(i=0;i<n;i++)
    {
        sum=sum+a[i];
        if(sum==s)
        {
            printf("True");
            f=1;
            goto x;
        }
        else 
        {
            if(sum>s)
            sum=sum-a[i];
        }
    }
    if(f==0)
    printf("False");
    x:  return 0;  
    }

Output:

6
1
4
45
6
10
8
22
True

4
1
3
12
4
14
False

You can also run it on online IDE: https://ide.geeksforgeeks.org/hsuixPLs8U
Your feedback are most Welcomed! If you have any doubts you can contact me or leave it in the comment!! Cheers!!!

Related Links: Collecting Candies

Bank Compare | Code Vita 2018 | round 1

Bank Compare

 Problem Description

There are two banks; Bank A and Bank B. Their interest rates vary. You have received offers from both bank in terms of annual rate of interest, tenure and variations of rate of interest over the entire tenure.
You have to choose the offer which costs you least interest and reject the other.
Do the computation and make a wise choice.
The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :
EMI = loanAmount * monthlyInterestRate /
( 1 - 1 / (1 + monthlyInterestRate)^(numberOfYears * 12))

Constraints

1 <= P <= 1000000
1 <=T <= 50
1<= N1 <= 30
1<= N2 <= 30
Input Format
First line : P – principal (Loan Amount)
Second line : T – Total Tenure (in years).
Third Line : N1 is number of slabs of interest rates for a given period by Bank A. First slab starts from first year and second slab starts from end of first slab and so on.
Next N1 line will contain the interest rate and their period.
After N1 lines we will receive N2 viz. the number of slabs offered by second bank.
Next N2 lines are number of slabs of interest rates for a given period by Bank B. First slab starts from first year and second slab starts from end of first slab and so on.
The period and rate will be delimited by single white space.
Output
Your decision – either Bank A or Bank B.
Explanation
Example 1
Input
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Output
Bank B
Example 2
Input
500000
26
3
13 9.5
3 6.9
10 5.6
3
14 8.5
6 7.4
6 9.6
Output
Bank A
Program:

#include <stdio.h>
double power(double b,int a)
{
    int i;
    double pow=1;
    for(i=0;i<a;i++)
    {
        pow=pow*b;
    }
    return pow;
}
int main() {
double p,s,mi,sum,emi,j1,j,bank[5],sq;
int y,n,k,i,yrs,y1,l=0;
    scanf("%lf",&p);
scanf("%d",&y);
for(k=0;k<2;k++)
{
scanf("%d",&n);
sum=0;
for(i=0;i<n;i++)
{
    scanf("%d",&yrs);
    scanf("%lf",&s);
    mi=0;
    j=s/1200;
    j1=1+j;
    y1=yrs*12;
    sq=power(j1,y1);
    emi=p*(j/(1-(1/(sq))));
    mi=emi*y1;
    sum=sum+mi;
}
bank[l++]=sum;
}
if(bank[0]<bank[1])
printf("Bank A");
else
printf("Bank B");
return 0;
}

Output:

10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9

Bank B
  
You can also run it in an online IDEhttps://ide.geeksforgeeks.org/sPEYMvF71Y

If you have any doubts you can leave it in the comment section or contact me!!
Your feedback are welcomed so kindly leave your feedback below! Cheers!

Related Links: Stone Game- One Four

TestVita | Code Vita 2015 | round 2

TestVita

Problem:

TCS is working on a new project called "TestVita". There are N modules in the project. Each module (i) has completion time denoted in number of hours (Hi) and may depend on other modules. If Module x depends on Module y then one needs to complete y before x.

As Project manager, you are asked to deliver the project as early as possible.
Provide an estimation of amount of time required to complete the project.
Input Format:

First line contains T, number of test cases.

For each test case: 
  1. First line contains N, number of modules.
  2. Next N lines, each contain:
    • (i) Module ID
    • (Hi) Number of hours it takes to complete the module
    • (D) Set of module ids that i depends on - integers delimited by space.

Output Format:

Output the minimum number of hours required to deliver the project.

Constraints:

1. 1 <= T <= 10
2. 0 < N < 1000; number of modules
3. 0 < i <= N; module ID 
4. 0 < Hi < 60; number of hours it takes to complete the module i
5. 0 <= |D| < N; number of dependencies
6. 0 < Dk <= N; module ID of dependencies

Sample Input and Output

SNo.InputOutput
1
1
5
1 5 0
2 6 1
3 3 2
4 2 3
5 1 3

16

Program:

#include <stdio.h>
int main() {
int n,m[10],t[10],d[10],q,a,i,sum=0;
scanf("%d",&a);
for(q=1;q<=a;q++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
    scanf("%d %d %d",&m[i],&t[i],&d[i]);
    for(i=0;i<n-1;i++)
    {
            if(d[i]==d[i+1])
            {
                if(t[i]<t[i+1])
                t[i]=0;
                else
                t[i+1]=0;
            }
    }
   for(i=0;i<n;i++)
   {
       sum=sum+t[i];
   }
    printf("%d",sum);
}
return 0;
}

Output:

1
5
1 5 0
2 6 1
3 3 2
4 2 3
5 1 3

16

You can also run it on an online IDEhttps://ide.geeksforgeeks.org/eTqOGzjCr5

Your feedback are welcomed! If you have any doubts you can contact me or comment below! Cheers!

 Related Link: Bank Compare

Jumble With Numbers

Jumble With Numbers

Problem

In NASA, two researchers, Mathew and John, started their work on a new planet, but while practicing research they faced a mathematical difficulty. In order to save the time they divided their work.
So scientist Mathew worked on a piece and invented a number computed with the following formula:
A Mathew number is computed as follows using the formula:
H(n) = n(2n-1)
And scientist John invented another number which is built by the following formula which is called John number.
T(n) = n(n+1)/2
Now Mathew and John are jumbled while combining their work. Now help them combine their research work by finding out number in a given range that satisfies both properties. Using the above formula, the first few Mathew-John numbers are:
1 6 15 28 …

Input Format:

Input consists of 3 integers T1,T2,M separated by space . T1 and T2 are upper and lower limits of the range. The range is inclusive of both T1 and T2. Find Mth number in range [T1,T2] which is actually a Mathew-John number.

Line 1
T1 T2 M,where T1 is upper limit of the range, T2 is lower limit of the range and M ,where Mth element of the series is required

Constraints:

0 < T1 < T2 < 1000000

Output Format:

Print Mth number from formed sequence between T1 and T2(inclusive).

Line 1
For Valid Input,print

Print Mth number from formed sequence between T1 and T2
Or
No number is present at this index

For Invalid Input,print

Invalid Input


Sample Input and Output:


SNo.InputOutput
1               
90 150 2             
120
2
20 80 6
No number is present at this index
3
-5 3 a
Invalid Input

Program:

#include <stdio.h>
int main() {
int t1,t2,m,i,j,c=0,a,b,th[100],k=0;
scanf("%d %d %d",&t1,&t2,&m);
if(t1>0&&t2>0&&m>0)
{
for(i=1;i<=t2/2;i++)
{
    a=i*((2*i)-1);
    for(j=1;j<=t2/2;j++)
    {
        b=j*(j+1)/2;
        if((a==b)&&(a>=t1&&a<=t2))
        th[k++]=a;
    }
}
   if(k<m)
    printf("No number is present at this index");
    else
    printf("%d",th[m-1]);
}
         else
         printf("Invalid Input");
return 0;
}

Output:

90 150 2

120

You can also run this in online IDE: https://ide.geeksforgeeks.org/ZndEODl1gU

Your comments and feedbacks are welcomed! If you have any doubts you can leave it on the comment! Cheers!!

Related Links: Test vita

Consecutive Prime Sum | Code Vita 2016 | round 1

Problem Description:

Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example

5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.

Input Format:
First line contains a number N
Output Format:
Print the total number of all such prime numbers which are less than or equal to N.
Sample Input and Output


SNo.InputOutputComment
1202
(Below 20, there are 2 such numbers: 5 and 17).
5=2+3
17=2+3+5+7
2151

Program:

#include <stdio.h>
int prime(int b)
{
    int j,cnt;
   cnt=1;
     for(j=2;j<=b/2;j++)
     {
         if(b%j==0)
         cnt=0;
     }
     if(cnt==0)
     return 1;
     else
     return 0;
}
int main() {
 int i,j,n,cnt,a[25],c,sum=0,count=0,k=0;
 scanf("%d",&n);
 for(i=2;i<=n;i++)
 {
     cnt=1;
     for(j=2;j<=n/2;j++)
     {
         if(i%j==0)
         cnt=0;
     }
     if(cnt==1)
     {
        a[k]=i;
        k++;
        }
 }
 for(i=0;i<k;i++)
 {
     sum=sum+a[i];
    c= prime(sum);
    if(c==1)
    count++;
 }
 printf("%d",count);
 return 0;
}

Output:

20

2

You can also run it on a online IDEhttps://ide.geeksforgeeks.org/XcOKzTd4Ik
If you have any doubt you can contact me or comment it below! Your comments and feedbacks are also welcomed!! Cheers!!

Related Links: Jumble with Numbers

Reverse Gear | Code Vita 2015 | round 1

Reverse Gear

Problem Description:

        A futuristic company is building an autonomous car. The scientists at the company are training the car to perform Reverse parking. To park, the car needs to be able to move in backward as well as forward direction. The car is programmed to move backwards B meters and forwards again, say F meters, in a straight line. The car does this repeatedly until it is able to park or collides with other objects. The car covers 1 meter in T units of time. There is a wall after distance D from car's initial position in the backward direction.

The car is currently not without defects and hence often hits the wall. The scientists are devising a strategy to prevent this from happening. Your task is to help the scientists by providing them with exact information on amount of time available before the car hits the wall.
Input Format:

First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, 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. D denotes distance from Car's starting position and the wall in backward direction

Output Format:

For each test case print time taken by the Car to hit the wall
Constraints:
First move will always be in backward direction
1 <= N <= 100
backward displacement > forward displacement i.e. (B > F)
forward displacement (F) > 0
backward displacement (B) > 0
time (T) > 0 
distance (D) > 0
All input values must be positive integers only

Sample Input and Output

SNo.InputOutput
1
2
6 9 3 18
3 7 5 20

162
220

Program:

#include <stdio.h>
int main() {
int f,b,d,t,sdist,sdisp,step,rd,td,tot,tt,test;
scanf("%d",&test);
while(test)
{
scanf("%d %d %d %d",&f,&b,&t,&d);
sdist=b+f;
sdisp=b-f;
step=(d-b)/sdisp;
if((d-b)%sdisp!=0)
step=step+1;
rd=d-(step*sdisp);
tot=(step*sdist)+rd;
tt=tot*t;
printf("%d\n",tt);
test--;
}
return 0;
}

Output:

2
6 9 3 18
3 7 5 20

162
220

You can also run it on an online IDE: https://ide.geeksforgeeks.org/S994F0QfMs
Your feedback and if you have any doubts it is welcomed! Do contact me or comment it below! Cheers!

Related Links: consecutive prime sum

Parallelograms | Code Vita 2018 | round 1


Parallelograms

Problem Description

A number (N) of lines (extending to infinity) in both directions are drawn on a plane. The lines are specified by the angle (positive or negative) made with the x axis (in degrees). It may be assumed that the different lines are not coincident (they do not overlap each other). The objective is to determine the number of parallelograms in the plane formed by these lines.
If the lines are given with an angle of 10, 70, 30 and 30, the figure looks like this
L1 is at 10 degrees to the x axis, L2 is at 70 degrees to the x axis, L3 and L4 are at 30 degrees to the x axis. It can be seen that there are no parallelograms formed by these lines

Constraints

N<=50
-89 <= angle for any line <=90

Input Format

The first line of the input consists of a single integer, N.
The next line consists of a series of comma separated integers (positive, zero or negative), each corresponding to a separate line, and giving the angle that it makes with the x axis (in degrees).

Output

The output is a single integer giving the number of parallelograms in the plane formed by the lines

Explanation

Example 1
Input
5
20,20,-20,-20,50
Output
1
Explanation
There are 5 lines (N=5), with angles at 20,20,-20,-20and 50 degrees with the x axis. The figure looks like this
There is one
Hence the output is 1.
Example 2
Input
6
20,20,-20,-20,50,50
Output
3
Explanation
There are 6 lines (N=6) with angles 20,20, -20,-20,50 and 50 degrees with the x axis.. The figure looks like this
There are three parallelograms formed by (L1, L2, L3, L4), (L1, L2, L5, L6) and (L3, L4, L5, L6). Hence the output is 3.

Program:

#include<stdio.h>
int pairfound(int angle[],int i,int n)
{
  int j;
  for(j=i+1;j<n;j++)
  {
    if(angle[i]==angle[j])
      return 1;
  }
  return 0;
}
int main()
{
  int n,i,count=0,pllgm,ang;
  scanf("%d",&n);
  int angle[n];
  for(i=0;i<n;i++)
  {
    scanf("%d,",&ang);
    if(ang>=-89&&ang<=90)
        angle[i]=ang;
  }
  for(i=0;i<n;i++)
  {
    if(pairfound(angle,i,n)==1)
    {
      count++;
    }
  }
  pllgm=(count-1)*count/2;
  printf("%d",pllgm);
  return 0;
}

Output

6
20,20,-20,-20,50,50
3
You can also try it in online IDE: https://ide.geeksforgeeks.org/vVEgz35Wso
Your comments and feedback are welcomed! You can contact me if you have any doubts! Cheers!
Related Links: Reverse Gear


Date Time | Code Vita 2018 | round 1


Date Time

Problem Description

Arun and his sister Usha are challenging each other with some mathematical puzzles. Usha, the cleverer one, has come up with the idea of givingArun 12 distinct digits from 0 to 9, and have him form the largest date time in 2018 with them. Arun is a little nervous, and asks you to help him with a computer program.
Usha will give Arun 12 distinct digits. He needs to create a date time combination in the year 2018: the date in the MM/DD form (all four digits must be present), and the time in the format HH:MM (all four digits must be present). The date may be from 01/01 to 12/31 and the time may be from 00:00 to 23:59 (in the 24 hour format). The digits provided may be used only once in the answer that Arun gives.
If more than one date time combination may be formed, Arun needs to give the latest valid date time possible in the year 2018.

Constraints

Single digits (any of 0-9)

Input Format

A line consisting of a sequence of 12 (not necessarily distinct) single digits (any of 0-9) separated by commas. The sequence will be non-decreasing.

Output

The maximum possible valid date time in the year 2018. The output must be in the format
MM/DD HH:MM
If no date time can be constructed, the output should be 0
 

Explanation

Example1 :
Input
0,0,1,2,2,2,3,5,9,9,9,9
Output
12/30 22:59
Explanation
The 12 digits to be used by Arun are given.
The maximum valid date time using only the digits given, and with each digit used at most once is
12/30 22:59
This is the output.
Example 2
Input
3,3,3,3,3,3,3,3,3,3,3,3
Output
0
Explanation
As no digit less than 3 is present in the input, a valid month cannot be formed. Hence no valid Date time can be formed with the input digits.

Program

#include<stdio.h>
int isLeapYear(int y)
{
    if(y % 4 == 0)
    {
        if(y % 100 == 0)
        {
            if(y % 400 == 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 1;
        }
    }
    
    return 0;
}

int is_valid_date_time(int date_time_arr[], int n)
{
    int month;
    if(n >= 2)
    {
        month = date_time_arr[0] * 10 + date_time_arr[1];
        if(!(month >= 1 && month <= 12))
        {
            return 0;
        }
    }
    
    if(n >= 4)
    {
        int days_in_each_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        days_in_each_month[1] += isLeapYear(2018);
        
        int day = date_time_arr[2] * 10 + date_time_arr[3];
        if(!(day >= 1 && day <= days_in_each_month[month - 1]))
        {
            return 0;
        }
    }
    
    if(n >= 6)
    {
        int hour = date_time_arr[4] * 10 + date_time_arr[5];
        if(!(hour >= 0 && hour <= 23))
        {
            return 0;
        }
    }
    
    if(n >= 8)
    {
        int min = date_time_arr[6] * 10 + date_time_arr[7];
        if(!(min >= 0 && min <= 59))
        {
            return 0;
        }
    }
    
    return 1;
}

int isFound = 0;
int max_time_in_2018[8] = {0};

int is_max(int inp_arr[])
{
    for(int i = 0; i < 8; i ++)
    {
        if(max_time_in_2018[i] == inp_arr[i])
        {
            continue;
        }
        else if(max_time_in_2018[i] >inp_arr[i])
        {
            return 0;
        }
        else 
        {
            return 1;
        }
    }
    
    return 0;
}

void time_computing(int inp_arr[], 
                    int date_time_arr[], int sel_cnt,
                    int used[])
{
    if(!is_valid_date_time(date_time_arr, sel_cnt)) 
    {
        return;
    }
    
    if(sel_cnt == 8)
    {
        if(is_valid_date_time(date_time_arr, sel_cnt))
        {
            isFound = 1;
            if(is_max(date_time_arr))
            {
                for(int i = 0; i < 8; i++)
                {
                    max_time_in_2018[i] = date_time_arr[i];
                }
            }
        }
        
        return;
    }
    
    // Select current item
    for(int i = 0; i< 12; i++)
    {
        if(used[i] == 1)
        {
            continue;
        }
        
        date_time_arr[sel_cnt] = inp_arr[i];
        used[i] = 1;
        time_computing(inp_arr,date_time_arr, sel_cnt + 1, used);
        used[i] = 0;
    }              
}

int main()
{
    int inp_arr[12], date_time_arr[8];
    scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
          &inp_arr[0],&inp_arr[1],&inp_arr[2],&inp_arr[3],
          &inp_arr[4],&inp_arr[5],&inp_arr[6],&inp_arr[7],
          &inp_arr[8],&inp_arr[9],&inp_arr[10],&inp_arr[11]);

    int used[12] = {0};
    
    time_computing(inp_arr, date_time_arr, 0, used);
    if(isFound == 1)
    {
        printf("%d%d/%d%d %d%d:%d%d",
         max_time_in_2018[0],max_time_in_2018[1],max_time_in_2018[2],max_time_in_2018[3],
         max_time_in_2018[4],max_time_in_2018[5],max_time_in_2018[6],max_time_in_2018[7]);
    }
    else
    {
        printf("0");
    }
    
    return 0;
}

Output

0,0,1,2,2,2,3,5,9,9,9,9

12/30 22:59

You can also run it on an online IDE: https://ide.geeksforgeeks.org/XZImlFQrQa

Your comments and feedback are welcomed!! If you have any doubts do contact me! Cheers!

Related Links: Parallelograms 2018

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...