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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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 Feb 2026

Contest runs February 1 - 28. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.