FastPrepRotate Array by K

Rotate Array by K

Apple logoApple● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given an integer array nums and a nonnegative integer k, return the array after rotating it k positions to the right. Values falling off the right end wrap to the beginning. Return an empty array when the input is empty.

The returned array must preserve every value and its relative circular order. No in-place mutation requirement applies.

Function

rotateArray(nums: int[], k: int) → int[]

Examples

Example 1

nums = [1,2,3,4,5]k = 2return = [4,5,1,2,3]

The last two values move to the front.

Example 2

nums = [-1,7,0]k = 4return = [0,-1,7]

Four steps on a length-three array equal one right rotation.

Constraints

  • 0 <= nums.length <= 10^5.
  • -10^9 <= nums[i] <= 10^9.
  • 0 <= k <= 10^9.

More Apple problems

See Apple hiring insights
public int[] rotateArray(int[] nums, int k) {
    // Write your solution here.
}
nums[1,2,3,4,5]
k2
expected[4,5,1,2,3]
Checking account…