cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2022 · EasyArrays & differences

Repair the Chess Set

National Olympiad — Municipal round (2022 season)

Submission name: chess.cpp

Problem summary

A complete chess set has, for each colour, 1 king, 1 queen, 2 rooks, 2 knights, 2 bishops and 8 pawns. Given the current counts for white and black, report how many of each piece to add; negative counts mean remove pieces.

Input

Two rows of six integers: kings, queens, rooks, knights, bishops, pawns for white, then black.

Output

Two rows of six signed adjustments in the same order.

Constraints

Every supplied piece count is between 0 and 16 inclusive.

Worked example

An independently prepared example for this explanation.

Input

0 1 2 2 2 7
0 1 2 1 2 7

Output

1 0 0 0 0 1
1 0 0 1 0 1

Solution analysis

  1. Keep the target counts in an array {1,1,2,2,2,8}. Each input count is independent of all others.
  2. For each colour and piece type, output target−current. Positive differences add missing pieces, negative differences remove extras, and zero leaves a correct count unchanged.
  3. In the sample, white lacks one king and one pawn. Black lacks a king, a knight and a pawn. The two output rows express those additions in the same piece order as the input.

Why it works

Adding target−current to the current count gives exactly target for every colour and type. The sign also matches whether the current count is below or above the target, so every adjustment has the required interpretation.

Complexity: O(1) time and O(1) space for twelve fixed counts.

Watch out. Subtract current from target, not the reverse. Treat the two colours separately and preserve piece order.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
using namespace std;
int main() {
    int target[6] = {1, 1, 2, 2, 2, 8};
    for (int colour = 0; colour < 2; colour++) {
        for (int piece = 0; piece < 6; piece++) {
            int current;
            cin >> current;
            if (piece > 0) {
                cout << ' ';
            }
            // Positive means add; negative means remove.
            cout << target[piece] - current;
        }
        cout << '\n';
    }
}

Source: Arena Olimpiici ↗