Reverse all elements of given circular array starting from index K
Last Updated :
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 = 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Sort M elements of given circular array starting from index K Given a circular array arr[] of size N and two integers K and M, the task is to sort M array elements starting from the index K. Examples: Input: arr[] = {4, 1, 6, 5, 3}, K = 2, M = 3Output: 4 1 3 5 6Explanation: After sorting 3 array elements starting from index 2 modifies arr[] to {4, 1, 3, 5, 6}.
13 min read
Remove the Kth element from a given range in circular order Given an integer K and a range [L, R] from that the elements are placed on a circle in clockwise order such that ith and (i+1)th elements are adjacent for all L ⤠i ⤠R, and L and R are also adjacent. In each move, find the integer that is at the Kth place from the smallest one in clockwise order an
5 min read
Replace every element in a circular array by sum of next K elements Given a circular array arr[] of N integers and an integer K, the task is to print the array after the following operations: If K is non-negative, then replace A[i] with the sum of the next K elements.If K is negative, then replace it with the sum of previous K elements. A cyclic array means the next
15+ min read
Find the next greater element in a Circular Array | Set 2 Given a circular array arr[] consisting of N integers, the task is to print the Next Greater Element for every element of the circular array. Elements for which no greater element exist, print â-1â. Examples: Input: arr[] = {5, 6, 7}Output: 6 7 -1Explanation: The next greater element for every array
7 min read
Find the next greater element in a Circular Array Given a circular array arr[ ] of size n having distinct elements, the task is to find the next greater element for each element of the array.Note : The next greater element of an element in the array is the first greater number to its traversing-order next in the array, which means you could search
9 min read
Find element at given index after a number of rotations Given an array of N integers, M ranges of indices as queries, and a specific index K, the task is to right rotate the array circularly between query ranges, and return the element at Kth index in the final array. Examples: Input: arr[] : {1, 2, 3, 4, 5}, M = 2, queries[] = { {0, 2}, {0, 3} }, K = 1O
13 min read
Shortest path to traverse all the elements of a circular array in increasing order There are N distinct integers arranged on a circle. The distance between any two adjacent numbers is 1. The task is to travel on this circle starting with the smallest number, then moving to the second smallest, third smallest, and so on until the largest number and print the minimum travel distance
6 min read
Next same parity element in Circular Array Given a circular array arr[] of size N, the task is to find the next integers of same parity for every element in arr[]. If the next integers with the same parity does not exist, return -1 for that number. Examples: Input: arr[] = {2, 4, 3, 6, 5}Output: 4 6 5 2 3Explanation: For 2 the next element w
6 min read
Find the index in a circular array from which prefix sum is always non-negative Given a circular array arr[] consisting of N integers, the task is to find the starting index of the circular array such that the prefix sum from that index is always non-negative. If there exists no such index, then print "-1". Examples: Input: arr[] = {3, -6, 7, -1, -4, 5, -1}Output: 2Explanation:
6 min read
Kth Smallest Element in a sorted array formed by reversing subarrays from a random index Given a sorted array arr[] of size N and an integer K, the task is to find Kth smallest element present in the array. The given array has been obtained by reversing subarrays {arr[0], arr[R]} and {arr[R + 1], arr[N - 1]} at some random index R. If the key is not present in the array, print -1. Examp
13 min read