Open In App

Reverse all elements of given circular array starting from index K

Improve
Improve
Like Article
Like
Save
Share
Report

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 k
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--) {
 
        // 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 Code
int 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 approach
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++)
    {
      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 k
def 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 Code
if __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 approach
using 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 k
function 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)



Last Updated : 08 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads