ExamAdda Logo

Rotate Array

Easy

Given an integer array nums and an integer k, rotate the array to the right by k steps.

After rotating, every element moves k positions to the right, and elements at the end wrap around to the beginning.

Return the rotated array.

Example 1

Input

7
1 2 3 4 5 6 7
3

Output

5 6 7 1 2 3 4

Explanation

Rotating the array to the right by 3 positions moves the last three elements to the front.

Example 2

Input

4
1 2 3 4
1

Output

4 1 2 3

Explanation

After one rotation, the last element becomes the first element.

Constraints

1 ≤ n ≤ 10⁵
0 ≤ k ≤ 10⁵
-10⁹ ≤ nums[i] ≤ 10⁹

Hints:

Hint 1

Rotating an array by k positions is the same as rotating by k % n positions.

Hint 2

Try solving the problem in O(1) extra space by reversing the entire array, then reversing the first k elements and the remaining elements separately.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Jul 29, 2026

Last updated

Aug 16, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output