Rotate Array Right

EasyArrayTwo Pointers

Description

Given an array nums and a non-negative integer k, return the array rotated to the right by k positions.

Examples

Input:nums = [1,2,3,4,5], k = 2
Output:[4,5,1,2,3]
Explanation:

Shifting every element 2 position(s) to the right wraps around to [4,5,1,2,3].

Input:nums = [1,2,3], k = 0
Output:[1,2,3]
Explanation:

Shifting every element 0 position(s) to the right wraps around to [1,2,3].

Input:nums = [1,2,3,4], k = 5
Output:[4,1,2,3]
Explanation:

Shifting every element 5 position(s) to the right wraps around to [4,1,2,3].

Constraints

  • 1 ≤ nums.length ≤ 10⁴
  • 0 ≤ k ≤ 10⁵

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.