Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 2 — Three-Element Average Combinations

Objective

Create a function that generates every unique combination of three different elements from an integer slice.

For each combination, calculate its average value.

Return the number of combinations whose average is greater than a specified threshold.

Input

The source slice is:

list := []int{
    41, 19, 25, 74, 85, 36,
    93, 47, 56, 76, 20, 39,
    34, 66, 60, 82, 88, 91,
    17, 44, 28, 31, 95, 51,
    40, 14,
}

The second input argument is an integer threshold.

For the provided example:

threshold := 70

Function

Create a function named:

CheckAverageOfThreeElements(...)

Conceptually:

CheckAverageOfThreeElements(list, threshold)

returns the number of valid three-element combinations.

Combination Rules

Each group must contain exactly three different elements from different index positions.

For indexes:

i < j < k

a combination is:

{list[i], list[j], list[k]}

Using increasing indexes ensures that the same combination of positions is not generated multiple times in different orders.

For example:

{41, 19, 25}

is the same positional combination as:

{25, 41, 19}

and should only be processed once.

Average Calculation

For every three-element combination:

{a, b, c}

calculate:

average = (a + b + c) / 3

The combination satisfies the condition when:

average > threshold

For the provided task:

average > 70

Equivalent Sum Comparison

Because every combination always contains exactly three elements, the condition:

(a + b + c) / 3 > threshold

can also be evaluated as:

a + b + c > threshold * 3

This avoids unnecessary floating-point arithmetic.

For:

threshold = 70

the condition becomes:

a + b + c > 210

Total Number of Combinations

The input contains:

26

elements.

The number of unique three-element combinations is:

C(26, 3) = 2600

Every one of these combinations should be evaluated exactly once.

Expected Result

For:

CheckAverageOfThreeElements(list, 70)

the number of combinations whose average is greater than 70 is:

288

Therefore, the expected result is:

288

Requirements

The function must:

  1. generate every unique combination of three different index positions
  2. calculate or evaluate the average of each combination
  3. compare the average with the threshold
  4. count only combinations where the average is strictly greater than the threshold
  5. return the final count

Strict Comparison

The condition is:

average > threshold

not:

average >= threshold

Therefore, a combination whose average is exactly equal to the threshold must not be counted.

Duplicate Values

The phrase “three different elements” refers to different elements or index positions in the input collection.

If the input contains equal numeric values at different indexes, they are still separate elements and may participate in a combination unless the implementation introduces an additional distinct-value restriction.

The original task does not require numeric values inside a combination to be unique.

Implementation Notes

The implementation does not need to allocate and store all 2600 combinations.

It may generate each combination, evaluate it immediately, and increment a counter when the condition is satisfied.

This keeps memory usage constant apart from the input collection.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation