Python/data_structures/arrays/rotate_elements.py
2024-11-03 12:25:55 +00:00

22 lines
530 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Given an Array of size N and a value K,
around which we need to right rotate the array.
How do you quickly print the right rotated array?
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}
"""
def right_rotate(arr, k):
n = len(arr)
k = k % n
arr = arr[n - k :] + arr[: n - k]
return arr
# Test the function with the provided input
print(right_rotate([1, 3, 5, 7, 9], 2)) # Expected output: [7, 9, 1, 3, 5]