Rotate an Array by K Steps (In-Place) |Rotate an Array to the Right by K Steps

Опубликовано: 04 Октябрь 2024
на канале: Programming Tutorials
25,861
288

In this tutorial, I have explained the java code to rotate an array by k steps to the right.

Given an array, rotate the array to the right by k steps, where k is non-negative.

Website - https://webrewrite.com/

Example 1:

Input: [1, 2, 3, 8, 9, 10] and k = 3

Output: [8, 9, 10, 1, 2, 3]

Explanation:

rotate 1 steps to the right: [10, 1, 2, 3, 8, 9]
rotate 2 steps to the right: [9, 10, 1, 2, 3, 8]
rotate 3 steps to the right: [8, 9, 10, 1, 2, 3]