Open In App

Program to Reverse the subarray in between given range

Improve
Improve
Like Article
Like
Save
Share
Report

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]

Recommended Practice

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




// C++ program to reverse subarray Array in the given range
 
#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 only one element present, then
    // return the array
    if(no_of_elements == 1){
        return arr;
    }
     
    // If only two elements present, then swap
    // both the numbers.
    else if(no_of_elements == 2){
        swap(arr[L], arr[R]);
        return arr;
    }
 
    // If more than two elements presents, then
    // swap beginning and ending numbers.
    else{
    }
        // Pointer variable to select the elements
    int i = 0;
     
 
    // Swap beginning and ending numbers in given
    // indexes.
    while(i < no_of_elements/2){
         swap(arr[L+i], arr[R-i]);
 
         // If beginning and ending number are same skip
         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]<<" ";
}
 
// Driver code
int main()
{
    vector<int> arr = {1, 2, 3, 4, 5, 6, 7};
    int L = 1, R = 5;
     
    cout<<"Original Array :";
      print(arr);
      cout<<endl;
     
    // Function call
    cout<<"Sub-Array reverse :";
    vector<int> ans = subArrReverse(arr, L, R);
    print(ans);
    return 0;
}


Java




// Java program to reverse an sublist in List
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 only one element present, then
    // return the array
    if(no_of_elements == 1){
      return arr;
    }
 
    // If only two elements present, then swap
    // both the numbers.
    else if(no_of_elements == 2){
      Collections.swap(arr, L, R);
      return arr;
    }
 
    // If more than two elements presents, then
    // swap beginning and ending numbers.
    else{
      // Pointer variable to select the elements
      int i = 0;
 
      // Swap beginning and ending numbers in given
      // indexes.
      while(i < no_of_elements/2){
        Collections.swap(arr, L+i, R-i);
 
        // If beginning and ending number are same skip
        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();
 
    // Function call
    System.out.print("Sub-Array reverse : ");
    List<Integer> ans = subArrReverse(arr, L, R);
    print(ans);
  }
}
 
// This code is contributed by lokesh.


Python3




# Python program to reverse an sublist in List
 
def subArrReverse(arr, L, R):
    no_of_elements = R-L + 1
 
    # If only one element present, then
    # return the array
    if(no_of_elements == 1):
        return arr
 
    # If only two elements present, then swap
    # both the numbers.
    elif(no_of_elements == 2):
        arr[L], arr[R], = arr[R], arr[L]
        return arr
 
    # If more than two elements presents, then
    # swap beginning and ending numbers.
    else:
 
        # Pointer variable to select the elements
        i = 0
 
    # Swap beginning and ending numbers in given
    # indexes.
        while(i < no_of_elements//2):
            arr[L + i], arr[R-i] = arr[R-i], arr[L + i]
 
    # If beginning and ending number are same skip
            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
 
 
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7]
L = 1
R = 5
 
print('Original Array    :', arr)
 
# Function call
print('Sub-Array reverse :', subArrReverse(arr, L, R))


C#




// C# code for the above approach
 
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 only one element present, then
    // return the array
    if (noOfElements == 1) {
      return arr;
    }
 
    // If only two elements present, then swap
    // both the numbers.
    else if (noOfElements == 2) {
      var temp = arr[L];
      arr[L] = arr[R];
      arr[R] = temp;
      return arr;
    }
 
    // If more than two elements presents, then
    // swap beginning and ending numbers.
    else {
      // Pointer variable to select the elements
      int i = 0;
 
      // Swap beginning and ending numbers in given
      // indexes.
      while (i < noOfElements / 2) {
        var temp = arr[L + i];
        arr[L + i] = arr[R - i];
        arr[R - i] = temp;
 
        // If beginning and ending number are same
        // skip
        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()
  {
 
    // Code
    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();
 
    // Function call
    Console.Write("Sub-Array reverse : ");
    var ans = SubArrReverse(arr, L, R);
    Print(ans);
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




       // JavaScript code for the above approach
       function subArrReverse(arr, L, R) {
           let no_of_elements = R - L + 1;
 
           // If only one element present, then
           // return the array
           if (no_of_elements == 1) {
               return arr;
           }
           // If only two elements present, then swap
           // both the numbers.
           else if (no_of_elements == 2) {
               [arr[L], arr[R]] = [arr[R], arr[L]];
               return arr;
           }
           // If more than two elements presents, then
           // swap beginning and ending numbers.
           else {
               // Pointer variable to select the elements
               let i = 0;
 
               // Swap beginning and ending numbers in given
               // indexes.
               while (i < no_of_elements / 2) {
                   [arr[L + i], arr[R - i]] = [arr[R - i], arr[L + i]];
 
                   // If beginning and ending number are same skip
                   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;
       }
 
       // Driver code
       let arr = [1, 2, 3, 4, 5, 6, 7];
       let L = 1;
       let R = 5;
 
       console.log('Original Array :', arr + "<br>");
 
       // Function call
       console.log('Sub-Array reverse :', subArrReverse(arr, L, R));
 
// This code is contributed by Potta Lokesh


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
Previous
Next
Share your thoughts in the comments
Similar Reads