DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Array

Rotate Array

**Problem Statement:** Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative. **Hint / Expected Approach:** Reverse segments: full, first k, last n-k **Edge Cases to Consider:** - (1) k > n - (2) k = 0 - (3) n = 1

Examples

Example 1
Input: [1,2,3,4,5,6,7], 3
Output: [5,6,7,1,2,3,4]
Example 2
Input: [-1,-100,3,99], 2
Output: [3,99,-1,-100]
Example 3
Input: [1], 0
Output: [1]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪k > n
  • ▪k = 0
  • ▪n = 1
Frequently Asked At
MicrosoftAmazonGoogleAdobe