Javascript Program to Print array after it is right rotated K times Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples : Input: Array[] = {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: Array[] = {1, 2, 3, 4, 5}, K = 4. Output: 2 3 4 5 1 Approach: We will first take mod of K by N (K = K % N) because after every N rotations array will become the same as the initial array. Now, we will iterate the array from i = 0 to i = N-1 and check, If i < K, Print rightmost Kth element (a[N + i -K]). Otherwise, Print array after 'K' elements (a[i - K]). Below is the implementation of the above approach. JavaScript // Javascript implementation of right rotation // of an array K number of times // Function to rightRotate array function RightRotate(a, n, k) { // If rotation is greater // than size of array k = k % n; for (let i = 0; i < n; i++) { if (i < k) { // Printing rightmost // kth elements document.write(a[n + i - k] + " "); } else { // Prints array after // 'k' elements document.write((a[i - k]) + " "); } } document.write("<br>"); } // Driver code let Array = [1, 2, 3, 4, 5]; let N = Array.length; let K = 2; RightRotate(Array, N, K); // This code is contributed by gfgking. Output: 4 5 1 2 3 Time complexity : O(n) Auxiliary Space : O(1) Please refer complete article on Print array after it is right rotated K times for more details! Create Quiz Comment K kartik Follow 1 Improve K kartik Follow 1 Improve Article Tags : JavaScript rotation Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like