Saturday, July 14, 2018

TCS CodeVita Problem : Sheldon Cooper and his beverage paradigm C-language Program

Problem : Sheldon Cooper and his beverage paradigm

www.matterhere.com - Nareddula Rajeev Reddy (NRR)


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
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.


TCS CodeVita 2015 Round1 Question: Sheldon Cooper and his beverage paradigm
C-Language Program (CodeVita/Sheldon-Cooper-and-his-beverage-paradigm.c)

#include <stdio.h>
#define M 200
long flag, get;
void combinationUtil(long arr[],long n,long r,long index,long data[],long i);

void printCombination(long arr[], long n, long r)
{
    long data[r];
    combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil(long arr[], long n, long r, long index, long data[], long i)
{
    if (index == r)
    {
long sum = 0;
        for (long j=0; j<r; j++)
        {
        sum += data[j];
            //printf("%ld $%ld ",data[j],j);
        }
        if(sum == get)
        flag = 1;
        //printf("\n%ld\n",sum);
        return;
    }
    if (i >= n)
        return;
    data[index] = arr[i];
    combinationUtil(arr, n, r, index+1, data, i+1);
    combinationUtil(arr, n, r, index, data, i+1);
}
int main()
{
long i,N, A[M], X, r = 3;;
flag = 0;
scanf("%ld",&N);
while(i<N)
{
scanf("%ld",&A[i]);
i++;
}
scanf("%ld",&X);

/*printf("%ld ",N);
i=0;
while(i<N)
{
printf("%ld ",A[i]);
i++;
}
printf("%ld\n\n",X);*/
get = X;
    printCombination(A, N, r);

if(flag == 1)
printf("True");
else
printf("False");
    return 0;
}

Sources:
https://www.programminggeek.in/
https://github.com/

* Ask us, what you want?
EmoticonEmoticon