# Brain Teasers

This is a growing collection of little problems that I found fun to solve. Some of these problems are taken from the Google Code Jam contest. Some of my solutions are on github.com/walidk/codejam.

## Coupon collecting problem

This problem is on codejam.

You've become addicted to the latest craze in collectible card games, PokeCraft: The Gathering. You've mastered the rules! You've crafted balanced, offensive, and defensive decks! You argue the merits of various cards on Internet forums! You compete in tournaments! And now, as they just announced their huge new set of cards coming in the year 2010, you've decided you'd like to collect every last one of them! Fortunately, the one remaining sane part of your brain is wondering: how much will this cost?

There are $$C$$ kinds of card in the coming set. The cards are going to be sold in “booster packs”, each of which contains $$N$$ cards of different kinds. There are many possible combinations for a booster pack where no card is repeated. When you pay for one pack, you will get any of the possible combinations with equal probability. You buy packs one by one, until you own all the $$C$$ kinds. What is the expected (average) number of booster packs you will need to buy?

## Goro sort

This problem is on codejam.

Goro has 4 arms. Goro is very strong. You don't mess with Goro. Goro needs to sort an array of $$N$$ different integers. Algorithms are not Goro's strength; strength is Goro's strength. Goro's plan is to use the fingers on two of his hands to hold down several elements of the array and hit the table with his third and fourth fists as hard as possible. This will make the unsecured elements of the array fly up into the air, get shuffled randomly, and fall back down into the empty array locations.

Goro wants to sort the array as quickly as possible. How many hits will it take Goro to sort the given array, on average, if he acts intelligently when choosing which elements of the array to hold down before each hit of the table? Goro has an infinite number of fingers on the two hands he uses to hold down the array.

More precisely, before each hit, Goro may choose any subset of the elements of the array to freeze in place. He may choose differently depending on the outcomes of previous hits. Each hit permutes the unfrozen elements uniformly at random. Each permutation is equally likely.

## Array balancing

This problem was given in the first round of a coding contest organized by Criteo, where I worked in 2010-2011. I helped write a solution that is given below.

Let $$A_0$$ be an array of $$N$$ non-negative integers, indexed from $$0$$ to $$N-1$$. We assume that the sum of its elements is a multiple of $$N$$

$\sum_{n=0}^{N-1} A_0(n) = N.a$

where $$a$$ is a positive integer. We would like to balance the array in a minimum number of iterations, where each iteration transforms the array using a number of allowed transfers described below. Balancing the array means transforming it into the uniform target array $$T = [a, \dots, a]$$. To describe the allowed transfers, let $$A_i$$ denote the state of the array at the beginning of iteration $$i$$. During iteration $$i$$, each postive cell $$A_i(n) > 0$$ can transfer one unit to its left neighbor (left transfer), or transfer one unit to its right neighbor (right transfer). A left transfer is described by the operations

$A_{i+1}(n-1) = A_i(n-1) + 1$

$A_{i+1}(n) = A_i(n) - 1 \geq 0$

Note that a left transfer is not possible for the left-most cell $$A_i(0)$$ (similarly, a right transfer is not possible for the right-most cell). Also note that all transfers occur simultaneously, meaning that in the example array $$A_0 = [3, 0, 0]$$ we cannot make a right transfer from $$A(0)$$ to $$A(1)$$ then a right transfer from $$A(1)$$ to $$A(2)$$ in a single iteration, obtaining $$A_1 = [2, 0, 1]$$. This transformation will require two iterations.

Example: In the following example we start from array $$A_0 = [0, 3, 0]$$. Balancing this array requires a minimum of $$2$$ iterations. Here is one example of an optimal sequence of transformations

$A_0 = [0, 3, 0]$$$A_1 = [1, 2, 0]$$$$A_2 = [1, 1, 1]$$

We would like to find the minimum number of iterations required to balance any given array $$A_0$$.