FOR FREE MATERIALS

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: