Home » Interview Questions » Array Interview Questions » Write a program to Reverse an Array

Write a program to Reverse an Array


()

Array is the sequential collection of similar elements. Array is a fixed size data structure. Arrays can be accessed by index which starts from zero. Reversing the array helps in arranging the all the elements of an array like we are reading elements from the end. We have to write a C++ program to reverse the given array.

Given an array of integers and you have to reverse it

There are various methods to reverse an array. Below is an array as input and we have to write a program to Reverse an Array

INPUT: 1 2 3 4 5

OUTPUT: 5 4 3 2 1

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Method 1″][/td_block_text_with_title][/vc_column][/vc_row]

TIME COMPLEXITY: O(N)

SPACE COMPLEXITY: O(1)

We take two variables start (point at first element of an array) and end (Point at last element of an array). Write a program to Reverse an Array

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Algorithm for Method 1″][/td_block_text_with_title][/vc_column][/vc_row]

1. Loop till start is less than end.

2. Keep on swapping the elements pointed by start and end.

3. Increment start and Decrement end variable.

4. As start equals end or Greater than end then we will stop the loop.

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Demonstration of Method 1″][/td_block_text_with_title][/vc_column][/vc_row]

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Program for Method 1″][/td_block_text_with_title][/vc_column][/vc_row]

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr[] = {4,1,8,9,11,3,77,2};
	int N = sizeof(arr)/sizeof(arr[0]);			
	
	int start =0 , end = N-1;
	
	while(start < end)
	{
		swap(arr[start++],arr[end--]);
	}
		
	for(int i=0;i<N;i++)
	{
		cout<<arr[i]<<" ";
	}
	
	cout<<endl;		
}

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Method 2″][/td_block_text_with_title][/vc_column][/vc_row]

TIME COMPLEXITY: O(N)

SPACE COMPLEXITY: O(N)

Algorithm

Like Method1, we will do traversing forward and backward by swapping values. But in this case we will use recursion. It uses extra space because of the usage of Stack. Write a program to Reverse an Array.

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Program for Method 2″][/td_block_text_with_title][/vc_column][/vc_row]

#include <bits/stdc++.h>
using namespace std;

void reverse(int arr[],int N,int start,int end)
{
	if(start > end)
	{
		return ;
	}	
	else
	{
		swap(arr[start],arr[end]);
		reverse(arr,N,start+1,end-1);
	}
}

int main()
{
	int arr[] = {4,1,8,9,11,3,77,2};
	int N = sizeof(arr)/sizeof(arr[0]);		
	reverse(arr,N,0,N-1);	

	for(int i=0;i<N;i++)
	{
		cout<<arr[i]<<" ";
	}
}

Try It

READ  Search Insert Position

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Method 3″][/td_block_text_with_title][/vc_column][/vc_row]

TIME COMPLEXITY: O(N)

SPACE COMPLEXITY: O(1)

Given and array, write a program to reverse an array.

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Algorithm for Method 3″][/td_block_text_with_title][/vc_column][/vc_row]

1. Take the input in vector variable .

2. Use STL function reverse(variable.begin(),variable.end())

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”Program for Method 3″][/td_block_text_with_title][/vc_column][/vc_row]

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr[] = {4,1,8,9,11,3,77,2};
	int N = sizeof(arr)/sizeof(arr[0]);
	vector V;
			
	for(int i=0;i<N;i++)
	{
		V.push_back(arr[i]);	
	}
		
	reverse(V.begin(),V.end());
		
	for(int i=0;i<N;i++)
	{
		cout<<V[i]<<" ";
	}		
	
	cout<<endl;
}

Try It

This is a commonly asked question in interviews where they want to check your programming and logical skills to reverse an array. Array reverse algorithm should be efficient. Array reverse program should be written in O(n) time complexity then only your solution will be considered. Commonly asked questions for this are

  1. Write a program to Reverse an Array
  2. Reverse an array
  3. Array reverse program
  4. Reverse array
  5. Can you reverse an array efficiently?
  6. Fastest way to reverse array

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote Count

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions