Reverse all elements of given circular array starting from index K

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 = 2
Output: 4 2 5 3 1
Explanation:
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 = 4
Output: 3 2 1 5 4
Explanation:
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.
- After the above steps, print the updated array obtained after the above steps.
Below is the implementation of the above approach:
C++
// 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
// 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
# 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#
// 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 |
Javascript
<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> |
Output:Â
4 2 5 3 1
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



