WWW Geeksforgeeks
WWW Geeksforgeeks
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
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
array or string
Input: Array[] = {1, 2, 3, 4, 5}, K = 4.
Print array after it is right
rotated K times
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
Easy problems on Array If i < K, Print rightmost Kth element (a[N + i -K]). Other wise,
// If rotation is greater
// than size of array
k = k % n;
// Printing rightmost
// kth elements
cout << a[n + i - k] << " ";
}
else
{
// 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);
}
Output
4 5 1 2 3
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 :
return 0;
}
Output
7 9 11 1 3 5
Complexit y Analysis :
Related Articles
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
10. Print the string after the specified character has occurred given no. of times
Like 40
Previous Next
Article Contributed B y :
mvk_0104
@mvk_0104
amankr0211
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
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
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
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 !