Frequency Constraint

Each distinct value appears a specific number of times.

Frequency Constraint

Problem Statement

Generate an array where each distinct value appears a specific number of times. This tests understanding of counting constraints and element frequency distribution.

Your task is to write constraints to generate arrays where: 1. Element 0 appears exactly 4 times 2. Element 1 appears exactly 3 times 3. Element 2 appears exactly 2 times 4. Element 3 appears exactly 1 time 5. Elements 4 and 5 do not appear (0 times)

Constraints

  • Array size: 10 elements
  • Value range: 0, 5
  • Total frequency: 4 + 3 + 2 + 1 = 10 (fills entire array)
  • Use sum(arr == value) to count occurrences

Examples

Example 1:

Array: [0, 0, 0, 0, 1, 1, 1, 2, 2, 3]

Frequency Distribution:
  Element 0: 4 times
  Element 1: 3 times
  Element 2: 2 times
  Element 3: 1 time
  Element 4: 0 times
  Element 5: 0 times

Explanation: Exact frequency distribution required.

Example 2:

Array: [3, 2, 2, 1, 0, 0, 1, 0, 1, 0]

Frequency Distribution:
  Element 0: 4 times
  Element 1: 3 times
  Element 2: 2 times
  Element 3: 1 time
  Element 4: 0 times
  Element 5: 0 times

Explanation: Same frequencies in different order.

Example 3 (Invalid):

Array: [0, 0, 0, 1, 1, 1, 1, 2, 2, 3]

Frequency Distribution:
  Element 0: 3 times (expected 4)
  Element 1: 4 times (expected 3)
  Element 2: 2 times (CORRECT)
  Element 3: 1 time (CORRECT)

Explanation: Frequency mismatch violates constraints.

Starter Code Pointer

The constraint demonstrates: 1. Using sum() with comparison operators to count occurrences 2. Aggregate frequency constraints ensuring exact counts 3. The complementary constraint system (all frequencies must sum to array size)

Loading the interactive workspace...