Left Rotation — HackerRank C++ Implementation
Problem:
A left rotation operation on an array of size n shifts each of the array’s elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5]
, then the array would become [3, 4, 5, 1, 2]
.
Given an array of n integers and a number, d, perform d left rotations on the array. Then print the updated array as a single line of space-separated integers.
Read the full problem here : Left Rotation
Solution:
This is a very easy problem. In the above figure we see that after two rotations, the element at index 2 is the first element in the new array. Thus after d rotations, the element at index d is the first element. And all elements after dth elements with follow.
Now, elements from 0 to d-1 indices are added to the new rotated array.
C++ Implementation on Programmercave
Other Competitive Programming Problems and Solutions
Originally published at https://programmercave.com/ on April 25, 2020.