Faculty of Computing and Information Technology
Khulais
Information Technology Department
Chapter 1. Arrays
Problem 1:
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.
Example:
Rotation of the above array by 2 will make array
METHOD 1 (Using temp array)
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store the first d elements in a temp array
temp[] = [1, 2]
2) Shift rest of the arr[] and Store back the d elements
arr[] = [3, 4, 5, 6, 7, 1, 2]
Q1.1. Implement the above approach
// Java program to rotate an array by
// d elements
class RotateArray {
public void Rotate(int arr[], int d, int n)
{
int temp[]=new int[d];
for (int i = 0; i < d; i++)
temp[i]=arr[i];
for (int i=0;i<n;i++){
if(i<n-d)
arr[i]=arr[i+d];
else
arr[i]=temp[i-n+d];
}
1
Faculty of Computing and Information Technology
Khulais
Information Technology Department
/* utility function to print an array */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.printArray(arr, 7);
rotate.rotate(arr,2,7);
rotate.printArray(arr, 7);
}
}
Q1.2. How many instructions do the program? O(n)
Q1.3. What is the size of the auxiliary space used by your program? O(d)
METHOD 2 (Rotate one by one)
To rotate by one, store arr[0] in a temporary variable temp, move
arr[1] to arr[0], arr[2] to arr[1] …and finally temp to arr[n-1]
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Rotate arr[] by one 2 times
We get [2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7,
1, 2] after second rotation.
Q2.1. Implement the above approach
// Java program to rotate an array by
// d elements
class RotateArray {
/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
2
Faculty of Computing and Information Technology
Khulais
Information Technology Department
/* utility function to print an array */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.leftRotate(arr, 2, 7);
rotate.printArray(arr, 7);
}
}
Q2.2. How many instructions does the program execute? O(n*d)
Q2.3. What is the size of the auxiliary space used by your program? O(1)
METHOD 3 (A Juggling Algorithm)
This is an extension of method 2. Instead of moving one by one, divide
the array in different sets
where number of sets is equal to GCD of n and d and move the elements
within sets.
If GCD is 1 as is for the above example array (n = 7 and d =2), then
elements will be moved within one set only, we just start with temp =
arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the
right place.
Here is an example for n =12 and d = 3. GCD is 3 and
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a) Elements are first moved in first set – (See below diagram for this
movement)
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
3
Faculty of Computing and Information Technology
Khulais
Information Technology Department
b) Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c) Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
This is the implementation:
// Java program to rotate an array by
// d elements
class RotateArray {
/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
/* To handle if d >= n */
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
temp = arr[i];
j = i;
while (true) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
}
/*Fuction to get gcd of a and b*/
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
4
Faculty of Computing and Information Technology
Khulais
Information Technology Department
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.leftRotate(arr, 2, 7);
rotate.printArray(arr, 7);
}
}
Q3.1. How many instructions does the program execute? O(n)
Q3.2. What is the size of the auxiliary space used by your program? O(1)