Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Rotate Digits of a Number to the Right

Basic Acceptance 86.67% Points 10.00

You are given a non-negative integer n and a non-negative integer k. Your task is to return the number formed by rotating the digits of n to the right by k positions.

During a right rotation, the last digit moves to the front, while all other digits shift one position to the right. If k is greater than or equal to the number of digits, the rotation wraps around accordingly.

Examples
Example 1

Input: n = 12345, k = 2
Output: 45123
Explanation: The last two digits, 4 and 5, move to the front, producing 45123.

Example 2

Input: n = 12345, k = 5
Output: 12345
Explanation: Rotating by the full length of the number returns the original number.

Example 3

Input: n = 12345, k = 1
Output: 51234
Explanation: The last digit, 5, moves to the front, producing 51234.

Constraints
  • 0 = n = 10¹8
  • 0 = k = 100
Companies
Samsung DE Shaw
Topics
Data Structures Basics String Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions