Given a list of integers and a rotation count, rotate the elements of the list to the right by that count. Elements at the end wrap around to the beginning. Return the rotated list. If the list is null or empty, return an empty list. If the rotation count is negative or zero, return the original list unchanged.
Examples:
Input: nums = [1, 2, 3, 4, 5], k = 2
Output: [4, 5, 1, 2, 3]
Explanation: Rotating right by 2 moves the last 2 elements to the front
Input: nums = [10, 20, 30], k = 5
Output: [20, 30, 10]
Explanation: k=5 is equivalent to k=2 since k % 3 = 2, rotates by 2 positions
Input: nums = [1, 2, 3], k = 0
Output: [1, 2, 3]
Explanation: Zero rotation returns the original list unchanged
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay October 2026 (Oct 4–15)Sponsored by FLXBL
Contest runs October 4 - 15. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 12 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.