ARRAY ROTATION

ARRAY ROTATION

This is a beginner level lab that deals with various approaches to solve questions related to array rotations.

 

WHAT WILL YOU LEARN:

  • Left rotation of arrays
  • Right rotation of arrays
  • Various approaches to rotate arrays along with their time complexities

 

PRE-REQUISITES:

Basic knowledge of arrays and C++.

 

Lets get started

 

LEFT ROTATION

In left rotation, each element of the array will be shifted to its left by one position and the first element of the array will be added to the end of the array.This process is continued a specified number of times, till the required output is obtained.

Lets say we are given an array with elements 1,2,3,4,5 and we are asked to left rotate the array 3 times and print the output.

 

The array will look as follows after

  • the first rotation

                                   2 3 4 5 1

  • the second rotation

                                   3 4 5 1 2

  • the third rotation

                                   4 5 1 2 3

 

So the output should be 4 5 1 2 3

 

Discussion

19