Right rotate given Array K times using Pointers
Last Updated :
19 Jan, 2022
Given an array arr[] of size N and an integer K, the task is to right rotate the array K times.
Examples:
Input: arr[] = {1, 3, 5, 7, 9}, K = 2
Output: 7 9 1 3 5
Explanation: After 1st rotation - {9, 1, 3, 5, 7}
After 2nd rotation - {7, 9, 1, 3, 5}
Input: {1, 2, 3, 4, 5, 6}, K = 2
Output: 5 6 1 2 3 4
Approach: The naive approach and approach based on reversing parts of the array is discussed here.
Pointer based approach: The base of this concept is the reversal algorithm for array rotation. The array is divided into two parts where the first part is of size (N-K) and the end part is of size K. These two parts are individually reversed. Then the whole array is reversed.
Below is the implementation of the above approach:
C++
// C++ code to implement above approach
#include <iostream>
using namespace std;
// Function to print the array
void print(int arr[], int N)
{
for (int i = 0; i < N; i++)
cout << *(arr + i) << " ";
}
// Function to reverse the array
// from start to end index
void reverse(int arr[], int start, int end)
{
int temp;
int size = end - start;
// Reversal based on pointer approach
for (int i = 0; i < (size / 2); i++) {
temp = *(arr + i + start);
*(arr + i + start) = *(arr + start
+ size - i - 1);
*(arr + start + size - i - 1) = temp;
}
}
// Function to right rotate the array K times
void right(int arr[], int K, int N)
{
reverse(arr, 0, N - K);
reverse(arr, N - K, N);
reverse(arr, 0, N);
print(arr, N);
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
right(arr, K, N);
return 0;
}
Java
// Java code to implement above approach
import java.util.*;
class GFG{
// Function to print the array
static void print(int arr[], int N)
{
for (int i = 0; i < N; i++)
System.out.print(arr[i]+ " ");
}
// Function to reverse the array
// from start to end index
static int[] reverse(int arr[], int start, int end)
{
int temp;
int size = end - start;
// Reversal based on pointer approach
for (int i = 0; i < (size / 2); i++) {
temp = arr[ i + start];
arr[i + start] = arr[start
+ size - i - 1];
arr[start + size - i - 1] = temp;
}
return arr;
}
// Function to right rotate the array K times
static void right(int arr[], int K, int N)
{
arr = reverse(arr, 0, N - K);
arr = reverse(arr, N - K, N);
arr = reverse(arr, 0, N);
print(arr, N);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = arr.length;
int K = 2;
right(arr, K, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code to implement above approach
# Function to print array
def print1(arr, N):
for i in range(N):
print(arr[i], end = " ");
# Function to reverse the array
# from start to end index
def reverse(arr, start, end):
temp = 0;
size = end - start;
# Reversal based on pointer approach
for i in range(size//2):
temp = arr[i + start];
arr[i + start] = arr[start + size - i - 1];
arr[start + size - i - 1] = temp;
return arr;
# Function to right rotate the array K times
def right(arr, K, N):
arr = reverse(arr, 0, N - K);
arr = reverse(arr, N - K, N);
arr = reverse(arr, 0, N);
print1(arr, N);
# Driver code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6];
N = len(arr);
K = 2;
right(arr, K, N);
# This code is contributed by 29AjayKumar
C#
// C# code to implement above approach
using System;
class GFG {
// Function to print the array
static void print(int[] arr, int N)
{
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
// Function to reverse the array
// from start to end index
static void reverse(int[] arr, int start, int end)
{
int temp;
int size = end - start;
// Reversal based on pointer approach
for (int i = 0; i < (size / 2); i++) {
temp = arr[i + start];
arr[i + start] = arr[start + size - i - 1];
arr[start + size - i - 1] = temp;
}
}
// Function to right rotate the array K times
static void right(int[] arr, int K, int N)
{
reverse(arr, 0, N - K);
reverse(arr, N - K, N);
reverse(arr, 0, N);
print(arr, N);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
int K = 2;
right(arr, K, N);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript code to implement above approach
// Function to print the array
const print = (arr, N) => {
for (let i = 0; i < N; i++)
document.write(`${arr[i]} `);
}
// Function to reverse the array
// from start to end index
const reverse = (arr, start, end) => {
let temp;
let size = end - start;
// Reversal based on pointer approach
for (let i = 0; i < parseInt(size / 2); i++) {
temp = arr[i + start];
arr[i + start] = arr[start + size - i - 1];
arr[start + size - i - 1] = temp;
}
}
// Function to right rotate the array K times
const right = (arr, K, N) => {
reverse(arr, 0, N - K);
reverse(arr, N - K, N);
reverse(arr, 0, N);
print(arr, N);
}
// Driver code
let arr = [1, 2, 3, 4, 5, 6];
let N = arr.length;
let K = 2;
right(arr, K, N);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Print array after it is right rotated K times | Set 2 Given an array arr[] of size N and a value K, the task is to print the array rotated by K times to the right. Examples: Input: arr = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5 Input: arr = {1, 2, 3, 4, 5}, K = 4Output: 2 3 4 5 1 Algorithm: The given problem can be solved by reversing subarrays. Below s
13 min read
Reverse an Array in groups of given size Given an array arr[] and an integer k, find the array after reversing every subarray of consecutive k elements in place. If the last subarray has fewer than k elements, reverse it as it is. Modify the array in place, do not return anything.Examples: Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], k = 3Outp
5 min read
Pointers vs Array in C++ Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
3 min read
implement k Queues in a single array Given an array of size n, the task is to implement k queues using the array.enqueue(qn, x) : Adds the element x into the queue number qn dequeue(qn, x) : Removes the front element from queue number qn isFull(qn) : Checks if the queue number qn is fullisEmpty(qn) : Checks if the queue number qn is em
15+ min read
Remove elements from the array which appear more than k times Given an array of integers, remove all the occurrences of those elements which appear strictly more than k times in the array.Examples: Input : arr[] = {1, 2, 2, 3, 2, 3, 4} k = 2Output : 1 3 3 4Input : arr[] = {2, 5, 5, 7} k = 1Output : 2 7Approach: Take a hash map, which will store the frequency o
8 min read