Program to reverse a Subarray in a given Array.


Given an array Arr of N integers, the task is to reverse a subarray of that array. The range of this subarray is given by L and 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 the sub-list is 1, then return the array.
  • If the size of the sub list 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:

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))

Output
Original Array    : [1, 2, 3, 4, 5, 6, 7]
Sub-Array reverse : [1, 6, 5, 4, 3, 2, 7]

Time complexity: O(log2(n))
Auxiliary space: O(1)

Related articles:


Given an array Arr of N integers, the task is to reverse a subarray of that array. The range of this subarray is given by L and 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 the sub-list is 1, then return the array.
  • If the size of the sub list 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:

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))

Output
Original Array    : [1, 2, 3, 4, 5, 6, 7]
Sub-Array reverse : [1, 6, 5, 4, 3, 2, 7]

Time complexity: O(log2(n))
Auxiliary space: O(1)

Related articles:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
arrayLatestProgramReverseSubarrayTechTechnoblender
Comments (0)
Add Comment