Important TCS CodeVita Questions Solved – Sheldon Cooper and his beverage paradigm
Important TCS CodeVita Questions Solved – 2015
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
- Number of bottles N corresponding to different beverages and hence their sizes
- Next N lines, contain a positive integer corresponding to the size of the beverage
- Last line consists of an integer value, denoted by X above
Input Format:
- First line contains number of bottles ordered denoted by N
- Next N lines, contains a positive integer Ai, the size of the ith bottle
- 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
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.
Important TCS CodeVita Questions Solved – Sheldon Cooper and his beverage paradigm
Sample Solution C program:
#include<stdio.h> int A[100],X; void subset(int s,int k,int r) { if(s+A[k]==X) { printf("True"); exit(0); } else if(s+A[k]+A[k+1]<=X) subset(s+A[k],k+1,r-A[k]); if((s+r-A[k]>=X) && (s+A[k+1]<=X)) subset(s,k+1,r-A[k]); } int main() { int N,i,j,temp,r=0; scanf("%d",&N); if(N<3) { printf("Constraint Violated"); exit(0); } for(i=1;i<=N;i++) { scanf("%d",&A[i]); if(A[i]<1) { printf("Constraint Violated"); exit(0); } r+=A[i]; } scanf("%d",&X); if(X<0) { printf("Constraint Violated"); exit(0); } for(i=1;i<=N;i++) for(j=i+1;j<=N;j++) if(A[i]>A[j]) { temp=A[i]; A[i]=A[j]; A[j]=temp; } subset(0,1,r); printf("False"); return 0; }
**logic: using SumOfSubsets Algorithm
Comments
best laptop for students
best gaming laptop under 50k
best gaming laptop under 50000
best laptop under 50k
best laptop under 40K
best laptop under 40,000
best smartphones under 10000
best smartphones under 10k
best gaming smartphones under 10000
best camera smartphones under 10000
best camera smartphones under 15000
best gaming smartphones under 15000
best battery smartphones under 15000
best camera smartphones under 15k
best gaming smartphones under 15k
best battery smartphones under 15k
best smartphones under 15k