HomeContactBlogPlayground

Published February 8, 2026

Problem C — Moroccan Carpet Patterns

Breakdown and solution for Problem C of Game of Codes 4 at INSEA: determining if a carpet tile sequence is harmonious.

ZB
Ziane BadreddineAuthor
MS
Mouad SadikAuthor
KB
Khalil BaidouriAuthor

Problem C — Moroccan Carpet Patterns

Game of Codes 4 · INSEA, Rabat, Morocco · February 08, 2026

This is one of the problems our team Hmama Miyта tackled during the contest. It turns out to be an elegant little problem hiding behind a beautiful cultural setting — Jihad, a master carpet weaver from the medina of Fes.


Problem Statement

Jihad creates Moroccan carpets represented as a sequence of nnn colored tiles. A carpet pattern is called harmonious if it can be split into exactly two parts where both parts contain the same number of each color tile.

Given a carpet pattern, determine if it's harmonious.

Input

  • First line: an integer nnn where 2≤n≤1002 \leq n \leq 1002≤n≤100 and nnn is even — the number of tiles
  • Second line: nnn integers a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ where 1≤ai≤1001 \leq a_i \leq 1001≤ai​≤100 — the tile colors

Output

Print YES if the pattern is harmonious, NO otherwise.


Key Insight

The problem note gives it away: a pattern is harmonious if and only if every color appears an even number of times.

If every color count is even, we can always split each color's tiles equally between the two parts — regardless of tile order.

This simplifies the problem dramatically. We don't need to think about how to partition — only whether the counts allow it.

Why is this equivalent?

Suppose every color ccc appears fcf_cfc​ times. For a valid split into two equal parts, each part must contain exactly fc/2f_c / 2fc​/2 tiles of color ccc. This is only an integer when fcf_cfc​ is even. Conversely, if all fcf_cfc​ are even, such a split is always constructible:

harmonious  ⟺  ∀ c∈colors:fc mod 2=0\text{harmonious} \iff \forall\, c \in \text{colors} : f_c \bmod 2 = 0 harmonious⟺∀c∈colors:fc​mod2=0

Walkthrough of Examples


Solution

Read input — get nnn and the array of tile colors.

Count frequencies — for each color, count how many times it appears.

Check parity — if any color has an odd frequency, output NO. Otherwise output YES.

Complexity

  • Time: O(n)O(n)O(n) — one pass to count, one pass to check
  • Space: O(C)O(C)O(C) where C=100C = 100C=100 is the number of distinct possible colors

Implementation

from collections import Counter

n = int(input())
tiles = list(map(int, input().split()))

freq = Counter(tiles)

if all(count % 2 == 0 for count in freq.values()):
print("YES")
else:
print("NO")

Common Pitfalls

Don't overthink it. This problem might tempt you to simulate actual splits or use dynamic programming. The note in the problem statement reveals the clean equivalence — always read the notes!


Reflection

This was a quick solve for us during the contest — a clean, satisfying problem that rewards reading carefully. The cultural framing around Moroccan carpet weaving gave it a lovely flavor that matched the INSEA setting perfectly.

Problems like this are a good reminder: the most elegant solutions often come from reformulating the question, not from implementing the naive approach.


Part of our Game of Codes 4 series — see the main article for the full contest recap.

On this page

Problem C — Moroccan Carpet PatternsProblem StatementInputOutputKey InsightWhy is this equivalent?Walkthrough of ExamplesSolutionComplexityImplementationCommon PitfallsReflection

Related Blogs

Jul 1,2026

Component Library

The underlying headless component library..

competitive-programming

game-of-codes

INSEA

FST-Settat

teamwork

algorithms

problem-solving

Feb 1,2026

Game of Codes — CP Contest

How our team "Hmama Miyта" competed at the 4th edition of the Game of Codes Competitive Programming Contest at INSEA.

competitive-programming

game-of-codes

INSEA

FST-Settat

teamwork

algorithms

problem-solving

Navigation

  • Home
  • About
  • Projects
  • Contact

Explore

  • Experience
  • Education
  • Activities

Blog

  • 17,600 Actions: Agent Security Is a Systems Problem
  • Building MaintInsight
  • Component Library
  • Problem C — Moroccan Carpet Patterns
  • Game of Codes — CP Contest
  • All posts

Language

Connect

GitHub
LinkedIn
Email

Ziane Badr Eddine.