Coins#2218

Coins (Editorial)

Let's find the sum of input integers — this is the number of coins that are heads-up. Let this sum be denoted as . To make all the coins heads-up, you need to flip the remaining coins. To make all the coins tails-up, you need to flip s coins that are currently heads-up. Thus, the answer is min(s, n - s).

Example

There are s = 3 coins heads-up.

To make all the coins heads-up, you need to flip n - s = 5 - 3 = 2 coins. To make all the coins tails-up, you need to flip s = 3 coins. The answer to the problem is min(s, n - s) = min(3, 2) = 2 coins.

Algorithm realization

Read the input data. Compute the sum of integers in the variable .

scanf("%d",&n);
for(i = 0; i < n; i++)
{
  scanf("%d",&v);
  s += v;
}

Compute and print the result — the value of min(s, n - s).

if (s < n - s) printf("%d\n",s);
else printf("%d\n",n - s);