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 1
6
1
4
45
6
10
8
22
Sample Output 1:
True
Sample Input 2:
4
1
3
12
4
14
Sample Output 2:
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.
C++ Implementation:
Time Complexity: O(n^3)
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 1
6
1
4
45
6
10
8
22
Sample Output 1:
True
Sample Input 2:
4
1
3
12
4
14
Sample Output 2:
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.
C++ Implementation:
Time Complexity: O(n^3)
#include <iostream> using namespace std; int main() { int N; cin>>N; int values[N]; for(int i=0;i<N;i++) cin>>values[i]; int key; cin>>key; int sum = 0; bool found = false; for(int i=0;i<N;i++) { if(found) break; sum += values[i]; for(int j=0;j<N;j++) { if(found) break; if(i!=j) { sum += values[j]; for(int k=0;k<N;k++) { if(found) break; if(k!=i && k!=j) { sum += values[k]; if(sum==key) { found=true; break; } sum -= values[k]; } } sum -= values[j]; } } sum -= values[i]; } if(found) cout<<"True"; else cout<<"False"; return 0; }
C++ Implementation with time complexity O(n^2):
#include <iostream> #include <algorithm> // used for sort() function using namespace std; bool checkArray(int *a, int n,int x) { for(int i=0;i<n;i++) { for(int l=i+1,r=n-1;l<r;) { if(a[i] + a[l] + a[r] == x) return true; else if(a[i] + a[l] + a[r] < x) l++; else r--; } } return false; } int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } int x; cin>>x; // sort the input array a[] sort(a,a+n); if(checkArray(a,n,x)) cout<<"True"; else cout<<"False"; return 0; }
Other Problems from TCS Mockvita - 1 and Mockvita - 2:
Solution : MockVita-I Problem D
Solution : MockVita-II Problem C
Solution : MockVita-II Problem B
Solution : MockVita-II Problem D
Solution : MockVita-II Problem A
Solution : MockVita-I Problem B
Solution : MockVita-I Problem C
Solution : MockVita-I Problem F
Solution : MockVita-I Problem A