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++
#include <bits/stdc++.h>
using namespace std;
void printArray( int arr[], int N)
{
for ( int i = 0; i < N; i++)
{
cout << arr[i] << " " ;
}
}
void reverseCircularArray( int arr[],
int N, int K)
{
int start = K, end = K - 1;
int count = N / 2;
while (count--) {
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
start++;
end--;
if (end == -1) {
end = N - 1;
}
}
printArray(arr, N);
}
int main()
{
int arr[] = { 3, 5, 2, 4, 1 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
reverseCircularArray(arr, N, K);
return 0;
}
|
Java
class GFG {
static void printArray( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + " " );
}
}
static void reverseCircularArray( int arr[],
int N, int K)
{
int start = K, end = K - 1 ;
int count = N / 2 ;
while (count != 0 )
{
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
start++;
end--;
if (end == - 1 )
{
end = N - 1 ;
}
count -= 1 ;
}
printArray(arr, N);
}
public static void main (String[] args)
{
int arr[] = { 3 , 5 , 2 , 4 , 1 };
int K = 2 ;
int N = arr.length;
reverseCircularArray(arr, N, K);
}
}
|
Python3
def printArray(arr, N):
for i in range (N):
print (arr[i], end = " " )
def reverseCircularArray(arr, N, K):
start, end = K, K - 1
count = N / / 2
while (count):
temp = arr[start % N]
arr[start % N] = arr[end % N]
arr[end % N] = temp
start + = 1
end - = 1
if (end = = - 1 ):
end = N - 1
count - = 1
printArray(arr, N)
if __name__ = = '__main__' :
arr = [ 3 , 5 , 2 , 4 , 1 ]
K = 2
N = len (arr)
reverseCircularArray(arr, N, K)
|
C#
using System;
class GFG
{
static void printArray( int []arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
static void reverseCircularArray( int []arr,
int N, int K)
{
int start = K, end = K - 1;
int count = N / 2;
while (count != 0)
{
int temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
start++;
end--;
if (end == -1)
{
end = N - 1;
}
count -= 1;
}
printArray(arr, N);
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 2, 4, 1 };
int K = 2;
int N = arr.Length;
reverseCircularArray(arr, N, K);
}
}
|
Javascript
<script>
function printArray(arr, N)
{
for (let i = 0; i < N; i++)
{
document.write(arr[i] + " " );
}
}
function reverseCircularArray(arr, N, K)
{
let start = K, end = K - 1;
let count = Math.floor(N / 2);
while (count--) {
let temp = arr[start % N];
arr[start % N] = arr[end % N];
arr[end % N] = temp;
start++;
end--;
if (end === -1) {
end = N - 1;
}
}
printArray(arr, N);
}
let arr = [ 3, 5, 2, 4, 1 ];
let K = 2;
let N = arr.length;
reverseCircularArray(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...