0000 Rotated the Array right | MyCareerwise

CREATE OWN LIBRARY

Rotated the Array right

Back to Programming

Description

The elements are taken as input. The array is rotated right (in that case we assume that it is a circular array). That means, the element of index 0 is stored in index 1, the element of index 1 is stored in index 2, and so on. And the element of index n-1 is stored in index 0.

Algorithm

INPUT: Array elements
OUTPUT: the array after right rotation
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
Step 2: [Rotating the array]
	Set t<-a[n-1]
	For i=n-1 to 1 repeat
		Set a[i]<-a[i-1]
	[End of ‘for’ loop]
	Set a[0]<-t
	Print “The array after rotation is: “
	For i=0 to n-1 repeat
		Print a[i]
	[End of ‘for’ loop]
Step 3: Stop.

Code

Complexity:

The time complexity to take the number of elements is O(n) and the time complexity to right rotate is also O(n).

Therefore, the time complexity of this program is O(n).