Insert at index
Warm-up · Trace the algorithm · about 4 min
The array [3, 8, 1, 9, 6] has one free slot at the end. Insert 7 at index 2 and answer each question before the step plays.
Example 1
Input:
{"values":[3,8,1,9,6],"index":2,"value":7}
Output:
[3,8,7,1,9,6]
Constraints
- The trace uses the input shown.
- Values shift one cell at a time, starting from the right end.
Learn the idea first: Arrays: indexing, traversal, insertion and deletion costs, a visual lesson.
More Arrays: indexing, traversal, insertion and deletion costs problems
- Count shifts Warm-up
- Reverse in place Warm-up
- Rotate by k Standard
- Delete at index by shifting Standard
- Move zeroes to the end in place Challenge