Arrays and Hashing Array basics

55. Left Rotate an Array by One

Easy

You are given an array of integers nums. Rotate it one place to the left and return it. Every element moves one position toward the start of the array, and the first element moves to the last position.

Do it in place: change nums itself and use only O(1) extra memory. Every value stays in the array, and the array keeps its length.

Example 1

Input: nums = [4, -7, 0, 9, 2]

Output: [-7,0,9,2,4]

Explanation: The elements from −7 to 2 each move one place to the left. The first element, 4, moves to the last position.

Example 2

Input: nums = [-1000000000, 5, 1000000000]

Output: [5,1000000000,-1000000000]

Explanation: 5 and 109 move one place to the left, and −109 moves from the front to the end.

Example 3

Input: nums = [8]

Output: [8]

Check your understanding

Work out the answer for this input before you write any code.

nums = [1, 2, 3, 4]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.