Left Rotate an Array by D
Given an integer array arr and a rotation count d, rotate the array to the left by d positions. A left rotation by one position moves every element one index…
Given an integer array `arr` and a rotation count `d`, rotate the array to the **left** by `d` positions. A left rotation by one position moves every element one index earlier, and the first element wraps around to the last position. After `d` left rotations, the element that was originally at index `d` ends up at index 0. Before rotating, normalise the rotation count by computing `d = d % n` (where `n` is the array length) — this handles the case where `d` is larger than the array length. Return the rotated array.
- `1 ≤ n ≤ 10^5` - `0 ≤ d ≤ 10^9` - Array elements are 32-bit signed integers. - The interactive visualizer accepts at most 20 values; longer statement-valid arrays are execution-only. - When `d = 0` or `d = n`, the array is unchanged. - When `d > n`, compute `d = d % n` before rotating.
Example 1: input arr = [7, 11, 3, 8, 2, 5], d = 3; output [8, 2, 5, 7, 11, 3]. After 3 left rotations: elements at indices 3, 4, 5 are now at the front, and elements at indices 0, 1, 2 are at the back.
Example 2: input arr = [4, 9, 6, 1], d = 6; output [6, 1, 4, 9]. d = 6, n = 4, so effective rotations = 6 % 4 = 2.
Example 3: input arr = [10, 20, 30, 40, 50], d = 0; output [10, 20, 30, 40, 50]. A rotation count of 0 means no rotation occurs.
Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: two-pointers.