package main
import "fmt"
func insertAtIndex(slice []int, index int, value int) []int {
// Step 1: Create a new slice with enough capacity to accommodate the new element.
result := make([]int, len(slice)+1)
// Step 2: Copy the elements before the insertion point from the original slice to the new slice.
copy(result[:index], slice[:index])
// Step 3: Insert the new element at the specified index in the new slice.
result[index] = value
// Step 4: Copy the elements after the insertion point from the original slice to the new slice.
copy(result[index+1:], slice[index:])
// Step 5: Return the modified slice with the new element inserted.
return result
}
func main() {
// Example usage:
originalSlice := []int{1, 2, 3, 4, 5}
index := 2
value := 10
modifiedSlice := insertAtIndex(originalSlice, index, value)
fmt.Printf("Original Slice: %v\n", originalSlice)
fmt.Printf("Modified Slice: %v\n", modifiedSlice)
}