Reverse all elements of given circular array starting from index K - GeeksforGeeks

08 Jun, 2021
Given a circular array arr[] of size N and an index K, the task is to reverse all elements of the circular array starting from the index K.
Examples:
Input: arr[] = {3, 5, 2, 4, 1}, K = 2Output: 4 2 5 3 1Explanation:After reversing the elements of the array from index K to K – 1, the modified arr[] is {4, 1, 2, 5, 3}.
Input: arr[] = {1, 2, 3, 4, 5}, K = 4Output: 3 2 1 5 4Explanation:After reversing the elements of the array from index K to K – 1, the modified arr[] is {3, 2, 1, 5, 4}.
Approach: To solve the given problem, the idea is to use Two Pointers Approach. Follow the steps below to solve the problem:
Initialize three variables start as K and end as (K – 1), to keep track of the boundary using two pointer approach, and count as N / 2.
Iterate until the value of count is positive and perform the following steps:Swap the elements arr[start % N] and arr[end % N].Increment start by 1 and decrement end by 1. If end is equal to -1, then update end as (N – 1).Decrement count by 1.
Swap the elements arr[start % N] and arr[end % N].
Increment start by 1 and decrement end by 1. If end is equal to -1, then update end as (N – 1).
Decrement count by 1.
After the above steps, print the updated array obtained after the above steps.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to print the array arr[]void printArray(int arr[], int N){    // Print the array    for (int i = 0; i < N; i++)    {        cout << arr[i] << " ";    }} // Function to reverse elements of given// circular array starting from index kvoid reverseCircularArray(int arr[],                          int N, int K){    // Initialize two variables as    // start = k and end = k-1    int start = K, end = K - 1;     // Initialize count = N/2    int count = N / 2;     // Loop while count > 0    while (count--) {         // Swap the elements at index        // (start%N) and (end%N)        int temp = arr[start % N];        arr[start % N] = arr[end % N];        arr[end % N] = temp;         // Update the start and end        start++;        end--;         // If end equals to -1        // set end = N-1        if (end == -1) {            end = N - 1;        }    }     // Print the circular array    printArray(arr, N);} // Driver Codeint main(){    int arr[] = { 3, 5, 2, 4, 1 };    int K = 2;    int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    reverseCircularArray(arr, N, K);     return 0;}
// Java program for the above approachclass GFG {   // Function to print the array arr[]  static void printArray(int arr[], int N)  {     // Print the array    for (int i = 0; i < N; i++)    {      System.out.print(arr[i] + " ");    }  }   // Function to reverse elements of given  // circular array starting from index k  static void reverseCircularArray(int arr[],                                   int N, int K)  {     // Initialize two variables as    // start = k and end = k-1    int start = K, end = K - 1;     // Initialize count = N/2    int count = N / 2;     // Loop while count > 0    while (count != 0)    {       // Swap the elements at index      // (start%N) and (end%N)      int temp = arr[start % N];      arr[start % N] = arr[end % N];      arr[end % N] = temp;       // Update the start and end      start++;      end--;       // If end equals to -1      // set end = N-1      if (end == -1)      {        end = N - 1;      }                 count -= 1;    }     // Print the circular array    printArray(arr, N);  }   // Driver Code  public static void main (String[] args)  {    int arr[] = { 3, 5, 2, 4, 1 };    int K = 2;    int N = arr.length;     // Function Call    reverseCircularArray(arr, N, K);    }} // This code is contributed by AnkThon
# Python3 program for the above approach # Function to print array arr[]def printArray(arr, N):         # Print the array    for i in range(N):        print(arr[i], end = " ") # Function to reverse elements of given# circular array starting from index kdef reverseCircularArray(arr, N, K):         # Initialize two variables as    # start = k and end = k-1    start, end = K, K - 1     # Initialize count = N/2    count = N // 2     # Loop while count > 0    while (count):                 # Swap the elements at index        # (start%N) and (end%N)        temp = arr[start % N]        arr[start % N] = arr[end % N]        arr[end % N] = temp         # Update the start and end        start += 1        end -= 1         # If end equals to -1        # set end = N-1        if (end == -1):            end = N - 1                     count -= 1     # Print the circular array    printArray(arr, N) # Driver Codeif __name__ == '__main__':         arr = [ 3, 5, 2, 4, 1 ]    K = 2    N = len(arr)         # Function Call    reverseCircularArray(arr, N, K) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System;class GFG{   // Function to print the array []arr  static void printArray(int []arr, int N)  {     // Print the array    for (int i = 0; i < N; i++)    {      Console.Write(arr[i] + " ");    }  }   // Function to reverse elements of given  // circular array starting from index k  static void reverseCircularArray(int []arr,                                   int N, int K)  {     // Initialize two variables as    // start = k and end = k-1    int start = K, end = K - 1;     // Initialize count = N/2    int count = N / 2;     // Loop while count > 0    while (count != 0)    {       // Swap the elements at index      // (start%N) and (end%N)      int temp = arr[start % N];      arr[start % N] = arr[end % N];      arr[end % N] = temp;       // Update the start and end      start++;      end--;       // If end equals to -1      // set end = N-1      if (end == -1)      {        end = N - 1;      }                 count -= 1;    }     // Print the circular array    printArray(arr, N);  }   // Driver Code  public static void Main(String[] args)  {    int []arr = { 3, 5, 2, 4, 1 };    int K = 2;    int N = arr.Length;     // Function Call    reverseCircularArray(arr, N, K);    }} // This code is contributed by 29AjayKumar
<script> // JavaScript program for the above approach // Function to print the array arr[]function printArray(arr, N){    // Print the array    for (let i = 0; i < N; i++)    {        document.write(arr[i] + " ");    }} // Function to reverse elements of given// circular array starting from index kfunction reverseCircularArray(arr, N, K){    // Initialize two variables as    // start = k and end = k-1    let start = K, end = K - 1;     // Initialize count = N/2    let count = Math.floor(N / 2);     // Loop while count > 0    while (count--) {         // Swap the elements at index        // (start%N) and (end%N)        let temp = arr[start % N];        arr[start % N] = arr[end % N];        arr[end % N] = temp;         // Update the start and end        start++;        end--;         // If end equals to -1        // set end = N-1        if (end === -1) {            end = N - 1;        }    }     // Print the circular array    printArray(arr, N);} // Driver Code    let arr = [ 3, 5, 2, 4, 1 ];    let K = 2;    let N = arr.length;     // Function Call    reverseCircularArray(arr, N, K); // This code is contributed by Surbhi Tyagi. </script>
4 2 5 3 1
 
Time Complexity: O(N)Auxiliary Space: O(1)
mohit kumar 29
ankthon
29AjayKumar
surbhityagi15
khushboogoyal499
circular-array
Reverse
two-pointer-algorithm
Arrays
Mathematical
two-pointer-algorithm
Arrays
Mathematical
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7