Back to Practice Dashboard
Python BasicsEasy
Circular rotation by K positions
Learn how to solve the 'Circular rotation by K positions' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function circular_rotate(arr, k) that performs a circular right rotation of the array arr by k positions and returns the result. Elements shifted past the end wrap around to the beginning. For example, rotating [1,2,3,4,5] right by 2 gives [4,5,1,2,3].
Constraints
- •0 <= len(arr) <= 10^5
- •0 <= k <= 10^6
- •-10^9 <= arr[i] <= 10^9
Examples
Example 1
Input
arr = [1, 2, 3, 4, 5], k = 2
Output
[4, 5, 1, 2, 3]
Explanation
Right rotate by 2: last 2 elements [4,5] move to the front.
Example 2
Input
arr = [10, 20, 30, 40], k = 1
Output
[40, 10, 20, 30]
Explanation
Right rotate by 1: 40 moves to the front.
Example 3
Input
arr = [1, 2, 3], k = 6
Output
[1, 2, 3]
Explanation
k=6 is a multiple of 3 (array length), so the array returns to its original position.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.