From 610f42396ee64c54a38f67830880c2574abbab8f Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Jan 2025 18:39:49 +0000 Subject: [PATCH] Added rotate_array.py --- data_structures/arrays/rotate_array.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 3d07c8ba4..e659d1e3a 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -58,9 +58,18 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]: >>> rotate_array_n_space([8, -7, 22, -6], 9) [-6, 8, -7, 22] """ + length = len(nums) k = k % length - return nums[-k:] + nums[: length - k] + new_nums = [] + + for i in range(length - k, length): + new_nums.append(nums[i]) + + for i in range(length - k): + new_nums.append(nums[i]) + + return new_nums # Time: O(n) Space: O(k)