Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

8. Rotate Array

MediumDay 2 of 60Arrays & Hashing

Given an integer array nums and a non-negative integer k, rotate the array to the right by k steps, so each element moves k places to the right and elements falling off the end wrap around to the front. Modify nums in place.

Example 1
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
The last 3 elements 5,6,7 wrap around to the front.
Example 2
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Constraints
  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^5
Hints
Frequently asked at: Microsoft, Amazon, Adobe, Zoho, Infosys
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min