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 4 — Sorted Range Insertion

Objective

Create a function that inserts multiple integer values into an existing two-dimensional collection of sorted groups.

Each new value must be placed into the appropriate group.

The groups must remain sorted, and duplicate values must not be added.

Input

The function has two input arguments.

Existing Groups

The first argument is:

[][]int

with the following values:

groups := [][]int{
    {6, 11, 17},
    {24, 32, 38},
    {45, 50, 55},
}

Each inner slice represents an existing sorted group.

Values to Insert

The second argument is:

[]int

with the following values:

values := []int{
    15, 30, 11, 49, 50, 32,
}

Function

The function is named:

AddElements(...)

Example:

AddElements(groups, values)

Requirements

Process every value from the second argument.

For each value:

  1. determine the appropriate existing group
  2. check whether the value already exists in that group
  3. if the value already exists, do not insert it again
  4. otherwise insert it into the group
  5. preserve ascending order inside the group

Duplicate Handling

Duplicate values inside a group are not allowed.

For the provided input:

11
50
32

already exist in their corresponding groups.

They must therefore not be inserted again.

The values:

15
30
49

are new and must be inserted.

Example

Initial groups:

[][]int{
    {6, 11, 17},
    {24, 32, 38},
    {45, 50, 55},
}

Values to process:

[]int{
    15, 30, 11, 49, 50, 32,
}

The new values are placed as follows:

15 -> first group
30 -> second group
49 -> third group

Existing values are ignored:

11 -> already exists
50 -> already exists
32 -> already exists

Expected Result

[][]int{
    {6, 11, 15, 17},
    {24, 30, 32, 38},
    {45, 49, 50, 55},
}

Group Selection

The original example clearly associates:

15 with the first group
30 with the second group
49 with the third group

However, the original task does not explicitly define numeric lower and upper boundaries for these groups.

The implementation must therefore define a consistent rule for determining which existing range should receive a new value.

That rule should preserve the intended separation demonstrated by the example.

Sorting

Every group is sorted before processing begins.

After all insertions are complete, every group must still be sorted in ascending order.

The implementation may either:

  • find the correct insertion position for each new value
  • or insert values and restore sorting afterward

The final result must be equivalent.

Implementation Notes

The function should operate on other valid collections following the same grouped and sorted structure.

Duplicate detection applies within the group where a value would be inserted.

The original task does not specify behavior for a value that cannot be associated with any existing group.

If such input is supported, that behavior should be explicitly defined by the implementation.

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