Given a list of integers and a rotation count k, rotate the list to the right by k positions. For example, rotating [1,2,3,4,5] by 2 positions results in [4,5,1,2,3]. If k is larger than the list size, handle it appropriately using modulo operation.

Examples:

Input: nums = [1, 2, 3, 4, 5], k = 2 Output: [4, 5, 1, 2, 3] Explanation: Rotate right by 2 positions, elements 4 and 5 move to the front Input: nums = [1, 2, 3], k = 0 Output: [1, 2, 3] Explanation: Rotating by 0 positions returns the same list Input: nums = [1, 2, 3, 4, 5], k = 7 Output: [4, 5, 1, 2, 3] Explanation: k=7 is equivalent to k=2 (7 mod 5 = 2) for a list of size 5
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 Jan 2026

Contest runs January 1 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.