0% found this document useful (0 votes)
30 views

WWW Geeksforgeeks

The document discusses rotating an array to the right by a given number of positions. It provides an example and two approaches to solve this problem - printing the rotated elements directly or reversing parts of the array. The key steps of the first approach are taking the modulo of the rotation count to handle wrapping, and conditionally printing either the rightmost element or elements after the rotation count. The time complexity is O(n) with constant auxiliary space.

Uploaded by

Test acc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

WWW Geeksforgeeks

The document discusses rotating an array to the right by a given number of positions. It provides an example and two approaches to solve this problem - printing the rotated elements directly or reversing parts of the array. The key steps of the first approach are taking the modulo of the rotation count to handle wrapping, and conditionally printing either the rightmost element or elements after the rotation count. The time complexity is O(n) with constant auxiliary space.

Uploaded by

Test acc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Courses Tutorials Jobs Practice Contests Sign In

DSA Array Matrix Strings Hashing Linked List Stack Queue Binary Tree Binary Search Tree Heap Graph Searching Sorting Divide & Conquer Mathematical Geom

Write an Article

C ourses
Write an Interview Experience

Print array after it is right rotated K times


Array Data Structure

Difficulty Level : Easy ● Last Updated : 07 Dec, 2022


What is Array?

Read Discuss Courses Practice Video 756k+ interested Geeks 111k+ in


Introduction to Arrays – Data

Structure and Algorithm Tutorials


Complete Interview Data Str
Given an Array of size N and a values K, around which we need to right rotate the array.
Preparation - Self Paced Algorithm

Applications, Advantages and How to quickly print the right rotated array?
Beginner to Advance Beginn
Disadvantages of Array
Examples : 

 
Subarrays, Subsequences, and

Subsets in Array

Input: Array[] = {1, 3, 5, 7, 9}, K = 2.


Basic operations in Array Output: 7 9 1 3 5
Explanation:
Array | Searching After 1st rotation - {9, 1, 3, 5, 7}
After 2nd rotation - {7, 9, 1, 3, 5}
Write a program to reverse an

array or string
Input: Array[] = {1, 2, 3, 4, 5}, K = 4.

Program for array left rotation


Output: 2 3 4 5 1
by d positions.

 
Print array after it is right

rotated K times

Recommended: Please tr y your approach on {IDE} first, before moving on to the

How to search, insert, and delete


solution.
in an unsorted array:

Search, insert and delete in a


Approach:

sorted array
 

Array | Sorting
1. We will first take mod of K by N (K = K % N) because af ter ever y N rotation array will

become the same as the initial array. 

Generating subarrays using


 
recursion

2. Now, we will iterate the array from i = 0 to i = N-1 and check, 

Easy problems on Array If i < K, Print rightmost Kth element (a[N + i -K]). Other wise, 

Intermediate problems on Array Print array af ter ‘K ’ element s (a[i – K]). 

Hard problems on Array

Below is the implementation of the above approach. 

C++ Java P ython3 C# Javascript

Interview Preparation Corner


// C++ implementation of right rotation Interview Experiences | Internship
// of an array K number of times
#include<bits/stdc++.h> Interview Experiences | SDE
using namespace std; Interview Experiences | Professional
Company-Wise Preparation
// Function to rightRotate array
void RightRotate(int a[], int n, int k)
{

// If rotation is greater
// than size of array
k = k % n;

for(int i = 0; i < n; i++)


{
if(i < k)
{

// Printing rightmost
// kth elements
cout << a[n + i - k] << " ";
}
else
{

// Prints array after


// 'k' elements
cout << (a[i - k]) << " ";
}
}
cout << "\n";
}

// Driver code
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int N = sizeof(Array) / sizeof(Array[0]);
int K = 2;

RightRotate(Array, N, K);
}

// This code is contributed by Surendra_Gangwar

Output

4 5 1 2 3

Time complexit y : O(n) 

Auxiliar y Space : O(1)

Method 2: Reversing the array 

Approach: The approach is simple yet optimized. The idea is to reverse the array three

times. For the first time we reverse only the last k element s. Second time we will reverse

first n-k(n=size of array) element s. Finally we will get our rotated array by reversing the

entire array.

Code :

C++ C Java P ython3 C# Javascript

// C++ program to rotate right an array by K times


#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3; //No. of rotations
k = k % n;
int i, j;
// Reverse last k numbers
for (i = n - k, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the first n-k terms
for (i = 0, j = n - k - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// Print the rotated array


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}

return 0;
}

Output

7 9 11 1 3 5

Complexit y Analysis :

Time Complexit y: O(N).

Auxiliar y Space : O(1).

Please see following post s for other methods of array rotation: 

https://www.geek sforgeek s.org/print-array-af ter-it-is-right-rotated-k-times-set-2/

Related Articles

1. C++ Program to Print array after it is right rotated K times

2. Java Program to Print array after it is right rotated K times

3. Python Program to Print array after it is right rotated K times

4. Javascript Program to Print array after it is right rotated K times

5. Print array after it is right rotated K times | Set 2

6. Print Array after it is right rotated K times where K can be large or negative

7. Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6

atmost Z times

8. Print a matrix in alternate manner (left to right then right to left)

9. Circularly Sorted Array (Sorted and Rotated Array)

10. Print the string after the specified character has occurred given no. of times

Like 40

Previous Next

Program for array left rotation How to search, insert, and

by d positions. delete in an unsorted array:

Article Contributed B y :

mvk_0104

@mvk_0104

Vote for di cult y

Current di culty : Easy

Easy Normal Medium Hard Expert

Improved By : ipg2016107, Rohit_ranjan, Code_Mech, gfgking, hguru001,

shreyasnaphad, singhh3010, pushpeshrajdx01, aadityapburujwale,

amankr0211

Article Tags : rotation, Arrays, DSA

Practice Tags : Arrays

Improve Article Report Issue

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
Skip to content
Start Your Coding Journey Now! Login Register

Company Learn News Languages Web Development Contribute

Top News
About Us DSA Python Web Tutorials Write an Article
A-143, 9th Floor, Sovereign Corporate Tower,
Sector-136, Noida, Uttar Pradesh - 201305 Technology
Careers Algorithms Java Django Tutorial Improve an Article

[email protected] Work & Career


In Media Data Structures CPP HTML Pick Topics to Write

Business
Contact Us SDE Cheat Sheet Golang JavaScript Write Interview Experience

Finance
Privacy Policy Machine learning C# Bootstrap Internships

Lifestyle
Copyright Policy CS Subjects SQL ReactJS Video Internship

Knowledge
Advertise with us Video Tutorials Kotlin NodeJS

Courses

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !

You might also like