Program to Reverse the subarray in between given range
Last Updated :
20 Jan, 2023
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
// 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
OutputOriginal 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:
Similar Reads
Reverse a subarray of the given array to minimize the sum of elements at even position Given an array arr[] of positive integers. The task is to reverse a subarray to minimize the sum of elements at even places and print the minimum sum. Note: Perform the move only one time. Subarray might not be reversed. Example: Input: arr[] = {1, 2, 3, 4, 5} Output: 7 Explanation: Sum of elements
15 min read
Minimum Subarray reversals to sort given Binary Array Given a binary array A[] of size N, the task is to find the minimum number of subarrays that need to be reversed to sort the binary array. Examples: Input: N = 4, A[]: {1, 0 , 0, 1} Output: 1Explanation: Reverse the array from 0 to 2 to change the array to {0, 0, 1, 1} Input: N = 4, A[]: {1, 0, 1 ,
5 min read
Find Subarray ranges having difference between max and min exactly K Given an array arr[] of length N and integer K, the task is to print subarray ranges (starting index, ending index) of the array where difference between max and min elements of the subarray is exactly K.( 1-based index ) Examples: Input: arr[] = {2, 1, 3, 4, 2, 6}, K = 2Output: (1, 3), (2, 3), (3,
11 min read
Minimum subarray reversals required to make given binary array alternating Given a binary array arr[] consisting of equal count of 0s and 1s, the task is to count the minimum number of subarray reversal operations required to make the binary array alternating. In each operation reverse any subarray of the given array. Examples: Input: arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 } Out
5 min read
Program to reverse columns in given 2D Array (Matrix) Given a 2D array arr[][]of integers of size M x N, where N is the number of columns and M is the number of rows in the array. The task is to reverse every column of the given 2D array Input: arr[][] = {{3, 2, 1} {4, 5, 6}, {9, 8, 7}} Output: 9 8 7 4 5 6 3 2 1 Input: arr[][] = {{7, 9}, {1, 5}, {4, 6}
7 min read
Find element at given index after given range reversals An array consisting of N elements is given. There are several reversals we do in unique ranges[L..R]. The task is to print the element at given index.Examples: Input : arr[] : 10 20 30 40 50ranges[] = {{1, 4}, {0, 2}}Query Index = 1Output : 50Explanation : Reverse range[1..4] : 10 50 40 30 20Reverse
10 min read
Count of all possible reverse bitonic subarrays Given an array arr[] of N integers, the task is to count the total number of Reverse Bitonic Subarray from the given array. A Reverse Bitonic Subarray is a subarray in which elements are arranged in decreasing order and then arranged in increasing order. A strictly increasing or strictly decreasing
8 min read
First subarray with negative sum from the given Array Given an array arr[] consisting of N integers, the task is to find the start and end indices of the first subarray with a Negative Sum. Print "-1" if no such subarray exists. Note: In the case of multiple negative-sum subarrays in the given array, the first subarray refers to the subarray with the l
15+ min read
Maximum sum subarray after altering the array Given an array arr[] of size N. The task is to find the maximum subarray sum possible after performing the given operation at most once. In a single operation, you can choose any index i and either the subarray arr[0...i] or the subarray arr[i...N-1] can be reversed. Examples: Input: arr[] = {3, 4,
15+ min read
Sort the Array by reversing the numbers in it Given an array arr[] of N non-negative integers, the task is to sort these integers according to their reverse. Examples: Input: arr[] = {12, 10, 102, 31, 15} Output: 10 31 12 15 102 Reversing the numbers: 12 -> 21 10 -> 01 102 -> 201 31 -> 13 15 -> 51 Sorting the reversed numbers: 01
6 min read