Given an array (or string), the task is to reverse the array/string.
Examples :
Input : arr = {1, 2, 3}
Output : arr = {3, 2, 1}
Input : arr = {4, 5, 1, 2}
Output : arr = {2, 1, 5, 4}
Iterative way :
- Initialize start and end indexes as start = 0, end = n-1
- In a loop, swap arr[start] with arr[end] and change start and end as follows :
start = start +1, end = end – 1
Below is the implementation of the above approach :
// Iterative java program to reverse an
// array
public class GFG {
/* Function to reverse arr[] from
start to end*/
static void rvereseArray( int arr[],
int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
/* Utility that prints out an
array on a line */
static void printArray( int arr[],
int size)
{
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
// Driver code
public static void main(String args[]) {
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
printArray(arr, 6 );
rvereseArray(arr, 0 , 5 );
System.out.print( "Reversed array is \n" );
printArray(arr, 6 );
}
}
Output :
1 2 3 4 5 6 Reversed array is 6 5 4 3 2 1
Time Complexity : O(n)
Recursive Way :
- Initialize start and end indexes as start = 0, end = n-1
- Swap arr[start] with arr[end]
- Recursively call reverse for rest of the array.
Below is the implementation of the above approach :
// Recursive Java Program to reverse an array
import java.io.*;
class ReverseArray {
/* Function to reverse arr[] from start to end*/
static void rvereseArray( int arr[], int start, int end)
{
int temp;
if (start >= end)
return ;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseArray(arr, start+ 1 , end- 1 );
}
/* Utility that prints out an array on a line */
static void printArray( int arr[], int size)
{
for ( int i= 0 ; i < size; i++)
System.out.print(arr[i] + " " );
System.out.println( "" );
}
/*Driver function to check for above functions*/
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
printArray(arr, 6 );
rvereseArray(arr, 0 , 5 );
System.out.println( "Reversed array is " );
printArray(arr, 6 );
}
}
Output :
1 2 3 4 5 6 Reversed array is 6 5 4 3 2 1
Time Complexity : O(n)