C# Cyclic Rotation
static int[] CyclicRotation(int[] a, int k)
{
if (a.Length == 0)
{
return a;
}
var list = new List<int>(a);
for (int i = 0; i < k; i++)
{
var last = list[^1];
list.RemoveAt(list.Count - 1);
list.Insert(0, last);
}
return list.ToArray();
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Elixir Cyclic Rotation
defmodule CyclicRotation do
def cyclic_rotation([], _k), do: []
def cyclic_rotation(a, k) do
size = length(a)
shift = rem(k, size)
{front, back} = Enum.split(a, size - shift)
back ++ front
end
end
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Erlang Cyclic Rotation
-module(cyclic_rotation).
-export([cyclic_rotation/2]).
cyclic_rotation([], _K) ->
[];
cyclic_rotation(A, K) ->
N = length(A),
Shift = K rem N,
{Front, Back} = lists:split(N - Shift, A),
Back ++ Front.
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Go Cyclic Rotation
func cyclicRotation(a []int, k int) []int {
size := len(a)
if size == 0 || k <= 0 {
return a
}
k %= size
result := make([]int, size)
for i, v := range a {
result[(i+k)%size] = v
}
return result
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Haskell Cyclic Rotation
cyclicRotation :: [Int] -> Int -> [Int]
cyclicRotation [] _ = []
cyclicRotation a k = back ++ front
where
n = length a
r = k `mod` n
(front, back) = splitAt (n - r) a
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Java Cyclic Rotation
public class Solution {
public static int[] cyclicRotation(int[] a, int k) {
if (a.length > 0) {
for (int i = 0; i < k; i++) {
int last = a[a.length - 1];
System.arraycopy(a, 0, a, 1, a.length - 1);
a[0] = last;
}
}
return a;
}
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Lisp Cyclic Rotation
(defun cyclic-rotation (a k)
(if (null a)
a
(let ((result (copy-list a)))
(dotimes (i k)
(let ((last (car (last result))))
(setf result (cons last (butlast result)))))
result)))
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
PHP Cyclic Rotation
function cyclicRotation(array $a, int $k): array
{
if ( ! empty($a)) {
for ($i = 0; $i < $k; $i++) {
array_unshift($a, array_pop($a));
}
}
return $a;
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Python Cyclic Rotation
from collections import deque
def cyclic_rotation(a: list[int], k: int) -> list[int]:
if not a:
return a
d = deque(a)
d.rotate(k)
return list(d)
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Rust Cyclic Rotation
fn cyclic_rotation(mut a: Vec<i64>, k: i64) -> Vec<i64> {
let len = a.len();
if len > 0 && k > 0 {
let k = (k as usize) % len;
a.rotate_right(k);
}
a
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.