Insert an element at specific array position

Hello friends, Let’s go through how can we add an element at specific array position. Let’s begin by adding the common code/caller.

Common code:

 public static void Driver()

{
int[] arr = new int[6];
arr[0] = 2;
arr[1] = 4;
arr[2] = 1;
arr[3] = 8;
arr[4] = 5;

int n = arr.Length;

Console.Write("Before Insertion: ");
for (int i = 0; i < n-1; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();

Console.Write("Plz enter the position followed by value, that to be inserted!");

List<int> input = Console.ReadLine().Split(' ').ToList().Select(x => Convert.ToInt32(x)).ToList();
int pos = input[0];
int x = input[1];

// Inserting key at specific position
//insertElement(arr, n, x, pos);
insertElement2(arr, n, x, pos);

Console.Write("\n\nAfter Insertion: ");
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}

Now time has come to create basic method to allow insertion of any element in specific position.

Approach-1 (Rudimentary)– Time complexity: O(n2)

private static void insertElement(int[] arr, int n, int x, int pos)

{
for (int i = 0; i <= n; i++)
{
if (i == pos)
{
int counter = n-1;
while (counter > i)
{
arr[counter] = arr[counter - 1];
counter--;
}
arr[i] = x;
}
}
}

Now let’s refine the basic approach and find effective approach.

Approach-2 (Efficient)– Time complexity: O(n)

private static void insertElement2(int[] arr, int n, int x, int pos)

{
//Move the elements to the right until Pos index
for (int i = n - 2; i >= pos; i--)
{
arr[i + 1] = arr[i];
}
arr[pos] = x;
}

Conclusion: We have seen how to insert an element in any specific position using both rudimentary and efficient approaches. Hope you liked the article.

You can find the explainer video and working code as following.

Explainer video: https://www.youtube.com/watch?v=zfL2QsrfUQQ

Working code: https://github.com/prakash-manit/DSA/blob/main/1.String_Array/InsPosArr.cs


Posted

in

by

Comments

Leave a comment