My issue
You are given an array in the IDE.
You are also given an element and an index.
Insert the element at the given index and output the new array to the console.
Check the sample output below for further clarity.
My code
# Function to insert an element at a given index in an array
def insert_element(array, element, index):
# Insert the element at the specified index
array.insert(index, element)
return array
# Example Input
array = [4, 5, 3, 10, 12, 18] # Initial array
element = 7 # Element to insert
index = 5 # Index to insert the element
# Call the function and get the updated array
updated_array = [4,5,3,10,12,7,18]
# Output the updated array
print(updated_array)
Learning course: Data structures & Algorithms lab
Problem Link: Insertion in an array - try it yourself! in Data structures & Algorithms lab