Be the first user to complete this post

  • 0
Add to List
Beginner

206. Reverse the given Array without using built in function

Objective: Given an array, write an algorithm to reverse the array.

Example:

int a[] = {1, 2, 3, 4, 5}
Output: {5, 4, 3, 2, 1}

Approach:

  • It's obvious that you cannot use any built-in function to reverse it.
  • It's a simple solution, we will solve it using recursive and non-recursive ways.
  • Take 2 elements at a time, one from the start and one from the end, and swap them.
  • Now for recursion, Make a recursive call to the rest of the string and for a non-recursive solution, use the for loop and swap the elements start +1 and end +1.

Output:

Original Array[1, 2, 3, 4, 5]
Reversed - Array(Iteration):[5, 4, 3, 2, 1]
Reversed Again - Array(Recursion):[1, 2, 3, 4, 5]