Card Lottery Game

Explanation of Problem : Card Lottery Game

There are ’n’ (1 to 50) number of cards in a box and each of them has a number written on it. Take a card from the box and return it after seeing the number written on it. Continue the process for 4 times. Now you have four numbers. If the summation of those four number equals ‘m’ then you win the game. If it does not then you lose.

Input Example:
n = 3
m = 9
k = {1, 2, 4}

On the above input example the number of cards in the box is 3. The targeted summation value after carrying on the game 4 times is 9. The numbers written on our three cards are 1, 2, 4.

Now if there is at least one combination of numbers which can give us the summation m = 9 then,

Output Example
YES

if there is no such combination then

Output Example
NO

In the above input example if the player can pick 4 cards as 1, 2, 4, 2 then the summation will be 9 which is equal to the value of ‘m’. So on the above input example the output will be definitely YES.

Now let’s have a look at an input example which will definitely give us output NO.

Input Example:
n = 3
m = 9
k = {1, 3, 5}

On the above input example there is no way you can pick cards from the box 4 times and make the summation of them equals to 9. Because it will always be greater than 9 no matter how you pick those cards from the box. So for this input example the output will be definitely NO.

Solution of Problem : Card Lottery Game

I am going to show you the solution of this problem using C Programming. Let’s have a look at our source code.

#include <stdio.h>
#include <stdbool.h>

const int MAX_N = 50;

int main() {
	int n, m, k[MAX_N];

	printf("n :");
	scanf("%d", &n);
	printf("m :");
	scanf("%d", &m);
	printf("Enter the numbers written on each card....\n");
	for (int i = 0; i < n; ++i) {
		printf("k[%d] :", i);
		scanf("%d", &k[i]);
	}

    bool f = false;

    for (int a = 0; a < n; ++a) {
    	for (int b = 0; b < n; ++b) {
    		for (int c = 0; c < n; ++c) {
    			for (int d = 0; d < n; ++d) {
    				if (k[a] + k[b] + k + k[d] == m) {
    					f = true;
    				}
    			}
    		}
    	}
    }

    if (f) {
    	printf("YES\n");
    } else {
    	printf("NO\n");
    }
}

Let’s have a look at input output example once you run the above code in the command line.

Card Lottery Game input/output example

Card Lottery Game input/output example

If you have any questions feel free to ask me in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.