Challenge: Insert a Value Before an Index in Sorted Order

Problem Statement:

In this challenge, you have to implement the insert function which will be taking three parameters as inputs: array, rightIndex, and value.


Before the insert function is called:

  • the elements from array[0] to array[rightIndex] are sorted in ascending order.

  • the value is in the array at an index from array[rightIndex] to array[end].

After calling the insert function:

  • the value from the array is removed from its original position and it is then inserted between array[0] to array[rightIndex+1], maintaining the ascending order.

In order to do this, the insert function will need to make room for value by moving items that are greater than value to the right. It should start at rightIndex, and stop when it finds an item that is less than or equal to value, or when it reaches the beginning of the array. Once the function has made room for the value, it can write it to the array.

Function Prototype:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy