Program to Reverse the subarray in between given range
Given an array arr and a range [L, R], the task is to reverse the subarray in between the given range [L, R].
Examples:
Input: arr = [1, 2, 3, 4, 5, 6, 7], L = 1, R = 5
Output: [1, 6, 5, 4, 3, 2, 7]
Input: arr = [10, 20, 30, 40, 50], L = 0, R = 2
Output: [30, 20, 10, 40, 50]
Approach: Follow the steps below to solve the problem:
- If the size of array is 1, then return the array.
- If the size of array is 2, then swap if the range lies between them.
- Else, Start swapping from L and R up to the mid of (L + R).
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
vector< int > subArrReverse(vector< int >& arr, int L, int R)
{
int no_of_elements = R-L + 1;
if (no_of_elements == 1){
return arr;
}
else if (no_of_elements == 2){
swap(arr[L], arr[R]);
return arr;
}
else {
}
int i = 0;
while (i < no_of_elements/2){
swap(arr[L+i], arr[R-i]);
if ((i != L + i+1 and i != R-i-1) and (L + i != L + i+1 and R-i != R-i-1)){
swap(arr[L + i+1], arr[R-i-1]);
}
i += 2;
}
return arr;
}
void print(vector< int >& arr)
{
for ( int i=0; i<arr.size(); i++)
cout<<arr[i]<< " " ;
}
int main()
{
vector< int > arr = {1, 2, 3, 4, 5, 6, 7};
int L = 1, R = 5;
cout<< "Original Array :" ;
print(arr);
cout<<endl;
cout<< "Sub-Array reverse :" ;
vector< int > ans = subArrReverse(arr, L, R);
print(ans);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static List<Integer> subArrReverse(List<Integer> arr, int L, int R) {
int no_of_elements = R-L + 1 ;
if (no_of_elements == 1 ){
return arr;
}
else if (no_of_elements == 2 ){
Collections.swap(arr, L, R);
return arr;
}
else {
int i = 0 ;
while (i < no_of_elements/ 2 ){
Collections.swap(arr, L+i, R-i);
if ((i != L + i+ 1 && i != R-i- 1 ) && (L + i != L + i+ 1 && R-i != R-i- 1 )){
Collections.swap(arr, L + i+ 1 , R-i- 1 );
}
i += 2 ;
}
return arr;
}
}
static void print(List<Integer> arr) {
for ( int i= 0 ; i<arr.size(); i++)
System.out.print(arr.get(i) + " " );
}
public static void main (String[] args) {
List<Integer> arr = Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 , 7 );
int L = 1 , R = 5 ;
System.out.print( "Original Array : " );
print(arr);
System.out.println();
System.out.print( "Sub-Array reverse : " );
List<Integer> ans = subArrReverse(arr, L, R);
print(ans);
}
}
|
Python3
def subArrReverse(arr, L, R):
no_of_elements = R - L + 1
if (no_of_elements = = 1 ):
return arr
elif (no_of_elements = = 2 ):
arr[L], arr[R], = arr[R], arr[L]
return arr
else :
i = 0
while (i < no_of_elements / / 2 ):
arr[L + i], arr[R - i] = arr[R - i], arr[L + i]
if ((i ! = L + i + 1 and i ! = R - i - 1 ) and (L + i ! = L + i + 1 and R - i ! = R - i - 1 )):
arr[L + i + 1 ], arr[R - i - 1 ] = arr[R - i - 1 ], arr[L + i + 1 ]
i + = 2
return arr
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
L = 1
R = 5
print ( 'Original Array :' , arr)
print ( 'Sub-Array reverse :' , subArrReverse(arr, L, R))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static List< int > SubArrReverse(List< int > arr, int L,
int R)
{
int noOfElements = R - L + 1;
if (noOfElements == 1) {
return arr;
}
else if (noOfElements == 2) {
var temp = arr[L];
arr[L] = arr[R];
arr[R] = temp;
return arr;
}
else {
int i = 0;
while (i < noOfElements / 2) {
var temp = arr[L + i];
arr[L + i] = arr[R - i];
arr[R - i] = temp;
if ((i != L + i + 1 && i != R - i - 1)
&& (L + i != L + i + 1
&& R - i != R - i - 1)) {
temp = arr[L + i + 1];
arr[L + i + 1] = arr[R - i - 1];
arr[R - i - 1] = temp;
}
i += 2;
}
return arr;
}
}
static void Print(List< int > arr)
{
foreach ( var element in arr)
{
Console.Write(element + " " );
}
}
static public void Main()
{
List< int > arr
= new List< int >{ 1, 2, 3, 4, 5, 6, 7 };
int L = 1, R = 5;
Console.Write( "Original Array : " );
Print(arr);
Console.WriteLine();
Console.Write( "Sub-Array reverse : " );
var ans = SubArrReverse(arr, L, R);
Print(ans);
}
}
|
Javascript
function subArrReverse(arr, L, R) {
let no_of_elements = R - L + 1;
if (no_of_elements == 1) {
return arr;
}
else if (no_of_elements == 2) {
[arr[L], arr[R]] = [arr[R], arr[L]];
return arr;
}
else {
let i = 0;
while (i < no_of_elements / 2) {
[arr[L + i], arr[R - i]] = [arr[R - i], arr[L + i]];
if ((i != L + i + 1 && i != R - i - 1) && (L + i != L + i + 1 && R - i != R - i - 1)) {
[arr[L + i + 1], arr[R - i - 1]] = [arr[R - i - 1], arr[L + i + 1]
];
}
i += 2;
}
}
return arr;
}
let arr = [1, 2, 3, 4, 5, 6, 7];
let L = 1;
let R = 5;
console.log( 'Original Array :' , arr + "<br>" );
console.log( 'Sub-Array reverse :' , subArrReverse(arr, L, R));
|
Output
Original Array : [1, 2, 3, 4, 5, 6, 7]
Sub-Array reverse : [1, 6, 5, 4, 3, 2, 7]
Time Complexity: O(n)
Auxiliary Space: O(1)
Related articles:
Last Updated :
20 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...