[3825 views]

An array is a data structure containing elements of the same data type. These elements are stored in contiguous locations in the memory. Hence, we can easily find out the position of each element of the array by adding the base address with an offset value. Offset is the difference between two indexes of the array and the base address is the memory location of the first element of the array. The elements of an array can be accessed through the it’s index. For example if we want to access the second element of an array, we can write: a[1]. Here the index the required variable in the array ‘a’ is 1.

Note: The index of an array always starts with 0.

When we perform left rotation on an array, it shifts each element of the array to its left by one unit. The first element is shifted to the last and this is followed for all the other elements. Hence, this rotation happens in a circular way for the number of times it is specified by the user.

Let us consider an example:

Example 1:

A[] = { 1, 6, 7, 4, 8 }

After performing 4 rotations, the array will become:

A[] = { 8, 1, 6, 7, 4 }

Example 2:

Arr[] = { 7, 21, 15, 40, 23, 55, 45, 32, 80 }

After performing 5 rotations, the array will become:

Arr[] = { 55, 45, 32, 80, 7, 21, 15, 40, 23 }

Let us have a look at the algorithm and flowchart to perform left rotation on an array for specified number of times.

To start off the algorithm, we first take the array as input from the user. Then, we take the number of times the array must be rotated, as input from the user and store it in a variable say ‘r’. Now, we calculate the length of the array and store it in a variable say ‘l’.

We now start a loop to perform the left rotation. The loop runs from 0 to r, that is, the loop will run the number of times the array must be rotated. In each iteration, we store the first element of the array in a variable ‘f’ as this rotation is done in a circular manner. Now, we start another loop which runs from 0 to the length of the array. In this loop, we shift the elements of the array towards left, one by one by performing: a[j] = a[j+1]. After the completion of this loop, the first element of the array is shifted to the last position by: a[j] = f.

Once the first loop completes all the iterations, the resultant array is left rotated. We can now display this array.

Let us consider an example:

A[] = { 1, 2, 3, 4, 5 }

r = 4

l = 5

Here the loop will run 4 times.

After first iteration,

A[] = { 2, 3, 4, 5, 1 }

After second iteration,

A[] = { 3, 4, 5, 1, 2 }

After third iteration,

A[] = { 4, 5, 1, 2, 3 }

After fourth iteration,

A[] = { 5, 1, 2, 3, 4 }

- Java IDE Online
- Python IDE Online
- JSON Formatter/Minifier Online
- Case Converter
- Reverse String
- HTML Encoder
- HTML Decoder
- URL Encoder
- URL Decoder
- Decimal To Binary
- Binary To Decimal
- Decimal To Octal
- Binary To Octal
- Decimal To Hexadecimal
- Hexadecimal To Decimal
- Hexadecimal To Binary
- Octal To Decimal
- Octal to Hexadecimal
- Octal to Binary
- Calculate String Length
- Remove Spaces
- Remove Line Breaks
- Remove Empty Lines
- Remove Duplicate Lines
- Word Counter
- Replace Space with hyphen
- Check Armstrong number
- Text to URL

- Algorithm and Flowchart to calculate the frequency of punctuation marks in a given string
- Flowchart and Algorithm for Converting Infix to Postfix notation
- Algorithm to Count number of Words in Sentence for Beginners
- Algorithm to perform Binary to Decimal Conversion
- Algorithm and flowchart to perform binary to octal conversion