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
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 |
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
I am so happy after read your blog. It’s very useful blog for us.
ReplyDeleteJava Corporate training for employees in Nigeria
Thank You so much! Happy coding!
Delete