Deck of Cards Trick

Here is a great critical thinking problem that I heard.

Scenario:

You are blind folded and cannot see.

I hand you a deck of cards telling you that a 5 are faced up and placed randomly in the deck (the rest are face down)  and ask you to split the group of cards into two groups such that each group has the same amount of face up cards.

This problem is great because one it forces you to think outside of the box and since the question itself can be easily represented in a computer science context.

 

(please try before just looking at solution, there is a way to split with 100% accuracy)

WARNING SPOILERS  SOLUTION BELOW!!!! 

 

 

 

Explanation:

The amount of cards in the deck doesn’t matter nor does the position of the cards face up in the deck. We can represent the problem as an array with 1s (face up ) and 0s (faced down). We can assume that based on the questions the groups don’t need to have the original amount of face up cards (5 is odd and you can’t really split it up evenly). Thus we must think how can to place the two groups separately.  If you take the amount of flipped cards from anywhere in the deck and invert them no matter their current orientation and take them placing it into group one and the rest into group two you will have met the problem conditions and have the correct solution.

#include <iostream>
#include <random>

using namespace std;
 //Compiler version g++ 6.3.0

int main()
 {
 	int deck[52];
 	int temp;
 	int flipCards = rand()%52+1;

	int* groupOne;
	int* groupTwo;

	for(int index =0; index < 52; index++)
 	{
 		deck[index] = 0;
 	}
	for(int index = 0; index < flipCards; index++)
	{

		do{
 			temp = rand()%52;
 		}while(1 == deck[temp]);
 		deck[temp]=1;
 	}
	groupOne = new int[flipCards]();
	groupTwo = new int[52-flipCards]();

	for(int index =0; index < 52; index++)
	{
		cout << deck[index] << endl;
	}
 	cout << endl;
	for(int index = 0; index < flipCards; index++)
 	{
		deck[index]= abs(deck[index]-1);
		groupOne[index] = deck[index];
 		cout << groupOne[index];
	}
	 cout << endl;

	for(int index = flipCards; index < 52; index++)
	{
		groupTwo[index] = deck[index];
 		cout << groupTwo[index];
 	}
 	cout << endl;
	return 0;
 }