![]() |
VOOZH | about |
Given an array and an integer k, the task is to split the array from the kth position and move the first part to the end. For Example:
Input: arr = [12, 10, 5, 6, 52, 36], k = 2
Output: [5, 6, 52, 36, 12, 10]
Explanation: Split the array at index k and move the first part [12, 10] (for k = 2) to the end.
Below are the different methods to perform this task:
deque from the collections module allows efficient rotation of elements. Its rotate() method handles both left and right rotations internally without explicit shifting.
[5, 6, 52, 36, 12, 10]
Explanation:
List slicing rotates a list by splitting it at position k taking elements from index k to the end first, then adding the first k elements to the end.
[5, 6, 52, 36, 12, 10]
Explanation:
This method uses modular arithmetic to calculate rotated positions directly. It constructs a new list where each element is taken from (i + k) % len(arr).
[5, 6, 52, 36, 12, 10]
Explanation:
Slicing combined with extend() achieves rotation while demonstrating list merging explicitly.
[5, 6, 52, 36, 12, 10]
Explanation: